如何在 VSCode 中访问代码段扩展的用户设置 [英] How to access user settings for a snippets extension in VSCode
问题描述
我正在尝试提供选项,以使用用户定义的首选报价样式的设置(配置)来自定义 VS Code 扩展.我已经在我的 package.json
中配置了它:
I am trying to give the option to customise a VS Code Extension with user defined settings (configuration) of their preferred quote style. I have configured it in my 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
中访问它:
And can access it in my settings.json
like this:
"jasmineSnippets.quoteStyle": "`"
我现在如何在我的 snippets.json
文件中使用该值?例如,对于此代码段,我想将硬编码的 ` 更改为配置的属性.
How can I now use that value in my snippets.json
file? For this snippet, for example, I want to change the hardcoded ` to the configured property.
"it": {
"prefix": "it",
"body": "it(`${1:should behave...}`, () => {\n\t$2\n});",
"description": "creates a test method",
"scope": "source.js"
},
我能从文档中找到的所有内容都不是很有帮助,因为它假定您是从 JavaScript 文件而不是 JSON 文件读取它:
All I could find from the docs is not helpful as it assumes you're reading it from a JavaScript file not a JSON file:
您可以使用 vscode.workspace.getConfiguration('myExtension')
从扩展程序中读取这些值.
You can read these values from your extension using
vscode.workspace.getConfiguration('myExtension')
.
推荐答案
我认为这需要实现一个 CompletionItemProvider
并从中返回片段,而不是在 JSON 中静态声明它.下面是一个示例:
I think this requires implementing a CompletionItemProvider
and returning the snippet from that, rather than statically declaring it in a JSON. Here's an example of what that might look like:
'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$2\n});`),
detail: "creates a test method",
kind: vscode.CompletionItemKind.Snippet,
},
];
}
});
}
然后在设置中使用 "jasmineSnippets.quoteStyle": "\""
:
这篇关于如何在 VSCode 中访问代码段扩展的用户设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!