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

Refactor Ruby and output channel #874

Merged
merged 3 commits into from
Nov 6, 2023
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
20 changes: 5 additions & 15 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Telemetry, RequestEvent } from "./telemetry";
import { Ruby } from "./ruby";
import { StatusItems, Command, ServerState, ClientInterface } from "./status";
import { TestController } from "./testController";
import { LOG_CHANNEL } from "./common";

const LSP_NAME = "Ruby LSP";
const asyncExec = promisify(exec);
Expand All @@ -37,7 +38,6 @@ export default class Client implements ClientInterface {
private readonly workingFolder: string;
private readonly telemetry: Telemetry;
private readonly statusItems: StatusItems;
private readonly outputChannel: vscode.OutputChannel;
private readonly testController: TestController;
private readonly customBundleGemfile: string = vscode.workspace
.getConfiguration("rubyLsp")
Expand All @@ -56,7 +56,6 @@ export default class Client implements ClientInterface {
telemetry: Telemetry,
ruby: Ruby,
testController: TestController,
outputChannel: vscode.OutputChannel,
workingFolder = vscode.workspace.workspaceFolders![0].uri.fsPath,
) {
this.workingFolder = workingFolder;
Expand All @@ -65,7 +64,6 @@ export default class Client implements ClientInterface {
this.testController = testController;
this.#context = context;
this.#ruby = ruby;
this.outputChannel = outputChannel;
this.#formatter = "";
this.statusItems = new StatusItems(this);
this.registerCommands();
Expand Down Expand Up @@ -111,7 +109,7 @@ export default class Client implements ClientInterface {
const clientOptions: LanguageClientOptions = {
documentSelector: [{ language: "ruby" }],
diagnosticCollectionName: LSP_NAME,
outputChannel: this.outputChannel,
outputChannel: LOG_CHANNEL,
revealOutputChannelOn: RevealOutputChannelOn.Never,
diagnosticPullOptions: this.diagnosticPullOptions(),
initializationOptions: {
Expand Down Expand Up @@ -231,9 +229,7 @@ export default class Client implements ClientInterface {
await this.client.start();
} catch (error: any) {
this.state = ServerState.Error;
this.outputChannel.appendLine(
`Error restarting the server: ${error.message}`,
);
LOG_CHANNEL.error(`Error restarting the server: ${error.message}`);
return;
}

Expand Down Expand Up @@ -274,16 +270,12 @@ export default class Client implements ClientInterface {
}
} catch (error: any) {
this.state = ServerState.Error;

this.outputChannel.appendLine(
`Error restarting the server: ${error.message}`,
);
LOG_CHANNEL.error(`Error restarting the server: ${error.message}`);
}
}

dispose() {
this.client?.dispose();
this.outputChannel.dispose();
}

get ruby(): Ruby {
Expand Down Expand Up @@ -461,9 +453,7 @@ export default class Client implements ClientInterface {
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
this.outputChannel.appendLine(
`Failed to update global ruby-lsp gem: ${error}`,
);
LOG_CHANNEL.error(`Failed to update global ruby-lsp gem: ${error}`);
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import fs from "fs/promises";
import { exec } from "child_process";
import { promisify } from "util";

import * as vscode from "vscode";

export const asyncExec = promisify(exec);
export const LOG_CHANNEL = vscode.window.createOutputChannel("Ruby LSP", {
log: true,
});

export async function pathExists(
path: string,
mode = fs.constants.R_OK,
): Promise<boolean> {
try {
await fs.access(path, mode);
return true;
} catch (error: any) {
return false;
}
}
18 changes: 5 additions & 13 deletions src/debugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ChildProcessWithoutNullStreams, spawn } from "child_process";
import * as vscode from "vscode";

import { Ruby } from "./ruby";
import { LOG_CHANNEL } from "./common";

export class Debugger
implements
Expand All @@ -16,16 +17,13 @@ export class Debugger
private debugProcess?: ChildProcessWithoutNullStreams;
private readonly console = vscode.debug.activeDebugConsole;
private readonly subscriptions: vscode.Disposable[];
private readonly outputChannel: vscode.OutputChannel;

constructor(
context: vscode.ExtensionContext,
ruby: Ruby,
outputChannel: vscode.OutputChannel,
workingFolder = vscode.workspace.workspaceFolders![0].uri.fsPath,
) {
this.ruby = ruby;
this.outputChannel = outputChannel;
this.subscriptions = [
vscode.debug.registerDebugConfigurationProvider("ruby_lsp", this),
vscode.debug.registerDebugAdapterDescriptorFactory("ruby_lsp", this),
Expand Down Expand Up @@ -183,15 +181,9 @@ export class Debugger
configuration.program,
];

this.outputChannel.appendLine(
`Ruby LSP> Spawning debugger in directory ${this.workingFolder}`,
);
this.outputChannel.appendLine(
`Ruby LSP> Command bundle ${args.join(" ")}`,
);
this.outputChannel.appendLine(
`Ruby LSP> Environment ${JSON.stringify(configuration.env)}`,
);
LOG_CHANNEL.info(`Spawning debugger in directory ${this.workingFolder}`);
LOG_CHANNEL.info(` Command bundle ${args.join(" ")}`);
LOG_CHANNEL.info(` Environment ${JSON.stringify(configuration.env)}`);

this.debugProcess = spawn("bundle", args, {
shell: true,
Expand Down Expand Up @@ -236,7 +228,7 @@ export class Debugger
if (code) {
const message = `Debugger exited with status ${code}. Check the output channel for more information.`;
this.console.append(message);
this.outputChannel.show();
LOG_CHANNEL.show();
reject(new Error(message));
}
});
Expand Down
7 changes: 3 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ let debug: Debugger | undefined;
let testController: TestController | undefined;

export async function activate(context: vscode.ExtensionContext) {
const outputChannel = vscode.window.createOutputChannel("Ruby LSP");
const ruby = new Ruby(context, outputChannel);
const ruby = new Ruby(context, vscode.workspace.workspaceFolders![0]);
await ruby.activateRuby();

const telemetry = new Telemetry(context);
Expand All @@ -26,10 +25,10 @@ export async function activate(context: vscode.ExtensionContext) {
telemetry,
);

client = new Client(context, telemetry, ruby, testController, outputChannel);
client = new Client(context, telemetry, ruby, testController);

await client.start();
debug = new Debugger(context, ruby, outputChannel);
debug = new Debugger(context, ruby);

vscode.workspace.registerTextDocumentContentProvider(
"ruby-lsp",
Expand Down
Loading