且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-02-08 21:47:19

我认为这需要实现一个 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": "\"":