vscode代码对齐配置文件优先级

271 阅读1分钟

处理项目代码对齐时,发现通过全局的vscode setting.json的改动并不生效

排查后发现是配置优先级导致的,下面是常见的配置优先级顺序:

代码对齐相关的配置文件一般有以下几个:

.prettierrc > .editor > .vscode/settings.json > vscode全局的settings.json 优先级从高到低

  1. prettier插件的配置
    • 项目下的.prettierrc文件,可以配置prettier插件的对齐属性,如:
semi: false
singleQuote: true
printWidth: 80
trailingComma: 'none'
arrowParens: 'avoid'

2. 项目下的.editorconfig文件,具体配置见介绍https://editorconfig.org/

# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab

  1. 项目下的.vscode/setting
    • .vscode下的配置文件有多个,可见blog.csdn.net/qq_46032105…
    • 我个人是用的prettier,因此在项目.vscode/extensitons.json下配置了
    {
      // 优先使用prettier,没有则用volar
      "recommendations": ["esbenp.prettier-vscode","Vue.volar"]
    }

注意,当存在.prettier或者.editorconfig文件时,.vscode下的配置与vscode全局配置将失效,这是为了保证不同编辑器(如IntelJ等)下代码对齐效果一致

也就是说,只有不存在.prettier.editorconfig文件,vscode编辑器专用的配置才会有效


另外,出发后,可在右下角查看完整的配置

image.png