Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access context key values in vscode extension?

Description

I am setting a configPath context variable using...

const configPath = '/some/file/path.yml';
vscode.commands.executeCommand(
  'setContext',
  'ext.configPath',
  configPath
);

... which can be used within when properties in package.json. For example, "when": "ext.configPath".

Problem

I cannot seem to obtain the value for string interpolation, or programmatically.

Ask

I would like to know...

  1. How to interpolate the context variable value within a string in the package.json file. For example, for tree data title...
{
  "id": "ext.treedata.accounts",
  "name": "Accounts - ${context:ext.configPath}",
  "when": "ext.configPath"
}

... I would expect the title to read Accounts - /some/file/path.yml.

  1. How to obtain the context variable value programmatically. For example...
const configPath = vscode.getContext('ext.configPath');
console.log(configPath); // Outputs: /some/file/path.yml

I've tried using ${context:ext.configPath} to interpolate, and I've tried looking for getContext methods. I've also tried searching the vscode extension development docs.

like image 973
Mike T Avatar asked Oct 21 '25 11:10

Mike T


1 Answers

There is no getContext API/command, unfortunately. There is however, an open issue (https://github.com/microsoft/vscode/issues/10471), but no ETA.

So, in your case, you will have to store that value somewhere. Depending on your scenarios, maybe you could take advantage of workspaceState or globalState Mementos.

Hope this helps

like image 199
alefragnani Avatar answered Oct 23 '25 01:10

alefragnani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!