vscode-csharp icon indicating copy to clipboard operation
vscode-csharp copied to clipboard

Add a command or function - Remove and Sort Usings

Open flyneth opened this issue 2 years ago • 3 comments

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

Describe the solution you would like

  • Add one item, "Remove and Sort Usings" in the right menu;
  • Add some configs to set the sort and group rule
  • A config flag to decide to add a new line when grouping Usings

Applicable Scenarios

  • Remove unused using statement
  • Sort and group using statement
  • Format code

Describe alternatives you've considered

  • Use Visual Studio to sort and group the using statement.

Additional context

  • Raw (Before updated): image

  • Updated: image

flyneth avatar Jul 28 '22 04:07 flyneth

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

filipw avatar Jul 28 '22 14:07 filipw

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.

Kotsuha avatar Feb 01 '24 18:02 Kotsuha

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.

jeremy-allocate avatar Apr 12 '24 22:04 jeremy-allocate