VSCode API:workspace.getConfiguration如何工作?

huangapple go评论63阅读模式
英文:

VSCode API: How does workspace.getConfiguration work?

问题

我正在阅读VSCode API文档,不太确定getConfiguration方法的section参数是如何派生的。例如,我查看了以下文件https://github.com/yanxiaodi/vscode-translator-helper/blob/master/src/config.ts,并不清楚为什么section参数被提供为"translatorHelper",当我在GitHub存储库中找不到任何有关translator helper的信息。如果我在文档中遗漏了什么或者这个问题太简单,我深感抱歉,但我一直在尝试弄清楚为什么上面链接中的示例代码能够运行。非常感谢任何帮助。谢谢!

英文:

I was reading through the VSCode API documentation, and I am not quite sure how the section argument for the getConfiguration method is derived. For example, I was looking through the following file https://github.com/yanxiaodi/vscode-translator-helper/blob/master/src/config.ts, and I am not sure how why the section argument is provided as "translatorHelper", when I cannot find translator helper anywhere else in the GitHub repository. I apologise if this question is trivial, or if I have missed out something in the documentation, but I have been trying to figure out why the example code in the link above works for a while now. Any help will be greatly appreciated. Thank you!

答案1

得分: 1

最重要的是,translatorHelper 在存储库的 package.json 中。以下是 contributes 部分的摘录:

"configuration": {
   "title": "Translator Helper",
   "properties": {
      "translatorHelper.api": {
         "type": "string",
         "default": "google",
         "enum": [
            "google",
            "google-cn"
         ],
         // 等等.....

还有更多设置,即配置。请注意,它们都是 translatorHelper.xxxx,所以如果你想检索这样的设置,可以使用:

export const getConfiguration = <T>(key: string) => {
  return workspace.getConfiguration('translatorHelper').get<T>(key);
};

其中 key 参数将是 apisourceLanguage 等,它们都是 translatorHelper.api 和类似的。

甚至间接调用如

export const getApiConfiguration = () => {
  return getConfiguration<string>('api') ?? 'google';
};

实际上是 getConfiguration(someKeyHere) 函数的简写。

因此,由于配置在 package.json 中被命名为 translatorHelper.something,你会在 getConfiguration() 中使用 translatorHelper 作为 sectionID。在 vscode 的设置界面下,它们将全部分组在 translaorHelper 作为节标题下。如果设置标识为 translatorHelper.other.api 例如,你可以使用 translatorHelper.other 作为 sectionID

英文:

Most importantly, translatorHelper is in the repository's package.json. Here is an excerpt from the contributes section:

&quot;configuration&quot;: {
   &quot;title&quot;: &quot;Translator Helper&quot;,
   &quot;properties&quot;: {
      &quot;translatorHelper.api&quot;: {
         &quot;type&quot;: &quot;string&quot;,
         &quot;default&quot;: &quot;google&quot;,
         &quot;enum&quot;: [
            &quot;google&quot;,
            &quot;google-cn&quot;
         ],
etc.....

and more settings, i.e. configurations. Note they are all translatorHelper.xxxx so if you are trying to retrieve a setting like that you would use:

export const getConfiguration = &lt;T&gt;(key: string) =&gt; {
  return workspace.getConfiguration(&#39;translatorHelper&#39;).get&lt;T&gt;(key);
};

where the key argument would be api, sourceLanguage, etc. which are all translatorHelper.api and similar.

Even the indirect calls like

export const getApiConfiguration = () =&gt; {
  return getConfiguration&lt;string&gt;(&#39;api&#39;) ?? &#39;google&#39;;
};

are really shorthand for the getConfiguration(someKeyHere) function.

So since the configurations were named translatorHelper.something in the package.json you would use translatorHelper as the sectionID in getConfiguration(). And in the Settings UI of vscode they will all be grouped under the section header translaorHelper. If the settings had been identified as translatorHelper.other.api for example, you can use translatorHelper.other as the sectionID.

huangapple
  • 本文由 发表于 2023年5月26日 09:31:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定