|
| 1 | +# @pleaseai/code-lsp |
| 2 | + |
| 3 | +LSP (Language Server Protocol) client implementation for AI coding tools. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This package provides a unified interface for interacting with multiple language servers, enabling real-time diagnostics, hover information, and symbol navigation. |
| 8 | + |
| 9 | +## Architecture |
| 10 | + |
| 11 | +``` |
| 12 | +src/ |
| 13 | +├── index.ts # Public API, LSPManager class |
| 14 | +├── client.ts # LSP client implementation (JSON-RPC) |
| 15 | +├── server.ts # LSP server definitions |
| 16 | +└── language.ts # Language ID mapping |
| 17 | +``` |
| 18 | + |
| 19 | +## Supported Language Servers |
| 20 | + |
| 21 | +| Server | ID | Extensions | Root Detection | |
| 22 | +|--------|-----|------------|----------------| |
| 23 | +| TypeScript | `typescript` | .ts, .tsx, .js, .jsx, .mjs, .cjs, .mts, .cts | package-lock.json, bun.lockb, bun.lock, yarn.lock, pnpm-lock.yaml | |
| 24 | +| Deno | `deno` | .ts, .tsx, .js, .jsx, .mjs | deno.json, deno.jsonc | |
| 25 | +| Oxlint | `oxlint` | .ts, .tsx, .js, .jsx, .mjs, .cjs, .mts, .cts, .vue, .astro, .svelte | .oxlintrc.json, package-lock.json, bun.lockb, bun.lock, pnpm-lock.yaml, yarn.lock, package.json | |
| 26 | +| Pyright | `pyright` | .py, .pyi | pyproject.toml, setup.py, requirements.txt, pyrightconfig.json | |
| 27 | +| Gopls | `gopls` | .go | go.work, go.mod, go.sum | |
| 28 | +| Rust Analyzer | `rust-analyzer` | .rs | Cargo.toml, Cargo.lock | |
| 29 | +| Kotlin | `kotlin` | .kt, .kts | build.gradle.kts, build.gradle, settings.gradle.kts, settings.gradle, pom.xml | |
| 30 | + |
| 31 | +## Adding a New Server |
| 32 | + |
| 33 | +1. Define the server in `server.ts`: |
| 34 | +```typescript |
| 35 | +export const MyServer: LSPServerInfo = { |
| 36 | + id: 'my-server', |
| 37 | + extensions: ['.ext'], |
| 38 | + root: nearestRoot(['config.json']), // or custom root function |
| 39 | + async spawn(root) { |
| 40 | + const proc = spawn('my-lsp', ['--stdio'], { cwd: root }) |
| 41 | + return { process: proc } |
| 42 | + }, |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +2. Add to `LSP_SERVERS` array in `server.ts` |
| 47 | + |
| 48 | +3. Export from `index.ts` |
| 49 | + |
| 50 | +4. Add tests in `__tests__/server.test.ts` |
| 51 | + |
| 52 | +## Auto-Download Pattern (Kotlin Example) |
| 53 | + |
| 54 | +For servers requiring runtime dependencies: |
| 55 | + |
| 56 | +```typescript |
| 57 | +const KOTLIN_RUNTIME_DEPS = { |
| 58 | + kotlinLsp: { url: '...', version: '...' }, |
| 59 | + java: { |
| 60 | + 'win-x64': { url: '...', javaHomePath: '...', javaPath: '...' }, |
| 61 | + 'linux-x64': { url: '...', javaHomePath: '...', javaPath: '...' }, |
| 62 | + // ... other platforms |
| 63 | + } as Record<PlatformId, { url: string, javaHomePath: string, javaPath: string }>, |
| 64 | +} |
| 65 | + |
| 66 | +async function setupKotlinDependencies(platformId: PlatformId) { |
| 67 | + const cacheDir = path.join(os.homedir(), '.cache', 'dora', 'kotlin-lsp') |
| 68 | + // Check if exists, download and extract if not |
| 69 | + // Verify files exist after download |
| 70 | + return { javaHomePath, kotlinLspPath } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +## Key APIs |
| 75 | + |
| 76 | +### LSPManager |
| 77 | + |
| 78 | +Main entry point for managing LSP clients: |
| 79 | + |
| 80 | +```typescript |
| 81 | +const manager = new LSPManager(projectPath) |
| 82 | + |
| 83 | +// Touch file to initialize LSP |
| 84 | +await manager.touchFile('src/index.ts', true) |
| 85 | + |
| 86 | +// Get diagnostics |
| 87 | +const diags = await manager.diagnostics() |
| 88 | + |
| 89 | +// Get hover info |
| 90 | +const hover = await manager.hover({ file, line, character }) |
| 91 | + |
| 92 | +// Search symbols |
| 93 | +const symbols = await manager.workspaceSymbol('query') |
| 94 | + |
| 95 | +// Cleanup |
| 96 | +await manager.shutdown() |
| 97 | +``` |
| 98 | + |
| 99 | +### Server Utilities |
| 100 | + |
| 101 | +```typescript |
| 102 | +import { getServerById, getServersForExtension } from '@pleaseai/code-lsp' |
| 103 | + |
| 104 | +const server = getServerById('typescript') |
| 105 | +const servers = getServersForExtension('.ts') |
| 106 | +``` |
| 107 | + |
| 108 | +## Testing |
| 109 | + |
| 110 | +```bash |
| 111 | +bun test ./src |
| 112 | +``` |
| 113 | + |
| 114 | +Tests cover: |
| 115 | +- Server definitions (ID, extensions, root, spawn functions) |
| 116 | +- LSP client lifecycle |
| 117 | +- Manager operations |
0 commit comments