我使用 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
文件中的内容?
我遇到的问题是我向 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
此外,我还需要打开“保存时编辑器格式”选项。
{
"editor.formatOnSave": true
}
2022 年末,我必须为每个自定义文件扩展名设置 VS Code 2 个空格缩进 (
*.openapi.json
),同时保持其他 .json
文件缩进 4 个空格。
这里的其他解决方案没有按书面方式工作。这是我如何让它发挥作用的。
.editorconfig
文件中,在文件顶部添加 root = true
(阅读它的作用)。Editor: Detect Indentation
规则(Ctrs+Shift+P
-> 首选项:打开用户设置 -> 搜索 detect indentation
-> 取消选中复选框)。.editorconfig
应如下所示。请注意,与编辑器配置文档不同,扩展通配符应带有星号*
。更多详细信息这里。
root = true
[*.openapi.json]
indent_style = space
indent_size = 2
现在你就可以使用了。
还有另一种方法可以通过 Prettier、其 VS Code 插件 和 Prettier 配置
overrides
部分为具有自定义扩展名的文件分配不同的格式。按照给定的文档进行设置非常容易。
我收到错误“Extension EditorConfig 无法格式化...”(位于 IDE 底部,几秒钟后消失)。
更改首选项(
ctrl+,
,搜索format
并将Editor:DefaultFormatter
设置为None
(而不是EditorConfig
))为我解决了这个问题(是的,很有趣,这确实启用了使用.editorconfig
- 不是任何其他格式规则)
Codium:1.60.2 与 EditorConfig:v0.16.6
对我来说,以上都不是答案。就我而言,我有几行这样的:
[*.{json}]
indent_style = space
indent_size = 2
出于某种原因,VS Code 不理解这一点。但如果我将其更改为:
[*.json]
indent_style = space
indent_size = 2
或
[*.{json,someotherformat}]
indent_style = space
indent_size = 2
然后就成功了。
¯\_(ツ)_/¯