代码片段
使用 snippets: configure snippets 操作为指定的范围创建新的代码片段文件或编辑现有代码片段文件。
代码片段位于 ~/.config/zed/snippets 目录中,您可以使用 snippets: open folder 操作导航到该目录。
配置示例
{
// Each snippet must have a name and body, but the prefix and description are optional.
// The prefix is used to trigger the snippet, but when omitted then the name is used.
// Use placeholders like $1, $2 or ${1:defaultValue} to define tab stops.
// The $0 determines the final cursor position.
// Placeholders with the same value are linked.
"Log to console": {
"prefix": "log",
"body": ["console.info(\"Hello, ${1:World}!\")", "$0"],
"description": "Logs to console"
}
}
范围
范围由小写语言名称决定,例如,Python 为 python.json,Shell Script 为 shell script.json,但此规则有一些例外。
| 范围 | 文件名 |
|---|---|
| 全局 | snippets.json |
| JSX | javascript.json |
| 纯文本 | plaintext.json |
要创建 JSX 代码片段,您必须使用 javascript.json 代码片段文件,而不是 jsx.json,但这不适用于遵循上述规则的 TSX 和 TypeScript。
已知限制
- 当传入前缀列表时,仅使用第一个前缀。
- 目前仅支持
json代码片段文件格式,尽管simple-completion-language-server同时支持json和toml文件格式。
另请参阅
simple-completion-language-server 中的 feature_paths 选项默认禁用。
如果您想启用它,可以将其添加到您的 settings.json 中
{
"lsp": {
"snippet-completion-server": {
"settings": {
"feature_paths": true
}
}
}
}
有关更多配置信息,请参阅 simple-completion-language-server 说明。