Open lsegal opened 2 years ago
I wonder if there's any documentation on what fixes are considered in formatting and what are not?
Add this to your settings.json and it should do it.
"[csharp]": {
"editor.defaultFormatter": "ms-dotnettools.csharp",
"editor.codeActionsOnSave": ["source.organizeImports", "source.fixAll"]
},
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?
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
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.
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.
To work, check this option
To work, check this option
Not working for me. 👎
@IanIsFluent If you are using OmniSharp, you would need to additionally enable omnisharp.organizeImportsOnFormat
To work, check this option
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'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
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"
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".
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