語言配置指南
contributes.languages 貢獻點允許你定義一個語言配置,該配置控制以下宣告式語言功能:
- 註釋切換
- 括號定義
- 自動閉合
- 自動環繞
- 摺疊
- 單詞模式
- 縮排規則
這是一個 語言配置示例,它配置了 JavaScript 檔案的編輯體驗。本指南將解釋 language-configuration.json 的內容。
注意:如果你的語言配置檔名是 language-configuration.json 或以它結尾,你將在 VS Code 中獲得自動完成和驗證功能。
{
"comments": {
"lineComment": "//",
"blockComment": ["/*", "*/"]
},
"brackets": [
["{", "}"],
["[", "]"],
["(", ")"]
],
"autoClosingPairs": [
{ "open": "{", "close": "}" },
{ "open": "[", "close": "]" },
{ "open": "(", "close": ")" },
{ "open": "'", "close": "'", "notIn": ["string", "comment"] },
{ "open": "\"", "close": "\"", "notIn": ["string"] },
{ "open": "`", "close": "`", "notIn": ["string", "comment"] },
{ "open": "/**", "close": " */", "notIn": ["string"] }
],
"autoCloseBefore": ";:.,=}])>` \n\t",
"surroundingPairs": [
["{", "}"],
["[", "]"],
["(", ")"],
["'", "'"],
["\"", "\""],
["`", "`"]
],
"folding": {
"markers": {
"start": "^\\s*//\\s*#?region\\b",
"end": "^\\s*//\\s*#?endregion\\b"
}
},
"wordPattern": "(-?\\d*\\.\\d\\w*)|([^\\`\\~\\!\\@\\#\\%\\^\\&\\*\\(\\)\\-\\=\\+\\[\\{\\]\\}\\\\\\|\\;\\:\\'\\\"\\,\\.\\<\\>\\/\\?\\s]+)",
"indentationRules": {
"increaseIndentPattern": "^((?!\\/\\/).)*(\\{[^}\"'`]*|\\([^)\"'`]*|\\[[^\\]\"'`]*)$",
"decreaseIndentPattern": "^((?!.*?\\/\\*).*\\*/)?\\s*[\\)\\}\\]].*$"
}
}
註釋切換
VS Code 提供了兩個用於註釋切換的命令:切換行註釋 和 切換塊註釋。你可以指定 comments.blockComment 和 comments.lineComment 來控制 VS Code 如何註釋掉行/塊。
{
"comments": {
"lineComment": "//",
"blockComment": ["/*", "*/"]
}
}
括號定義
當你的游標移到一個在這裡定義的括號時,VS Code 會高亮顯示該括號及其匹配的配對。
{
"brackets": [
["{", "}"],
["[", "]"],
["(", ")"]
]
}
此外,當你執行 轉到括號 或 選擇到括號 命令時,VS Code 將使用上述定義來查詢最近的括號及其匹配的配對。
自動閉合
當你鍵入 ' 時,VS Code 會建立一個單引號對,並將你的游標放在中間:'|'。此部分定義了這樣的對。
{
"autoClosingPairs": [
{ "open": "{", "close": "}" },
{ "open": "[", "close": "]" },
{ "open": "(", "close": ")" },
{ "open": "'", "close": "'", "notIn": ["string", "comment"] },
{ "open": "\"", "close": "\"", "notIn": ["string"] },
{ "open": "`", "close": "`", "notIn": ["string", "comment"] },
{ "open": "/**", "close": " */", "notIn": ["string"] }
]
}
notIn 鍵會在特定程式碼範圍內停用此功能。例如,當你編寫以下程式碼時
// ES6's Template String
`ES6's Template String`;
單引號將不會被自動閉合。
不需要 notIn 屬性的配對也可以使用更簡單的語法。
{
"autoClosingPairs": [
["{", "}"],
["[", "]"]
]
}
使用者可以透過 editor.autoClosingQuotes 和 editor.autoClosingBrackets 設定來調整自動閉合行為。
在之前自動閉合
預設情況下,VS Code 僅在游標右側有空格時才自動閉合配對。因此,當你在以下 JSX 程式碼中鍵入 { 時,你不會得到自動閉合。
const Component = () =>
<div className={>
^ Does not get autoclosed by default
</div>
然而,此定義會覆蓋該行為。
{
"autoCloseBefore": ";:.,=}])>` \n\t"
}
現在,當你鍵入 { 緊跟在 > 之前時,VS Code 會用 } 自動閉合它。
自動環繞
當你在 VS Code 中選擇一個範圍並鍵入一個開括號時,VS Code 會用一對括號環繞選定的內容。此功能稱為自動環繞,你可以在此處為特定語言定義自動環繞配對。
{
"surroundingPairs": [
["{", "}"],
["[", "]"],
["(", ")"],
["'", "'"],
["\"", "\""],
["`", "`"]
]
}
使用者可以透過 editor.autoSurround 設定來調整自動環繞行為。
摺疊
在 VS Code 中,摺疊是透過基於縮排定義的,或者由貢獻的摺疊範圍提供者定義的。
- 基於縮排的摺疊(帶標記):如果給定的語言沒有可用的摺疊範圍提供者,或者使用者將
editor.foldingStrategy設定為indentation,則使用基於縮排的摺疊。當一行比其後的一行或多行縮排較小時,摺疊區域開始;當一行與後續行具有相同或更小的縮排時,摺疊區域結束。空行將被忽略。此外,語言配置還可以定義開始和結束標記。這些在folding.markers中定義為start和end正則表示式。當找到匹配的行時,會在配對內部建立一個摺疊範圍。摺疊標記必須非空,通常看起來像//#region和//#endregion。
以下 JSON 為 //#region 和 //#endregion 建立了摺疊標記。
{
"folding": {
"markers": {
"start": "^\\s*//\\s*#?region\\b",
"end": "^\\s*//\\s*#?endregion\\b"
}
}
}
- 語言伺服器摺疊:語言伺服器響應
textDocument/foldingRange請求,並提供一個摺疊範圍列表,VS Code 會將這些範圍渲染為摺疊標記。在 程式語言功能 主題中瞭解有關語言伺服器協議中摺疊支援的更多資訊。
單詞模式
wordPattern 定義了程式語言中什麼是單詞。如果設定了 wordPattern,程式碼建議功能將使用此設定來確定單詞邊界。請注意,此設定不會影響與單詞相關的編輯器命令,這些命令由編輯器設定 editor.wordSeparators 控制。
{
"wordPattern": "(-?\\d*\\.\\d\\w*)|([^\\`\\~\\!\\@\\#\\%\\^\\&\\*\\(\\)\\-\\=\\+\\[\\{\\]\\}\\\\\\|\\;\\:\\'\\\"\\,\\.\\<\\>\\/\\?\\s]+)"
}
縮排規則
indentationRules 定義了編輯器在輸入、貼上和移動行時應如何調整當前行或下一行的縮排。
{
"indentationRules": {
"increaseIndentPattern": "^((?!\\/\\/).)*(\\{[^}\"'`]*|\\([^)\"'`]*|\\[[^\\]\"'`]*)$",
"decreaseIndentPattern": "^((?!.*?\\/\\*).*\\*/)?\\s*[\\)\\}\\]].*$"
}
}
例如,if (true) { 匹配 increaseIndentPattern,然後如果你在開括號 { 後按 Enter,編輯器將自動增加一次縮排,你的程式碼將變成這樣:
if (true) {
console.log();
除了 increaseIndentPattern 和 decreaseIndentPattern 之外,還有兩個其他縮排規則:
indentNextLinePattern- 如果一行匹配此模式,則**僅下一行**應增加一次縮排。unIndentedLinePattern- 如果一行匹配此模式,則其縮排不應更改,並且不應根據其他規則進行評估。
如果未為程式語言設定縮排規則,編輯器將在行以開括號結尾時增加縮排,在鍵入閉括號時減少縮排。這裡的括號由 brackets 定義。
請注意,editor.formatOnPaste 設定由 DocumentRangeFormattingEditProvider 控制,不受自動縮排影響。
Enter 鍵規則
onEnterRules 定義了一個規則列表,這些規則將在編輯器中按下 Enter 鍵時進行評估。
{
"onEnterRules": [
{
"beforeText": "^\\s*(?:def|class|for|if|elif|else|while|try|with|finally|except|async).*?:\\s*$",
"action": { "indent": "indent" }
}
]
}
按下 Enter 鍵時,將檢查游標之前、之後或一行之上的文字是否與以下屬性匹配:
beforeText(必需)。一個匹配游標之前文字(僅限於當前行)的正則表示式。afterText。一個匹配游標之後文字(僅限於當前行)的正則表示式。previousLineText。一個匹配游標上一行文字的正則表示式。
如果所有指定的屬性都匹配,則認為規則匹配,並且不會再評估其他 onEnterRules。onEnterRule 可以指定以下操作:
indent(必需)。none, indent, outdent, indentOutdent中的一個。none表示新行將繼承當前行的縮排。indent表示新行將相對於當前行進行縮排。outdent表示新行將相對於當前行進行縮排。indentOutdent表示將插入兩行新行,一行縮排,另一行縮排。
appendText。一個將在新行和縮排之後追加的字串。removeText。要從新行的縮排中移除的字元數。