skip to Main Content

I’m developing a plugin for VSCode.

The parameters used are read from the settings.json file and can be modified by the user on the extension settings page.

The line of code used to read a parameter from settings.json is:

const value = vscode.workspace.getConfiguration(configKey).get('parameterName');

When I start the plugin the parameters are read correctly, but if I change them in the extension settings page the changes are not recognized. However, if I close the editor and restart it, the modified parameters are finally recognized and used.

Is this a normal behavior for a plugin or is there a system that I don’t know about to make the plugin immediately recognize parameter changes as soon as they are set by the user?

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution on my own by reading the VSCode API documentation and the code written for the extensions of many software houses.

    There is no direct and clean way to intercept changes made to configuration parameters and consequently make them automatically active.

    The only way to activate the new configuration parameter values ​​is to restart VSCode or call the Reload window command:

    1. F1 or Ctrl+Shift+P or Shift+Command+P;
    2. type reload window;
    3. choose Developer: Reload Window;

    If, however, you are developing a new extension for VSCode, remember to add a paragraph to the README.md file in which you explain to the user the procedure I have just written.


  2. I’m pretty sure vscode.workspace.getConfiguration should give you the current value of the setting at the time that you call it. Once you read the value out into a variable, that variable isn’t going to magically update when the setting changes. You’d need to read the setting value out again by calling getConfiguration again. You can listen for configuration changes with onDidChangeConfiguration, but I’ve seen a fair share of extensions that don’t bother caching configuration values, and just read the value out every time they’re going to use it to perform some action. Then you don’t need to bother listening for changes

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search