razor icon indicating copy to clipboard operation
razor copied to clipboard

Add "Remove and Sort Usings" to Blazor .razor files

Open vsfeedback opened this issue 2 years ago • 3 comments

This issue has been moved from a ticket on Developer Community.


Provide similar functionality in .razor files to "Remove and Sort Usings" and greying out unused Usings in .cs files


Original Comments

Feedback Bot on 3/29/2023, 11:21 PM:

(private comment, text removed)

Feedback Bot on 3/30/2023, 02:09 PM:

(private comment, text removed)

vsfeedback avatar Mar 30 '23 21:03 vsfeedback

We're probably good for this from the diagnostic point of view - they should get mapped properly. We're probably okay with this from the code action point of view - our existing handlers should be able to deal with the removal of using statements.

Unfortunately what this actually needs is either a compiler change, or a lot more logic in Razor tooling. The way a Razor file is transformed into C# right now means that any using statement that is added for a Razor component is actually unused in the generated C#. The compiler always fully qualifies anything it generates, so without any user written C# code, all using statements would be seen as unused. Obviously this isn't correct, and the using statements are needed for component discovery (by that very same compiler!).

So we either change the compiler, or we do a second pass over the unused usings. We find out which usings Roslyn thinks are unused, so that we know we're not going to break use written C# code, and then filter that list by the namespaces of all of the components in use in the file. At the moment that isn't too much work, but if we implement https://github.com/dotnet/razor/issues/7670 it might get more complicated.

davidwengier avatar May 30 '23 06:05 davidwengier

I would very much appreciate this feature aswell, I was about to create a feature suggestion, when I found this issue here. Though I have one addtion: If you do this, please also do this for .cshtml files, the OP only mentions .razor files. (Maybe this is self-evident, but just in case, I wanted it to mention explicitly) Thanks!

AMUCOMO avatar Jan 18 '24 18:01 AMUCOMO

Please consider implementing this soon because right now I'm manually deleting all usings and then looking through build errors to figure out which are necessary after code changes.

ErroneousFatality avatar Oct 28 '25 11:10 ErroneousFatality