Browse Source

feat: support astro lsp (#3242)

Hieu Nguyen 4 months ago
parent
commit
4d8268c818

+ 1 - 0
packages/opencode/src/lsp/language.ts

@@ -99,4 +99,5 @@ export const LANGUAGE_EXTENSIONS: Record<string, string> = {
   ".vue": "vue",
   ".vue": "vue",
   ".zig": "zig",
   ".zig": "zig",
   ".zon": "zig",
   ".zon": "zig",
+  ".astro": "astro",
 } as const
 } as const

+ 51 - 0
packages/opencode/src/lsp/server.ts

@@ -701,6 +701,57 @@ export namespace LSPServer {
     },
     },
   }
   }
 
 
+  export const Astro: Info = {
+    id: "astro",
+    extensions: [".astro"],
+    root: NearestRoot(["package-lock.json", "bun.lockb", "bun.lock", "pnpm-lock.yaml", "yarn.lock"]),
+    async spawn(root) {
+      const tsserver = await Bun.resolve("typescript/lib/tsserver.js", Instance.directory).catch(() => {})
+      if (!tsserver) {
+        log.info("typescript not found, required for Astro language server")
+        return
+      }
+      const tsdk = path.dirname(tsserver)
+
+      let binary = Bun.which("astro-ls")
+      const args: string[] = []
+      if (!binary) {
+        const js = path.join(Global.Path.bin, "node_modules", "@astrojs", "language-server", "bin", "nodeServer.js")
+        if (!(await Bun.file(js).exists())) {
+          if (Flag.OPENCODE_DISABLE_LSP_DOWNLOAD) return
+          await Bun.spawn([BunProc.which(), "install", "@astrojs/language-server"], {
+            cwd: Global.Path.bin,
+            env: {
+              ...process.env,
+              BUN_BE_BUN: "1",
+            },
+            stdout: "pipe",
+            stderr: "pipe",
+            stdin: "pipe",
+          }).exited
+        }
+        binary = BunProc.which()
+        args.push("run", js)
+      }
+      args.push("--stdio")
+      const proc = spawn(binary, args, {
+        cwd: root,
+        env: {
+          ...process.env,
+          BUN_BE_BUN: "1",
+        },
+      })
+      return {
+        process: proc,
+        initialization: {
+          typescript: {
+            tsdk,
+          },
+        },
+      }
+    },
+  }
+
   export const JDTLS: Info = {
   export const JDTLS: Info = {
     id: "jdtls",
     id: "jdtls",
     root: NearestRoot(["pom.xml", "build.gradle", "build.gradle.kts", ".project", ".classpath"]),
     root: NearestRoot(["pom.xml", "build.gradle", "build.gradle.kts", ".project", ".classpath"]),

+ 1 - 0
packages/web/src/content/docs/lsp.mdx

@@ -26,6 +26,7 @@ OpenCode comes with several built-in LSP servers for popular languages:
 | rust       | .rs                                                  | `rust-analyzer` command available                            |
 | rust       | .rs                                                  | `rust-analyzer` command available                            |
 | clangd     | .c, .cpp, .cc, .cxx, .c++, .h, .hpp, .hh, .hxx, .h++ | Auto-installs for C/C++ projects                             |
 | clangd     | .c, .cpp, .cc, .cxx, .c++, .h, .hpp, .hh, .hxx, .h++ | Auto-installs for C/C++ projects                             |
 | svelte     | .svelte                                              | Auto-installs for Svelte projects                            |
 | svelte     | .svelte                                              | Auto-installs for Svelte projects                            |
+| astro      | .astro                                               | Auto-installs for Astro projects                             |
 | jdtls      | .java                                                | `Java SDK (version 21+)` installed                           |
 | jdtls      | .java                                                | `Java SDK (version 21+)` installed                           |
 
 
 LSP servers are automatically enabled when one of the above file extensions are detected and the requirements are met.
 LSP servers are automatically enabled when one of the above file extensions are detected and the requirements are met.