如何在 VSCode 中访问代码片段扩展的用户设置
How to access user settings for a snippets extension in VSCode
我正在尝试提供使用用户定义的首选引用样式设置(配置)自定义 VS 代码扩展的选项。我已经在 package.json
:
中配置了它
"contributes": {
"configuration": {
"type": "object",
"title": "Jasmine code snippets configuration",
"properties": {
"jasmineSnippets.quoteStyle": {
"type": "string",
"enum": [
"'",
"\"",
"`"
],
"default": "'",
"description": "Code snippets quote style"
}
}
}
},
并且可以像这样在我的 settings.json
中访问它:
"jasmineSnippets.quoteStyle": "`"
现在如何在我的 snippets.json
文件中使用该值?例如,对于此代码段,我想将硬编码的 ` 更改为配置的 属性.
"it": {
"prefix": "it",
"body": "it(`${1:should behave...}`, () => {\n\t\n});",
"description": "creates a test method",
"scope": "source.js"
},
我从 the docs 中找到的所有内容都没有帮助,因为它假定您是从 JavaScript 文件而不是 JSON 文件中读取它:
You can read these values from your extension using vscode.workspace.getConfiguration('myExtension')
.
我认为这需要实现 CompletionItemProvider
并从中返回代码片段,而不是在 JSON 中静态声明它。这是一个可能看起来像的例子:
'use strict';
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
vscode.languages.registerCompletionItemProvider('javascript', {
provideCompletionItems(doc, pos, token, context) {
var quote = vscode.workspace.getConfiguration('jasmineSnippets').get("quoteStyle", "`");
return [
{
label: "it",
insertText: new vscode.SnippetString(
`it(${quote}${1:should behave...}${quote}, () => {\n\t\n});`),
detail: "creates a test method",
kind: vscode.CompletionItemKind.Snippet,
},
];
}
});
}
然后在设置中使用"jasmineSnippets.quoteStyle": "\""
:
我正在尝试提供使用用户定义的首选引用样式设置(配置)自定义 VS 代码扩展的选项。我已经在 package.json
:
"contributes": {
"configuration": {
"type": "object",
"title": "Jasmine code snippets configuration",
"properties": {
"jasmineSnippets.quoteStyle": {
"type": "string",
"enum": [
"'",
"\"",
"`"
],
"default": "'",
"description": "Code snippets quote style"
}
}
}
},
并且可以像这样在我的 settings.json
中访问它:
"jasmineSnippets.quoteStyle": "`"
现在如何在我的 snippets.json
文件中使用该值?例如,对于此代码段,我想将硬编码的 ` 更改为配置的 属性.
"it": {
"prefix": "it",
"body": "it(`${1:should behave...}`, () => {\n\t\n});",
"description": "creates a test method",
"scope": "source.js"
},
我从 the docs 中找到的所有内容都没有帮助,因为它假定您是从 JavaScript 文件而不是 JSON 文件中读取它:
You can read these values from your extension using
vscode.workspace.getConfiguration('myExtension')
.
我认为这需要实现 CompletionItemProvider
并从中返回代码片段,而不是在 JSON 中静态声明它。这是一个可能看起来像的例子:
'use strict';
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
vscode.languages.registerCompletionItemProvider('javascript', {
provideCompletionItems(doc, pos, token, context) {
var quote = vscode.workspace.getConfiguration('jasmineSnippets').get("quoteStyle", "`");
return [
{
label: "it",
insertText: new vscode.SnippetString(
`it(${quote}${1:should behave...}${quote}, () => {\n\t\n});`),
detail: "creates a test method",
kind: vscode.CompletionItemKind.Snippet,
},
];
}
});
}
然后在设置中使用"jasmineSnippets.quoteStyle": "\""
: