## 一、EditorConfig

### 1. 是什么？

我们直接看官网：

> [EditorConfig](https://editorconfig.org/) helps maintain consistent coding styles for multiple developers working on the same project across various editors and IDEs. The EditorConfig project consists of **a file format** for defining coding styles and a collection of **text editor plugins** that enable editors to read the file format and adhere to defined styles. EditorConfig files are easily readable and they work nicely with version control systems.

EditorConfig 文件的名称为 *.editorconfig*，它不是一个软件，而是一个自定义文件。该文件用于定义项目的编码规范，编辑器的行为会与 *.editorconfig* 文件中定义的一致，并且其优先级比编辑器自身的设置要高。

### 2. 示例文件

以下是一个示例 `.editorconfig` 文件，用于设置 Python 和 JavaScript 文件的行尾和缩进样式。

```ini
# EditorConfig is awesome: https://editorconfig.org

# 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
```

### 3. `.editconfig` 文件放在哪里

打开文件时，EditorConfig 插件会在打开文件所在的目录及其每个父目录中查找名为 `.editorconfig`（全小写）的文件。当到达根文件路径或找到包含 `root=true` 的 EditorConfig 文件时，对 `.editorconfig` 文件的搜索将停止。

EditorConfig 文件从上到下读取，最新找到的规则优先。匹配的 EditorConfig 部分中的属性会按照读取顺序应用，因此较近的文件中的属性优先。

**Windows 用户注意：** 要在 Windows 资源管理器中创建 `.editorconfig` 文件，需要创建一个名为 `.editorconfig.` 的文件（注意末尾的点），Windows 资源管理器会自动将其重命名为 `.editorconfig`。

## 二、文件格式详情

EditorConfig 文件采用与 [Python configparser 库](https://docs.python.org/3/library/configparser.html) 所用格式兼容的 INI 格式，但 `[` 和 `]` 允许出现在节名称中。节名称是文件路径 [通配符](https://en.wikipedia.org/wiki/Glob_(programming))（区分大小写），类似于 [gitignore](https://git-scm.com/docs/gitignore#_pattern_format) 接受的格式。仅使用正斜杠（`/`，而非反斜杠）作为路径分隔符，井号（`#`）或分号（`;`）用于注释。注释应单独成行。EditorConfig 文件应以 UTF-8 编码，行分隔符可以是 `CRLF` 或 `LF`。EditorConfig 文件从上到下读取，最新找到的规则优先。

### 1. 通配符

通配符匹配中节名所识别的特殊字符：

|字符                  |说明                                           |
| -------------- | ------------------------------------------------------------ |
| `*`            | 匹配任何字符串，但不包括路径分隔符（`/`） |
| `**`           | 匹配任意字符串                      |
| `?`            | 匹配任何单个字符                         |
| `[name]`       | 匹配 *name* 中的任意单个字符       |
| `[!name]`      | 匹配不在 *name* 中的任何单个字符 |
| `{s1,s2,s3}`   | 匹配所提供的任何字符串（用逗号分隔）（**自 EditorConfig Core 0.11.0 起可用**） |
| `{num1..num2}` | 匹配 *num1* 和 *num2* 之间的任何整数，其中 num1 和 num2 可以是正数或负数 |

特殊字符可以用反斜杠转义，这样它们就不会被解释为通配符模式。

### 2. 支持的属性

请注意，并非所有属性都被每个插件支持。Github Wiki 上有一份完整的属性列表：[EditorConfig Properties · editorconfig/editorconfig Wiki · GitHub](https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties)

- `indent_style`：设置为 `tab` 或 `space`，分别使用硬制表符或软制表符。
- `indent_size`：一个整数，用于定义每个 **缩进** 级别所用的列数以及软制表符的宽度（在支持的情况下）。当设置为 `tab` 时，将使用 **`tab_width`** 的值（若已指定）。
- `tab_width`：一个整数，用于定义表示制表符的列数。默认值为 `indent_size` </b1 的值，通常无需指定。
- `end_of_line`：设置为 `lf`、`cr` 或 `crlf`，以控制换行符的表示方式。
- `charset`：设置为 `latin1`、`utf-8`、`utf-8-bom`、`utf-16be` 或 `utf-16le` 以控制字符集。
- `trim_trailing_whitespace`：设置为 `true` 可移除换行符前的所有空白字符，设置为 `false` 则保留这些空白字符。
- `insert_final_newline`：设置为 `true` 可确保保存文件时以换行符结尾，设置为 `false` 则确保不以换行符结尾。
- `root`：应在文件顶部、任何节之外指定的特殊属性。设置为 `true` 可停止在当前文件中搜索 `.editorconfig` 文件。

目前所有的属性和值都是不区分大小写的。它们在解析时会被转换为小写。通常情况下，如果未指定某个属性，将使用编辑器设置，也就是说，EditorConfig 对该部分不起作用。对于任何属性，值为 `unset` 意味着移除该属性的效果，即使它之前已被设置。例如，添加 `indent_size = unset` 可以取消定义 **`indent_size`** 属性（并使用编辑器默认值）。

可以接受且通常更建议不指定某些 EditorConfig 属性。例如，除非 **`tab_width`** 与 **`indent_size`** 的值不同，否则无需指定它。此外，当 **`indent_style`** 设置为 `tab` 时，可能最好不指定 **`indent_size`**，这样读者可以使用他们偏好的缩进宽度来查看文件。另外，如果某个属性在你的项目中没有标准化（例如 **`end_of_line`**），最好将其留空。

## 三、EditorConfig for VS Code

### 1. 简介

[editorconfig/editorconfig-vscode](https://github.com/editorconfig/editorconfig-vscode) 是在 vscode 中使用 EditorConfig 的插件，此插件会尝试用在.editorconfig 文件中找到的设置覆盖用户/工作区设置。不需要额外的或特定于 vscode 的文件。与任何 EditorConfig 插件一样，如果未指定 root = true，EditorConfig 会继续在项目外部查找.editorconfig 文件。

### 2. 扩展安装

- Github 仓库是：[GitHub - editorconfig/editorconfig-vscode: EditorConfig extension for Visual Studio Code · GitHub](https://github.com/editorconfig/editorconfig-vscode)
- VSIX 下载：

```shell
editorconfig:
  publisher: editorconfig
  extension: editorconfig
  version: 0.17.4
  vsix-url: https://marketplace.visualstudio.com/_apis/public/gallery/publishers/editorconfig/vsextensions/editorconfig/0.17.4/vspackage
```

### 3. 扩展配置

这个也没什么可配置的，会自动识别 `.editconfig` 文件。

