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 OmniSharp config.json
format, so setting the brace + newline behavior properties isn't working, e.g. methodBraceStyle
. It does work to set the tabSize
, etc., however.
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.
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 } }
If anyone is having issues (like I was earlier today) with Omnisharp not recognizing the omnisharp.json file put the following in your vscode settings.json
:
"omnisharp.enableEditorConfigSupport": false
and Restart Omnisharp
Here is the link for better information on this: https://github.com/OmniSharp/omnisharp-vscode/issues/5446#issuecomment-1308655891
Linux users:
- Go to Linux Home directory
- Open the .omnisharp folder (it's hidden by default so check the "Show Hidden Files" of your file explorer)
- Create a file 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.
/home/username
. –
Haunt For me the thing was that I had Prettier installed and configured as the default formatter (editor.defaultFormatter
). Once I changed this setting and restarted Omnisharp, the formatting worked.
It's also important notice that the setting omnisharp.enableEditorConfigSupport
should be set to false
(as g0nk pointed in their answer), otherwise it won't work.
© 2022 - 2024 — McMap. All rights reserved.