Elffuss BraidHub

kikocis/continue public

main · 3.4 KiBRawPermalink
1/**
2 * If we wanted to run or use another language server from our extension, this is how we would do it.
3 */
4
5import * as path from "node:path";
6
7import { type ExtensionContext, extensions, workspace } from "vscode";
8import {
9 LanguageClient,
10 type LanguageClientOptions,
11 type ServerOptions,
12 State,
13 type StateChangeEvent,
14 TransportKind,
15} from "vscode-languageclient/node";
16
17import { getExtensionUri } from "../util/vscode";
18
19let client: LanguageClient;
20
21export async function startLanguageClient(context: ExtensionContext) {
22 const pythonLS = startPythonLanguageServer(context);
23 pythonLS.start();
24}
25
26export async function makeRequest(method: string, param: any): Promise<any> {
27 if (!client) {
28 return;
29 } else if (client.state === State.Starting) {
30 return new Promise((resolve, reject) => {
31 const stateListener = client.onDidChangeState((e: StateChangeEvent) => {
32 if (e.newState === State.Running) {
33 stateListener.dispose();
34 resolve(client.sendRequest(method, param));
35 } else if (e.newState === State.Stopped) {
36 stateListener.dispose();
37 reject(new Error("Language server stopped unexpectedly"));
38 }
39 });
40 });
41 } else {
42 return client.sendRequest(method, param);
43 }
44}
45
46export function deactivate(): Thenable<void> | undefined {
47 if (!client) {
48 return undefined;
49 }
50 return client.stop();
51}
52
53function startPythonLanguageServer(context: ExtensionContext): LanguageClient {
54 const extensionPath = getExtensionUri().fsPath;
55 const command = `cd ${path.join(
56 extensionPath,
57 "scripts",
58 )} && source ${path.join("env", "bin", "activate.fish")} && python -m pyls`;
59 const serverOptions: ServerOptions = {
60 command: command,
61 args: ["-vv"],
62 };
63 const clientOptions: LanguageClientOptions = {
64 documentSelector: ["python"],
65 synchronize: {
66 configurationSection: "pyls",
67 },
68 };
69 return new LanguageClient(command, serverOptions, clientOptions);
70}
71
72async function startPylance(context: ExtensionContext) {
73 const pylance = extensions.getExtension("ms-python.vscode-pylance");
74 await pylance?.activate();
75 if (!pylance) {
76 return;
77 }
78 const { path: lsPath } = await pylance.exports.languageServerFolder();
79
80 // The server is implemented in node
81 const serverModule = context.asAbsolutePath(lsPath);
82 // The debug options for the server
83 // --inspect=6009: runs the server in Node's Inspector mode so VS Code can attach to the server for debugging
84 const debugOptions = { execArgv: ["--nolazy", "--inspect=6009"] };
85
86 // If the extension is launched in debug mode then the debug server options are used
87 // Otherwise the run options are used
88 const serverOptions: ServerOptions = {
89 run: { module: serverModule, transport: TransportKind.ipc },
90 debug: {
91 module: serverModule,
92 transport: TransportKind.ipc,
93 options: debugOptions,
94 },
95 };
96
97 // Options to control the language client
98 const clientOptions: LanguageClientOptions = {
99 // Register the server for plain text documents
100 documentSelector: [{ scheme: "file", language: "python" }],
101 synchronize: {
102 // Notify the server about file changes to '.clientrc files contained in the workspace
103 fileEvents: workspace.createFileSystemWatcher("**/.clientrc"),
104 },
105 };
106
107 // Create the language client and start the client.
108 client = new LanguageClient(
109 "languageServerExample",
110 "Language Server Example",
111 serverOptions,
112 clientOptions,
113 );
114 return client;
115}