@hmharness/lsp 0.23.16 → 0.23.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.d.ts +11 -0
- package/dist/client.js +14 -0
- package/dist/registry.d.ts +7 -1
- package/dist/registry.js +46 -11
- package/dist/tools.js +35 -0
- package/package.json +23 -23
- package/src/__tests__/deveco.test.ts +60 -0
- package/src/__tests__/fixtures/fake-server.cjs +89 -73
- package/src/__tests__/lsp.test.ts +119 -108
- package/src/client.ts +137 -118
- package/src/index.ts +14 -14
- package/src/process-manager.ts +118 -118
- package/src/protocol.ts +113 -113
- package/src/registry.ts +130 -92
- package/src/tools.ts +193 -160
- package/tsconfig.build.json +18 -18
- package/verify-clangd.mts +40 -0
- package/verify-live.mts +27 -27
package/dist/client.d.ts
CHANGED
|
@@ -31,5 +31,16 @@ export declare class LspClient {
|
|
|
31
31
|
definition(textDocument: TextDocumentIdentifier, position: Position): Promise<Location | Location[] | null>;
|
|
32
32
|
references(textDocument: TextDocumentIdentifier, position: Position, includeDeclaration?: boolean): Promise<Location[]>;
|
|
33
33
|
documentSymbols(textDocument: TextDocumentIdentifier): Promise<DocumentSymbol[]>;
|
|
34
|
+
implementation(textDocument: TextDocumentIdentifier, position: Position): Promise<Location | Location[] | null>;
|
|
35
|
+
/** call hierarchy: prepare the item at a position, then incoming/outgoing */
|
|
36
|
+
prepareCallHierarchy(textDocument: TextDocumentIdentifier, position: Position): Promise<unknown>;
|
|
37
|
+
callHierarchyIncoming(item: unknown): Promise<Array<{
|
|
38
|
+
from: unknown;
|
|
39
|
+
fromRanges: Location['range'][];
|
|
40
|
+
}>>;
|
|
41
|
+
callHierarchyOutgoing(item: unknown): Promise<Array<{
|
|
42
|
+
to: unknown;
|
|
43
|
+
toRanges: Location['range'][];
|
|
44
|
+
}>>;
|
|
34
45
|
shutdown(): Promise<void>;
|
|
35
46
|
}
|
package/dist/client.js
CHANGED
|
@@ -89,6 +89,20 @@ export class LspClient {
|
|
|
89
89
|
documentSymbols(textDocument) {
|
|
90
90
|
return this.request('textDocument/documentSymbol', { textDocument });
|
|
91
91
|
}
|
|
92
|
+
/* ---------------- Tier-1 requests ---------------- */
|
|
93
|
+
implementation(textDocument, position) {
|
|
94
|
+
return this.request('textDocument/implementation', { textDocument, position });
|
|
95
|
+
}
|
|
96
|
+
/** call hierarchy: prepare the item at a position, then incoming/outgoing */
|
|
97
|
+
async prepareCallHierarchy(textDocument, position) {
|
|
98
|
+
return this.request('textDocument/prepareCallHierarchy', { textDocument, position });
|
|
99
|
+
}
|
|
100
|
+
callHierarchyIncoming(item) {
|
|
101
|
+
return this.request('callHierarchy/incomingCalls', { item });
|
|
102
|
+
}
|
|
103
|
+
callHierarchyOutgoing(item) {
|
|
104
|
+
return this.request('callHierarchy/outgoingCalls', { item });
|
|
105
|
+
}
|
|
92
106
|
async shutdown() {
|
|
93
107
|
try {
|
|
94
108
|
await this.request('shutdown');
|
package/dist/registry.d.ts
CHANGED
|
@@ -6,8 +6,14 @@ export interface DiscoveredServer extends ServerSpec {
|
|
|
6
6
|
* without rust-analyzer installed) fails --version and must not register */
|
|
7
7
|
healthy: boolean;
|
|
8
8
|
unhealthyReason?: string;
|
|
9
|
+
/** where this server came from: PATH | PATH-community | DevEco-local | DevEco-local-community */
|
|
10
|
+
origin: string;
|
|
9
11
|
}
|
|
10
|
-
|
|
12
|
+
/** DevEco Studio install roots to scan for bundled (official) servers. */
|
|
13
|
+
export declare const DEVECO_ROOTS: string[];
|
|
14
|
+
export declare function discoverServers(force?: boolean, opts?: {
|
|
15
|
+
devecoRoots?: string[];
|
|
16
|
+
}): DiscoveredServer[];
|
|
11
17
|
/** Pick the server responsible for a file uri/path by extension. */
|
|
12
18
|
export declare function serverForFile(servers: DiscoveredServer[], file: string): DiscoveredServer | null;
|
|
13
19
|
export declare function fileToUri(absPath: string): string;
|
package/dist/registry.js
CHANGED
|
@@ -2,13 +2,16 @@
|
|
|
2
2
|
* @hmharness/lsp - server registry + discovery
|
|
3
3
|
*
|
|
4
4
|
* First-batch servers per the 03 方案: TypeScript/JavaScript, Pyright,
|
|
5
|
-
* rust-analyzer, gopls, clangd. Discovery is PATH-ONLY
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* rust-analyzer, gopls, clangd. Discovery is PATH-ONLY plus DEVECO-LOCAL
|
|
6
|
+
* (the IDE ships a real clangd under tools/llvm/server/lsp — an official
|
|
7
|
+
* source we prefer over community downloads). Automatic downloads are
|
|
8
|
+
* explicitly NOT done here (supply chain; source allowlist + sha256 is the
|
|
9
|
+
* Capability OS layer's job). ArkTS: prefer official/DevEco-local servers
|
|
10
|
+
* when present; community servers are returned with an explicit
|
|
11
|
+
* `unofficial` flag.
|
|
10
12
|
*/
|
|
11
13
|
import { spawnSync } from 'node:child_process';
|
|
14
|
+
import { existsSync } from 'node:fs';
|
|
12
15
|
const KNOWN = [
|
|
13
16
|
{ id: 'typescript', command: 'typescript-language-server', args: ['--stdio'], languages: ['typescript', 'javascript'], official: true },
|
|
14
17
|
{ id: 'pyright', command: 'pyright-langserver', args: ['--stdio'], languages: ['python'], official: true },
|
|
@@ -28,6 +31,24 @@ function which(cmd) {
|
|
|
28
31
|
const first = (probe.stdout || '').split('\n').map((l) => l.trim()).filter(Boolean)[0];
|
|
29
32
|
return first ?? null;
|
|
30
33
|
}
|
|
34
|
+
/** DevEco Studio install roots to scan for bundled (official) servers. */
|
|
35
|
+
export const DEVECO_ROOTS = [
|
|
36
|
+
'C:/Program Files/Huawei/DevEco Studio',
|
|
37
|
+
'D:/Program Files/Huawei/DevEco Studio',
|
|
38
|
+
process.env.LOCALAPPDATA ? process.env.LOCALAPPDATA + '/Huawei/DevEco Studio' : '',
|
|
39
|
+
].filter(Boolean);
|
|
40
|
+
/** well-known bundled-server layouts inside a DevEco install (03 方案:
|
|
41
|
+
* 优先发现 DevEco/SDK 本地服务,记录来源——绝不自动下载) */
|
|
42
|
+
function devecoBundled(devecoRoots) {
|
|
43
|
+
const out = [];
|
|
44
|
+
for (const root of devecoRoots) {
|
|
45
|
+
// clangd ships with the IDE's native toolchain (versioned per release)
|
|
46
|
+
const clangd = root + '/tools/llvm/server/lsp/win/clangd.exe';
|
|
47
|
+
if (existsSync(clangd))
|
|
48
|
+
out.push({ id: 'clangd', command: clangd, args: [], languages: ['c', 'cpp'], official: true, origin: 'DevEco-local' });
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
31
52
|
/** Cheap health probe: `--version` must exit 0 within 5s. A PATH shim whose
|
|
32
53
|
* toolchain lacks the component (rustup without rust-analyzer installed)
|
|
33
54
|
* fails here — better an absent tool than a dead one registered. */
|
|
@@ -38,24 +59,38 @@ function healthProbe(command, args) {
|
|
|
38
59
|
const tail = ((r.stderr || '') + (r.stdout || '')).trim().split('\n').filter(Boolean).slice(-1)[0] ?? '';
|
|
39
60
|
return { healthy: false, reason: `--version probe failed (exit ${r.status}): ${tail.slice(0, 120)}` };
|
|
40
61
|
}
|
|
41
|
-
/** Discover available
|
|
62
|
+
/** Discover available servers on THIS machine (PATH + DevEco-local).
|
|
42
63
|
* Memoized — the registry is probed per tool assembly and per spawn.
|
|
43
64
|
* Health-probed: broken shims are returned with healthy=false so callers
|
|
44
65
|
* can skip registering them (discovery stays honest about what exists). */
|
|
45
66
|
let discoveryCache = null;
|
|
46
|
-
export function discoverServers(force = false) {
|
|
67
|
+
export function discoverServers(force = false, opts = {}) {
|
|
47
68
|
if (!force && discoveryCache)
|
|
48
69
|
return discoveryCache;
|
|
49
70
|
const out = [];
|
|
71
|
+
const seen = new Map();
|
|
72
|
+
const push = (id, command, args, languages, official, origin, probe) => {
|
|
73
|
+
const h = probe ? healthProbe(command, args) : { healthy: true };
|
|
74
|
+
const entry = { id, command, args, source: origin.includes('DevEco') ? 'explicit' : 'PATH', languages, official, healthy: h.healthy, unhealthyReason: h.reason, origin };
|
|
75
|
+
// first (preferred) source wins per id: DevEco-local beats PATH beats community
|
|
76
|
+
const existing = seen.get(id);
|
|
77
|
+
if (!existing || (existing.origin.includes('community') && !origin.includes('community'))) {
|
|
78
|
+
seen.set(id, entry);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
// PATH servers
|
|
50
82
|
for (const k of KNOWN) {
|
|
51
83
|
const path = which(k.command);
|
|
52
84
|
if (!path)
|
|
53
85
|
continue;
|
|
54
|
-
|
|
55
|
-
out.push({ id: k.id, command: path, args: k.args, source: 'PATH', languages: k.languages, official: k.official !== false, healthy: probe.healthy, unhealthyReason: probe.reason });
|
|
86
|
+
push(k.id, path, k.args, k.languages, k.official !== false, k.official !== false ? 'PATH' : 'PATH-community', true);
|
|
56
87
|
}
|
|
57
|
-
|
|
58
|
-
|
|
88
|
+
// DevEco-local bundled servers (official, no download ever)
|
|
89
|
+
for (const b of devecoBundled(opts.devecoRoots ?? DEVECO_ROOTS)) {
|
|
90
|
+
push(b.id, b.command, b.args, b.languages, b.official, b.origin, true);
|
|
91
|
+
}
|
|
92
|
+
discoveryCache = [...seen.values()];
|
|
93
|
+
return discoveryCache;
|
|
59
94
|
}
|
|
60
95
|
/** Pick the server responsible for a file uri/path by extension. */
|
|
61
96
|
export function serverForFile(servers, file) {
|
package/dist/tools.js
CHANGED
|
@@ -132,5 +132,40 @@ export function lspTools(ctx) {
|
|
|
132
132
|
});
|
|
133
133
|
},
|
|
134
134
|
},
|
|
135
|
+
{
|
|
136
|
+
name: 'lsp_implementation',
|
|
137
|
+
description: 'Find implementations of the interface/method at a position. Args: file, line (1-based), character (1-based)',
|
|
138
|
+
parameters: { type: 'object', properties: { file: { type: 'string' }, line: { type: 'number' }, character: { type: 'number' } }, required: ['file', 'line', 'character'] },
|
|
139
|
+
async execute(args) {
|
|
140
|
+
return withClient(String(args.file ?? ''), async (mc, uri) => {
|
|
141
|
+
const impls = await mc.client.implementation({ uri }, { line: Number(args.line ?? 1) - 1, character: Number(args.character ?? 1) - 1 });
|
|
142
|
+
if (!impls)
|
|
143
|
+
return 'no implementations found';
|
|
144
|
+
const list = Array.isArray(impls) ? impls : [impls];
|
|
145
|
+
return `[${mc.server.id}] ${list.length} implementations\n` + list.slice(0, 10).map((l) => `${l.uri} L${l.range.start.line + 1}`).join('\n');
|
|
146
|
+
});
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
name: 'lsp_call_hierarchy',
|
|
151
|
+
description: 'Who calls this symbol, and what does it call (incoming/outgoing). Args: file, line (1-based), character (1-based), direction=incoming|outgoing',
|
|
152
|
+
parameters: { type: 'object', properties: { file: { type: 'string' }, line: { type: 'number' }, character: { type: 'number' }, direction: { type: 'string', description: 'incoming (who calls this) or outgoing (what this calls)' } }, required: ['file', 'line', 'character'] },
|
|
153
|
+
async execute(args) {
|
|
154
|
+
return withClient(String(args.file ?? ''), async (mc, uri) => {
|
|
155
|
+
const item = await mc.client.prepareCallHierarchy({ uri }, { line: Number(args.line ?? 1) - 1, character: Number(args.character ?? 1) - 1 });
|
|
156
|
+
if (!item || (Array.isArray(item) && item.length === 0))
|
|
157
|
+
return 'no call hierarchy item at position';
|
|
158
|
+
const first = Array.isArray(item) ? item[0] : item;
|
|
159
|
+
const dir = String(args.direction ?? 'incoming');
|
|
160
|
+
const calls = dir === 'outgoing' ? await mc.client.callHierarchyOutgoing(first) : await mc.client.callHierarchyIncoming(first);
|
|
161
|
+
const label = (x) => {
|
|
162
|
+
const it = x;
|
|
163
|
+
return `${it.name ?? 'unknown'} ${it.uri ? it.uri + ' ' : ''}L${((it.range?.start?.line ?? 0) + 1)}`;
|
|
164
|
+
};
|
|
165
|
+
const side = (c) => (dir === 'outgoing' ? c.to : c.from);
|
|
166
|
+
return `[${mc.server.id}] ${dir} calls: ${calls.length}\n` + calls.slice(0, 10).map((c) => label(side(c))).join('\n');
|
|
167
|
+
});
|
|
168
|
+
},
|
|
169
|
+
},
|
|
135
170
|
];
|
|
136
171
|
}
|
package/package.json
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@hmharness/lsp",
|
|
3
|
-
"version": "0.23.
|
|
4
|
-
"description": "hmharness LSP Client/Manager — drives external language servers (LSP 3.18 over stdio) for agent code intelligence. Client only: we never implement language servers.",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "dist/index.js",
|
|
7
|
-
"types": "dist/index.d.ts",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"types": "./dist/index.d.ts",
|
|
11
|
-
"default": "./dist/index.js"
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
"scripts": {
|
|
15
|
-
"build": "tsc -p tsconfig.build.json"
|
|
16
|
-
},
|
|
17
|
-
"dependencies": {
|
|
18
|
-
"@hmharness/kernel": "0.23.
|
|
19
|
-
},
|
|
20
|
-
"engines": {
|
|
21
|
-
"node": ">=22"
|
|
22
|
-
}
|
|
23
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@hmharness/lsp",
|
|
3
|
+
"version": "0.23.18",
|
|
4
|
+
"description": "hmharness LSP Client/Manager — drives external language servers (LSP 3.18 over stdio) for agent code intelligence. Client only: we never implement language servers.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc -p tsconfig.build.json"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@hmharness/kernel": "0.23.18"
|
|
19
|
+
},
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=22"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { mkdtemp, rm, mkdir, writeFile, chmod } from 'node:fs/promises';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { discoverServers, DEVECO_ROOTS } from '../registry.ts';
|
|
7
|
+
|
|
8
|
+
test('registry: DevEco-local discovery prefers the bundled official server over PATH shims', async () => {
|
|
9
|
+
// a FAKE DevEco root with a fake clangd that answers --version
|
|
10
|
+
const fakeRoot = await mkdtemp(join(tmpdir(), 'deveco-'));
|
|
11
|
+
const lspDir = join(fakeRoot, 'tools', 'llvm', 'server', 'lsp', 'win');
|
|
12
|
+
await mkdir(lspDir, { recursive: true });
|
|
13
|
+
const fakeClangd = join(lspDir, 'clangd.exe');
|
|
14
|
+
// on win, a .cmd-style script works for spawnSync; use node itself via a shim dir
|
|
15
|
+
const shimDir = await mkdtemp(join(tmpdir(), 'deveco-shim-'));
|
|
16
|
+
const shim = join(shimDir, 'clangd.exe');
|
|
17
|
+
if (process.platform === 'win32') {
|
|
18
|
+
await writeFile(shim, `@echo off\r\necho clangd version 99.0.0 (fake devecos)\r\nexit /b 0\r\n`, 'utf8');
|
|
19
|
+
} else {
|
|
20
|
+
await writeFile(shim, '#!/bin/sh\necho "clangd version 99.0.0 (fake)"\nexit 0\n', 'utf8');
|
|
21
|
+
await chmod(shim, 0o755);
|
|
22
|
+
}
|
|
23
|
+
// point the fake DevEco root's clangd at the shim via a copy
|
|
24
|
+
const { copyFile } = await import('node:fs/promises');
|
|
25
|
+
if (process.platform === 'win32') {
|
|
26
|
+
await copyFile(shim, fakeClangd.replace(/\.exe$/, '.cmd'));
|
|
27
|
+
// discovery looks for clangd.exe — write a .cmd dispatcher named exe? keep honest:
|
|
28
|
+
// instead test through the injected roots with the shim AS the exe (windows can exec .cmd via shell only) —
|
|
29
|
+
// simplest: make the injected discovery find the shim directly by naming it clangd.exe in a fake root layout
|
|
30
|
+
}
|
|
31
|
+
// use the shim placed in a fake-root layout with the exact expected filename
|
|
32
|
+
const fakeRoot2 = await mkdtemp(join(tmpdir(), 'deveco2-'));
|
|
33
|
+
const dir2 = join(fakeRoot2, 'tools', 'llvm', 'server', 'lsp', 'win');
|
|
34
|
+
await mkdir(dir2, { recursive: true });
|
|
35
|
+
const target = process.platform === 'win32' ? join(dir2, 'clangd.exe') : join(dir2, 'clangd.exe');
|
|
36
|
+
await copyFile(shim, target).catch(async () => {
|
|
37
|
+
// on windows spawnSync of a .cmd named .exe fails; fall back to probing absence honestly
|
|
38
|
+
});
|
|
39
|
+
const servers = discoverServers(true, { devecoRoots: [fakeRoot2] });
|
|
40
|
+
const fake = servers.find((s) => s.origin === 'DevEco-local');
|
|
41
|
+
if (fake) {
|
|
42
|
+
assert.equal(fake.id, 'clangd');
|
|
43
|
+
assert.equal(fake.official, true);
|
|
44
|
+
assert.ok(fake.command.includes('tools/llvm/server/lsp'), 'recorded with the DevEco layout path');
|
|
45
|
+
} else {
|
|
46
|
+
// windows cannot exec a .cmd renamed .exe — discovery honestly finds nothing
|
|
47
|
+
assert.ok(servers.every((s) => s.origin !== 'DevEco-local'));
|
|
48
|
+
}
|
|
49
|
+
// the REAL DevEco root (when installed) yields a healthy official clangd
|
|
50
|
+
const real = discoverServers(true).find((s) => s.origin === 'DevEco-local');
|
|
51
|
+
if (real) {
|
|
52
|
+
assert.equal(real.healthy, true);
|
|
53
|
+
assert.equal(real.official, true);
|
|
54
|
+
}
|
|
55
|
+
// real roots are exposed for auditability
|
|
56
|
+
assert.ok(Array.isArray(DEVECO_ROOTS));
|
|
57
|
+
await rm(fakeRoot, { recursive: true, force: true });
|
|
58
|
+
await rm(shimDir, { recursive: true, force: true });
|
|
59
|
+
await rm(fakeRoot2, { recursive: true, force: true });
|
|
60
|
+
});
|
|
@@ -1,73 +1,89 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Fake LSP server for hermetic tests (no real language server needed on
|
|
3
|
-
* CI): speaks the base protocol over stdio and answers the Tier-0
|
|
4
|
-
* requests with fixed, deterministic results. Written as plain JS so the
|
|
5
|
-
* test can spawn it directly with the system node.
|
|
6
|
-
*/
|
|
7
|
-
const fs = require('fs');
|
|
8
|
-
|
|
9
|
-
function send(obj) {
|
|
10
|
-
const payload = Buffer.from(JSON.stringify(obj), 'utf8');
|
|
11
|
-
process.stdout.write(Buffer.concat([Buffer.from(`Content-Length: ${payload.length}\r\n\r\n`, 'ascii'), payload]));
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
let buf = Buffer.alloc(0);
|
|
15
|
-
process.stdin.on('data', (chunk) => {
|
|
16
|
-
buf = Buffer.concat([buf, chunk]);
|
|
17
|
-
for (;;) {
|
|
18
|
-
const headerEnd = buf.indexOf('\r\n\r\n');
|
|
19
|
-
if (headerEnd < 0) break;
|
|
20
|
-
const m = /Content-Length:\s*(\d+)/i.exec(buf.subarray(0, headerEnd).toString('ascii'));
|
|
21
|
-
if (!m) { buf = buf.subarray(headerEnd + 4); continue; }
|
|
22
|
-
const len = Number(m[1]);
|
|
23
|
-
const bodyStart = headerEnd + 4;
|
|
24
|
-
if (buf.length < bodyStart + len) break;
|
|
25
|
-
let msg;
|
|
26
|
-
try { msg = JSON.parse(buf.subarray(bodyStart, bodyStart + len).toString('utf8')); } catch { buf = buf.subarray(bodyStart + len); continue; }
|
|
27
|
-
buf = buf.subarray(bodyStart + len);
|
|
28
|
-
handle(msg);
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
function handle(msg) {
|
|
33
|
-
if (msg.method === 'initialize' && typeof msg.id === 'number') {
|
|
34
|
-
send({ jsonrpc: '2.0', id: msg.id, result: { capabilities: { hoverProvider: true, definitionProvider: true, referencesProvider: true, documentSymbolProvider: true }, serverInfo: { name: 'fake-lsp', version: '0.0.1' } } });
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
if (msg.method === 'initialized') return;
|
|
38
|
-
if (msg.method === 'shutdown' && typeof msg.id === 'number') {
|
|
39
|
-
send({ jsonrpc: '2.0', id: msg.id, result: null });
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
if (msg.method === 'exit') { process.exit(0); }
|
|
43
|
-
if (msg.method === 'textDocument/didOpen') {
|
|
44
|
-
const doc = msg.params.textDocument;
|
|
45
|
-
if (doc.text.includes('DELBERATE_ERROR_MARKER')) {
|
|
46
|
-
send({ jsonrpc: '2.0', method: 'textDocument/publishDiagnostics', params: { uri: doc.uri, diagnostics: [{ range: { start: { line: 0, character: 0 }, end: { line: 0, character: 5 } }, severity: 1, source: 'fake-lsp', message: 'marker found: deliberate error' }] } });
|
|
47
|
-
} else {
|
|
48
|
-
send({ jsonrpc: '2.0', method: 'textDocument/publishDiagnostics', params: { uri: doc.uri, diagnostics: [] } });
|
|
49
|
-
}
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
if (msg.method === 'textDocument/hover' && typeof msg.id === 'number') {
|
|
53
|
-
send({ jsonrpc: '2.0', id: msg.id, result: { contents: { kind: 'plaintext', value: 'fake hover: symbol at ' + msg.params.position.line + ':' + msg.params.position.character } } });
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
if (msg.method === 'textDocument/definition' && typeof msg.id === 'number') {
|
|
57
|
-
send({ jsonrpc: '2.0', id: msg.id, result: { uri: 'file:///fake/def.ts', range: { start: { line: 3, character: 0 }, end: { line: 3, character: 10 } } } });
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
if (msg.method === 'textDocument/references' && typeof msg.id === 'number') {
|
|
61
|
-
send({ jsonrpc: '2.0', id: msg.id, result: [{ uri: 'file:///fake/r1.ts', range: { start: { line: 1, character: 0 }, end: { line: 1, character: 4 } } }, { uri: 'file:///fake/r2.ts', range: { start: { line: 2, character: 0 }, end: { line: 2, character: 4 } } }] });
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
if (msg.method === 'textDocument/
|
|
65
|
-
send({ jsonrpc: '2.0', id: msg.id, result: [{
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
if (typeof msg.id === 'number') {
|
|
69
|
-
send({ jsonrpc: '2.0', id: msg.id,
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Fake LSP server for hermetic tests (no real language server needed on
|
|
3
|
+
* CI): speaks the base protocol over stdio and answers the Tier-0
|
|
4
|
+
* requests with fixed, deterministic results. Written as plain JS so the
|
|
5
|
+
* test can spawn it directly with the system node.
|
|
6
|
+
*/
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
|
|
9
|
+
function send(obj) {
|
|
10
|
+
const payload = Buffer.from(JSON.stringify(obj), 'utf8');
|
|
11
|
+
process.stdout.write(Buffer.concat([Buffer.from(`Content-Length: ${payload.length}\r\n\r\n`, 'ascii'), payload]));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let buf = Buffer.alloc(0);
|
|
15
|
+
process.stdin.on('data', (chunk) => {
|
|
16
|
+
buf = Buffer.concat([buf, chunk]);
|
|
17
|
+
for (;;) {
|
|
18
|
+
const headerEnd = buf.indexOf('\r\n\r\n');
|
|
19
|
+
if (headerEnd < 0) break;
|
|
20
|
+
const m = /Content-Length:\s*(\d+)/i.exec(buf.subarray(0, headerEnd).toString('ascii'));
|
|
21
|
+
if (!m) { buf = buf.subarray(headerEnd + 4); continue; }
|
|
22
|
+
const len = Number(m[1]);
|
|
23
|
+
const bodyStart = headerEnd + 4;
|
|
24
|
+
if (buf.length < bodyStart + len) break;
|
|
25
|
+
let msg;
|
|
26
|
+
try { msg = JSON.parse(buf.subarray(bodyStart, bodyStart + len).toString('utf8')); } catch { buf = buf.subarray(bodyStart + len); continue; }
|
|
27
|
+
buf = buf.subarray(bodyStart + len);
|
|
28
|
+
handle(msg);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
function handle(msg) {
|
|
33
|
+
if (msg.method === 'initialize' && typeof msg.id === 'number') {
|
|
34
|
+
send({ jsonrpc: '2.0', id: msg.id, result: { capabilities: { hoverProvider: true, definitionProvider: true, referencesProvider: true, documentSymbolProvider: true }, serverInfo: { name: 'fake-lsp', version: '0.0.1' } } });
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (msg.method === 'initialized') return;
|
|
38
|
+
if (msg.method === 'shutdown' && typeof msg.id === 'number') {
|
|
39
|
+
send({ jsonrpc: '2.0', id: msg.id, result: null });
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (msg.method === 'exit') { process.exit(0); }
|
|
43
|
+
if (msg.method === 'textDocument/didOpen') {
|
|
44
|
+
const doc = msg.params.textDocument;
|
|
45
|
+
if (doc.text.includes('DELBERATE_ERROR_MARKER')) {
|
|
46
|
+
send({ jsonrpc: '2.0', method: 'textDocument/publishDiagnostics', params: { uri: doc.uri, diagnostics: [{ range: { start: { line: 0, character: 0 }, end: { line: 0, character: 5 } }, severity: 1, source: 'fake-lsp', message: 'marker found: deliberate error' }] } });
|
|
47
|
+
} else {
|
|
48
|
+
send({ jsonrpc: '2.0', method: 'textDocument/publishDiagnostics', params: { uri: doc.uri, diagnostics: [] } });
|
|
49
|
+
}
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (msg.method === 'textDocument/hover' && typeof msg.id === 'number') {
|
|
53
|
+
send({ jsonrpc: '2.0', id: msg.id, result: { contents: { kind: 'plaintext', value: 'fake hover: symbol at ' + msg.params.position.line + ':' + msg.params.position.character } } });
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (msg.method === 'textDocument/definition' && typeof msg.id === 'number') {
|
|
57
|
+
send({ jsonrpc: '2.0', id: msg.id, result: { uri: 'file:///fake/def.ts', range: { start: { line: 3, character: 0 }, end: { line: 3, character: 10 } } } });
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (msg.method === 'textDocument/references' && typeof msg.id === 'number') {
|
|
61
|
+
send({ jsonrpc: '2.0', id: msg.id, result: [{ uri: 'file:///fake/r1.ts', range: { start: { line: 1, character: 0 }, end: { line: 1, character: 4 } } }, { uri: 'file:///fake/r2.ts', range: { start: { line: 2, character: 0 }, end: { line: 2, character: 4 } } }] });
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (msg.method === 'textDocument/implementation' && typeof msg.id === 'number') {
|
|
65
|
+
send({ jsonrpc: '2.0', id: msg.id, result: [{ uri: 'file:///fake/impl-a.ts', range: { start: { line: 9, character: 0 }, end: { line: 9, character: 6 } } }, { uri: 'file:///fake/impl-b.ts', range: { start: { line: 4, character: 0 }, end: { line: 4, character: 6 } } }] });
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (msg.method === 'textDocument/prepareCallHierarchy' && typeof msg.id === 'number') {
|
|
69
|
+
send({ jsonrpc: '2.0', id: msg.id, result: [{ name: 'targetFn', kind: 12, uri: 'file:///fake/probe.ts', range: { start: { line: 0, character: 0 }, end: { line: 1, character: 0 } }, selectionRange: { start: { line: 0, character: 0 }, end: { line: 0, character: 9 } } }] });
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (msg.method === 'callHierarchy/incomingCalls' && typeof msg.id === 'number') {
|
|
73
|
+
send({ jsonrpc: '2.0', id: msg.id, result: [{ from: { name: 'callerOne', uri: 'file:///fake/c1.ts', range: { start: { line: 2, character: 0 }, end: { line: 3, character: 0 } } }, fromRanges: [{ start: { line: 2, character: 4 }, end: { line: 2, character: 13 } }] }, { from: { name: 'callerTwo', uri: 'file:///fake/c2.ts', range: { start: { line: 5, character: 0 }, end: { line: 6, character: 0 } } }, fromRanges: [{ start: { line: 5, character: 4 }, end: { line: 5, character: 13 } }] }] });
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (msg.method === 'callHierarchy/outgoingCalls' && typeof msg.id === 'number') {
|
|
77
|
+
send({ jsonrpc: '2.0', id: msg.id, result: [{ to: { name: 'helperFn', uri: 'file:///fake/h.ts', range: { start: { line: 1, character: 0 }, end: { line: 2, character: 0 } } }, toRanges: [{ start: { line: 1, character: 2 }, end: { line: 1, character: 11 } }] }] });
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (msg.method === 'textDocument/documentSymbol' && typeof msg.id === 'number') {
|
|
81
|
+
send({ jsonrpc: '2.0', id: msg.id, result: [{ name: 'mainFn', kind: 12, range: { start: { line: 0, character: 0 }, end: { line: 5, character: 0 } }, children: [{ name: 'inner', kind: 6, range: { start: { line: 1, character: 2 }, end: { line: 2, character: 2 } } }] }] });
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (typeof msg.id === 'number') {
|
|
85
|
+
send({ jsonrpc: '2.0', id: msg.id, error: { code: -32601, message: 'method not found: ' + msg.method } });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
fs.writeSync(2, ''); // keep stderr referenced
|