Skip to content

Commit

Permalink
use extension mode for optionally running in debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander Ronde committed Oct 6, 2024
1 parent 22e64a8 commit d9024c8
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 21 deletions.
15 changes: 8 additions & 7 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ async function startLanguageServer(
}

export async function activate(context: ExtensionContext): Promise<void> {
log(CLIENT_PREFIX, 'Initializing PHPStan extension');
log(context, CLIENT_PREFIX, 'Initializing PHPStan extension');
createOutputChannel();

const client = await startLanguageServer(context);
const statusBar = new StatusBar(context, client);
const watcher = new DocumentManager(client);
Expand All @@ -91,14 +92,14 @@ export async function activate(context: ExtensionContext): Promise<void> {
const zombieKiller = new ZombieKiller(client, context);

registerListeners(context, client, errorManager, proManager);
registerEditorConfigurationListener(client);
registerEditorConfigurationListener(context, client);
registerLogMessager(context, client);
context.subscriptions.push(
statusBar,
watcher,
errorManager,
proManager,
zombieKiller
zombieKiller,
);

let wasReady = false;
Expand All @@ -108,13 +109,13 @@ export async function activate(context: ExtensionContext): Promise<void> {
if (ready) {
if (!wasReady) {
// First time it's ready, start watching
log(SERVER_PREFIX, 'Language server started');
log(context, SERVER_PREFIX, 'Language server started');
void watcher.watch();
} else {
// Language server was already alive but then died
// and restarted. Clear local state that depends
// on language server.
log(SERVER_PREFIX, 'Language server restarted...');
log(context, SERVER_PREFIX, 'Language server restarted...');
statusBar.clearAllRunning();
}
wasReady = true;
Expand All @@ -126,7 +127,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
});
})
);
log(CLIENT_PREFIX, 'Initializing done');
log(context, CLIENT_PREFIX, 'Initializing done');

void (async () => {
if (
Expand All @@ -148,7 +149,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
}
})();

log(CLIENT_PREFIX, 'Showing one-time messages (if needed)');
log(context, CLIENT_PREFIX, 'Showing one-time messages (if needed)');
const installationConfig = await getInstallationConfig(context);
const version = (context.extension.packageJSON as { version: string })
.version;
Expand Down
2 changes: 0 additions & 2 deletions client/src/lib/dev.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
declare const _DEBUG: boolean | undefined;
declare const _INSPECT_BRK: boolean | undefined;

export const DEBUG = typeof _DEBUG === 'undefined' ? false : _DEBUG;
export const INSPECT_BRK =
typeof _INSPECT_BRK === 'undefined' ? false : _INSPECT_BRK;
3 changes: 3 additions & 0 deletions client/src/lib/editorConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { LanguageClient } from 'vscode-languageclient/node';
import type { ConfigSettings } from '../../../shared/config';
import { watcherNotification } from './notificationChannels';
import { config } from '../../../shared/commands/defs';
import type { ExtensionContext } from 'vscode';
import { window, workspace } from 'vscode';
import { CLIENT_PREFIX, log } from './log';

Expand All @@ -19,6 +20,7 @@ export function getEditorConfiguration(): TypedWorkspaceConfiguration<ConfigSett
}

export function registerEditorConfigurationListener(
context: ExtensionContext,
client: LanguageClient
): void {
const editorConfig = getEditorConfiguration();
Expand All @@ -27,6 +29,7 @@ export function registerEditorConfigurationListener(
configValues[key] = editorConfig.get(key as keyof ConfigSettings);
}
log(
context,
CLIENT_PREFIX,
'Starting extension with configuration:',
JSON.stringify(configValues, null, '\t')
Expand Down
12 changes: 8 additions & 4 deletions client/src/lib/errorUtil.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { window, type ExtensionContext } from 'vscode';
import { ERROR_PREFIX, log } from './log';
import * as vscode from 'vscode';

const shownWarnings: Set<string> = new Set();

export function showErrorOnce(message: string, ...extra: string[]): void {
log(ERROR_PREFIX, message, ...extra);
export function showErrorOnce(
context: ExtensionContext,
message: string,
...extra: string[]
): void {
log(context, ERROR_PREFIX, message, ...extra);
if (shownWarnings.has(message)) {
return;
}
Expand All @@ -18,7 +22,7 @@ interface ErrorOption {
}

export function showError(message: string, options?: ErrorOption[]): void {
void vscode.window
void window
.showErrorMessage(message, ...(options || []).map((o) => o.title))
.then((choice) => {
if (!options || !choice) {
Expand Down
13 changes: 8 additions & 5 deletions client/src/lib/log.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { LanguageClient } from 'vscode-languageclient/node';
import type { ExtensionContext, OutputChannel } from 'vscode';
import { logNotification } from './notificationChannels';
import { window } from 'vscode';
import { DEBUG } from './dev';
import { ExtensionMode, window } from 'vscode';

let channel: OutputChannel | null;

Expand All @@ -16,7 +15,7 @@ export function registerLogMessager(
): void {
context.subscriptions.push(
client.onNotification(logNotification, ({ data }) => {
log(...(data as [Prefix, ...string[]]));
log(context, ...(data as [Prefix, ...string[]]));
})
);
}
Expand All @@ -25,9 +24,13 @@ type Prefix = string & {
__isPrefix: true;
};

export function log(prefix: Prefix, ...data: string[]): void {
export function log(
context: ExtensionContext,
prefix: Prefix,
...data: string[]
): void {
data = [`[${new Date().toLocaleString()}]`, prefix, ...data];
if (DEBUG) {
if (context.extensionMode === ExtensionMode.Development) {
console.log(data.join(' '));
}
if (channel) {
Expand Down
8 changes: 5 additions & 3 deletions client/src/notificationReceivers/statusBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ export class StatusBar implements Disposable {
private _hideTimeout: NodeJS.Timer | undefined;

public constructor(
context: vscode.ExtensionContext,
private readonly _context: vscode.ExtensionContext,
client: LanguageClient
) {
context.subscriptions.push(
this._context.subscriptions.push(
client.onNotification(statusBarNotification, (params) => {
log(
this._context,
STATUS_BAR_PREFIX,
"notification:'",
JSON.stringify(params)
Expand Down Expand Up @@ -82,7 +83,7 @@ export class StatusBar implements Disposable {
}

private _showStatusBar(): void {
log(STATUS_BAR_PREFIX, 'Showing status bar');
log(this._context, STATUS_BAR_PREFIX, 'Showing status bar');
if (!getEditorConfiguration().get('phpstan.enableStatusBar')) {
return;
}
Expand Down Expand Up @@ -111,6 +112,7 @@ export class StatusBar implements Disposable {
result: OperationStatus
): void {
log(
this._context,
STATUS_BAR_PREFIX,
'Hiding status bar, last operation result =',
result
Expand Down
1 change: 1 addition & 0 deletions client/src/notificationReceivers/zombieKiller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class ZombieKiller implements Disposable {
processNotification,
({ pid, children, timeout }) => {
log(
this._context,
PROCESS_SPAWNER_PREFIX,
'Spawning process',
String(pid),
Expand Down

0 comments on commit d9024c8

Please sign in to comment.