You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the feature you'd like
I'd like to propose adding an option to remove unused imports/usings in open files automatically on save. I would even advocate for making it part of the default behavior for the source.organizeImports code action, as it is for many of the other major language extensions/LSPs - JavaScript/TypeScript, Java, Go, to name a few that I'm sure of - but since this has never been part of the standard source.organizeImports1 behavior of the C# extension to my knowledge, even just an off-by-default option in settings.json, e.g. "Remove unused imports when organizing imports", would suffice.
Apparently the C# extension's import organization fix has never actually been hooked up to the source.organizeImports code action as I assumed it was, but rather the Omnisharp-specific omnisharp.organizeImportsOnFormat setting. In any case, it seems that with the transition away from OmniSharp, this option is no longer recognized and source.organizeImports isn't being recognized in its stead, so I can only conclude that the latest version of the C# extension provides no way to have imports organized at all - with or without removing unnecessary ones. I can open a separate issue for this if it would be helpful.
Alternatives considered
While the previous OmniSharp version of the extension did provide a manual way to remove unused imports via a command palette-activated action called "OmniSharp: Fix all occurrences of a code issue" -> "CS8019: Unnecessary removing directive", it has never been possible to have this fix run automatically on save without also enabling a whole host of other on-save code fixes that you wouldn't necessarily want - e.g., generating stub implementations for unimplemented interface members, removing any unreferenced local variables (particularly annoying because this would often wipe out code you were in the middle of writing if you happened to save before using all the variables!) - which impeded the editing experience to the point where it was ultimately less trouble to just leave the setting disabled. (That is, to leave source.fixAll out of your editor.codeActionsOnSave.)
One user in dotnet/vscode-csharp#5261 was apparently able to work around this by finessing his Directory.Build.props and .editorconfig files into just the right configuration so that only the "remove unnecessary imports" fix would get applied by the formatter and none of the other fixes that weren't specified, but I've not been able to reproduce his success despite repeated attempts, and in any case, this approach requires changing your source files to enable documentation comments (seemingly due to an #41640), which isn't always possible or desired.
I threw together an extension several weeks ago which hackily implements this request by circumventing the OmniSharp extension and sending CS8019 fix requests directly to the server in response to the source.organizeImports action, but now that a different language server is in use, that extension no longer works.
Environment Information
No response
The text was updated successfully, but these errors were encountered:
Describe the feature you'd like
I'd like to propose adding an option to remove unused imports/usings in open files automatically on save. I would even advocate for making it part of the default behavior for the source.organizeImports code action, as it is for many of the other major language extensions/LSPs - JavaScript/TypeScript, Java, Go, to name a few that I'm sure of - but since this has never been part of the standard source.organizeImports1 behavior of the C# extension to my knowledge, even just an off-by-default option in settings.json, e.g. "Remove unused imports when organizing imports", would suffice.
Alternatives considered
While the previous OmniSharp version of the extension did provide a manual way to remove unused imports via a command palette-activated action called "OmniSharp: Fix all occurrences of a code issue" -> "CS8019: Unnecessary removing directive", it has never been possible to have this fix run automatically on save without also enabling a whole host of other on-save code fixes that you wouldn't necessarily want - e.g., generating stub implementations for unimplemented interface members, removing any unreferenced local variables (particularly annoying because this would often wipe out code you were in the middle of writing if you happened to save before using all the variables!) - which impeded the editing experience to the point where it was ultimately less trouble to just leave the setting disabled. (That is, to leave source.fixAll out of your editor.codeActionsOnSave.)
One user in dotnet/vscode-csharp#5261 was apparently able to work around this by finessing his Directory.Build.props and .editorconfig files into just the right configuration so that only the "remove unnecessary imports" fix would get applied by the formatter and none of the other fixes that weren't specified, but I've not been able to reproduce his success despite repeated attempts, and in any case, this approach requires changing your source files to enable documentation comments (seemingly due to an #41640), which isn't always possible or desired.
I threw together an extension several weeks ago which hackily implements this request by circumventing the OmniSharp extension and sending CS8019 fix requests directly to the server in response to the source.organizeImports action, but now that a different language server is in use, that extension no longer works.
Environment Information
No response
The text was updated successfully, but these errors were encountered: