-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: restart command + restart when config changes
- Loading branch information
Showing
2 changed files
with
95 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |