Dax Raad 8 месяцев назад
Родитель
Сommit
43b429db93

+ 10 - 16
packages/opencode/src/lsp/client.ts

@@ -29,18 +29,13 @@ export namespace LSPClient {
     ),
   }
 
-  export async function create(input: LSPServer.Info) {
+  export async function create(serverID: string, server: LSPServer.Handle) {
     const app = App.info()
-    log.info("starting client", {
-      id: input.id,
-    })
-
-    const server = await input.spawn(app)
-    if (!server) return
+    log.info("starting client", { id: serverID })
 
     const connection = createMessageConnection(
-      new StreamMessageReader(server.stdout),
-      new StreamMessageWriter(server.stdin),
+      new StreamMessageReader(server.process.stdout),
+      new StreamMessageWriter(server.process.stdin),
     )
 
     const diagnostics = new Map<string, Diagnostic[]>()
@@ -50,16 +45,15 @@ export namespace LSPClient {
         path,
       })
       diagnostics.set(path, params.diagnostics)
-      Bus.publish(Event.Diagnostics, { path, serverID: input.id })
+      Bus.publish(Event.Diagnostics, { path, serverID })
     })
     connection.onRequest("workspace/configuration", async () => {
       return [{}]
     })
     connection.listen()
 
-    const initialization = await input.initialization?.(app)
     await connection.sendRequest("initialize", {
-      processId: server.pid,
+      processId: server.process.pid,
       workspaceFolders: [
         {
           name: "workspace",
@@ -67,7 +61,7 @@ export namespace LSPClient {
         },
       ],
       initializationOptions: {
-        ...initialization,
+        ...server.initialization,
       },
       capabilities: {
         workspace: {
@@ -92,8 +86,8 @@ export namespace LSPClient {
     } = {}
 
     const result = {
-      get clientID() {
-        return input.id
+      get serverID() {
+        return serverID
       },
       get connection() {
         return connection
@@ -153,7 +147,7 @@ export namespace LSPClient {
             unsub = Bus.subscribe(Event.Diagnostics, (event) => {
               if (
                 event.properties.path === input.path &&
-                event.properties.serverID === result.clientID
+                event.properties.serverID === result.serverID
               ) {
                 log.info("got diagnostics", input)
                 clearTimeout(timeout)

+ 3 - 2
packages/opencode/src/lsp/index.ts

@@ -33,8 +33,9 @@ export namespace LSP {
     for (const match of matches) {
       const existing = s.clients.get(match.id)
       if (existing) continue
-      const client = await LSPClient.create(match)
-      if (!client) continue
+      const handle = await match.spawn(App.info())
+      if (!handle) continue
+      const client = await LSPClient.create(match.id, handle)
       s.clients.set(match.id, client)
     }
     if (waitForDiagnostics) {

+ 30 - 19
packages/opencode/src/lsp/server.ts

@@ -7,11 +7,15 @@ import { Log } from "../util/log"
 export namespace LSPServer {
   const log = Log.create({ service: "lsp.server" })
 
+  export interface Handle {
+    process: ChildProcessWithoutNullStreams
+    initialization?: Record<string, any>
+  }
+
   export interface Info {
     id: string
     extensions: string[]
-    initialization?: (app: App.Info) => Promise<Record<string, any>>
-    spawn(app: App.Info): Promise<ChildProcessWithoutNullStreams | undefined>
+    spawn(app: App.Info): Promise<Handle | undefined>
   }
 
   export const All: Info[] = [
@@ -27,30 +31,35 @@ export namespace LSPServer {
         ".mts",
         ".cts",
       ],
-      async initialization(app) {
-        const path = Bun.resolve(
+      async spawn(app) {
+        const tsserver = Bun.resolve(
           "typescript/lib/tsserver.js",
           app.path.cwd,
         ).catch(() => {})
-        if (!path) return {}
-        return {
-          tsserver: {
-            path,
-          },
-        }
-      },
-      async spawn() {
+        if (!tsserver) return
         const root =
           process.argv0 !== "bun"
             ? path.resolve(process.cwd(), process.argv0)
             : process.argv0
-        return spawn(root, ["x", "typescript-language-server", "--stdio"], {
-          argv0: "bun",
-          env: {
-            ...process.env,
-            BUN_BE_BUN: "1",
+        const proc = spawn(
+          root,
+          ["x", "typescript-language-server", "--stdio"],
+          {
+            argv0: "bun",
+            env: {
+              ...process.env,
+              BUN_BE_BUN: "1",
+            },
           },
-        })
+        )
+        return {
+          process: proc,
+          initialization: {
+            tsserver: {
+              path: tsserver,
+            },
+          },
+        }
       },
     },
     {
@@ -79,7 +88,9 @@ export namespace LSPServer {
             bin,
           })
         }
-        return spawn(bin!)
+        return {
+          process: spawn(bin!),
+        }
       },
     },
   ]