编辑器配置在VS Code中无法工作。

66
我使用VS Code作为编辑器。我们有一个包含格式配置的.editorconfig文件。我们所有人都使用扩展程序EditorConfig来格式化我们的HTML和CSS代码。我已经从这里安装了适用于VS Code的EditorConfig扩展程序:https://github.com/editorconfig/editorconfig-vscode 我们的.editorconfig文件如下所示:
# This is the top-most .editorconfig file (do not search in parent directories)
root = true

### All files
[*]
# Force charset utf-8
charset = utf-8
# Indentation
indent_style = tab
indent_size = 4
# line breaks and whitespace
insert_final_newline = true
trim_trailing_whitespace = true
# end_of_line = lf

### Frontend files
[*.{css,scss,less,js,json,ts,sass,php,html,hbs,mustache,phtml,html.twig}]

### Markdown
[*.md]
indent_style = space
indent_size = 4
trim_trailing_whitespace = false

### YAML
[*.yml]
indent_style = space
indent_size = 2

### Specific files
[{package,bower}.json]
indent_style = space
indent_size = 2

我找不到任何键盘快捷键,设置或其他东西。如何让我的扩展从.editorconfig文件中执行操作?


不支持“charset”,请投票:https://github.com/editorconfig/editorconfig-vscode/issues/35 https://github.com/microsoft/vscode/issues/824 - Thorsten Schöning
4个回答

93
我遇到的问题是我在我的VS Code中添加了扩展EditorConfig.EditorConfig,但没有安装npm包。仅仅将扩展添加到你的VS Code中是不够的,你还需要安装包,这样它才能运行。
我像这样全局安装了npm包:npm install -g editorconfig 之后,我添加了扩展并启用了它。现在它完美地工作了。
所需的npm包:https://www.npmjs.com/package/editorconfig 所需的VS Code扩展:https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig

9
这个解决方案对我也起作用了。真是疯狂,这个扩展程序没有检查并警告你! - johey
9
至少,我希望在“vscode”扩展的描述中找到这些信息......但我最终自己想出来了并且很幸运 :) Translated: At least, I hope to find this information in the description of the vscode extension... But I figured it out by myself in the end and was lucky :) - webta.st.ic
5
我仍然无法使用它。我已经安装了Node、editorconfig包和VSCode扩展,还缺少什么吗?是不是在VSCode设置中有问题? - Nachokhan
2
@Nachokhan请确保您没有任何“竞争”插件来处理样式,并覆盖您的editorconfig设置。 - AndreasW
@Nachokhan,我已经让它工作了。请看我的答案 - Valentine Shi

7
除了上述内容之外,我还需要打开“保存时编辑器格式”选项。
{
   "editor.formatOnSave": true
}

3

到2022年底,我必须根据自定义文件扩展名(*.openapi.json)设置VS Code 2个空格缩进,同时保持其他.json文件的4个空格缩进。

这里提供的其他解决方案并没有按照预期工作。以下是我成功实现的方法。

  1. 根据@webta.st.ic的回答,安装节点包(EditorConfig JavaScript Core)和VS Code扩展(EditorConfig for VS Code)。
  2. 在您的.editorconfig文件中,在文件顶部添加root = true(阅读它的作用)。
  3. 在VS Code中关闭自动缩进检测:Editor: Detect Indentation规则(Ctrs+Shift+P -> Preferences: Open user settings -> 搜索detect indentation -> 取消勾选复选框)。
  4. 您的.editorconfig应如下所示。请注意,与编辑器配置文档不同,扩展通配符应该有一个星号*。更多细节在这里
    root = true
    [*.openapi.json] 
    indent_style = space
    indent_size = 2
    
现在你已经让它正常工作了。
通过Prettier、其VS Code插件和Prettier配置文件中的overrides部分,还有另一种方法可以为具有自定义扩展名的文件分配不同的格式。按照提供的文档进行设置非常简单。

这是正确的解决方案,使用最新的VS代码更新覆盖了editorconfig缩进大小...对我有用。谢谢! - Slaven Tomac

1
我遇到了“Extension EditorConfig cannot format ...”的错误(在IDE底部,几秒钟后消失)。
更改首选项(ctrl+,,搜索format并将Editor:DefaultFormatter设置为None(而不是EditorConfig))解决了我的问题(是的,有趣的是,这确实启用了.editorconfig的使用-而不是任何其他格式规则)。 Codium: 1.60.2 with EditorConfig: v0.16.6

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接