Skip to content

Commit 6b395b2

Browse files
authored
refactor: extension activation and performance improvements (#29)
* Refactored the `activate` function to set up the logger first, initialize extension data and configuration. * Ensured that development environment variables are only loaded when not in production mode, i.e. in development or testing modes. * Consolidated multiple configuration change handlers into a single handler that checks for a list of settings requiring extension reload. If any are changed, a single reload prompt is shown using the new `showReloadMessage` helper function, reducing code duplication. * Improved performance and event handling to prevent memory leaks by properly disposing of old comment configurations.
2 parents 59d9e54 + 9e33532 commit 6b395b2

1 file changed

Lines changed: 76 additions & 120 deletions

File tree

src/extension.ts

Lines changed: 76 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,33 @@ import {logger} from "./logger";
77
import {ExtensionData} from "./extensionData";
88
import {addDevEnvVariables} from "./utils";
99

10-
logger.setupOutputChannel();
11-
addDevEnvVariables();
12-
13-
const extensionData = new ExtensionData();
14-
let configuration = new Configuration();
15-
16-
const disposables: vscode.Disposable[] = [];
17-
1810
export function activate(context: vscode.ExtensionContext) {
19-
const configureCommentBlocksDisposable = configuration.configureCommentBlocks();
11+
// Setup logger first
12+
logger.setupOutputChannel();
2013

21-
configuration.registerCommands(context);
22-
23-
disposables.push(...configureCommentBlocksDisposable);
14+
// Only load dev environment variables when not in production
15+
if (context.extensionMode !== vscode.ExtensionMode.Production) {
16+
addDevEnvVariables();
17+
}
2418

19+
// Initialize extension data and configuration
20+
const extensionData = new ExtensionData();
21+
const configuration = new Configuration();
2522
const extensionName = extensionData.get("namespace");
26-
2723
const extensionDisplayName = extensionData.get("displayName");
2824

29-
let disabledLangConfig: string[] = configuration.getConfigurationValue("disabledLanguages");
25+
// Store disposables for cleanup
26+
const disposables: vscode.Disposable[] = [];
27+
let commentBlocksDisposables: vscode.Disposable[] = [];
28+
29+
// Initial configuration
30+
commentBlocksDisposables = configuration.configureCommentBlocks();
31+
disposables.push(...commentBlocksDisposables);
3032

33+
configuration.registerCommands(context);
34+
35+
// Show disabled languages message
36+
const disabledLangConfig: string[] = configuration.getConfigurationValue("disabledLanguages");
3137
if (disabledLangConfig.length > 0) {
3238
vscode.window.showInformationMessage(`${disabledLangConfig.join(", ")} languages are disabled for ${extensionDisplayName}.`);
3339
}
@@ -36,134 +42,84 @@ export function activate(context: vscode.ExtensionContext) {
3642
* When the configuration/user settings are changed, set the extension
3743
* to reflect the settings and output a message to the user.
3844
*/
39-
vscode.workspace.onDidChangeConfiguration((event: vscode.ConfigurationChangeEvent) => {
45+
const configChangeDisposable = vscode.workspace.onDidChangeConfiguration((event: vscode.ConfigurationChangeEvent) => {
4046
// TODO: Work on automatically updating the languages instead of making the user reload the extension.
4147

4248
/**
43-
* Blade Override Comments
49+
* Blade Override Comments - can be updated without reload
4450
*/
45-
// If the affected setting is bladeOverrideComments...
4651
if (event.affectsConfiguration(`${extensionName}.bladeOverrideComments`)) {
47-
// Get the setting.
48-
let bladeOverrideComments: boolean = configuration.getConfigurationValue("bladeOverrideComments");
49-
52+
const bladeOverrideComments: boolean = configuration.getConfigurationValue("bladeOverrideComments");
5053
configuration.setBladeComments(bladeOverrideComments);
5154

5255
if (!configuration.isLangIdDisabled("blade")) {
5356
vscode.window.showInformationMessage(`${bladeOverrideComments === false ? "Disabled" : "Enabled"} Blade Override Comments setting.`);
5457
}
5558
}
5659

57-
/**
58-
* Disabled Languages
59-
*/
60-
if (event.affectsConfiguration(`${extensionName}.disabledLanguages`)) {
61-
vscode.window
62-
.showInformationMessage(
63-
`The ${extensionName}.disabledLanguages setting has been changed. Please reload the Extension Host to take effect.`,
64-
"Reload"
65-
)
66-
.then((selection) => {
67-
if (selection === "Reload") {
68-
vscode.commands.executeCommand("workbench.action.restartExtensionHost");
69-
}
70-
});
60+
// Settings that require an extension host reload when changed.
61+
const reloadRequiredSettings = [
62+
"disabledLanguages",
63+
"overrideDefaultLanguageMultiLineComments",
64+
"multiLineStyleBlocks",
65+
"slashStyleBlocks",
66+
"hashStyleBlocks",
67+
"semicolonStyleBlocks",
68+
];
69+
70+
// Settings that require extension host reload
71+
for (const setting of reloadRequiredSettings) {
72+
if (event.affectsConfiguration(`${extensionName}.${setting}`)) {
73+
showReloadMessage(extensionName, setting);
74+
break; // Only show one reload message at a time
75+
}
7176
}
77+
});
7278

73-
/**
74-
* Override Default Language Block Comments
75-
*/
76-
if (event.affectsConfiguration(`${extensionName}.overrideDefaultLanguageMultiLineComments`)) {
77-
vscode.window
78-
.showInformationMessage(
79-
`The ${extensionName}.overrideDefaultLanguageMultiLineComments setting has been changed. Please reload the Extension Host to take effect.`,
80-
"Reload"
81-
)
82-
.then((selection) => {
83-
if (selection === "Reload") {
84-
vscode.commands.executeCommand("workbench.action.restartExtensionHost");
85-
}
86-
});
87-
}
79+
disposables.push(configChangeDisposable);
8880

89-
/**
90-
* Multi-line style Block Comments
91-
*/
92-
if (event.affectsConfiguration(`${extensionName}.multiLineStyleBlocks`)) {
93-
vscode.window
94-
.showInformationMessage(
95-
`The ${extensionName}.multiLineStyleBlocks setting has been changed. Please reload the Extension Host to take effect.`,
96-
"Reload"
97-
)
98-
.then((selection) => {
99-
if (selection === "Reload") {
100-
vscode.commands.executeCommand("workbench.action.restartExtensionHost");
101-
}
102-
});
103-
}
104-
105-
/**
106-
* //-style single-line comments
107-
*/
108-
if (event.affectsConfiguration(`${extensionName}.slashStyleBlocks`)) {
109-
vscode.window
110-
.showInformationMessage(
111-
`The ${extensionName}.slashStyleBlocks setting has been changed. Please reload the Extension Host to take effect.`,
112-
"Reload"
113-
)
114-
.then((selection) => {
115-
if (selection === "Reload") {
116-
vscode.commands.executeCommand("workbench.action.restartExtensionHost");
117-
}
118-
});
119-
}
81+
/**
82+
* An event that is emitted when a text document is opened or when the
83+
* language id of a text document has been changed. As described in
84+
* https://github.com/microsoft/vscode/blob/4e8fbaef741afebd24684b88cac47c2f44dfb8eb/src/vscode-dts/vscode.d.ts#L13716-L13728
85+
*
86+
* Called when active editor language is changed, so re-configure the comment blocks.
87+
*/
88+
const documentOpenDisposable = vscode.workspace.onDidOpenTextDocument(() => {
89+
logger.info("Active editor language changed, re-configuring comment blocks.");
12090

121-
/**
122-
* #-style single-line comments
123-
*/
124-
if (event.affectsConfiguration(`${extensionName}.hashStyleBlocks`)) {
125-
vscode.window
126-
.showInformationMessage(
127-
`The ${extensionName}.hashStyleBlocks setting has been changed. Please reload the Extension Host to take effect.`,
128-
"Reload"
129-
)
130-
.then((selection) => {
131-
if (selection === "Reload") {
132-
vscode.commands.executeCommand("workbench.action.restartExtensionHost");
133-
}
134-
});
135-
}
91+
// Dispose of old comment block configurations to prevent memory leaks
92+
commentBlocksDisposables.forEach((disposable) => disposable.dispose());
93+
commentBlocksDisposables = [];
13694

137-
/**
138-
* ;-style single-line comments
139-
*/
140-
if (event.affectsConfiguration(`${extensionName}.semicolonStyleBlocks`)) {
141-
vscode.window
142-
.showInformationMessage(
143-
`The ${extensionName}.semicolonStyleBlocks setting has been changed. Please reload the Extension Host to take effect.`,
144-
"Reload"
145-
)
146-
.then((selection) => {
147-
if (selection === "Reload") {
148-
vscode.commands.executeCommand("workbench.action.restartExtensionHost");
149-
}
150-
});
151-
}
95+
// Create new comment block configurations
96+
commentBlocksDisposables = configuration.configureCommentBlocks();
97+
disposables.push(...commentBlocksDisposables);
15298
});
15399

154-
// An event that is emitted when a text document is opened or when the
155-
// language id of a text document has been changed. As described in
156-
// https://github.com/microsoft/vscode/blob/4e8fbaef741afebd24684b88cac47c2f44dfb8eb/src/vscode-dts/vscode.d.ts#L13716-L13728
157-
158-
// Called when active editor language is changed, so re-configure the comment blocks.
159-
vscode.workspace.onDidOpenTextDocument(() => {
160-
logger.info("Active editor language changed, re-configuring comment blocks.");
161-
const configureCommentBlocksDisposable = configuration.configureCommentBlocks();
162-
disposables.push(...configureCommentBlocksDisposable);
163-
});
100+
disposables.push(documentOpenDisposable);
164101

165102
context.subscriptions.push(...disposables);
166103
}
104+
167105
export function deactivate() {
168106
logger.disposeLogger();
169107
}
108+
109+
/**
110+
* Shows a message prompting the user to reload the extension host.
111+
* @param extensionName The namespace of the extension
112+
* @param settingName The name of the setting that was changed
113+
*/
114+
function showReloadMessage(extensionName: string, settingName: string): void {
115+
vscode.window
116+
.showInformationMessage(
117+
`The ${extensionName}.${settingName} setting has been changed. Please reload the Extension Host to take effect.`,
118+
"Reload"
119+
)
120+
.then((selection) => {
121+
if (selection === "Reload") {
122+
vscode.commands.executeCommand("workbench.action.restartExtensionHost");
123+
}
124+
});
125+
}

0 commit comments

Comments
 (0)