1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
|
import type { ChildProcessWithoutNullStreams } from "node:child_process";
import { readFile } from "node:fs/promises";
import { extname, isAbsolute, resolve } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import {
createMessageConnection,
type MessageConnection,
StreamMessageReader,
StreamMessageWriter,
} from "vscode-jsonrpc/node";
import type { Diagnostic } from "vscode-languageserver-types";
import { languageIdForExtension } from "./language.js";
export type { Diagnostic } from "vscode-languageserver-types";
// ─── Timing constants (mirrors opencode) ─────────────────────────
const DIAGNOSTICS_DEBOUNCE_MS = 150;
const DIAGNOSTICS_DOCUMENT_WAIT_TIMEOUT_MS = 5_000;
const DIAGNOSTICS_FULL_WAIT_TIMEOUT_MS = 10_000;
const DIAGNOSTICS_REQUEST_TIMEOUT_MS = 3_000;
const INITIALIZE_TIMEOUT_MS = 45_000;
// ─── LSP spec constants ──────────────────────────────────────────
const FILE_CHANGE_CREATED = 1;
const FILE_CHANGE_CHANGED = 2;
const TEXT_DOCUMENT_SYNC_INCREMENTAL = 2;
/**
* A live spawned language-server process plus the `initializationOptions` to
* hand it. Produced by the server-spawning layer (`server.ts`) and consumed by
* `createLspClient`.
*/
export interface LspServerHandle {
process: ChildProcessWithoutNullStreams;
initialization?: Record<string, unknown>;
}
interface ServerCapabilities {
textDocumentSync?: number | { change?: number };
diagnosticProvider?: unknown;
[key: string]: unknown;
}
interface DiagnosticRequestResult {
handled: boolean;
matched: boolean;
byFile: Map<string, Diagnostic[]>;
}
interface CapabilityRegistration {
id: string;
method: string;
registerOptions?: {
identifier?: string;
workspaceDiagnostics?: boolean;
};
}
type DocumentDiagnosticReport = {
items?: Diagnostic[];
relatedDocuments?: Record<string, DocumentDiagnosticReport>;
};
type WorkspaceDiagnosticReport = {
items?: { uri?: string; items?: Diagnostic[] }[];
};
/** Public shape of a connected LSP client. */
export interface LspClient {
readonly serverID: string;
readonly root: string;
readonly connection: MessageConnection;
/**
* Open (or re-sync) a file with the server. Returns the document version
* sent — pass it to `waitForDiagnostics` to wait for diagnostics matching
* this exact sync.
*/
notifyOpen(path: string): Promise<number>;
/** Snapshot of all known diagnostics keyed by absolute file path. */
readonly diagnostics: Map<string, Diagnostic[]>;
/** Wait until diagnostics for `path` settle (push and/or pull). */
waitForDiagnostics(request: {
path: string;
version: number;
mode?: "document" | "full";
after?: number;
}): Promise<void>;
/** Generic LSP request passthrough (hover, definition, references, …). */
request<T = unknown>(method: string, params: unknown): Promise<T | null>;
/** Shut the connection and child process down. */
shutdown(): Promise<void>;
}
function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
return new Promise<T>((resolvePromise, reject) => {
const timer = setTimeout(() => reject(new Error(`LSP request timed out after ${ms}ms`)), ms);
promise.then(
(value) => {
clearTimeout(timer);
resolvePromise(value);
},
(err) => {
clearTimeout(timer);
reject(err);
},
);
});
}
function getFilePath(uri: string): string | undefined {
if (!uri.startsWith("file://")) return undefined;
return fileURLToPath(uri);
}
function getSyncKind(capabilities?: ServerCapabilities): number | undefined {
if (!capabilities) return undefined;
const sync = capabilities.textDocumentSync;
if (typeof sync === "number") return sync;
return sync?.change;
}
function endPosition(text: string) {
const lines = text.split(/\r\n|\r|\n/);
return { line: lines.length - 1, character: lines.at(-1)?.length ?? 0 };
}
function dedupeDiagnostics(items: Diagnostic[]): Diagnostic[] {
const seen = new Set<string>();
return items.filter((item) => {
const key = JSON.stringify({
code: item.code,
severity: item.severity,
message: item.message,
source: item.source,
range: item.range,
});
if (seen.has(key)) return false;
seen.add(key);
return true;
});
}
function configurationValue(settings: unknown, section?: string): unknown {
if (!section) return settings ?? null;
const result = section.split(".").reduce<unknown>((acc, key) => {
if (!acc || typeof acc !== "object" || !(key in acc)) return undefined;
return (acc as Record<string, unknown>)[key];
}, settings);
return result ?? null;
}
/**
* Create and initialize an LSP client over a spawned server's stdio.
*
* Performs the full `initialize`/`initialized` handshake (with a 45s timeout),
* wires push (`textDocument/publishDiagnostics`) and pull
* (`textDocument/diagnostic`, `workspace/diagnostic`) diagnostics, answers the
* `workspace/configuration`, `workspaceFolders`, and capability-registration
* requests servers commonly make, and returns a small client surface used by
* the manager and tools. Plain-TypeScript port of opencode's `lsp/client.ts`.
*/
export async function createLspClient(input: {
serverID: string;
server: LspServerHandle;
root: string;
directory: string;
}): Promise<LspClient> {
const { serverID, server, root, directory } = input;
const connection = createMessageConnection(
new StreamMessageReader(server.process.stdout),
new StreamMessageWriter(server.process.stdin),
);
// Server stderr is routine for many tools (luau-lsp logs sourcemap status
// there). Keep it quiet unless debugging.
server.process.stderr?.on("data", () => {
/* swallowed — see opencode: stderr is mostly informational */
});
// ─── Connection state ───
const pushDiagnostics = new Map<string, Diagnostic[]>();
const pullDiagnostics = new Map<string, Diagnostic[]>();
const published = new Map<string, { at: number; version?: number }>();
const diagnosticRegistrations = new Map<string, CapabilityRegistration>();
const registrationListeners = new Set<() => void>();
const diagnosticListeners = new Set<(input: { path: string; serverID: string }) => void>();
const files: Record<string, { version: number; text: string }> = {};
const mergedDiagnostics = (filePath: string) =>
dedupeDiagnostics([
...(pushDiagnostics.get(filePath) ?? []),
...(pullDiagnostics.get(filePath) ?? []),
]);
const updatePushDiagnostics = (filePath: string, next: Diagnostic[]) => {
pushDiagnostics.set(filePath, next);
for (const listener of diagnosticListeners) listener({ path: filePath, serverID });
};
const updatePullDiagnostics = (filePath: string, next: Diagnostic[]) => {
pullDiagnostics.set(filePath, next);
};
const emitRegistrationChange = () => {
for (const listener of [...registrationListeners]) listener();
};
// ─── Notification / request handlers ───
connection.onNotification(
"textDocument/publishDiagnostics",
(params: { uri: string; diagnostics: Diagnostic[]; version?: number }) => {
const filePath = getFilePath(params.uri);
if (!filePath) return;
published.set(filePath, {
at: Date.now(),
version: typeof params.version === "number" ? params.version : undefined,
});
updatePushDiagnostics(filePath, params.diagnostics);
},
);
connection.onRequest("window/workDoneProgress/create", () => null);
connection.onRequest("workspace/configuration", (params: { items?: { section?: string }[] }) => {
const items = params.items ?? [];
return items.map((item) => configurationValue(server.initialization, item.section));
});
connection.onRequest(
"client/registerCapability",
(params: { registrations?: CapabilityRegistration[] }) => {
const registrations = params.registrations ?? [];
let changed = false;
for (const registration of registrations) {
if (registration.method !== "textDocument/diagnostic") continue;
diagnosticRegistrations.set(registration.id, registration);
changed = true;
}
if (changed) emitRegistrationChange();
return null;
},
);
connection.onRequest(
"client/unregisterCapability",
(params: { unregisterations?: { id: string; method: string }[] }) => {
const registrations = params.unregisterations ?? [];
let changed = false;
for (const registration of registrations) {
if (registration.method !== "textDocument/diagnostic") continue;
diagnosticRegistrations.delete(registration.id);
changed = true;
}
if (changed) emitRegistrationChange();
return null;
},
);
connection.onRequest("workspace/workspaceFolders", () => [
{ name: "workspace", uri: pathToFileURL(root).href },
]);
connection.onRequest("workspace/diagnostic/refresh", () => null);
connection.listen();
// ─── Initialize handshake ───
const initialized = await withTimeout(
connection.sendRequest<{ capabilities?: ServerCapabilities }>("initialize", {
rootUri: pathToFileURL(root).href,
processId: server.process.pid ?? null,
workspaceFolders: [{ name: "workspace", uri: pathToFileURL(root).href }],
initializationOptions: { ...server.initialization },
capabilities: {
window: { workDoneProgress: true },
workspace: {
configuration: true,
didChangeWatchedFiles: { dynamicRegistration: true },
diagnostics: { refreshSupport: false },
},
textDocument: {
synchronization: { didOpen: true, didChange: true },
diagnostic: { dynamicRegistration: true, relatedDocumentSupport: true },
publishDiagnostics: { versionSupport: false },
},
},
}),
INITIALIZE_TIMEOUT_MS,
);
const syncKind = getSyncKind(initialized.capabilities);
const hasStaticPullDiagnostics = Boolean(initialized.capabilities?.diagnosticProvider);
await connection.sendNotification("initialized", {});
if (server.initialization) {
await connection.sendNotification("workspace/didChangeConfiguration", {
settings: server.initialization,
});
}
// ─── Pull-diagnostics helpers ───
const mergeResults = (filePath: string, results: DiagnosticRequestResult[]) => {
const handled = results.some((r) => r.handled);
const matched = results.some((r) => r.matched);
if (!handled) return { handled: false, matched: false };
const merged = new Map<string, Diagnostic[]>();
for (const result of results) {
for (const [target, items] of result.byFile.entries()) {
merged.set(target, (merged.get(target) ?? []).concat(items));
}
}
if (matched && !merged.has(filePath)) merged.set(filePath, []);
for (const [target, items] of merged.entries()) {
updatePullDiagnostics(target, dedupeDiagnostics(items));
}
return { handled, matched };
};
async function requestDiagnosticReport(
filePath: string,
identifier?: string,
): Promise<DiagnosticRequestResult> {
const report = await withTimeout(
connection.sendRequest<DocumentDiagnosticReport | null>("textDocument/diagnostic", {
...(identifier ? { identifier } : {}),
textDocument: { uri: pathToFileURL(filePath).href },
}),
DIAGNOSTICS_REQUEST_TIMEOUT_MS,
).catch(() => null);
const empty: DiagnosticRequestResult = {
handled: false,
matched: false,
byFile: new Map(),
};
if (!report) return empty;
const byFile = new Map<string, Diagnostic[]>();
const push = (target: string, items: Diagnostic[]) => {
byFile.set(target, (byFile.get(target) ?? []).concat(items));
};
let handled = false;
let matched = false;
if (Array.isArray(report.items)) {
push(filePath, report.items);
handled = true;
matched = true;
}
for (const [uri, related] of Object.entries(report.relatedDocuments ?? {})) {
const relatedPath = getFilePath(uri);
if (!relatedPath || !Array.isArray(related.items)) continue;
push(relatedPath, related.items);
handled = true;
matched = matched || relatedPath === filePath;
}
return { handled, matched, byFile };
}
async function requestWorkspaceDiagnosticReport(
filePath: string,
identifier?: string,
): Promise<DiagnosticRequestResult> {
const report = await withTimeout(
connection.sendRequest<WorkspaceDiagnosticReport | null>("workspace/diagnostic", {
...(identifier ? { identifier } : {}),
previousResultIds: [],
}),
DIAGNOSTICS_REQUEST_TIMEOUT_MS,
).catch(() => null);
if (!report) return { handled: false, matched: false, byFile: new Map() };
const byFile = new Map<string, Diagnostic[]>();
let matched = false;
for (const item of report.items ?? []) {
const relatedPath = item.uri ? getFilePath(item.uri) : undefined;
if (!relatedPath || !Array.isArray(item.items)) continue;
byFile.set(relatedPath, (byFile.get(relatedPath) ?? []).concat(item.items));
matched = matched || relatedPath === filePath;
}
return { handled: true, matched, byFile };
}
function documentPullState() {
const documentRegistrations = [...diagnosticRegistrations.values()].filter(
(r) => r.registerOptions?.workspaceDiagnostics !== true,
);
return {
documentIdentifiers: [
...new Set(documentRegistrations.flatMap((r) => r.registerOptions?.identifier ?? [])),
],
supported: hasStaticPullDiagnostics || documentRegistrations.length > 0,
};
}
function workspacePullState() {
const workspaceRegistrations = [...diagnosticRegistrations.values()].filter(
(r) => r.registerOptions?.workspaceDiagnostics === true,
);
return {
workspaceIdentifiers: [
...new Set(workspaceRegistrations.flatMap((r) => r.registerOptions?.identifier ?? [])),
],
supported: workspaceRegistrations.length > 0,
};
}
const hasCurrentFileDiagnostics = (filePath: string, results: DiagnosticRequestResult[]) =>
results.some((r) => (r.byFile.get(filePath)?.length ?? 0) > 0);
async function requestDiagnostics(
filePath: string,
requests: Promise<DiagnosticRequestResult>[],
done: (results: DiagnosticRequestResult[]) => boolean,
) {
if (!requests.length) return { handled: false, matched: false };
const results: DiagnosticRequestResult[] = [];
return new Promise<{ handled: boolean; matched: boolean }>((resolvePromise) => {
let pending = requests.length;
let resolved = false;
const finish = (merged: { handled: boolean; matched: boolean }, force = false) => {
if (resolved) return;
if (!force && !done(results)) return;
resolved = true;
resolvePromise(merged);
};
for (const request of requests) {
request.then((result) => {
results.push(result);
pending -= 1;
const merged = mergeResults(filePath, results);
finish(merged);
if (pending === 0) finish(merged, true);
});
}
});
}
async function requestDocumentDiagnostics(filePath: string) {
const state = documentPullState();
if (!state.supported) return { handled: false, matched: false };
return requestDiagnostics(
filePath,
[
requestDiagnosticReport(filePath),
...state.documentIdentifiers.map((id) => requestDiagnosticReport(filePath, id)),
],
(results) => hasCurrentFileDiagnostics(filePath, results),
);
}
async function requestFullDiagnostics(filePath: string) {
const documentState = documentPullState();
const workspaceState = workspacePullState();
if (!documentState.supported && !workspaceState.supported) {
return { handled: false, matched: false };
}
return mergeResults(
filePath,
await Promise.all([
...(documentState.supported ? [requestDiagnosticReport(filePath)] : []),
...documentState.documentIdentifiers.map((id) => requestDiagnosticReport(filePath, id)),
...(workspaceState.supported ? [requestWorkspaceDiagnosticReport(filePath)] : []),
...workspaceState.workspaceIdentifiers.map((id) =>
requestWorkspaceDiagnosticReport(filePath, id),
),
]),
);
}
function waitForRegistrationChange(timeout: number) {
if (timeout <= 0) return Promise.resolve(false);
return new Promise<boolean>((resolvePromise) => {
let finished = false;
let timer: ReturnType<typeof setTimeout> | undefined;
const finish = (result: boolean) => {
if (finished) return;
finished = true;
if (timer) clearTimeout(timer);
registrationListeners.delete(listener);
resolvePromise(result);
};
const listener = () => finish(true);
registrationListeners.add(listener);
timer = setTimeout(() => finish(false), timeout);
});
}
function waitForFreshPush(request: {
path: string;
version: number;
after: number;
timeout: number;
}) {
if (request.timeout <= 0) return Promise.resolve(false);
return new Promise<boolean>((resolvePromise) => {
let finished = false;
let debounceTimer: ReturnType<typeof setTimeout> | undefined;
let timeoutTimer: ReturnType<typeof setTimeout> | undefined;
let unsub: (() => void) | undefined;
const finish = (result: boolean) => {
if (finished) return;
finished = true;
if (debounceTimer) clearTimeout(debounceTimer);
if (timeoutTimer) clearTimeout(timeoutTimer);
unsub?.();
resolvePromise(result);
};
const schedule = () => {
const hit = published.get(request.path);
if (!hit) return;
if (typeof hit.version === "number" && hit.version !== request.version) return;
if (hit.at < request.after && hit.version !== request.version) return;
if (debounceTimer) clearTimeout(debounceTimer);
debounceTimer = setTimeout(
() => finish(true),
Math.max(0, DIAGNOSTICS_DEBOUNCE_MS - (Date.now() - hit.at)),
);
};
timeoutTimer = setTimeout(() => finish(false), request.timeout);
const listener = (event: { path: string; serverID: string }) => {
if (event.path !== request.path || event.serverID !== serverID) return;
schedule();
};
diagnosticListeners.add(listener);
unsub = () => diagnosticListeners.delete(listener);
schedule();
});
}
async function waitForDocumentDiagnostics(request: {
path: string;
version: number;
after?: number;
}) {
const startedAt = request.after ?? Date.now();
const pushWait = waitForFreshPush({
path: request.path,
version: request.version,
after: startedAt,
timeout: DIAGNOSTICS_DOCUMENT_WAIT_TIMEOUT_MS,
});
while (Date.now() - startedAt < DIAGNOSTICS_DOCUMENT_WAIT_TIMEOUT_MS) {
const result = await requestDocumentDiagnostics(request.path);
if (result.matched) return;
const remaining = DIAGNOSTICS_DOCUMENT_WAIT_TIMEOUT_MS - (Date.now() - startedAt);
if (remaining <= 0) return;
const next = await Promise.race([
pushWait.then((ready) => (ready ? "push" : "timeout")),
waitForRegistrationChange(remaining).then((c) => (c ? "registration" : "timeout")),
]);
if (next !== "registration") return;
}
}
async function waitForFullDiagnostics(request: {
path: string;
version: number;
after?: number;
}) {
const startedAt = request.after ?? Date.now();
const pushWait = waitForFreshPush({
path: request.path,
version: request.version,
after: startedAt,
timeout: DIAGNOSTICS_FULL_WAIT_TIMEOUT_MS,
});
while (Date.now() - startedAt < DIAGNOSTICS_FULL_WAIT_TIMEOUT_MS) {
const result = await requestFullDiagnostics(request.path);
if (result.handled || result.matched) return;
const remaining = DIAGNOSTICS_FULL_WAIT_TIMEOUT_MS - (Date.now() - startedAt);
if (remaining <= 0) return;
const next = await Promise.race([
pushWait.then((ready) => (ready ? "push" : "timeout")),
waitForRegistrationChange(remaining).then((c) => (c ? "registration" : "timeout")),
]);
if (next !== "registration") return;
}
}
const normalize = (p: string) => (isAbsolute(p) ? p : resolve(directory, p));
// ─── Public surface ───
const client: LspClient = {
serverID,
root,
connection,
async notifyOpen(path: string) {
const filePath = normalize(path);
const text = await readFile(filePath, "utf8");
const languageId = languageIdForExtension(extname(filePath));
const uri = pathToFileURL(filePath).href;
const document = files[filePath];
if (document !== undefined) {
await connection.sendNotification("workspace/didChangeWatchedFiles", {
changes: [{ uri, type: FILE_CHANGE_CHANGED }],
});
const next = document.version + 1;
files[filePath] = { version: next, text };
await connection.sendNotification("textDocument/didChange", {
textDocument: { uri, version: next },
contentChanges:
syncKind === TEXT_DOCUMENT_SYNC_INCREMENTAL
? [
{
range: {
start: { line: 0, character: 0 },
end: endPosition(document.text),
},
text,
},
]
: [{ text }],
});
return next;
}
await connection.sendNotification("workspace/didChangeWatchedFiles", {
changes: [{ uri, type: FILE_CHANGE_CREATED }],
});
pushDiagnostics.delete(filePath);
pullDiagnostics.delete(filePath);
await connection.sendNotification("textDocument/didOpen", {
textDocument: { uri, languageId, version: 0, text },
});
files[filePath] = { version: 0, text };
return 0;
},
get diagnostics() {
const result = new Map<string, Diagnostic[]>();
for (const key of new Set([...pushDiagnostics.keys(), ...pullDiagnostics.keys()])) {
result.set(key, mergedDiagnostics(key));
}
return result;
},
async waitForDiagnostics(request) {
const normalizedPath = normalize(request.path);
if (request.mode === "document") {
await waitForDocumentDiagnostics({
path: normalizedPath,
version: request.version,
after: request.after,
});
return;
}
await waitForFullDiagnostics({
path: normalizedPath,
version: request.version,
after: request.after,
});
},
async request<T = unknown>(method: string, params: unknown): Promise<T | null> {
return connection.sendRequest<T>(method, params).catch(() => null);
},
async shutdown() {
try {
connection.end();
connection.dispose();
} catch {
/* connection may already be closed */
}
server.process.kill();
},
};
return client;
}
|