Procházet zdrojové kódy

Add support for the Zig Language Server (ZLS) (#756)

Frank Denis před 7 měsíci
rodič
revize
f7d6175283

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

@@ -94,4 +94,6 @@ export const LANGUAGE_EXTENSIONS: Record<string, string> = {
   ".yml": "yaml",
   ".mjs": "javascript",
   ".cjs": "javascript",
+  ".zig": "zig",
+  ".zon": "zig",
 } as const

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

@@ -181,4 +181,106 @@ export namespace LSPServer {
       }
     },
   }
+
+  export const Zls: Info = {
+    id: "zls",
+    extensions: [".zig", ".zon"],
+    async spawn() {
+      let bin = Bun.which("zls", {
+        PATH: process.env["PATH"] + ":" + Global.Path.bin,
+      })
+
+      if (!bin) {
+        const zig = Bun.which("zig")
+        if (!zig) {
+          log.error("Zig is required to use zls. Please install Zig first.")
+          return
+        }
+
+        log.info("downloading zls from GitHub releases")
+
+        const releaseResponse = await fetch("https://api.github.com/repos/zigtools/zls/releases/latest")
+        if (!releaseResponse.ok) {
+          log.error("Failed to fetch zls release info")
+          return
+        }
+
+        const release = await releaseResponse.json()
+
+        const platform = process.platform
+        const arch = process.arch
+        let assetName = ""
+
+        let zlsArch: string = arch
+        if (arch === "arm64") zlsArch = "aarch64"
+        else if (arch === "x64") zlsArch = "x86_64"
+        else if (arch === "ia32") zlsArch = "x86"
+
+        let zlsPlatform: string = platform
+        if (platform === "darwin") zlsPlatform = "macos"
+        else if (platform === "win32") zlsPlatform = "windows"
+
+        const ext = platform === "win32" ? "zip" : "tar.xz"
+
+        assetName = `zls-${zlsArch}-${zlsPlatform}.${ext}`
+
+        const supportedCombos = [
+          "zls-x86_64-linux.tar.xz",
+          "zls-x86_64-macos.tar.xz",
+          "zls-x86_64-windows.zip",
+          "zls-aarch64-linux.tar.xz",
+          "zls-aarch64-macos.tar.xz",
+          "zls-aarch64-windows.zip",
+          "zls-x86-linux.tar.xz",
+          "zls-x86-windows.zip",
+        ]
+
+        if (!supportedCombos.includes(assetName)) {
+          log.error("Unsupported platform/architecture for zls", { platform, arch, assetName })
+          return
+        }
+
+        const asset = release.assets?.find((a: any) => a.name === assetName)
+
+        if (!asset) {
+          log.error("Could not find zls download for platform", { platform, arch, assetName })
+          return
+        }
+
+        const downloadUrl = asset.browser_download_url
+        log.info("downloading zls", { url: downloadUrl })
+
+        const response = await fetch(downloadUrl)
+        if (!response.ok) {
+          log.error("Failed to download zls")
+          return
+        }
+
+        const isZip = assetName.endsWith(".zip")
+        const archivePath = path.join(Global.Path.bin, isZip ? "zls.zip" : "zls.tar.xz")
+        await Bun.file(archivePath).write(response)
+
+        if (isZip) {
+          await $`unzip -o -q ${archivePath} -d ${Global.Path.bin}`.nothrow()
+        } else {
+          await $`tar -xf ${archivePath} -C ${Global.Path.bin}`.quiet()
+        }
+
+        await fs.rm(archivePath, { force: true })
+
+        if (platform !== "win32") {
+          bin = path.join(Global.Path.bin, "zls")
+          await $`chmod +x ${bin}`.quiet()
+        } else {
+          bin = path.join(Global.Path.bin, "zls.exe")
+        }
+
+        log.info("installed zls", { bin })
+      }
+
+      return {
+        process: spawn(bin!),
+      }
+    },
+  }
 }