Skip to content

Commit

Permalink
feat: better error handling + loglevel
Browse files Browse the repository at this point in the history
  • Loading branch information
tekumara committed Apr 8, 2023
1 parent 22d9eba commit 42b36d1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
15 changes: 14 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,20 @@
"scope": "machine-overridable",
"type": "string",
"default": null,
"description": "When set to a path to the `typos-lsp` binary, extension will use that."
"description": "Path to the `typos-lsp` binary."
},
"typos.logLevel": {
"scope": "window",
"type": "string",
"enum": [
"error",
"warn",
"info",
"debug",
"trace"
],
"default": "error",
"description": "Logging level of the language server."
},
"typos.trace.server": {
"scope": "window",
Expand Down
60 changes: 31 additions & 29 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
commands,
ConfigurationChangeEvent,
ExtensionContext,
OutputChannel,
OutputChannel,
} from "vscode";

import {
Expand All @@ -14,23 +14,21 @@ import {
Executable,
} from "vscode-languageclient/node";

let client: LanguageClient;
let client: LanguageClient | undefined;


export function activate(context: ExtensionContext) {
let name = "Typos";
export function activate(context: ExtensionContext) {
let name = "Typos";

const outputChannel = window.createOutputChannel(name);
context.subscriptions.push(outputChannel);

client = createClient(name, outputChannel);

context.subscriptions.push(
workspace.onDidChangeConfiguration(
async (e: ConfigurationChangeEvent) => {
const restartTriggeredBy = ["typos.path"].find((s) =>
e.affectsConfiguration(s)
);
const restartTriggeredBy = [
"typos.path",
"typos.logLevel",
].find((s) => e.affectsConfiguration(s));

if (restartTriggeredBy) {
await commands.executeCommand("typos.restart");
Expand All @@ -41,35 +39,44 @@ export function activate(context: ExtensionContext) {

context.subscriptions.push(
commands.registerCommand("typos.restart", async () => {
//void window.showInformationMessage("Restarting typos...");

// don't stop if the client has previously failed to start
if (client.needsStop()) {
await client.stop();
}
// can't stop if the client has previously failed to start
if (client && client.needsStop()) {
await client.stop();
}

client = createClient(name, outputChannel);
try {
client = createClient(name, outputChannel);
} catch (err) {
window.showErrorMessage(
`Typos: ${err instanceof Error ? err.message : err}`
);
return;
}
await client.start();
})
);

client = createClient(name, outputChannel);

// Start the client. This will also launch the server
client.start();
}

function createClient(name: string, outputChannel: OutputChannel): LanguageClient {
function createClient(
name: string,
outputChannel: OutputChannel
): LanguageClient {
const env = { ...process.env };

// TODO: move into config
env.RUST_LOG = "trace";

let config = workspace.getConfiguration("typos");
let path = config.get<null | string>("path");

if (!path) {
throw new Error(`Please specify the typos.path setting.`);
}

env.RUST_LOG = config.get("logLevel");

const run: Executable = {
command: path,
options: { env: env },
Expand All @@ -86,16 +93,11 @@ function createClient(name: string, outputChannel: OutputChannel): LanguageClien
// We use scheme = file to ignore Untitled documents because that generates
// too much request chatter
documentSelector: [{ scheme: "file", pattern: "**" }],
outputChannel: outputChannel,
traceOutputChannel: outputChannel,
outputChannel: outputChannel,
traceOutputChannel: outputChannel,
};

return new LanguageClient(
"typos",
name,
serverOptions,
clientOptions
);
return new LanguageClient("typos", name, serverOptions, clientOptions);
}

export function deactivate(): Thenable<void> | undefined {
Expand Down

0 comments on commit 42b36d1

Please sign in to comment.