Skip to content

Instantly share code, notes, and snippets.

@CompuIves
Created June 7, 2023 18:41
Show Gist options
  • Save CompuIves/9d006f8bd08488361d5878e6491358e3 to your computer and use it in GitHub Desktop.
Save CompuIves/9d006f8bd08488361d5878e6491358e3 to your computer and use it in GitHub Desktop.
import path from 'path';
import { processToDisposable, spawnLanguageServer } from "../../utils/process";
import { forwardConnection, LSPConnection } from "../../utils/connection";
import { captureError } from "@codesandbox/pitcher-node-common";
import { Disposable } from "@codesandbox/pitcher-common";
import { InitializeParams } from "vscode-languageserver";
import { LSPServer, LSPServerOptions } from "../server";
export class AstroServer extends LSPServer {
lspConnection: LSPConnection | null = null;
processConnection: LSPConnection | null = null;
constructor(opts: LSPServerOptions) {
super(opts);
}
override async start(): Promise<Disposable> {
const serverBinaryPath = require.resolve(
"@astrojs/language-server/bin/nodeServer.js"
);
if (!serverBinaryPath) {
throw new Error('Could not resolve "astro-languageserver" binary');
}
console.log("[LSP] Starting Astro LSP...");
const { process, connection } = spawnLanguageServer({
command: "sudo",
args: ["-u", "pitcher-host", "node", serverBinaryPath, "--stdio"],
options: {
cwd: this.projectRoot,
},
onError: (err) => {
console.error("Error from astro lang server:", err);
},
onErrorLog: (err) => {
console.error("Error from astro lang server:", err);
},
onExit: (code) =>
captureError(new Error(`Astro language server exited with ${code}`)),
});
this.processConnection = connection;
this.lspConnection = this.addDisposable(
forwardConnection(this.serverConnection, this.processConnection)
);
this.isReady = true;
return processToDisposable(process);
}
override async stop(): Promise<void> {
this.isReady = false;
this.processConnection?.dispose();
this.processConnection = null;
}
override getInitializeParams(): InitializeParams {
const existingInitializeParams = super.getInitializeParams();
existingInitializeParams.initializationOptions = {
typescript: {
// TODO: find a way to resolve the local typescript version if available
tsdk: path.dirname(
require.resolve("typescript/lib/tsserverlibrary.js")
),
},
};
return existingInitializeParams;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment