dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.86k stars 671 forks source link

Add a command or function - Remove and Sort Usings #5320

Open flyneth opened 2 years ago

flyneth commented 2 years ago

Is your feature request related to a problem? Please describe.

Describe the solution you would like

Applicable Scenarios

Describe alternatives you've considered

Additional context

filipw commented 2 years ago

you can set omnisharp.organizeImportsOnFormat: true in VS code to sort usings when formatting the document.

You can also enable editorconfig and analyzer support support by setting:

omnisharp.enableEditorConfigSupport: true
omnisharp.enableRoslynAnalyzers: true

You can then configure the behavior of Organize Imports by changing settings within your .editorconfig file. See https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/formatting-rules#organize-using-directives for more details.

Below is an example which will disable sorting system directives to the top.

root = true

# .NET formatting rules
[*.{cs,vb}]
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true
Kotsuha commented 8 months ago

The above settings cannot remove unused usings, or am I missing something? It would be nice to be able to do that. Previously I used this extension to remove unused usings, but it doesn't seem to work anymore.

jeremy-allocate commented 6 months ago

With Microsoft's push to move people off of omnisharp and over to C# Dev Kit, I'm wondering if there are any other ways of accomplishing this, especially when saving a code file. I've been unsuccessful pointing editor.codeActionsOnSave at the command from an extension I use that can organize imports for example.