Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10.自定义插件首选项配置,根据配置执行不同逻辑—例子 #10

Open
PDKSophia opened this issue Feb 19, 2021 · 0 comments
Labels
documentation Improvements or additions to documentation vscode

Comments

@PDKSophia
Copy link
Owner

PDKSophia commented Feb 19, 2021

场景

每一个插件都可以自行添加首选项的配置,当打开 vscode 时,根据首选项选择的配置,执行不同的逻辑,接下来说说如何实现此效果

代码展示

我们现在 package.json 中配置一下我们首选项参数

{
   "contributes": {
      "configuration": {
        "title": "sugar-demo-vscod",
        "properties": {
          "sugar-demo-vscode.matchConfig": {
            "type": "string",
            "description": "sugar-demo-vscod 配置,默认低配版本",
            "enum": [
              "lowMatch",
              "middleMatch",
              "highMatch"
            ],
            "default": "lowMatch",
            "scope": "window"
          }
        }
      }
  }
}

这里需要注意,名称 sugar-demo-vscode 要一致!

上面我们已经实现了首选项配置,看看效果

我们再获取配置,然后执行不同逻辑

    // 获取用户配置的版本设置
    const matchConfig = vscode.workspace.getConfiguration().get('vscode-beehive-extension.matchConfig')
    if (matchConfig === MATCH_CONFIG_MAPS.LOW) {
       console.log('低配')
    } else if (matchConfig === MATCH_CONFIG_MAPS.MIDDLE) {
        console.log('中配')
    } else if (matchConfig === MATCH_CONFIG_MAPS.HIGH) {
        console.log('高配')
    } else {
      vscode.window.showErrorMessage(`unknown error`)
    }

如果要通过代码修改 matchConfig 内容,可以通过

// 最后一个参数,为true时表示写入全局配置,为false或不传时则只写入工作区配置
vscode.workspace.getConfiguration().update('vscode-beehive-extension.matchConfig', 'middleMatch, true);

源码展示

@PDKSophia PDKSophia added documentation Improvements or additions to documentation vscode labels Feb 19, 2021
@PDKSophia PDKSophia changed the title 10.自定义插件首选项配置,根据配置执行不同逻辑 10.自定义插件首选项配置,根据配置执行不同逻辑——例子 Feb 19, 2021
@PDKSophia PDKSophia changed the title 10.自定义插件首选项配置,根据配置执行不同逻辑——例子 10.自定义插件首选项配置,根据配置执行不同逻辑—例子 Feb 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation vscode
Projects
None yet
Development

No branches or pull requests

1 participant