EditorConfig入门

本文最后更新于:6 个月前

EditorConfig实际上是一个用于定义编码样式的文件.editorconfig,本文介绍了.editorconfig中能够配置的规则,以及规则匹配的语法机制。如果安装了Prettier,实际上.editorconfig的规则会被转化为Prettier的规则,并在Prettier中起作用

EditorConfig入门

官方文档:EditorConfig

一、EditorConfig入门

1. EditorConfig是什么

EditorConfig有助于为在不同编辑器和IDE中处理同一项目的多个开发人员维护一致的编码风格。EditorConfig项目由一个用于定义编码样式的文件格式和一组文本编辑器插件组成,这些插件使编辑器能够读取文件格式并遵守定义的样式。EditorConfig文件很容易阅读,并且可以很好地与版本控制系统配合使用。

2. EditorConfig文件长什么样

EditorConfig一般以.editorconfig文件的形式存在,并且还需要搭配插件才能在IDE(如VSCode)中使用,以下是一个文件示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

# Matches multiple files with brace expansion notation
# Set default charset
[*.{js,py}]
charset = utf-8

# 4 space indentation
[*.py]
indent_style = space
indent_size = 4

# Tab indentation (no size specified)
[Makefile]
indent_style = tab

# Indentation override for all JS under lib directory
[lib/**.js]
indent_style = space
indent_size = 2

# Matches the exact files either package.json or .travis.yml
[{package.json,.travis.yml}]
indent_style = space
indent_size = 2

如上:

  • .editorconfig文件顶部,必须写上root = true(文档中是这么解释的: Set to true to stop .editorconfig files search on current file.)
如果到达根文件路径或找到root=true的editorconfig文件,则将停止搜索.editorconfig文件。
  • 使用通配符,可以针对不同类型的文件使用不同的编辑风格(下面的内容覆盖上面)
  • .editorconfig文件的注释以#开头,必须单独放在一行,不能放在行尾
  • .editorconfig文件本身必须是UTF-8编码

3. path匹配规则

image-20230619130523266

4. 配置项

以下列举可以配置的项目

1
2
3
4
5
6
7
8
indent_style	# 缩进风格,tab或space
indent_size # 每个缩进单位的列数,当它被设置为tab时,将使用tab_width的值(如果tab_width指定了的话)
tab_width # 一个tab制表符代表的列数,默认为indent_size的值,通常不需要指定。
trim_trailing_whitespace # 删除换行符之前(即行尾)的任何空白字符,true 或 false
insert_final_newline # 用一个空白行结束文件,true 或 false
end_of_line # 行尾用什么符号结束,lf、cr、crlf
charset # 规定文件的字符集,utf-8、utf-8-bom、utf-16be、utf-16le、latinl
root # 在文件顶部声明的属性,如果设置为true,则编辑器对EditorConfig文件的搜索到这个文件停止
  • 没有在.editorconfig文件中配置的项,将使用IDE默认的配置
  • 将指定项设置为unset,可以清除所有之前的.editorconfig对它的设置,从而使用IDE默认值
  • 通常一些配置项是不用指定的。比如指定了indent_style = tab之后,就不用再指定indent_size这个配置了

EditorConfig Properties · editorconfig/editorconfig Wiki (github.com) 中还介绍了一些扩展的配置项(但是EditorConfig表示并不想标准化它们,不过可以使用),下面列举一些常用的:

1
2
3
4
5
6
quote_type		# 字符串的引号类型(限于支持多种引用符号的语言),single、double、auto

curly_bracket_next_line # 左侧大括号 { 是否要另起一行,true/false
spaces_around_brackets # 大括号{}前后是否加空格,none、inside、outside、both
spaces_around_operators # 运算符前后是否加空格,true、false、hybrid
indent_brace_style # 缩进风格,K&R、Allman、GNU、Horstmann等等,具体示例见下文

indent_brace_style缩进风格,下面通过示例展示不同的缩进风格

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// K&R
while (x == y) {
something();
somethingelse();
}

// Allman
while (x == y)
{
something();
somethingelse();
}

// GNU
while (x == y)
{
something();
somethingelse();
}

// Horstmann
while (x == y)
{ something();
somethingelse();
}

// Lisp
while (x == y) {
something();
somethingelse(); }

5. IDE插件

一些IDE不需要插件,即可解析.editorconfig文件。它们包括但不限于(只列举常见的):

  • IDEA
  • Pycharm
  • WebStorm
  • VisualStudio

但是另外一些IDE需要安装插件才能解析.editorconfig文件,如:

  • VSCode
image-20230909231331492

GitHub - editorconfig/editorconfig-vscode: EditorConfig extension for Visual Studio Code


EditorConfig入门
http://timegogo.top/2023/06/19/效率/规范化:EditorConfig入门/
作者
丘智聪
发布于
2023年6月19日
更新于
2023年9月9日
许可协议