Skip to content

Commit

Permalink
fixup! Use extension context for output window disposable (#671) (#673)
Browse files Browse the repository at this point in the history
These channels should only be created once during the activation of the
extension. Otherwise, restarting the extension hangs.
  • Loading branch information
dhruvmanila authored Jan 22, 2025
1 parent 099da0e commit adb92f8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
16 changes: 3 additions & 13 deletions src/common/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
FIND_RUFF_BINARY_SCRIPT_PATH,
RUFF_BINARY_NAME,
} from "./constants";
import { LazyOutputChannel, logger } from "./logger";
import { logger } from "./logger";
import { getDebuggerPath } from "./python";
import {
getExtensionSettings,
Expand All @@ -39,7 +39,6 @@ import { updateServerKind, updateStatus } from "./status";
import { getDocumentSelector } from "./utilities";
import { execFile } from "child_process";
import which = require("which");
import { registerCommand } from "./vscodeapi";

export type IInitializationOptions = {
settings: ISettings[];
Expand Down Expand Up @@ -416,24 +415,15 @@ async function createServer(
let _disposables: Disposable[] = [];

export async function startServer(
context: vscode.ExtensionContext,
projectRoot: vscode.WorkspaceFolder,
workspaceSettings: ISettings,
serverId: string,
serverName: string,
outputChannel: OutputChannel,
traceOutputChannel: OutputChannel,
): Promise<LanguageClient | undefined> {
updateStatus(undefined, LanguageStatusSeverity.Information, true);

// Create output channels for the server and trace logs
const outputChannel = vscode.window.createOutputChannel(`${serverName} Language Server`);
context.subscriptions.push(outputChannel);
const traceOutputChannel = new LazyOutputChannel(`${serverName} Language Server Trace`);
context.subscriptions.push(traceOutputChannel);
// And, a command to show the server logs
context.subscriptions.push(
registerCommand(`${serverId}.showServerLogs`, () => outputChannel.show()),
);

const extensionSettings = await getExtensionSettings(serverId);
const globalSettings = await getGlobalSettings(serverId);

Expand Down
23 changes: 20 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from "vscode";
import { ExecuteCommandRequest, LanguageClient } from "vscode-languageclient/node";
import { logger } from "./common/logger";
import { LazyOutputChannel, logger } from "./common/logger";
import {
checkVersion,
initializePython,
Expand Down Expand Up @@ -42,6 +42,13 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
logger.info(`Module: ${serverInfo.module}`);
logger.debug(`Full Server Info: ${JSON.stringify(serverInfo)}`);

// Create output channels for the server and trace logs
const outputChannel = vscode.window.createOutputChannel(`${serverName} Language Server`);
const traceOutputChannel = new LazyOutputChannel(`${serverName} Language Server Trace`);

// Make sure that these channels are disposed when the extension is deactivated.
context.subscriptions.push(outputChannel);
context.subscriptions.push(traceOutputChannel);
context.subscriptions.push(logger.channel);

context.subscriptions.push(
Expand Down Expand Up @@ -127,7 +134,14 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
}
}

lsClient = await startServer(context, projectRoot, workspaceSettings, serverId, serverName);
lsClient = await startServer(
projectRoot,
workspaceSettings,
serverId,
serverName,
outputChannel,
traceOutputChannel,
);
} finally {
// Ensure that we reset the flag in case of an error, early return, or success.
restartInProgress = false;
Expand All @@ -150,9 +164,12 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
onDidGrantWorkspaceTrust(async () => {
await runServer();
}),
registerCommand(`${serverId}.showLogs`, async () => {
registerCommand(`${serverId}.showLogs`, () => {
logger.channel.show();
}),
registerCommand(`${serverId}.showServerLogs`, () => {
outputChannel.show();
}),
registerCommand(`${serverId}.restart`, async () => {
await runServer();
}),
Expand Down

0 comments on commit adb92f8

Please sign in to comment.