Skip to content

Commit

Permalink
feat: restart command + restart when config changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tekumara committed Apr 8, 2023
1 parent a69923e commit 22d9eba
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 49 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
"contributes": {
"commands": [
{
"command": "typos-vscode.helloWorld",
"title": "Hello World"
"category": "Typos",
"command": "typos.restart",
"title": "Restart"
}
],
"configuration": {
Expand Down
139 changes: 92 additions & 47 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,106 @@
import { workspace, ExtensionContext } from 'vscode';
import {
window,
workspace,
commands,
ConfigurationChangeEvent,
ExtensionContext,
OutputChannel,
} from "vscode";

import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
Executable
} from 'vscode-languageclient/node';
LanguageClient,
LanguageClientOptions,
ServerOptions,
Executable,
} from "vscode-languageclient/node";

let client: LanguageClient;

export function activate(context: ExtensionContext) {

const serverId = "typos";
const serverName = "Typos Language Server";
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 env = { ...process.env };
if (restartTriggeredBy) {
await commands.executeCommand("typos.restart");
}
}
)
);

// TODO: move into config
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();
}

client = createClient(name, outputChannel);
await client.start();
})
);

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

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

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

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

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

const run: Executable = {
command: command,
options: { env: env },
};

const serverOptions: ServerOptions = {
run: run,
// used when launched in debug mode
debug: run
};

const clientOptions: LanguageClientOptions = {
// Register the server for all documents
documentSelector: [{ scheme: 'file', pattern: '**' }],
};

client = new LanguageClient(
serverId,
serverName,
serverOptions,
clientOptions
);

// Start the client. This will also launch the server
client.start();
let config = workspace.getConfiguration("typos");
let path = config.get<null | string>("path");

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

const run: Executable = {
command: path,
options: { env: env },
};

const serverOptions: ServerOptions = {
run: run,
// used when launched in debug mode
debug: run,
};

const clientOptions: LanguageClientOptions = {
// Register the server for all documents
// We use scheme = file to ignore Untitled documents because that generates
// too much request chatter
documentSelector: [{ scheme: "file", pattern: "**" }],
outputChannel: outputChannel,
traceOutputChannel: outputChannel,
};

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

export function deactivate(): Thenable<void> | undefined {
if (!client) {
return undefined;
}
return client.stop();
if (!client) {
return undefined;
}
return client.stop();
}

0 comments on commit 22d9eba

Please sign in to comment.