You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode中C#输入时大括号无法自动换行的问题求助

解决VSCode中C#输入大括号回车后上括号不换行的问题

以下是针对你问题的具体排查和解决步骤:

  • 确保OmniSharp扩展为最新版本
    打开VSCode的扩展面板,找到「C#」扩展(由Microsoft发布),检查是否有更新,旧版本可能存在格式化逻辑的bug。

  • 正确配置omnisharp.json
    在项目根目录(和Assets同级)创建或修改omnisharp.json,写入以下配置:

    {
      "FormattingOptions": {
        "NewLinesForBracesInMethods": true,
        "NewLinesForBracesInProperties": true,
        "NewLinesForBracesInControlBlocks": true,
        "NewLinesForBracesInAnonymousMethods": true,
        "NewLinesForBracesInAnonymousTypes": true,
        "NewLinesForBracesInObjectCollectionArrayInitializers": true,
        "NewLinesForBracesInLambdaExpressionBody": true
      },
      "omnisharp.enableEditorConfigSupport": false,
      "omnisharp.useEditorFormattingSettings": false
    }
    

    这里强制关闭EditorConfig支持和VSCode格式化设置复用,确保OmniSharp完全使用自身配置的规则。

  • 锁定VSCode的C#格式化器
    打开VSCode的设置(Ctrl+,),搜索「settings.json」并打开编辑,添加以下配置:

    "editor.formatOnType": true,
    "[csharp]": {
      "editor.formatOnType": true,
      "editor.defaultFormatter": "ms-dotnettools.csharp"
    }
    

    明确指定C#文件的默认格式化器为官方C#扩展,避免其他插件干扰。

  • 重启OmniSharp服务器
    按下Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac),输入「OmniSharp: Restart OmniSharp」并执行,强制加载新的配置规则。

  • 排查.editorconfig冲突
    如果项目中存在.editorconfig文件,它的格式化规则会优先于omnisharp.json。要么删除该文件,要么修改其中的csharp_new_line_before_open_brace参数为all:

    csharp_new_line_before_open_brace = all
    
  • 禁用冲突插件
    检查是否安装了其他C#格式化相关插件(比如Prettier的C#扩展),暂时禁用这些插件,排除冲突可能。

完成以上步骤后,重新打开VSCode测试输入方法后敲{}回车的场景,应该能实现上下大括号自动换行的效果。

内容的提问来源于stack exchange,提问作者Vandal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 14:27:51