Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 447
csells Avatar asked Dec 22 '15 03:12

csells


2 Answers

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.

like image 83
igorrafael Avatar answered Sep 25 '22 08:09

igorrafael


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
        }
    }
    
like image 40
Lance U. Matthews Avatar answered Sep 26 '22 08:09

Lance U. Matthews