VS Code无法用.editorconfig格式化C#文件,「格式化文档」功能无效
解决VS Code中C#项目EditorConfig格式化无效问题
开启C#扩展的EditorConfig支持
VS Code的C#扩展(ms-dotnettools.csharp)默认用Roslyn格式化,可能覆盖EditorConfig设置:- 打开设置(快捷键
Ctrl+,) - 搜索
omnisharp.enableEditorConfigSupport,确保勾选为true
- 打开设置(快捷键
确认EditorConfig文件位置
文件必须放在项目根目录(和.csproj同级),否则只会影响子目录文件,检查你的文件位置是否正确。排查编辑器缩进显示冲突
编辑器显示3个空格可能是全局设置覆盖了EditorConfig:- 打开设置,搜索
editor.tabSize和editor.insertSpaces - 将这两个选项设为
inherit,让编辑器优先读取EditorConfig配置
- 打开设置,搜索
重启Omnisharp服务
配置修改后需重启服务生效:- 打开命令面板(快捷键
Ctrl+Shift+P) - 执行
OmniSharp: Restart OmniSharp
- 打开命令面板(快捷键
调整EditorConfig配置(可选)
你的配置中indent_style = tab搭配indent_size = 3属于特殊组合,若想用3个空格替代tab,可修改为:[*.cs] indent_style = space indent_size = 3
内容的提问来源于stack exchange,提问作者Hung P.T.
相关产品推荐
相关产品推荐

