Skip to content

Commit 1b3b170

Browse files
committed
feat: enable the language definitions to be auto-updated on settings change
- Added new `updateLanguageDefinitions` function in Configuration class to update the language definitions. - Refactored the `onDidChangeConfiguration` event in the `activate` function of the extension to auto-update the language definitions and reconfigure the comment blocks when a user changes the settings. It uses the new `updateLanguageDefinitions` function to update the definitions before reconfiguring the comment blocks. - Removed the old `reloadRequiredSettings` array and the `showReloadMessage` function.
1 parent 3b58c94 commit 1b3b170

2 files changed

Lines changed: 35 additions & 26 deletions

File tree

src/configuration.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,20 @@ export class Configuration {
682682
utils.writeJsonFile(this.multiLineLangDefinitionFilePath, multiLineData);
683683
}
684684

685+
/**
686+
* Update language definitions.
687+
*/
688+
public updateLanguageDefinitions() {
689+
// Remove all elements from the current Map, so we can update
690+
// the definitions with an empty Map.
691+
this.singleLineBlocksMap.clear();
692+
this.multiLineBlocksMap.clear();
693+
// Update the definitions.
694+
this.setSingleLineCommentLanguageDefinitions();
695+
this.setMultiLineCommentLanguageDefinitions();
696+
this.writeCommentLanguageDefinitionsToJsonFile();
697+
}
698+
685699
/**
686700
* Sets the language configuration for a given language ID.
687701
*

src/extension.ts

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,34 @@ export function activate(context: vscode.ExtensionContext) {
7575
logger.setLogLevel(logLevel);
7676
}
7777

78-
// Settings that require an extension host reload when changed.
79-
const reloadRequiredSettings = [
80-
"disabledLanguages",
81-
"overrideDefaultLanguageMultiLineComments",
78+
/**
79+
* Automatically update (without extension host reload) language definitions and
80+
* reconfigure the comment blocks when any of the following settings are changed.
81+
*/
82+
const languageSettings = [
8283
"multiLineStyleBlocks",
8384
"slashStyleBlocks",
8485
"hashStyleBlocks",
8586
"semicolonStyleBlocks",
87+
"disabledLanguages",
88+
"overrideDefaultLanguageMultiLineComments",
8689
];
8790

88-
// Settings that require extension host reload
89-
for (const setting of reloadRequiredSettings) {
91+
for (const setting of languageSettings) {
9092
if (event.affectsConfiguration(`${extensionName}.${setting}`)) {
91-
showReloadMessage(extensionName, setting);
92-
break; // Only show one reload message at a time
93+
logger.info(`Configuration setting ${extensionName}.${setting} has changed.`);
94+
// Dispose of old comment block configurations to prevent memory leaks
95+
commentBlocksDisposables.forEach((disposable) => disposable.dispose());
96+
commentBlocksDisposables = [];
97+
98+
configuration.updateLanguageDefinitions();
99+
100+
commentBlocksDisposables = configuration.configureCommentBlocks();
101+
disposables.push(...commentBlocksDisposables);
102+
103+
logger.info("Comment block configurations have been updated.");
104+
105+
break; // Only update once per change
93106
}
94107
}
95108
});
@@ -132,21 +145,3 @@ export function activate(context: vscode.ExtensionContext) {
132145
export function deactivate() {
133146
logger.disposeLogger();
134147
}
135-
136-
/**
137-
* Shows a message prompting the user to reload the extension host.
138-
* @param extensionName The namespace of the extension
139-
* @param settingName The name of the setting that was changed
140-
*/
141-
function showReloadMessage(extensionName: string, settingName: string): void {
142-
vscode.window
143-
.showInformationMessage(
144-
`The ${extensionName}.${settingName} setting has been changed. Please reload the Extension Host to take effect.`,
145-
"Reload"
146-
)
147-
.then((selection) => {
148-
if (selection === "Reload") {
149-
vscode.commands.executeCommand("workbench.action.restartExtensionHost");
150-
}
151-
});
152-
}

0 commit comments

Comments
 (0)