Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format document on save with Remove unused usings on vscode

I want to setup to fire "format document" and "remove unused usings" on save for C# code on Visual Studio Code. Or add a keyboard shortcut for remove unused usings.

I have added to user settings below.

"editor.formatOnSave": true

This fires format document on save. But I want also to remove unused usings. VS code warns me if there are unused usings and I can Ctrl + . to popup about remove unused usings.

  1. Can I setup fire remove unused usings on save?
  2. Can I add a keyboard shortcut for the remove unused usings?

I also added keyboard shortcut for format document.

{ "key": "ctrl+k ctrl+f",         "command": "editor.action.formatSelection",
                                  "when": "editorHasDocumentSelectionFormattingProvider && editorHasSelection && editorTextFocus && !editorReadonly" },

I want to add Ctrl+R Ctrl+G for remove unused usings. (by Visual Studio defaults). But I don't know how I can configure keyboard shortcut settings...

{ "key": "ctrl+r ctrl+g",         "command": "editor.action.???",
                                  "when": "???" },
like image 702
Scott Tiger Avatar asked Jan 29 '17 19:01

Scott Tiger


People also ask

How do you automatically remove unused imports in VS Code?

While refactoring your javascript/typescript code it's easy to remove things but forget to get rid of the unused imports. In VSCode, you can easily remove these with the shortcut : Shift + Alt + O.

How do I delete all unnecessary Usings in Visual Studio?

First is putting your cursor on one of the grayed out lines and clicking the light bulb, then clicking Remove Unnecessary Usings. (NOTE: The shortcut key for this is CTRL-.)

How do you format a document in VS Code?

VS Code has great support for source code formatting. The editor has two explicit format actions: Format Document (Ctrl+Shift+I) - Format the entire active file. Format Selection (Ctrl+K Ctrl+F) - Format the selected text.

How do I enable format in save VS Code?

Click the “Settings” gear icon in the bottom-left corner. Search “Formatter” and click the “Editor: Default Formatter” option. From the drop-down menu, select whichever code formatter you want to use. Scroll down a bit and check the box next to the “Editor: Format On Save” option.


2 Answers

The configuration to remove unused usings must be done through omnisharp.

Omnisharp settings can be available in two locations:

For global settings:

For global settings, use %USERPROFILE%/.omnisharp/omnisharp.json file

For project specific settings:

use omnisharp.json at the root of your workspace

Settings are the following:

{
     "FormattingOptions": {
         "OrganizeImports": true
     }
}

Other default settings available in 5/5/2020:

{
    "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
                          }
}
like image 117
Daniel Reis Avatar answered Oct 19 '22 23:10

Daniel Reis


I fear that, writing today, there's no plugin on the VSCode Marketplace - nor a built in setting/functionality offering what you want regarding "unused usings" behaviour like in the full blown Visual Studio.

My advice would be to ask for this functionality at the official Microsoft plugin called "OmniSharp" (default C# plugin that also powers other editor's C# functionality): https://github.com/OmniSharp/omnisharp-vscode/issues.
Side note: there is an issue open there about "unused usings", to disable the warnings it generates: https://github.com/OmniSharp/omnisharp-vscode/issues/315

Or to go to the VSCode GitHub issues page and ask for it there: https://github.com/microsoft/vscode/issues.

Or the last route would be to dive in and write your own plugin/extensions: https://code.visualstudio.com/docs/extensions/overview.

like image 23
Yves Schelpe Avatar answered Oct 20 '22 00:10

Yves Schelpe