18

我正在尝试利用与 Visual Studio Code 的集成,但不知道如何设置 C# 格式选项。config.json我的 Mac ( ) 上 OmniSharp exe 旁边的右侧与标准 OmniSharp格式/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/jrieken.vscode-omnisharp/bin/packages/OmniSharp/config.json不匹配,因此设置大括号 + 换行行为属性不起作用,例如. 但是,它确实可以设置等。config.jsonmethodBraceStyletabSize

4

3 回答 3

28

刚刚使用最新的omnisharp(开发分支)和omnisharp.json(粘贴在下面)与我的项目位于同一文件夹中的.sln. 它应该适用于v1.9-beta18 以来的所有版本,我只是从源代码编译,因为我不使用受支持的系统。

{
    "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,
    }
}

可用属性列在FormattingOptions.cs存储omnisharp-roslyn中。

于 2016-10-29T21:48:43.363 回答
9

wiki上的配置选项omnisharp-roslyn

在启动时,OmniSharp 使用以下(分层)顺序获取配置选项:

  • 它自己的硬编码默认值
  • 环境变量
  • 命令行参数
  • 一个omnisharp.json文件位于%USERPROFILE%/.omnisharp/
  • omnisharp.jsonOmniSharp 指向的工作目录中的文件

每个配置源都可以覆盖前一个源设置的任何设置。

根据其中一位开发人员的博客文章总结上述配置位置:

  • 默认值config.json在 OmniSharp 扩展的目录中指定。不建议修改此文件。
  • 环境变量和命令行参数都不适用于 C# 扩展。
  • 放置omnisharp.json%USERPROFILE%\.omnisharp\(或~/.omnisharp/)中以进行用户特定的设置。
  • 放置omnisharp.json在项目目录中以进行项目特定的设置。
  • 在每个级别上,您都覆盖了单独的设置;您无需重复整个配置。

在 Visual Code v1.42.0 上使用扩展的 v1.21.11 进行测试ms-vscode.csharp,似乎 OmniSharp 仅适用omnisharp.json于工作区文件夹的根目录,而不是后代目录

Visual Studio Code的C# 扩展也支持EditorConfig,您可以通过以下方法之一启用:

  • FilePreferencesSettingsExtensionsC# configurationOmniSharp: Enable Editor Config Support
  • settings.json...
    {
        "omnisharp.enableEditorConfigSupport": true,
    }
    
  • omnisharp.json
    {
        "FormattingOptions": {
            "enableEditorConfigSupport": true
        }
    }
    
于 2020-02-07T23:33:57.467 回答
1

Linux用户:

  1. 转到主目录> .omnisharp > 创建omnisharp.json
  2. 输入上面给出的代码

对于那些不想为每个项目一遍又一遍地重复步骤的人来说,这是一个全球性的解决方案。

重要提示:为从doc.microsoft.com/...安装 dotNet SDK 选择正确的 Linux 版本很重要!否则,omnisharp 将无法正确安装,并且上述代码将无法正常工作。

于 2021-07-28T22:08:17.393 回答