Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused usings on format #5261

Open
lsegal opened this issue Jun 9, 2022 · 13 comments
Open

Remove unused usings on format #5261

lsegal opened this issue Jun 9, 2022 · 13 comments

Comments

@lsegal
Copy link

lsegal commented Jun 9, 2022

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 #1245 (comment)). 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
@Meligy
Copy link

Meligy commented Aug 18, 2022

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

@devonpmack
Copy link

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

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

@Tantol
Copy link

Tantol commented Dec 8, 2022

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
Copy link

Meligy commented Dec 9, 2022

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
Copy link

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
Copy link

Meligy commented Dec 20, 2022

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
Copy link

terdong commented Sep 18, 2023

To work, check this option

image

@IanIsFluent
Copy link

To work, check this option

image

Not working for me. 👎

@JoeRobich
Copy link
Member

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

@terdong
Copy link

terdong commented Nov 27, 2023

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
Copy link

mcuthbe commented Jan 10, 2024

@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
Copy link

HSPiira commented Oct 28, 2024

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
Copy link

JeremyCaron commented Oct 30, 2024

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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests