英文:
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
参数将是 api
、sourceLanguage
等,它们都是 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:
"configuration": {
"title": "Translator Helper",
"properties": {
"translatorHelper.api": {
"type": "string",
"default": "google",
"enum": [
"google",
"google-cn"
],
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 = <T>(key: string) => {
return workspace.getConfiguration('translatorHelper').get<T>(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 = () => {
return getConfiguration<string>('api') ?? 'google';
};
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论