Setting C# formatting options for OmniSharp on Visual Studio Code?
Asked Answered
Y

5

28

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.

Yl answered 22/12, 2015 at 3:15 Comment(1)
Did you ever solve this, I've spent half a day just trying to get my C# braces set correctlySelfrestraint
A
34

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.

Arsenious answered 29/10, 2016 at 21:48 Comment(0)
A
15

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 at

Each 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:

  • FilePreferencesSettingsExtensionsC# configurationOmniSharp: Enable Editor Config Support
  • In settings.json...
    {
        "omnisharp.enableEditorConfigSupport": true,
    }
    
  • In omnisharp.json
    {
        "FormattingOptions": {
            "enableEditorConfigSupport": true
        }
    }
    
Americaamerican answered 7/2, 2020 at 23:33 Comment(0)
J
13

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

Jankey answered 17/11, 2022 at 22:35 Comment(1)
Thank you! I've been pulling my hair for 5 hours trying to get the config to work. This was the missing piece.Edible
F
6

Linux users:

  1. Go to Linux Home directory
  2. Open the .omnisharp folder (it's hidden by default so check the "Show Hidden Files" of your file explorer)
  3. Create a file omnisharp.json
  4. 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.

Filamentous answered 28/7, 2021 at 22:8 Comment(4)
Putting the onmisharp.json file in the project root didn't work for me, but this did work (Manjaro Linux), thank you.Altagraciaaltaic
I'm googling like an idiot, trying to figure out what this magical Home directory should be. My project folder? VS Code installation folder?Consonantal
Answer states "Linux users", so i presume it is your user's home directory, usually /home/username.Haunt
I'm on a Debian based Linux which has a Home directory by default. It's neither your project's nor the VSCode's folder.Filamentous
P
1

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.

Peruzzi answered 27/5, 2023 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.