【问题标题】:Setting C# formatting options for OmniSharp on Visual Studio Code?在 Visual Studio Code 上为 OmniSharp 设置 C# 格式选项?
【发布时间】:2016-03-28 06:00:10
【问题描述】:

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

【问题讨论】:

  • 你有没有解决这个问题,我花了半天时间才试图正确设置我的 C# 大括号

标签: visual-studio-code code-formatting omnisharp


【解决方案1】:

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

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

可用属性在omnisharp-roslyn repository 中的FormattingOptions.cs 中列出。

【讨论】:

    【解决方案2】:

    来自Configuration Options on the omnisharp-roslyn wiki:

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

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

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

    根据a blog article by one of the developers总结以上配置位置:

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

    在 Visual Code v1.42.0 上使用 ms-vscode.csharp 扩展的 v1.21.11 进行测试,似乎 OmniSharp 仅适用于在工作区文件夹 not descendant directories 的根目录中找到的 omnisharp.json。

    Visual Studio Code also supports EditorConfig 的 C# extension,您可以通过以下方法之一启用:

    • File → Preferences → Settings → Extensions → C# configuration → OmniSharp: Enable Editor Config Support
    • 在settings.json...
      {
          "omnisharp.enableEditorConfigSupport": true,
      }
      
    • 在omnisharp.json
      {
          "FormattingOptions": {
              "enableEditorConfigSupport": true
          }
      }
      

    【讨论】:

      【解决方案3】:

      Linux 用户:

      1. 转到主目录 > .omnisharp > 创建omnisharp.json
      2. 输入above given code

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

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

      【讨论】:

      • 将 onmisharp.json 文件放在项目根目录中对我不起作用,但这确实有效(Manjaro Linux),谢谢。
      猜你喜欢
      • 2017-03-29
      • 2018-11-16
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      • 2016-07-15
      • 2015-07-15
      • 2017-02-10
      相关资源
      最近更新 更多