|
| 1 | +# LSP API |
| 2 | + |
| 3 | +Use this API to register/manage language servers used by Acode's CodeMirror LSP client. |
| 4 | + |
| 5 | +## Import |
| 6 | + |
| 7 | +```js |
| 8 | +const lsp = acode.require("lsp"); |
| 9 | +``` |
| 10 | + |
| 11 | +## Important Transport Reality |
| 12 | + |
| 13 | +CodeMirror's LSP client in Acode communicates via **WebSocket** transport. |
| 14 | + |
| 15 | +- `transport.kind: "websocket"` is the recommended and practical mode. |
| 16 | +- `transport.kind: "stdio"` is **not a direct stdio pipe** to the editor. |
| 17 | +- In this codebase, `stdio` mode still expects a **WebSocket bridge URL**. |
| 18 | + |
| 19 | +For local stdio language servers, use an **AXS bridge** (`launcher.bridge`) and keep transport as websocket. |
| 20 | + |
| 21 | +::: warning |
| 22 | +`stdio` with only `transport.command` is not enough in Acode. |
| 23 | +You still need a WebSocket bridge endpoint (AXS or equivalent) for the client connection. |
| 24 | +::: |
| 25 | + |
| 26 | +## Recommended Setup (Local Server via AXS Bridge) |
| 27 | + |
| 28 | +```js |
| 29 | +lsp.registerServer( |
| 30 | + { |
| 31 | + id: "typescript-custom", |
| 32 | + label: "TypeScript (Custom)", |
| 33 | + languages: ["javascript", "javascriptreact", "typescript", "typescriptreact", "tsx", "jsx"], |
| 34 | + transport: { |
| 35 | + kind: "websocket", |
| 36 | + // url can be omitted when using launcher.bridge auto-port mode |
| 37 | + }, |
| 38 | + launcher: { |
| 39 | + bridge: { |
| 40 | + kind: "axs", |
| 41 | + command: "typescript-language-server", |
| 42 | + args: ["--stdio"], |
| 43 | + }, |
| 44 | + checkCommand: "which typescript-language-server", |
| 45 | + install: { |
| 46 | + command: |
| 47 | + "apk add --no-cache nodejs npm && npm install -g typescript-language-server typescript", |
| 48 | + }, |
| 49 | + }, |
| 50 | + enabled: true, |
| 51 | + initializationOptions: { |
| 52 | + provideFormatter: true, |
| 53 | + }, |
| 54 | + }, |
| 55 | + { replace: true }, |
| 56 | +); |
| 57 | +``` |
| 58 | + |
| 59 | +## Remote/External WebSocket Server |
| 60 | + |
| 61 | +```js |
| 62 | +lsp.registerServer({ |
| 63 | + id: "remote-lsp", |
| 64 | + label: "Remote LSP", |
| 65 | + languages: ["json"], |
| 66 | + transport: { |
| 67 | + kind: "websocket", |
| 68 | + url: "ws://127.0.0.1:2087/", |
| 69 | + options: { |
| 70 | + binary: true, |
| 71 | + timeout: 5000, |
| 72 | + }, |
| 73 | + }, |
| 74 | + enabled: true, |
| 75 | +}); |
| 76 | +``` |
| 77 | + |
| 78 | +## API Reference |
| 79 | + |
| 80 | +### `registerServer(definition, options?)` |
| 81 | + |
| 82 | +- `options.replace?: boolean` (default `false`) |
| 83 | + - `false`: existing server with same id is kept |
| 84 | + - `true`: existing server is replaced |
| 85 | + |
| 86 | +Definition fields commonly used: |
| 87 | + |
| 88 | +- `id` (required): normalized to lowercase |
| 89 | +- `label` (optional) |
| 90 | +- `languages` (required): non-empty array, normalized to lowercase |
| 91 | +- `enabled` (optional, default `true`) |
| 92 | +- `transport` (required) |
| 93 | + - `kind`: `"websocket"` or `"stdio"` (see transport note above) |
| 94 | + - `url?: string` |
| 95 | + - `command?: string` (required by registry validation when using `stdio`) |
| 96 | + - `args?: string[]` |
| 97 | + - `options?: { binary?, timeout?, reconnect?, maxReconnectAttempts? }` |
| 98 | +- `launcher` (optional) |
| 99 | + - `startCommand?: string | string[]` |
| 100 | + - `command?: string` |
| 101 | + - `args?: string[]` |
| 102 | + - `checkCommand?: string` |
| 103 | + - `install?: { command?: string }` |
| 104 | + - `bridge?: { kind: "axs", command: string, args?: string[], port?: number, session?: string }` |
| 105 | +- `initializationOptions?: object` |
| 106 | +- `clientConfig?: object` |
| 107 | +- `startupTimeout?: number` |
| 108 | +- `capabilityOverrides?: object` |
| 109 | +- `rootUri?: (uri, context) => string | null` |
| 110 | +- `resolveLanguageId?: (context) => string | null` |
| 111 | +- `useWorkspaceFolders?: boolean` |
| 112 | + |
| 113 | +### Other Registry Methods |
| 114 | + |
| 115 | +- `unregisterServer(id)` |
| 116 | +- `updateServer(id, updater)` |
| 117 | +- `getServer(id)` |
| 118 | +- `listServers()` |
| 119 | +- `getServersForLanguage(languageId, options?)` |
| 120 | + |
| 121 | +```js |
| 122 | +const jsServers = lsp.getServersForLanguage("javascript"); |
| 123 | + |
| 124 | +lsp.updateServer("typescript-custom", (current) => ({ |
| 125 | + ...current, |
| 126 | + enabled: false, |
| 127 | +})); |
| 128 | +``` |
| 129 | + |
| 130 | +`getServersForLanguage` options: |
| 131 | + |
| 132 | +- `includeDisabled?: boolean` (default `false`) |
| 133 | + |
| 134 | +## Client Manager |
| 135 | + |
| 136 | +- `clientManager.setOptions(options)` |
| 137 | +- `clientManager.getActiveClients()` |
| 138 | + |
| 139 | +```js |
| 140 | +lsp.clientManager.setOptions({ |
| 141 | + diagnosticsUiExtension: [], |
| 142 | +}); |
| 143 | + |
| 144 | +const activeClients = lsp.clientManager.getActiveClients(); |
| 145 | +console.log(activeClients); |
| 146 | +``` |
0 commit comments