Sfoglia il codice sorgente

feat: add nixd as lsp for nix language (#5929)

Luo Chen 2 mesi fa
parent
commit
ac4b8d62e3

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

@@ -110,4 +110,5 @@ export const LANGUAGE_EXTENSIONS: Record<string, string> = {
   ".tf": "terraform",
   ".tfvars": "terraform-vars",
   ".hcl": "hcl",
+  ".nix": "nix",
 } as const

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

@@ -1746,4 +1746,35 @@ export namespace LSPServer {
       }
     },
   }
+
+  export const Nixd: Info = {
+    id: "nixd",
+    extensions: [".nix"],
+    root: async (file) => {
+      // First, look for flake.nix - the most reliable Nix project root indicator
+      const flakeRoot = await NearestRoot(["flake.nix"])(file)
+      if (flakeRoot && flakeRoot !== Instance.directory) return flakeRoot
+
+      // If no flake.nix, fall back to git repository root
+      if (Instance.worktree && Instance.worktree !== Instance.directory) return Instance.worktree
+
+      // Finally, use the instance directory as fallback
+      return Instance.directory
+    },
+    async spawn(root) {
+      const nixd = Bun.which("nixd")
+      if (!nixd) {
+        log.info("nixd not found, please install nixd first")
+        return
+      }
+      return {
+        process: spawn(nixd, [], {
+          cwd: root,
+          env: {
+            ...process.env,
+          },
+        }),
+      }
+    },
+  }
 }

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

@@ -26,6 +26,7 @@ OpenCode comes with several built-in LSP servers for popular languages:
 | gopls              | .go                                                                 | `go` command available                                       |
 | jdtls              | .java                                                               | `Java SDK (version 21+)` installed                           |
 | lua-ls             | .lua                                                                | Auto-installs for Lua projects                               |
+| nixd               | .nix                                                                | `nixd` command available                                     |
 | ocaml-lsp          | .ml, .mli                                                           | `ocamllsp` command available                                 |
 | oxlint             | .ts, .tsx, .js, .jsx, .mjs, .cjs, .mts, .cts, .vue, .astro, .svelte | `oxlint` dependency in project                               |
 | php intelephense   | .php                                                                | Auto-installs for PHP projects                               |