Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
Add workspace logger (#979)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock authored Jan 15, 2024
1 parent 79d5355 commit 0957a15
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 16 deletions.
8 changes: 6 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import {
MessageSignature,
} from "vscode-languageclient/node";

import { LOG_CHANNEL, LSP_NAME, ClientInterface } from "./common";
import { LSP_NAME, ClientInterface } from "./common";
import { Telemetry, RequestEvent } from "./telemetry";
import { Ruby } from "./ruby";
import { WorkspaceChannel } from "./workspaceChannel";

type EnabledFeatures = Record<string, boolean>;

Expand Down Expand Up @@ -89,6 +90,7 @@ function getLspExecutables(
function collectClientOptions(
configuration: vscode.WorkspaceConfiguration,
workspaceFolder: vscode.WorkspaceFolder,
outputChannel: WorkspaceChannel,
): LanguageClientOptions {
const pullOn: "change" | "save" | "both" =
configuration.get("pullDiagnosticsOn")!;
Expand All @@ -107,7 +109,7 @@ function collectClientOptions(
],
workspaceFolder,
diagnosticCollectionName: LSP_NAME,
outputChannel: LOG_CHANNEL,
outputChannel,
revealOutputChannelOn: RevealOutputChannelOn.Never,
diagnosticPullOptions,
initializationOptions: {
Expand Down Expand Up @@ -139,13 +141,15 @@ export default class Client extends LanguageClient implements ClientInterface {
ruby: Ruby,
createTestItems: (response: CodeLens[]) => void,
workspaceFolder: vscode.WorkspaceFolder,
outputChannel: WorkspaceChannel,
) {
super(
LSP_NAME,
getLspExecutables(workspaceFolder, ruby.env),
collectClientOptions(
vscode.workspace.getConfiguration("rubyLsp"),
workspaceFolder,
outputChannel,
),
);

Expand Down
14 changes: 10 additions & 4 deletions src/ruby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import fs from "fs/promises";

import * as vscode from "vscode";

import { asyncExec, pathExists, LOG_CHANNEL, RubyInterface } from "./common";
import { asyncExec, pathExists, RubyInterface } from "./common";
import { WorkspaceChannel } from "./workspaceChannel";

export enum VersionManager {
Asdf = "asdf",
Expand All @@ -29,13 +30,16 @@ export class Ruby implements RubyInterface {
private readonly context: vscode.ExtensionContext;
private readonly customBundleGemfile?: string;
private readonly cwd: string;
private readonly outputChannel: WorkspaceChannel;

constructor(
context: vscode.ExtensionContext,
workingFolder: vscode.WorkspaceFolder,
outputChannel: WorkspaceChannel,
) {
this.context = context;
this.workingFolderPath = workingFolder.uri.fsPath;
this.outputChannel = outputChannel;

const customBundleGemfile: string = vscode.workspace
.getConfiguration("rubyLsp")
Expand Down Expand Up @@ -78,7 +82,9 @@ export class Ruby implements RubyInterface {
// If the version manager is auto, discover the actual manager before trying to activate anything
if (this.versionManager === VersionManager.Auto) {
await this.discoverVersionManager();
LOG_CHANNEL.info(`Discovered version manager ${this.versionManager}`);
this.outputChannel.info(
`Discovered version manager ${this.versionManager}`,
);
}

try {
Expand Down Expand Up @@ -184,7 +190,7 @@ export class Ruby implements RubyInterface {
command += "'";
}

LOG_CHANNEL.info(
this.outputChannel.info(
`Trying to activate Ruby environment with command: ${command} inside directory: ${this.cwd}`,
);

Expand Down Expand Up @@ -322,7 +328,7 @@ export class Ruby implements RubyInterface {
command += "'";
}

LOG_CHANNEL.info(
this.outputChannel.info(
`Checking if ${tool} is available on the path with command: ${command}`,
);

Expand Down
7 changes: 5 additions & 2 deletions src/test/suite/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { State } from "vscode-languageclient/node";
import { Ruby, VersionManager } from "../../ruby";
import { Telemetry, TelemetryApi, TelemetryEvent } from "../../telemetry";
import Client from "../../client";
import { asyncExec } from "../../common";
import { LOG_CHANNEL, asyncExec } from "../../common";
import { WorkspaceChannel } from "../../workspaceChannel";

class FakeApi implements TelemetryApi {
public sentEvents: TelemetryEvent[];
Expand Down Expand Up @@ -66,8 +67,9 @@ suite("Client", () => {
update: (_name: string, _value: any) => Promise.resolve(),
},
} as unknown as vscode.ExtensionContext;
const outputChannel = new WorkspaceChannel("fake", LOG_CHANNEL);

const ruby = new Ruby(context, workspaceFolder);
const ruby = new Ruby(context, workspaceFolder, outputChannel);
await ruby.activateRuby();

await asyncExec("gem install ruby-lsp", {
Expand All @@ -82,6 +84,7 @@ suite("Client", () => {
ruby,
() => {},
workspaceFolder,
outputChannel,
);

try {
Expand Down
15 changes: 11 additions & 4 deletions src/test/suite/ruby.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import * as os from "os";
import * as vscode from "vscode";

import { Ruby, VersionManager } from "../../ruby";
import { WorkspaceChannel } from "../../workspaceChannel";
import { LOG_CHANNEL } from "../../common";

suite("Ruby environment activation", () => {
let ruby: Ruby;
Expand All @@ -22,10 +24,15 @@ suite("Ruby environment activation", () => {
const context = {
extensionMode: vscode.ExtensionMode.Test,
} as vscode.ExtensionContext;

ruby = new Ruby(context, {
uri: { fsPath: tmpPath },
} as vscode.WorkspaceFolder);
const outputChannel = new WorkspaceChannel("fake", LOG_CHANNEL);

ruby = new Ruby(
context,
{
uri: { fsPath: tmpPath },
} as vscode.WorkspaceFolder,
outputChannel,
);
await ruby.activateRuby(
// eslint-disable-next-line no-process-env
process.env.CI ? VersionManager.None : VersionManager.Chruby,
Expand Down
27 changes: 27 additions & 0 deletions src/test/suite/workspaceChannel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as assert from "assert";

import * as vscode from "vscode";

import { WorkspaceChannel } from "../../workspaceChannel";

class FakeChannel {
public readonly messages: string[] = [];

info(message: string) {
this.messages.push(message);
}
}

suite("Workspace channel", () => {
test("prepends name as a prefix", () => {
const fakeChannel = new FakeChannel();
const channel = new WorkspaceChannel(
"test",
fakeChannel as unknown as vscode.LogOutputChannel,
);

channel.info("hello!");
assert.strictEqual(fakeChannel.messages.length, 1);
assert.strictEqual(fakeChannel.messages[0], "(test) hello!");
});
});
17 changes: 13 additions & 4 deletions src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
WorkspaceInterface,
STATUS_EMITTER,
} from "./common";
import { WorkspaceChannel } from "./workspaceChannel";

export class Workspace implements WorkspaceInterface {
public lspClient?: Client;
Expand All @@ -20,6 +21,7 @@ export class Workspace implements WorkspaceInterface {
public readonly workspaceFolder: vscode.WorkspaceFolder;
private readonly context: vscode.ExtensionContext;
private readonly telemetry: Telemetry;
private readonly outputChannel: WorkspaceChannel;
private needsRestart = false;
#rebaseInProgress = false;
#error = false;
Expand All @@ -32,8 +34,12 @@ export class Workspace implements WorkspaceInterface {
) {
this.context = context;
this.workspaceFolder = workspaceFolder;
this.outputChannel = new WorkspaceChannel(
workspaceFolder.name,
LOG_CHANNEL,
);
this.telemetry = telemetry;
this.ruby = new Ruby(context, workspaceFolder);
this.ruby = new Ruby(context, workspaceFolder, this.outputChannel);
this.createTestItems = createTestItems;

this.registerRestarts(context);
Expand Down Expand Up @@ -87,6 +93,7 @@ export class Workspace implements WorkspaceInterface {
this.ruby,
this.createTestItems,
this.workspaceFolder,
this.outputChannel,
);

try {
Expand All @@ -102,7 +109,7 @@ export class Workspace implements WorkspaceInterface {
}
} catch (error: any) {
this.error = true;
LOG_CHANNEL.error(`Error starting the server: ${error.message}`);
this.outputChannel.error(`Error starting the server: ${error.message}`);
}
}

Expand Down Expand Up @@ -144,7 +151,7 @@ export class Workspace implements WorkspaceInterface {
}
} catch (error: any) {
this.error = true;
LOG_CHANNEL.error(`Error restarting the server: ${error.message}`);
this.outputChannel.error(`Error restarting the server: ${error.message}`);
}
}

Expand Down Expand Up @@ -199,7 +206,9 @@ export class Workspace implements WorkspaceInterface {
this.context.workspaceState.update("rubyLsp.lastGemUpdate", Date.now());
} catch (error) {
// If we fail to update the global installation of `ruby-lsp`, we don't want to prevent the server from starting
LOG_CHANNEL.error(`Failed to update global ruby-lsp gem: ${error}`);
this.outputChannel.error(
`Failed to update global ruby-lsp gem: ${error}`,
);
}
}
}
Expand Down
75 changes: 75 additions & 0 deletions src/workspaceChannel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import * as vscode from "vscode";

export class WorkspaceChannel implements vscode.LogOutputChannel {
public readonly onDidChangeLogLevel: vscode.Event<vscode.LogLevel>;
private readonly actualChannel: vscode.LogOutputChannel;
private readonly prefix: string;

constructor(workspaceName: string, actualChannel: vscode.LogOutputChannel) {
this.prefix = `(${workspaceName})`;
this.actualChannel = actualChannel;
this.onDidChangeLogLevel = this.actualChannel.onDidChangeLogLevel;
}

get name(): string {
return this.actualChannel.name;
}

get logLevel(): vscode.LogLevel {
return this.actualChannel.logLevel;
}

trace(message: string, ...args: any[]): void {
this.actualChannel.trace(`${this.prefix} ${message}`, ...args);
}

debug(message: string, ...args: any[]): void {
this.actualChannel.debug(`${this.prefix} ${message}`, ...args);
}

info(message: string, ...args: any[]): void {
this.actualChannel.info(`${this.prefix} ${message}`, ...args);
}

warn(message: string, ...args: any[]): void {
this.actualChannel.warn(`${this.prefix} ${message}`, ...args);
}

error(error: string | Error, ...args: any[]): void {
this.actualChannel.error(`${this.prefix} ${error}`, ...args);
}

append(value: string): void {
this.actualChannel.append(`${this.prefix} ${value}`);
}

appendLine(value: string): void {
this.actualChannel.appendLine(`${this.prefix} ${value}`);
}

replace(value: string): void {
this.actualChannel.replace(`${this.prefix} ${value}`);
}

clear(): void {
this.actualChannel.clear();
}

show(preserveFocus?: boolean | undefined): void;
show(
column?: vscode.ViewColumn | undefined,
preserveFocus?: boolean | undefined,
): void;

show(_column?: unknown, preserveFocus?: boolean | undefined): void {
this.actualChannel.show(preserveFocus);
}

hide(): void {
this.actualChannel.hide();
}

dispose(): void {
this.actualChannel.dispose();
}
}

0 comments on commit 0957a15

Please sign in to comment.