diff --git a/src/extension.ts b/src/extension.ts index 95c8bb7..6acf50b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -7,27 +7,33 @@ import {logger} from "./logger"; import {ExtensionData} from "./extensionData"; import {addDevEnvVariables} from "./utils"; -logger.setupOutputChannel(); -addDevEnvVariables(); - -const extensionData = new ExtensionData(); -let configuration = new Configuration(); - -const disposables: vscode.Disposable[] = []; - export function activate(context: vscode.ExtensionContext) { - const configureCommentBlocksDisposable = configuration.configureCommentBlocks(); + // Setup logger first + logger.setupOutputChannel(); - configuration.registerCommands(context); - - disposables.push(...configureCommentBlocksDisposable); + // Only load dev environment variables when not in production + if (context.extensionMode !== vscode.ExtensionMode.Production) { + addDevEnvVariables(); + } + // Initialize extension data and configuration + const extensionData = new ExtensionData(); + const configuration = new Configuration(); const extensionName = extensionData.get("namespace"); - const extensionDisplayName = extensionData.get("displayName"); - let disabledLangConfig: string[] = configuration.getConfigurationValue("disabledLanguages"); + // Store disposables for cleanup + const disposables: vscode.Disposable[] = []; + let commentBlocksDisposables: vscode.Disposable[] = []; + + // Initial configuration + commentBlocksDisposables = configuration.configureCommentBlocks(); + disposables.push(...commentBlocksDisposables); + configuration.registerCommands(context); + + // Show disabled languages message + const disabledLangConfig: string[] = configuration.getConfigurationValue("disabledLanguages"); if (disabledLangConfig.length > 0) { vscode.window.showInformationMessage(`${disabledLangConfig.join(", ")} languages are disabled for ${extensionDisplayName}.`); } @@ -36,17 +42,14 @@ export function activate(context: vscode.ExtensionContext) { * When the configuration/user settings are changed, set the extension * to reflect the settings and output a message to the user. */ - vscode.workspace.onDidChangeConfiguration((event: vscode.ConfigurationChangeEvent) => { + const configChangeDisposable = vscode.workspace.onDidChangeConfiguration((event: vscode.ConfigurationChangeEvent) => { // TODO: Work on automatically updating the languages instead of making the user reload the extension. /** - * Blade Override Comments + * Blade Override Comments - can be updated without reload */ - // If the affected setting is bladeOverrideComments... if (event.affectsConfiguration(`${extensionName}.bladeOverrideComments`)) { - // Get the setting. - let bladeOverrideComments: boolean = configuration.getConfigurationValue("bladeOverrideComments"); - + const bladeOverrideComments: boolean = configuration.getConfigurationValue("bladeOverrideComments"); configuration.setBladeComments(bladeOverrideComments); if (!configuration.isLangIdDisabled("blade")) { @@ -54,116 +57,69 @@ export function activate(context: vscode.ExtensionContext) { } } - /** - * Disabled Languages - */ - if (event.affectsConfiguration(`${extensionName}.disabledLanguages`)) { - vscode.window - .showInformationMessage( - `The ${extensionName}.disabledLanguages setting has been changed. Please reload the Extension Host to take effect.`, - "Reload" - ) - .then((selection) => { - if (selection === "Reload") { - vscode.commands.executeCommand("workbench.action.restartExtensionHost"); - } - }); + // Settings that require an extension host reload when changed. + const reloadRequiredSettings = [ + "disabledLanguages", + "overrideDefaultLanguageMultiLineComments", + "multiLineStyleBlocks", + "slashStyleBlocks", + "hashStyleBlocks", + "semicolonStyleBlocks", + ]; + + // Settings that require extension host reload + for (const setting of reloadRequiredSettings) { + if (event.affectsConfiguration(`${extensionName}.${setting}`)) { + showReloadMessage(extensionName, setting); + break; // Only show one reload message at a time + } } + }); - /** - * Override Default Language Block Comments - */ - if (event.affectsConfiguration(`${extensionName}.overrideDefaultLanguageMultiLineComments`)) { - vscode.window - .showInformationMessage( - `The ${extensionName}.overrideDefaultLanguageMultiLineComments setting has been changed. Please reload the Extension Host to take effect.`, - "Reload" - ) - .then((selection) => { - if (selection === "Reload") { - vscode.commands.executeCommand("workbench.action.restartExtensionHost"); - } - }); - } + disposables.push(configChangeDisposable); - /** - * Multi-line style Block Comments - */ - if (event.affectsConfiguration(`${extensionName}.multiLineStyleBlocks`)) { - vscode.window - .showInformationMessage( - `The ${extensionName}.multiLineStyleBlocks setting has been changed. Please reload the Extension Host to take effect.`, - "Reload" - ) - .then((selection) => { - if (selection === "Reload") { - vscode.commands.executeCommand("workbench.action.restartExtensionHost"); - } - }); - } - - /** - * //-style single-line comments - */ - if (event.affectsConfiguration(`${extensionName}.slashStyleBlocks`)) { - vscode.window - .showInformationMessage( - `The ${extensionName}.slashStyleBlocks setting has been changed. Please reload the Extension Host to take effect.`, - "Reload" - ) - .then((selection) => { - if (selection === "Reload") { - vscode.commands.executeCommand("workbench.action.restartExtensionHost"); - } - }); - } + /** + * An event that is emitted when a text document is opened or when the + * language id of a text document has been changed. As described in + * https://github.com/microsoft/vscode/blob/4e8fbaef741afebd24684b88cac47c2f44dfb8eb/src/vscode-dts/vscode.d.ts#L13716-L13728 + * + * Called when active editor language is changed, so re-configure the comment blocks. + */ + const documentOpenDisposable = vscode.workspace.onDidOpenTextDocument(() => { + logger.info("Active editor language changed, re-configuring comment blocks."); - /** - * #-style single-line comments - */ - if (event.affectsConfiguration(`${extensionName}.hashStyleBlocks`)) { - vscode.window - .showInformationMessage( - `The ${extensionName}.hashStyleBlocks setting has been changed. Please reload the Extension Host to take effect.`, - "Reload" - ) - .then((selection) => { - if (selection === "Reload") { - vscode.commands.executeCommand("workbench.action.restartExtensionHost"); - } - }); - } + // Dispose of old comment block configurations to prevent memory leaks + commentBlocksDisposables.forEach((disposable) => disposable.dispose()); + commentBlocksDisposables = []; - /** - * ;-style single-line comments - */ - if (event.affectsConfiguration(`${extensionName}.semicolonStyleBlocks`)) { - vscode.window - .showInformationMessage( - `The ${extensionName}.semicolonStyleBlocks setting has been changed. Please reload the Extension Host to take effect.`, - "Reload" - ) - .then((selection) => { - if (selection === "Reload") { - vscode.commands.executeCommand("workbench.action.restartExtensionHost"); - } - }); - } + // Create new comment block configurations + commentBlocksDisposables = configuration.configureCommentBlocks(); + disposables.push(...commentBlocksDisposables); }); - // An event that is emitted when a text document is opened or when the - // language id of a text document has been changed. As described in - // https://github.com/microsoft/vscode/blob/4e8fbaef741afebd24684b88cac47c2f44dfb8eb/src/vscode-dts/vscode.d.ts#L13716-L13728 - - // Called when active editor language is changed, so re-configure the comment blocks. - vscode.workspace.onDidOpenTextDocument(() => { - logger.info("Active editor language changed, re-configuring comment blocks."); - const configureCommentBlocksDisposable = configuration.configureCommentBlocks(); - disposables.push(...configureCommentBlocksDisposable); - }); + disposables.push(documentOpenDisposable); context.subscriptions.push(...disposables); } + export function deactivate() { logger.disposeLogger(); } + +/** + * Shows a message prompting the user to reload the extension host. + * @param extensionName The namespace of the extension + * @param settingName The name of the setting that was changed + */ +function showReloadMessage(extensionName: string, settingName: string): void { + vscode.window + .showInformationMessage( + `The ${extensionName}.${settingName} setting has been changed. Please reload the Extension Host to take effect.`, + "Reload" + ) + .then((selection) => { + if (selection === "Reload") { + vscode.commands.executeCommand("workbench.action.restartExtensionHost"); + } + }); +}