使用在 vscode 上删除未使用的用途来格式化文档

Sco*_*ger 8 .net c# visual-studio-code

我想设置为在 Visual Studio Code 上保存 C# 代码时触发“格式化文档”和“删除未使用的使用”。或者添加一个键盘快捷键来删除未使用的使用。

我已经添加到下面的用户设置。

"editor.formatOnSave": true
Run Code Online (Sandbox Code Playgroud)

这会在保存时触发格式文档。但我也想删除未使用的用途。如果有未使用的用途,VS 代码会警告我,我可以 Ctrl + . 弹出关于删除未使用的使用。

  1. 我可以设置 fire 在保存时删除未使用的使用吗?
  2. 我可以为删除未使用的使用添加键盘快捷键吗?

我还为格式化文档添加了键盘快捷键。

{ "key": "ctrl+k ctrl+f",         "command": "editor.action.formatSelection",
                                  "when": "editorHasDocumentSelectionFormattingProvider && editorHasSelection && editorTextFocus && !editorReadonly" },
Run Code Online (Sandbox Code Playgroud)

我想添加 Ctrl+R Ctrl+G 以删除未使用的用途。(由 Visual Studio 默认)。但我不知道如何配置键盘快捷键设置...

{ "key": "ctrl+r ctrl+g",         "command": "editor.action.???",
                                  "when": "???" },
Run Code Online (Sandbox Code Playgroud)

Dan*_*eis 5

删除未使用的使用的配置必须通过 omnisharp 完成。

Omnisharp 设置可在两个位置使用:

对于全局设置:

对于全局设置,使用%USERPROFILE%/.omnisharp/omnisharp.json文件

对于项目特定设置:

omnisharp.json在工作区的根目录使用

设置如下:

{
     "FormattingOptions": {
         "OrganizeImports": true
     }
}
Run Code Online (Sandbox Code Playgroud)

2020 年 5 月 5 日可用的其他默认设置:

{
    "FormattingOptions":  {
                              "OrganizeImports":  false,
                              "EnableEditorConfigSupport":  false,
                              "NewLine":  "\n",
                              "UseTabs":  false,
                              "TabSize":  4,
                              "IndentationSize":  4,
                              "SpacingAfterMethodDeclarationName":  false,
                              "SpaceWithinMethodDeclarationParenthesis":  false,
                              "SpaceBetweenEmptyMethodDeclarationParentheses":  false,
                              "SpaceAfterMethodCallName":  false,
                              "SpaceWithinMethodCallParentheses":  false,
                              "SpaceBetweenEmptyMethodCallParentheses":  false,
                              "SpaceAfterControlFlowStatementKeyword":  true,
                              "SpaceWithinExpressionParentheses":  false,
                              "SpaceWithinCastParentheses":  false,
                              "SpaceWithinOtherParentheses":  false,
                              "SpaceAfterCast":  false,
                              "SpacesIgnoreAroundVariableDeclaration":  false,
                              "SpaceBeforeOpenSquareBracket":  false,
                              "SpaceBetweenEmptySquareBrackets":  false,
                              "SpaceWithinSquareBrackets":  false,
                              "SpaceAfterColonInBaseTypeDeclaration":  true,
                              "SpaceAfterComma":  true,
                              "SpaceAfterDot":  false,
                              "SpaceAfterSemicolonsInForStatement":  true,
                              "SpaceBeforeColonInBaseTypeDeclaration":  true,
                              "SpaceBeforeComma":  false,
                              "SpaceBeforeDot":  false,
                              "SpaceBeforeSemicolonsInForStatement":  false,
                              "SpacingAroundBinaryOperator":  "single",
                              "IndentBraces":  false,
                              "IndentBlock":  true,
                              "IndentSwitchSection":  true,
                              "IndentSwitchCaseSection":  true,
                              "IndentSwitchCaseSectionWhenBlock":  true,
                              "LabelPositioning":  "oneLess",
                              "WrappingPreserveSingleLine":  true,
                              "WrappingKeepStatementsOnSingleLine":  true,
                              "NewLinesForBracesInTypes":  true,
                              "NewLinesForBracesInMethods":  true,
                              "NewLinesForBracesInProperties":  true,
                              "NewLinesForBracesInAccessors":  true,
                              "NewLinesForBracesInAnonymousMethods":  true,
                              "NewLinesForBracesInControlBlocks":  true,
                              "NewLinesForBracesInAnonymousTypes":  true,
                              "NewLinesForBracesInObjectCollectionArrayInitializers":  true,
                              "NewLinesForBracesInLambdaExpressionBody":  true,
                              "NewLineForElse":  true,
                              "NewLineForCatch":  true,
                              "NewLineForFinally":  true,
                              "NewLineForMembersInObjectInit":  true,
                              "NewLineForMembersInAnonymousTypes":  true,
                              "NewLineForClausesInQuery":  true
                          }
}
Run Code Online (Sandbox Code Playgroud)

  • 这实际上并没有删除它们,只是对它们进行排序。 (2认同)

Yve*_*lpe 1

我担心,今天写的内容, VSCode Marketplace上没有插件- 也没有内置的设置/功能提供您想要的有关“未使用的使用”行为的内容,就像在完整的 Visual Studio 中一样。

我的建议是在名为“ OmniSharp ”的官方 Microsoft 插件(默认 C# 插件,也支持其他编辑器的 C# 功能)中请求此功能:https: //github.com/OmniSharp/omnisharp-vscode/issues
旁注:有一个关于“未使用的使用”的问题,以禁用它生成的警告:https ://github.com/OmniSharp/omnisharp-vscode/issues/315

或者转到 VSCode GitHub 问题页面并在那里询问: https: //github.com/microsoft/vscode/issues

或者最后一条路线是深入研究并编写自己的插件/扩展:https://code.visualstudio.com/docs/extensions/overview

  • 我已经将 C# 用于由 OmniSharp 提供支持的 Visual Studio Code,因此我可以通过 GUI 操作使用“删除未使用的使用”功能,通过 Ctrl + 弹出它们。并单击它们以删除未使用的用途。但我想在保存文档时触发它们,或者我想通过键盘快捷键来执行它们。我明白现在已经没有办法了。目前 Productivity Power Tools 2017 上的这些功能也无法正常工作... (4认同)