Skip to content

Cleanup extension activation #1491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/WorkspaceContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ export class WorkspaceContext implements vscode.Disposable {
this.buildStatus,
];
this.lastFocusUri = vscode.window.activeTextEditor?.document.uri;

this.setupEventListeners();
}

async stop() {
Expand Down Expand Up @@ -278,7 +280,7 @@ export class WorkspaceContext implements vscode.Disposable {
}

/** Setup the vscode event listeners to catch folder changes and active window changes */
setupEventListeners() {
private setupEventListeners() {
// add event listener for when a workspace folder is added/removed
const onWorkspaceChange = vscode.workspace.onDidChangeWorkspaceFolders(event => {
if (this === undefined) {
Expand Down
34 changes: 34 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import * as vscode from "vscode";
import * as os from "os";
import * as path from "path";
import { showReloadExtensionNotification } from "./ui/ReloadExtension";
import { WorkspaceContext } from "./WorkspaceContext";

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

/**
* Handler for configuration change events that triggers a reload of the extension
* if the setting changed requires one.
* @param ctx The workspace context.
* @returns A disposable that unregisters the provider when disposed.
*/
export function handleConfigurationChangeEvent(
ctx: WorkspaceContext
): (event: vscode.ConfigurationChangeEvent) => void {
return (event: vscode.ConfigurationChangeEvent) => {
// on toolchain config change, reload window
if (
event.affectsConfiguration("swift.path") &&
configuration.path !== ctx.toolchain?.swiftFolderPath
) {
showReloadExtensionNotification(
"Changing the Swift path requires Visual Studio Code be reloaded."
);
} else if (
// on sdk config change, restart sourcekit-lsp
event.affectsConfiguration("swift.SDK") ||
event.affectsConfiguration("swift.swiftSDK")
) {
vscode.commands.executeCommand("swift.restartLSPServer");
} else if (event.affectsConfiguration("swift.swiftEnvironmentVariables")) {
showReloadExtensionNotification(
"Changing environment variables requires the project be reloaded."
);
}
};
}

export default configuration;
15 changes: 15 additions & 0 deletions src/contextKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//

import * as vscode from "vscode";
import { Version } from "./utilities/version";

/**
* References:
Expand Down Expand Up @@ -82,6 +83,11 @@ interface ContextKeys {
* Whether the swift.switchPlatform command is available.
*/
switchPlatformAvailable: boolean;

/**
* Sets values for context keys that are enabled/disabled based on the toolchain version in use.
*/
updateKeysBasedOnActiveVersion(toolchainVersion: Version): void;
}

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

return {
updateKeysBasedOnActiveVersion(toolchainVersion: Version) {
this.createNewProjectAvailable = toolchainVersion.isGreaterThanOrEqual(
new Version(5, 8, 0)
);
this.switchPlatformAvailable = toolchainVersion.isGreaterThanOrEqual(
new Version(6, 1, 0)
);
},

get isActivated() {
return isActivated;
},
Expand Down
29 changes: 25 additions & 4 deletions src/debugger/debugAdapterFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,34 @@ import configuration from "../configuration";
* @returns A disposable to be disposed when the extension is deactivated
*/
export function registerDebugger(workspaceContext: WorkspaceContext): vscode.Disposable {
const subscriptions: vscode.Disposable[] = [
registerLoggingDebugAdapterTracker(),
registerLLDBDebugAdapter(workspaceContext.toolchain, workspaceContext.outputChannel),
];
let subscriptions: vscode.Disposable[] = [];

// Monitor the swift.debugger.disable setting and register automatically
// when the setting is changed to enable.
const configurationEvent = vscode.workspace.onDidChangeConfiguration(event => {
if (event.affectsConfiguration("swift.debugger.disable")) {
subscriptions.map(sub => sub.dispose());
subscriptions = [];
if (!configuration.debugger.disable) {
register();
}
}
});

function register() {
subscriptions.push(registerLoggingDebugAdapterTracker());
subscriptions.push(
registerLLDBDebugAdapter(workspaceContext.toolchain, workspaceContext.outputChannel)
);
}

if (!configuration.debugger.disable) {
register();
}

return {
dispose: () => {
configurationEvent.dispose();
subscriptions.map(sub => sub.dispose());
},
};
Expand Down
Loading