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

BBl*_*kwo 5 visual-studio-code vscode-extensions

我试图提供使用用户定义的首选引用样式设置(配置)来自定义 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"
      }
    }
  }
},
Run Code Online (Sandbox Code Playgroud)

settings.json并可以像这样在我的中访问它:

"jasmineSnippets.quoteStyle": "`"
Run Code Online (Sandbox Code Playgroud)

我现在如何在我的snippets.json文件中使用该值?例如,对于此代码片段,我想将硬编码的 ` 更改为配置的属性。

"it": {
  "prefix": "it",
  "body": "it(`${1:should behave...}`, () => {\n\t$2\n});",
  "description": "creates a test method",
  "scope": "source.js"
},
Run Code Online (Sandbox Code Playgroud)

我从文档中找到的所有内容都没有帮助,因为它假设您是从 JavaScript 文件而不是 JSON 文件中读取它:

您可以使用从扩展程序读取这些值vscode.workspace.getConfiguration('myExtension')

Gam*_*a11 2

我认为这需要实现 aCompletionItemProvider并从中返回片段,而不是在 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,
                },
            ];
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

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