Setting C# formatting options for OmniSharp on Visual Studio Code?
Solution 1
Just got this to work using the latest omnisharp (dev branch) and the omnisharp.json
(pasted below) in the same folder as my project's .sln
.
It should work with all releases since v1.9-beta18, I just compiled from source because I don't use a supported system.
{
"FormattingOptions": {
"newLine": "\n",
"useTabs": false,
"tabSize": 4,
"indentationSize": 4,
"NewLinesForBracesInTypes": false,
"NewLinesForBracesInMethods": false,
"NewLinesForBracesInProperties": false,
"NewLinesForBracesInAccessors": false,
"NewLinesForBracesInAnonymousMethods": false,
"NewLinesForBracesInControlBlocks": false,
"NewLinesForBracesInAnonymousTypes": false,
"NewLinesForBracesInObjectCollectionArrayInitializers": false,
"NewLinesForBracesInLambdaExpressionBody": false,
"NewLineForElse": false,
"NewLineForCatch": false,
"NewLineForFinally": false,
"NewLineForMembersInObjectInit": false,
"NewLineForMembersInAnonymousTypes": false,
"NewLineForClausesInQuery": false,
}
}
The available properties are listed in FormattingOptions.cs
in the omnisharp-roslyn
repository.
Solution 2
From Configuration Options on the omnisharp-roslyn
wiki:
At startup, OmniSharp obtains the configuration options using the following (hierarchical) order:
- its own hardcoded defaults
- Environment variables
- Command line arguments
- An
omnisharp.json
file located in%USERPROFILE%/.omnisharp/
- An
omnisharp.json
file located in the working directory which OmniSharp has been pointed atEach of the configuration sources, can overwrite any of the settings set by the previous source.
To summarize the above configuration locations according to a blog article by one of the developers:
- The defaults are specified in
config.json
in the OmniSharp extension's directory. It is not recommend to modify this file. - Neither environment variables nor command line parameters are really applicable/useful for the C# extension.
- Place
omnisharp.json
in%USERPROFILE%\.omnisharp\
(or~/.omnisharp/
) for user-specific settings. - Place
omnisharp.json
in a project directory for project-specific settings. - At each level you override individual settings; you don't need to repeat the entire configuration.
Testing with v1.21.11 of the ms-vscode.csharp
extension on Visual Code v1.42.0, it seems OmniSharp only applies omnisharp.json
found in the root of a workspace folder, not descendant directories.
The C# extension for Visual Studio Code also supports EditorConfig
, which you can enable via one of the following methods:
-
File
→Preferences
→Settings
→Extensions
→C# configuration
→OmniSharp: Enable Editor Config Support
- In
settings.json
...{ "omnisharp.enableEditorConfigSupport": true, }
- In
omnisharp.json
{ "FormattingOptions": { "enableEditorConfigSupport": true } }
Solution 3
Linux users:
- Go to Home directory > .omnisharp > create omnisharp.json
- Enter the above given code
This is the global solution for those who don't want to repeat the steps over and over for each project.
IMPORTANT: Choosing the right Linux version for installing dotNet SDK from doc.microsoft.com/... matters! Otherwise omnisharp will not get installed properly and the above code won't work.
csells
Updated on June 07, 2022Comments
-
csells about 2 years
I'm attempting to take advantage of the integration with Visual Studio Code, but can't figure out how to set the C# formatting options. The
config.json
right next to the OmniSharp exe on my Mac (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/jrieken.vscode-omnisharp/bin/packages/OmniSharp/config.json
) doesn't match the standard OmniSharpconfig.json
format, so setting the brace + newline behavior properties isn't working, e.g.methodBraceStyle
. It does work to set thetabSize
, etc., however. -
Scott Hather almost 3 yearsPutting the onmisharp.json file in the project root didn't work for me, but this did work (Manjaro Linux), thank you.
-
Raoul L'artiste about 2 yearsI'm googling like an idiot, trying to figure out what this magical Home directory should be. My project folder? VS Code installation folder?