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
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:F1
orCtrl+Shift+P
orShift+Command+P
;reload window
;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.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 callinggetConfiguration
again. You can listen for configuration changes withonDidChangeConfiguration
, 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