EditorConfig 排版規則
在寫入或修改任何程式碼檔案時,必須遵守以下排版規則。
規則優先順序
- •專案 .editorconfig — 若專案根目錄(或任何父目錄)存在
.editorconfig,一律以其設定為準 - •範例 .editorconfig — 若專案未提供
.editorconfig,則依照本 skill 同目錄下的.editorconfig.example作為排版依據
步驟
在每次編輯或建立檔案之前:
- •從專案根目錄往上搜尋
.editorconfig檔案 - •若找到,解析其中對應檔案類型的設定(indent_style、indent_size、tab_width、end_of_line、charset、trim_trailing_whitespace、insert_final_newline 等),並嚴格遵守
- •若未找到
.editorconfig,讀取本 skill 目錄下的.editorconfig.example,並依照其中對應檔案類型的設定來排版
範例 .editorconfig 重點摘要
本 skill 附帶的 .editorconfig.example 包含以下預設:
全域預設
- •縮排方式:tab
- •tab 寬度:4
- •換行符號:lf
- •編碼:utf-8
- •移除行尾空白:是
- •檔案結尾加換行:是
語言專屬覆寫(僅限語法本身強制的規則)
| 檔案類型 | 縮排方式 | 縮排寬度 | 原因 |
|---|---|---|---|
*.go | tab | — | gofmt 強制 |
Makefile / *.mk | tab | — | 語法要求 |
*.py | space | 4 | PEP 8 規範 |
*.rs | space | 4 | rustfmt 預設 |
*.toml | space | 2 | 慣例 |
*.{yml,yaml} | space | 2 | YAML 規範不允許 tab |
特殊處理
| 檔案類型 | 規則 | 原因 |
|---|---|---|
*.{md,mdx} | 不移除行尾空白 | Markdown 行尾兩空白代表換行 |
*.{diff,patch} | 不移除行尾空白、不加結尾換行 | 避免破壞 diff 內容 |
重要提醒
- •修改既有檔案時:優先保持該檔案現有的縮排風格一致性。如果檔案已全部使用 spaces,繼續使用 spaces;已全部使用 tabs,繼續使用 tabs。不要在同一個檔案中混用
- •新建檔案時:依照上述優先順序決定排版方式
- •專案 .editorconfig 的設定永遠優先:即使範例 .editorconfig 有不同設定,只要專案有自己的
.editorconfig,一律以專案為準 - •不要在程式碼中加入排版相關的註解說明