Skip to content

Commit a1a2a72

Browse files
authored
Cleanup extension activation (#1491)
Refactor the extension activation method such that it only starts up the necessary services and registers their disposables for extension deactivation. The startup order of services has been tweaked slightly so that it will fail fast if the toolchain is not configured properly. Fixes a few services that were registred for disposal twice. Fixes up `registerDebugger` so that toggling the setting works immediately instead of silently requiring an extension restart before taking effect.
1 parent 87e6248 commit a1a2a72

8 files changed

+236
-173
lines changed

src/WorkspaceContext.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ export class WorkspaceContext implements vscode.Disposable {
183183
this.buildStatus,
184184
];
185185
this.lastFocusUri = vscode.window.activeTextEditor?.document.uri;
186+
187+
this.setupEventListeners();
186188
}
187189

188190
async stop() {
@@ -278,7 +280,7 @@ export class WorkspaceContext implements vscode.Disposable {
278280
}
279281

280282
/** Setup the vscode event listeners to catch folder changes and active window changes */
281-
setupEventListeners() {
283+
private setupEventListeners() {
282284
// add event listener for when a workspace folder is added/removed
283285
const onWorkspaceChange = vscode.workspace.onDidChangeWorkspaceFolders(event => {
284286
if (this === undefined) {

src/configuration.ts

+34
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import * as vscode from "vscode";
1616
import * as os from "os";
1717
import * as path from "path";
18+
import { showReloadExtensionNotification } from "./ui/ReloadExtension";
19+
import { WorkspaceContext } from "./WorkspaceContext";
1820

1921
export type DebugAdapters = "auto" | "lldb-dap" | "CodeLLDB";
2022
export type SetupCodeLLDBOptions =
@@ -502,4 +504,36 @@ function computeVscodeVar(varName: string): string | null {
502504
return varName in supportedVariables ? supportedVariables[varName]() : null;
503505
}
504506

507+
/**
508+
* Handler for configuration change events that triggers a reload of the extension
509+
* if the setting changed requires one.
510+
* @param ctx The workspace context.
511+
* @returns A disposable that unregisters the provider when disposed.
512+
*/
513+
export function handleConfigurationChangeEvent(
514+
ctx: WorkspaceContext
515+
): (event: vscode.ConfigurationChangeEvent) => void {
516+
return (event: vscode.ConfigurationChangeEvent) => {
517+
// on toolchain config change, reload window
518+
if (
519+
event.affectsConfiguration("swift.path") &&
520+
configuration.path !== ctx.toolchain?.swiftFolderPath
521+
) {
522+
showReloadExtensionNotification(
523+
"Changing the Swift path requires Visual Studio Code be reloaded."
524+
);
525+
} else if (
526+
// on sdk config change, restart sourcekit-lsp
527+
event.affectsConfiguration("swift.SDK") ||
528+
event.affectsConfiguration("swift.swiftSDK")
529+
) {
530+
vscode.commands.executeCommand("swift.restartLSPServer");
531+
} else if (event.affectsConfiguration("swift.swiftEnvironmentVariables")) {
532+
showReloadExtensionNotification(
533+
"Changing environment variables requires the project be reloaded."
534+
);
535+
}
536+
};
537+
}
538+
505539
export default configuration;

src/contextKeys.ts

+15
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import * as vscode from "vscode";
16+
import { Version } from "./utilities/version";
1617

1718
/**
1819
* References:
@@ -82,6 +83,11 @@ interface ContextKeys {
8283
* Whether the swift.switchPlatform command is available.
8384
*/
8485
switchPlatformAvailable: boolean;
86+
87+
/**
88+
* Sets values for context keys that are enabled/disabled based on the toolchain version in use.
89+
*/
90+
updateKeysBasedOnActiveVersion(toolchainVersion: Version): void;
8591
}
8692

8793
/** Creates the getters and setters for the VS Code Swift extension's context keys. */
@@ -100,6 +106,15 @@ function createContextKeys(): ContextKeys {
100106
let switchPlatformAvailable: boolean = false;
101107

102108
return {
109+
updateKeysBasedOnActiveVersion(toolchainVersion: Version) {
110+
this.createNewProjectAvailable = toolchainVersion.isGreaterThanOrEqual(
111+
new Version(5, 8, 0)
112+
);
113+
this.switchPlatformAvailable = toolchainVersion.isGreaterThanOrEqual(
114+
new Version(6, 1, 0)
115+
);
116+
},
117+
103118
get isActivated() {
104119
return isActivated;
105120
},

src/debugger/debugAdapterFactory.ts

+25-4
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,34 @@ import configuration from "../configuration";
3131
* @returns A disposable to be disposed when the extension is deactivated
3232
*/
3333
export function registerDebugger(workspaceContext: WorkspaceContext): vscode.Disposable {
34-
const subscriptions: vscode.Disposable[] = [
35-
registerLoggingDebugAdapterTracker(),
36-
registerLLDBDebugAdapter(workspaceContext.toolchain, workspaceContext.outputChannel),
37-
];
34+
let subscriptions: vscode.Disposable[] = [];
35+
36+
// Monitor the swift.debugger.disable setting and register automatically
37+
// when the setting is changed to enable.
38+
const configurationEvent = vscode.workspace.onDidChangeConfiguration(event => {
39+
if (event.affectsConfiguration("swift.debugger.disable")) {
40+
subscriptions.map(sub => sub.dispose());
41+
subscriptions = [];
42+
if (!configuration.debugger.disable) {
43+
register();
44+
}
45+
}
46+
});
47+
48+
function register() {
49+
subscriptions.push(registerLoggingDebugAdapterTracker());
50+
subscriptions.push(
51+
registerLLDBDebugAdapter(workspaceContext.toolchain, workspaceContext.outputChannel)
52+
);
53+
}
54+
55+
if (!configuration.debugger.disable) {
56+
register();
57+
}
3858

3959
return {
4060
dispose: () => {
61+
configurationEvent.dispose();
4162
subscriptions.map(sub => sub.dispose());
4263
},
4364
};

0 commit comments

Comments
 (0)