diff --git a/package.json b/package.json index c6cd529..e348e2c 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,9 @@ "contributes": { "commands": [ { - "command": "typos-vscode.helloWorld", - "title": "Hello World" + "category": "Typos", + "command": "typos.restart", + "title": "Restart" } ], "configuration": { diff --git a/src/extension.ts b/src/extension.ts index 334cf44..fe87d43 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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("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("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 | undefined { - if (!client) { - return undefined; - } - return client.stop(); + if (!client) { + return undefined; + } + return client.stop(); }