.editorconfig 程式碼風格配置

雪花飄發表於2019-02-15

前面寫了一篇部落格關於不同作業系統回車出現的 \r 對shell的影響,@Wi1dcard 評論建議使用 .editorconfig 來規範程式碼風格,以前在一些專案裡看過這個檔案,但是沒有仔細研究過個檔案,今天特地測試了一下這個配置。
editorconfig 說明可參考 https://editorconfig.org/

  • 以下編輯器不需要安裝外掛,只需要配置.editorconfig
    file
  • 以下編輯器需要安裝外掛和配置.editorconfig檔案
    file
  • 以下是phpstorm 安裝 editorconfig 流程
    file
    file
    file
    安裝完後重啟 phpstorm 就生效了
  • 在專案目錄下新增 .editorconfig 檔案,內容如下
#EditorConfig檔案使用INI格式。斜槓(/)作為路徑分隔符,#或者;作為註釋。路徑支援萬用字元:
#表明是最頂層的配置檔案,發現設為true時,才會停止查詢.editorconfig檔案
root = true
#* 匹配除/之外的任意字元
#**    匹配任意字串
#? 匹配任意單個字元
#[name]    匹配name字元
#[!name]   不匹配name字元
#[s1,s2,s3]    匹配給定的字串
#[num1..num2]  匹配num1到mun2直接的整數
[*]
#檔案的charset。有以下幾種型別:latin1, utf-8, utf-8-bom, utf-16be, utf-16le
charset = utf-8

#縮排使用 tab 或者 space
indent_style = space

#縮排為 space 時,縮排的字元數
indent_size = 2

#縮排為 tab 時,縮排的寬度
#tab_width = 2

#換行符的型別。lf, cr, crlf三種
end_of_line = lf

#是否將行尾空格自動刪除
trim_trailing_whitespace = true

#是否使檔案以一個空白行結尾
insert_final_newline = true

把 end_of_line 值改為 cr,修改檔案並傳到 linux,vi 開啟檔案
file
把 end_of_line 值改為 crlf, 修改檔案並傳到 linux, vi 開啟檔案
file
把 end_of_line 值改為 lf,變動檔案儲存並上傳到linux,vi 開啟檔案
file
trim_trailing_whitespace = true 行尾的空格會被去掉
insert_final_newline = true 時,總會在末尾新增一個 \n

  • 雖然.editorconfig 提供的配置比較少,但個人覺得這個回車換行的約束很有必要!!!
本作品採用《CC 協議》,轉載必須註明作者和本文連結

雪花飄

相關文章