程式碼片段指南
contributes.snippets
貢獻點允許你將程式碼片段打包到 Visual Studio Code 擴充套件中進行共享。
“建立程式碼片段”主題包含建立程式碼片段的所有資訊。本指南/示例僅展示瞭如何將你自己的程式碼片段轉換為擴充套件以供共享。建議的工作流程是:
- 使用
Snippets: Configure User Snippets
命令建立並測試你的程式碼片段 - 一旦你對程式碼片段滿意,將整個 JSON 檔案複製到擴充套件資料夾中,例如
snippets.json
- 將以下程式碼片段貢獻新增到你的
package.json
中
{
"contributes": {
"snippets": [
{
"language": "javascript",
"path": "./snippets.json"
}
]
}
}
提示:在你的 package.json
中使用以下配置將你的擴充套件標記為程式碼片段擴充套件
{
"categories": ["Snippets"]
}
你可以在以下位置找到完整的原始碼:https://github.com/microsoft/vscode-extension-samples/tree/main/snippet-sample。
使用 TextMate 程式碼片段
你還可以使用 yo code 擴充套件生成器將 TextMate 程式碼片段 (.tmSnippets) 新增到你的 VS Code 安裝中。該生成器有一個選項 New Code Snippets
,它允許你指向包含多個 .tmSnippets 檔案的資料夾,它們將被打包成 VS Code 程式碼片段擴充套件。該生成器還支援 Sublime 程式碼片段 (.sublime-snippets)。
最終的生成器輸出有兩個檔案:一個擴充套件清單 package.json
,其中包含將程式碼片段整合到 VS Code 的元資料,以及一個 snippets.json
檔案,其中包含轉換為 VS Code 程式碼片段格式的程式碼片段。
.
├── snippets // VS Code integration
│ └── snippets.json // The JSON file w/ the snippets
└── package.json // extension's manifest
將生成的程式碼片段資料夾複製到你的 .vscode/extensions
資料夾下的新資料夾中,然後重新啟動 VS Code。