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

Remove unused usings on format

Open lsegal opened this issue 3 years ago • 11 comments

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

  • Ctrl+P >Format Document
  • Format on Save

lsegal avatar Jun 09 '22 16:06 lsegal

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

Meligy avatar Aug 18 '22 06:08 Meligy

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

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

devonpmack avatar Aug 30 '22 22:08 devonpmack

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?

Tantol avatar Dec 08 '22 18:12 Tantol

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

Meligy avatar Dec 09 '22 00:12 Meligy

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.

Sweepertank avatar Dec 20 '22 00:12 Sweepertank

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

Meligy avatar Dec 20 '22 02:12 Meligy

To work, check this option

image

terdong avatar Sep 18 '23 14:09 terdong

To work, check this option

image

Not working for me. 👎

IanIsFluent avatar Nov 24 '23 13:11 IanIsFluent

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

JoeRobich avatar Nov 27 '23 18:11 JoeRobich

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.

terdong avatar Nov 27 '23 21:11 terdong

@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

mcuthbe avatar Jan 10 '24 10:01 mcuthbe