roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

Razor Editor (VS2022) Comment & Uncomment working incorrectly

Open arashberlin opened this issue 4 years ago • 6 comments

Describe the bug:

The new Razor Editor introduced in Visual Studio 2022 has issues commenting out code on HTML or Razor code. "Ctrl+K" or "Ctrl+U" short cuts for comments are working incorrectly.

Switching to "legacy Razor editor for ASP.NET Core" via options helped. I'm just wondering if the behavior of the new Razor Editor is correct or if this a bug?

Comments should contain @* *@. However, we get //

Version used: VS2022 17.1.2

To reproduce: Steps to reproduce the behavior:

  1. open a complex razor page containing HTML, Razor and JavaScript Code
  2. try to comment/uncomment code by using the short cuts: "Ctrl+K" or "Ctrl+U"
  3. watch the video

Expected behavior: HTML or Razor code comments should contain @* *@. However, we get //

https://user-images.githubusercontent.com/12381397/160365666-2765dba6-a177-4a96-bf42-b629abe26fa1.mp4

arashberlin avatar Mar 28 '22 09:03 arashberlin

@arashberlin thank you for the bug report! I've dug into your issue and found a minimal repro:

Comment between the | pipes

@{
	<strong>
		|<span>
			
		</span>|
	</strong>
}

Expected:

@{
	<strong>
		@*<span>
			
		</span>*@
	</strong>
}

Actual:

@{
	<strong>
		//<span>
			
		//</span>
	</strong>
}

@arashberlin as a workaround you can remove the overarching @{...} from your code to get things back into a working state.

NTaylorMullen avatar Mar 31 '22 00:03 NTaylorMullen

I have found another example:

@{
	void f() 
	{
		<p class="x"></p>
	}
}

//<div>
// text
//</div>

Why is there the C# comment if the HTML is not inside a code block ? Do you know any workaround ?

plastovicka avatar May 31 '22 08:05 plastovicka

The issue is still not fixed in VS 2022 17.2.2.

I run Razor editor in legacy mode with no problems untill this bug is fixed.

arashberlin avatar May 31 '22 10:05 arashberlin

I am also experiencing this issue with VS 2022 17.12.3, which is frustrating. I don't know which update I took where this began, but the Code Comments started using slashes for all comments regardless of file type. It used to be smart enough to know what type of comment to use based on whether it was C#, Razor, HTML, or CSS.

Thank you for looking into it for us!

bdparkhurst avatar Apr 07 '25 11:04 bdparkhurst

Today, I upgraded to VS 2022 17.12.5 and it fixed the Razor comment issue. The code comments are now completed in a Razor file with the @* *@ block! Awesome.

bdparkhurst avatar Apr 09 '25 15:04 bdparkhurst

I have version 17.14.27 and still have this problem but could notice something strange. After a code block this bug occurs otherwise not.

Problem after a code block. Comment with double slash:

Image

And when I am commenting out the code block, the subsequent comment is formatted correctly:

Image

wibram avatar Feb 18 '26 07:02 wibram