import path from "node:path"; import process from "node:process"; import { ensureJdtlsInstalled, jdtlsSpawnEnv, jdtlsWrapperCommand, } from "./jdtls-installer.js"; import { createCliRunner, DEFAULT_BIOME_EXTENSIONS, RUNNER_PRIORITIES, } from "./runners/builtin.js"; import type { InternalLspServer, LspServerAdapter, RunnerDefinition, } from "./types.js"; import { uvToolCustomInstaller } from "./uv-installer.js"; export const COMMON_SKIP_DIRECTORIES = new Set([ ".git", ".hg", ".mypy_cache", ".next", ".nuxt", ".output", ".ruff_cache", ".svelte-kit", ".tox", ".venv", "__pycache__", "coverage", "dist", "node_modules", "out", "target", "vendor", "venv", ]); // --- LSP server install recipes (same machinery as the CLI runners) --- // Only servers installable through a package manager available in a normal // environment get recipes; everything else keeps its honest missingCommandHint. // winget IDs verified against the winget catalog. function winget(id: string) { return { label: `winget install ${id}`, command: "winget", args: [ "install", "--id", id, "--silent", "--accept-source-agreements", "--accept-package-agreements", ], }; } function npmInstall(pkg: string) { return { label: `npm install -g ${pkg}`, command: "npm", args: ["install", "-g", pkg], }; } export const DEFAULT_SERVER_CONFIGS: InternalLspServer[] = [ { name: "biome", command: ["biome", "lsp-proxy"], extensions: DEFAULT_BIOME_EXTENSIONS, autoInstall: true, installRecipes: [npmInstall("@biomejs/biome")], }, { // Full type/semantic diagnostics for JS/TS (tsserver wrapper), running // alongside biome (lint/style) — mirrors the Python ty+ruff pairing. name: "typescript-language-server", command: ["typescript-language-server", "--stdio"], extensions: [".js", ".jsx", ".ts", ".tsx", ".mts", ".cts", ".mjs", ".cjs"], // tsserver builds the whole-project symbol index on the first pull, so // a full-tree run cannot fit the global 20s per-request budget. timeoutMs: 60_000, autoInstall: true, // The server shells out to tsserver from the typescript package. // Pin typescript@5: the "typescript@7" package is the native Go // (typescript-go) build without the tsserver library layout. installRecipes: [ { label: "npm install -g typescript@5 typescript-language-server", command: "npm", args: ["install", "-g", "typescript@5", "typescript-language-server"], }, ], }, { name: "ty", command: ["ty", "server"], extensions: [".py", ".pyi"], autoInstall: true, // uv installs ty as a standalone tool with its shim in ~/.local/bin — // independent of whichever (Store, per-user, venv) Python is present. customInstaller: uvToolCustomInstaller(["ty"]), }, { name: "ruff", command: ["ruff", "server"], extensions: [".py", ".pyi"], autoInstall: true, // Same uv channel as ty; no pip/pipx fallback — a pip-installed ruff // inside the Store Python is exactly the "installed but not on PATH" // situation uv exists to avoid. customInstaller: uvToolCustomInstaller(["ruff"]), }, { name: "rust-analyzer", command: ["rust-analyzer"], extensions: [".rs"], autoInstall: true, installRecipes: [winget("Rustlang.rust-analyzer")], // rust-analyzer can return an empty pull result before its initial analysis // publishes the real diagnostics. pullDiagnosticsGraceMs: 5_000, }, { name: "gopls", command: ["gopls"], extensions: [".go"], // Push-only servers below: they publish on didOpen but may stay silent // for clean documents. The grace window turns "20s lane timeout" into // "settled empty" instead. Servers that advertise pull diagnostics // (tsserver, clangd, ruff, ty, ...) do not need this — the pull path // ignores pushDiagnosticsGraceMs entirely. pushDiagnosticsGraceMs: 3_000, }, { name: "rubocop", command: ["rubocop", "--lsp"], extensions: [".rb", ".rake", ".gemspec", ".ru"], pushDiagnosticsGraceMs: 3_000, }, { name: "elixir-ls", command: [ process.platform === "win32" ? "language_server.bat" : "language_server.sh", ], extensions: [".ex", ".exs"], skipDirectories: ["_build", "deps"], pushDiagnosticsGraceMs: 3_000, }, { name: "zls", command: ["zls"], extensions: [".zig", ".zon"], skipDirectories: [".zig-cache", "zig-out"], pushDiagnosticsGraceMs: 3_000, }, { name: "csharp", command: ["roslyn-language-server", "--stdio", "--autoLoadProjects"], extensions: [".cs", ".csx"], skipDirectories: ["bin", "obj"], // Roslyn can stay silent for a clean document; without a grace // window the push path would stall to the full lane timeout. pushDiagnosticsGraceMs: 3_000, }, { name: "fsharp", command: ["fsautocomplete"], extensions: [".fs", ".fsi", ".fsx", ".fsscript"], skipDirectories: ["bin", "obj"], initialization: { AutomaticWorkspaceInit: true }, pushDiagnosticsGraceMs: 3_000, }, { name: "sourcekit-lsp", command: ["sourcekit-lsp"], extensions: [".swift", ".mm"], skipDirectories: [".build", "DerivedData"], pushDiagnosticsGraceMs: 3_000, }, { name: "clangd", command: ["clangd", "--background-index", "--clang-tidy"], extensions: [ ".c", ".cpp", ".cc", ".cxx", ".c++", ".h", ".hpp", ".hh", ".hxx", ".h++", ], skipDirectories: ["build"], autoInstall: true, installRecipes: [winget("LLVM.clangd")], }, { name: "jdtls", // eclipse.jdt.ls has no package-manager channel; it is downloaded // (together with a managed JDK 21) into ~/.pi/agent/lsp on first use. command: [jdtlsWrapperCommand()], extensions: [".java"], skipDirectories: [".gradle", "build"], pushDiagnosticsGraceMs: 3_000, autoInstall: true, customInstaller: ensureJdtlsInstalled, spawnEnv: jdtlsSpawnEnv, missingCommandHint: process.platform === "win32" ? "jdtls auto-installs to ~/.pi/agent/lsp on first use (downloads JDK 21 + Eclipse JDT.LS)." : "jdtls auto-install is Windows-only; install Eclipse JDT.LS and a JDK 21 manually.", }, { name: "kotlin-lsp", command: ["kotlin-lsp", "--stdio"], extensions: [".kt", ".kts"], skipDirectories: [".gradle", "build"], pushDiagnosticsGraceMs: 3_000, }, { name: "yaml-language-server", command: ["yaml-language-server", "--stdio"], extensions: [".yaml", ".yml"], pushDiagnosticsGraceMs: 3_000, autoInstall: true, installRecipes: [npmInstall("yaml-language-server")], }, { name: "lua-language-server", command: ["lua-language-server"], extensions: [".lua"], // LuaLS does not publish an empty diagnostic set for a clean document. pushDiagnosticsGraceMs: 3_000, }, { name: "intelephense", command: ["intelephense", "--stdio"], extensions: [".php"], initialization: { intelephense: { telemetry: { enabled: false } } }, autoInstall: true, // intelephense treats any argument as a connection flag (no --version), // so post-install verification is resolvability only. installRecipes: [{ ...npmInstall("intelephense"), verifyArgs: [] }], // Publishes empty on didOpen, then real diagnostics ~0.2-3s later. diagnosticsSettleMs: 4000, }, { name: "prisma", command: ["prisma-language-server", "--stdio"], extensions: [".prisma"], pushDiagnosticsGraceMs: 3_000, }, { name: "dart", command: ["dart", "language-server"], extensions: [".dart"], skipDirectories: [".dart_tool", "build"], // The analysis server can remain silent when a document is clean. pushDiagnosticsGraceMs: 2_000, }, { name: "ocaml-lsp", command: ["ocamllsp"], extensions: [".ml", ".mli"], skipDirectories: ["_build", "_opam"], pushDiagnosticsGraceMs: 3_000, }, { name: "bash-language-server", command: ["bash-language-server", "start"], extensions: [".sh", ".bash"], pushDiagnosticsGraceMs: 3_000, autoInstall: true, installRecipes: [npmInstall("bash-language-server")], }, { name: "terraform-ls", command: ["terraform-ls", "serve"], extensions: [".tf", ".tfvars"], skipDirectories: [".terraform"], autoInstall: true, installRecipes: [winget("Hashicorp.TerraformLanguageServer")], pushDiagnosticsGraceMs: 2_000, initialization: { experimentalFeatures: { prefillRequiredFields: true }, }, }, { name: "texlab", command: ["texlab"], extensions: [".tex", ".bib"], pushDiagnosticsGraceMs: 3_000, }, { name: "gleam", command: ["gleam", "lsp"], extensions: [".gleam"], skipDirectories: ["build"], pushDiagnosticsGraceMs: 2_000, }, { name: "clojure-lsp", command: ["clojure-lsp", "listen"], extensions: [".clj", ".cljs", ".cljc", ".edn"], skipDirectories: [".cpcache"], pushDiagnosticsGraceMs: 3_000, }, { name: "nixd", command: ["nixd"], extensions: [".nix"], pushDiagnosticsGraceMs: 3_000, }, { name: "tinymist", command: ["tinymist"], extensions: [".typ", ".typc"], autoInstall: true, installRecipes: [winget("Myriad-Dreamin.Tinymist")], pushDiagnosticsGraceMs: 2_000, }, { name: "haskell-language-server", command: ["haskell-language-server-wrapper", "--lsp"], extensions: [".hs", ".lhs"], skipDirectories: [".stack-work", "dist-newstyle"], pushDiagnosticsGraceMs: 3_000, }, ]; // --- Built-in CLI runner catalog (pi-lens-style runners, pi-lsp-shaped defaults) --- export interface ConfiguredRunner { command: string[]; extensions: string[]; timeoutMs?: number; autoInstall?: boolean; disabled?: boolean; } // Builtin runner ids known to this extension. export const BUILTIN_RUNNER_IDS = ["ruff", "biome", "shellcheck"] as const; export type BuiltinRunnerId = (typeof BUILTIN_RUNNER_IDS)[number]; export const DEFAULT_RUNNER_CONFIGS: Record = { ruff: { command: ["ruff", "check", "--output-format", "json"], extensions: [".py", ".pyi"], autoInstall: true, }, biome: { command: [ "biome", "check", "--reporter=json", "--no-errors-on-unmatched", ], extensions: DEFAULT_BIOME_EXTENSIONS, autoInstall: true, }, shellcheck: { command: ["shellcheck", "--json", "-x"], extensions: [".sh", ".bash"], autoInstall: true, }, }; export interface Runtime { adapters: LspServerAdapter[]; runners: RunnerDefinition[]; timeoutMs: number; } export function loadRuntime(): Runtime { return { adapters: DEFAULT_SERVER_CONFIGS.map((server) => configToAdapter({ ...server, isDefault: true }), ), runners: buildRunners(DEFAULT_RUNNER_CONFIGS), timeoutMs: 20_000, }; } function buildRunners( config: Record, ): RunnerDefinition[] { const runners: RunnerDefinition[] = []; for (const id of BUILTIN_RUNNER_IDS) { const entry = config[id]; if (entry && !entry.disabled) { runners.push(createCliRunner(id, entry, RUNNER_PRIORITIES[id])); } } return runners; } function configToAdapter(config: InternalLspServer): LspServerAdapter { const extensionSet = new Set(config.extensions.map(normalizeExtension)); const [command, ...args] = config.command; if (!command) throw new Error(`${config.name}.command must contain at least one string.`); return { name: config.name, isDefault: config.isDefault ?? false, defaultCommand: { command, args }, missingCommandHint: config.missingCommandHint ?? `Install ${config.name} to enable it.`, extensions: config.extensions, timeoutMs: config.timeoutMs, env: config.env, autoInstall: config.autoInstall, installRecipes: config.installRecipes, customInstaller: config.customInstaller, initialization: config.initialization, skipDirectories: new Set([ ...COMMON_SKIP_DIRECTORIES, ...(config.skipDirectories ?? []), ]), diagnosticsSettleMs: config.diagnosticsSettleMs, pushDiagnosticsGraceMs: config.pushDiagnosticsGraceMs, pullDiagnosticsGraceMs: config.pullDiagnosticsGraceMs, isSupportedFile: (filePath) => extensionSet.has(path.extname(filePath)), languageIdFor: (filePath) => languageIdFor(config, filePath), spawnEnv: config.spawnEnv, }; } function languageIdFor(_config: InternalLspServer, filePath: string) { const extension = path.extname(filePath); return LANGUAGE_IDS[extension] ?? extension.slice(1); } const LANGUAGE_IDS: Record = { ".bash": "shellscript", ".bib": "bibtex", ".c": "c", ".c++": "cpp", ".cc": "cpp", ".cjs": "javascript", ".clj": "clojure", ".cljc": "clojure", ".cljs": "clojure", ".cpp": "cpp", ".cs": "csharp", ".csx": "csharp", ".cts": "typescript", ".cxx": "cpp", ".dart": "dart", ".edn": "clojure", ".ex": "elixir", ".exs": "elixir", ".fs": "fsharp", ".fsi": "fsharp", ".fsscript": "fsharp", ".fsx": "fsharp", ".gemspec": "ruby", ".go": "go", ".gql": "graphql", ".h": "c", ".h++": "cpp", ".hh": "cpp", ".hpp": "cpp", ".hs": "haskell", ".hxx": "cpp", ".jl": "julia", ".js": "javascript", ".jsx": "javascriptreact", ".jsonc": "jsonc", ".ksh": "shellscript", ".kt": "kotlin", ".kts": "kotlin", ".lhs": "lhaskell", ".m": "objective-c", ".mjs": "javascript", ".ml": "ocaml", ".mli": "ocaml.interface", ".mm": "objective-cpp", ".mts": "typescript", ".nix": "nix", ".php": "php", ".py": "python", ".pyi": "python", ".rake": "ruby", ".rb": "ruby", ".rs": "rust", ".ru": "ruby", ".sh": "shellscript", ".swift": "swift", ".tex": "latex", ".tf": "terraform", ".tfvars": "terraform-vars", ".ts": "typescript", ".tsx": "typescriptreact", ".typ": "typst", ".typc": "typst-code", ".yaml": "yaml", ".yml": "yaml", ".zig": "zig", ".zon": "zig", ".zsh": "shellscript", }; export function normalizeExtension(extension: string) { return extension.startsWith(".") ? extension : `.${extension}`; }