gpt4 book ai didi

visual-studio-code - 如何在 VSCode 中访问代码段扩展的用户设置

转载 作者:行者123 更新时间:2023-12-04 13:01:50 26 4
gpt4 key购买 nike

我正在尝试提供选项,以使用用户定义的首选报价样式的设置(配置)来自定义 VS Code 扩展。我已经在我的 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$2\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$2\n});`),
detail: "creates a test method",
kind: vscode.CompletionItemKind.Snippet,
},
];
}
});
}

然后用 "jasmineSnippets.quoteStyle": "\""在设置中:

关于visual-studio-code - 如何在 VSCode 中访问代码段扩展的用户设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55136207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com