Skip to content

Instantly share code, notes, and snippets.

@cjwcommuny
Created March 16, 2022 04:11
Show Gist options
  • Save cjwcommuny/3e14c17175e0cb0d061df7c5a047b21a to your computer and use it in GitHub Desktop.
Save cjwcommuny/3e14c17175e0cb0d061df7c5a047b21a to your computer and use it in GitHub Desktop.
language server extension typescript template.ts
let langserver: ExampleLanguageServer | null = null;
export const activate = function() {
langserver = new ExampleLanguageServer();
}
export const deactivate = function() {
if (langserver) {
langserver.stop();
langserver = null;
}
}
class ExampleLanguageServer {
languageClient: LanguageClient | null = null;
constructor() {
nova.config.observe(
'example.language-server-path',
function(this: ExampleLanguageServer, newPath: string, oldPath: string) { this.start(newPath) }
);
}
deactivate() {
this.stop();
}
start(path: string) {
if (this.languageClient) {
this.languageClient.stop();
nova.subscriptions.remove(this.languageClient);
}
// Use the default server path
if (!path) {
path = '/usr/local/bin/example';
}
const serverOptions = {
path: path
};
const clientOptions = {
// The set of document syntaxes for which the server is valid
syntaxes: ['javascript']
};
const client = new LanguageClient(
'example-langserver',
'Example Language Server',
serverOptions,
clientOptions
);
try {
// Start the client
client.start();
// Add the client to the subscriptions to be cleaned up
nova.subscriptions.add(client);
this.languageClient = client;
} catch (err) {
// If the .start() method throws, it's likely because the path to the language server is invalid
if (nova.inDevMode()) {
console.error(err);
}
}
}
stop() {
if (this.languageClient) {
this.languageClient.stop();
nova.subscriptions.remove(this.languageClient);
this.languageClient = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment