about : .editorconfig https://editorconfig.org/ powered by shuke 20230922
EditorConfig
在多人开发的项目中,不能保证团队成员都使用相同的编辑器,那么不同的编辑器之间的格式设置各不相同,无法保证项目代码格式的统一,往往会出现意想不到的问题。
比如编辑器中一个制表符等于的空格数的大小(即缩进大小),各编辑器中及各个开发人员设置的大小是不一致的,那么如何保证团队代码在多人协同开发,使用编辑器格式不一的情况下仍然保证相同的格式呢,这就是EditorConfig 要做的事情。
在项目中创建编辑配置文件
.editorconfig (webstorm vscode ...)
# 告诉EditorConfig插件,这是根文件,不用继续往上查找
root = true
# 匹配全部文件
[*]
# 设置字符集
charset = utf-8
# 缩进风格,可选space、tab
indent_style = space
# 缩进的空格数
indent_size = 2
# 结尾换行符,可选lf、cr、crlf
end_of_line = lf
# 在文件结尾插入新行
insert_final_newline = true
# 删除一行中的前后空格
trim_trailing_whitespace = true
# 匹配md结尾的文件
[*.md]
insert_final_newline = false
trim_trailing_whitespace = false
配置格式工具.editorconfig方案
格式化编译代码的格式化风格 .editorconfig格式化高于编译器的格式化规则
安装
全局安装
npm install -g editorconfig
项目安装
npm install -D editorconfig
不安装的可能会导致.editorconfig无法正常解析,尚未验证
三、注释语法
使用井号(#)或分号(;)为注释符号,注释需要与注释符号在通一行内
四、匹配文件语法
1.语法
[需要匹配的文件]
2.通配符
* 匹配除/之外的任意字符串
** 匹配任意字符串
? 匹配任意单个字符
[name] 匹配name中的任意一个单一字符
[!name] 匹配不存在name中的任意一个单一字符
{s1,s2,s3} 匹配给定的字符串中的任意一个(用逗号分隔)
{num1..num2} 匹配num1到num2之间的任意一个整数, 这里的num1和num2可以为正整数也可以为负整数
五、属性和属性值语法
属性 | 属性值 | 描述 |
---|
indent_style | tab是硬缩进,space为软缩进 | 设置缩进风格 |
indent_size | 如果indent_style为tab,则此属性默认为tab_width | 用一个整数定义的列数来设置缩进的宽度 |
tab_width | 默认是indent_size | 用一个整数来设置tab缩进的列数 |
end_of_line | 值为lf、cr和crlf | 设置换行符 |
charset | latin1、utf - 8、utf - 8 - bom(不建议)、utf - 16be和utf - 16le | 设置编码 |
trim_trailing_whitespace | boolean | 是否去除换行行首的任意空白字符 |
insert_final_newline | 设为true表示使文件以一个空白行结尾 | |
root | boolean | 是否最顶层 |
六、vscode编译器使用
vscode默认是不执行.editorconfig, 需要安装 ** EditorConfig for VS Code ** 插件
七、.editoeconfig例子
# editorconfig顶级配置文件,停止向上寻找配置文件
root = true
# 匹配所有文件
[*]
# 缩进样式=空格 【space:空格,tab:tab键】
indent_style = space
# 缩进大小=2
indent_size = 2
# 换行符类型 = lf 【lf,cr,crlf】
end_of_line = lf
# 字符集=utf-8 【latin1、utf - 8、utf - 8 - bom(不建议)、utf - 16be和utf - 16le】
charset = utf-8
# 删除行尾空格 = 是
trim_trailing_whitespace = true
# 插入最后一行=真
insert_final_newline = true
# 匹配markdown文件
[*.md]
# 删除行尾空格 = 否
trim_trailing_whitespace = false
# 匹配package.json文件
[package.json]
# 缩进样式=空格
indent_style = space
# 缩进大小=2
indent_size = 2