dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.88k stars 676 forks source link

Remove unused usings on format #5261

Open lsegal opened 2 years ago

lsegal commented 2 years ago

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

tl;dr: OmniSharp should allow configuring the formatter (omnisharp.json) to remove unused imports.

1245 shows that it is possible to "remove unused usings" in a document using Omnisharp. However, this feature cannot be used during a "Format" operation (as per comment https://github.com/OmniSharp/omnisharp-vscode/issues/1245#issuecomment-763723115). Only "organizing" imports is possible via the omnisharp.json configuration (#1686).

Describe the solution you would like

OmniSharp should allow configuring the formatter (omnisharp.json) to remove unused imports.

Applicable Scenarios

Meligy commented 2 years ago

I wonder if there's any documentation on what fixes are considered in formatting and what are not?

devonpmack commented 2 years ago

Add this to your settings.json and it should do it.

  "[csharp]": {
    "editor.defaultFormatter": "ms-dotnettools.csharp",
    "editor.codeActionsOnSave": ["source.organizeImports", "source.fixAll"]
  },
Tantol commented 1 year ago

Add this to your settings.json and it should do it.

  "[csharp]": {
    "editor.defaultFormatter": "ms-dotnettools.csharp",
    "editor.codeActionsOnSave": ["source.organizeImports", "source.fixAll"]
  },

Not working for me. Unused usings still exists after save.

Any other solutions or plans to implement this feature in to omnisharp?

Meligy commented 1 year ago

For this to work, I read somewhere that you need to turn on XML comments (sorry, I don't remember where):

So, in my project (in my Directory.Build.props file really, which applies to all projects(, I also had:

  <PropertyGroup>
    <!-- Needed to help with 'usings' removal, as some are not removed in case they are in XML comments -->
    <GenerateDocumentationFiles>true</GenerateDocumentationFiles>
  </PropertyGroup>

Then I needed to have an .editorconfig file, which had the following rules for C# files, among other rules that I had anyway:

# unused directives
dotnet_diagnostic.IDE0005.severity = none
dotnet_diagnostic.CS8019.severity = silent
# Disable: Missing XML comment for publicly visible type or member
# We only turned on XML comments to help with detecting unused usings
dotnet_diagnostic.CS1591.severity = none

A few other relevant but not necessary .editorconfig rules:

# File-scoped namespace declarations
dotnet_diagnostic.IDE0161.severity = refactoring
csharp_style_namespace_declarations = file_scoped
# Sort using and Import directives with System.* appearing first
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = false
dotnet_style_namespace_match_folder = true
dotnet_diagnostic.IDE0130.severity = warning

You can generate a default .editorconfig file by running:

dotnet new editorconfig
marshmallie7 commented 1 year ago

Add this to your settings.json and it should do it.

  "[csharp]": {
    "editor.defaultFormatter": "ms-dotnettools.csharp",
    "editor.codeActionsOnSave": ["source.organizeImports", "source.fixAll"]
  },

This works for me, but source.fixAll seems to be doing more than just removing unused usings - for instance, it also causes stub implementations of unimplemented interface members to be generated. It would be great if there were an option just to remove unused usings on save without enabling any other on-save actions.

Meligy commented 1 year ago

It would be great if there were an option just to remove unused usings on save without enabling any other on-save actions.

You are expected to control which fixes are enabled and which are not using the editorconfig file.

Docs https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-options?source=recommendations

terdong commented 1 year ago

To work, check this option

image

IanIsFluent commented 1 year ago

To work, check this option

image

Not working for me. 👎

JoeRobich commented 12 months ago

@IanIsFluent If you are using OmniSharp, you would need to additionally enable omnisharp.organizeImportsOnFormat

terdong commented 12 months ago

To work, check this option image

Not working for me. 👎

I experienced other side effects even though the Remove unused import in omnisharp worked. That's way I stopped using csharp in vscode and went back to visual studio. I think omnisharp's latest support is out of date. I'm sorry I can not help.

mcuthbe commented 10 months ago

@terdong's fix works for me, but does anyone know why it doesn't work with the newer LSP powered version of the extension? Is it a roslyn thing, the extension itself or just up to configuration? Struggling to find any information about it

HSPiira commented 3 weeks ago

Any one still struggling with this, do this:

Press Ctrl + . (Control key + period key) for Windows or Command + . for Mac when the cursor is inside the file. This opens up the Quick Actions menu, where you can choose "Remove Unnecessary Usings"

JeremyCaron commented 3 weeks ago

I maintain an extension called C# Organize Usings (repo) that can also handle this, along with sorting & spacing as well. It's compatible with both Omnisharp and Roslyn and it can be configured to run "on save".