通过扩展更新 VS Code 用户设置

2024-01-01

我正在尝试创建一个简单的扩展来切换 VS Code 中测试文件的可见性。这是我目前的方法:

const testGlobs = [
  '**/__tests__',
  '**/__mocks__',
  '**/*.spec.js',
]

function hideTests() {
  const exclude = workspace.getConfiguration('files.exclude', vscode.ConfigurationTarget.Global)
  testGlobs.forEach(glob => exclude.update(glob, true, vscode.ConfigurationTarget.Global));
  console.log(exclude) // does not reflect the updated values
}

这似乎没有影响。文件模式的设置保持不变false在我的用户设置文件中,就像注销值时一样exclude在代码片段的末尾。

如何通过扩展代码正确更新设置?


解决了。我发布的代码实际上抛出了一个错误,但是update方法是异步的,因此错误被吞噬。通过在函数上使用 async/await,我能够看到错误,类似于:

'files.exclude.**/__tests__' is not a registered configuration.

基本上,我必须更新exclude整个配置,而不是其下的单个键,因为这些键只是配置值的一部分 - 它们本身不是实际的配置键。工作解决方案:

async function hideTests() {
  const files = workspace.getConfiguration('files', ConfigurationTarget.Global)
  const exclude = files.get('exclude')
  testGlobs.forEach(g => exclude[g] = true)
  await files.update('exclude', exclude, ConfigurationTarget.Global)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过扩展更新 VS Code 用户设置 的相关文章

随机推荐