@razdolbai/merls 1.2.1 → 1.3.0
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/AGENTS.md +64 -64
- package/dist/src/lsp/completion.js +14 -11
- package/dist/test/completion.test.js +1 -1
- package/dist/test/definition-references.test.js +1 -1
- package/dist/test/diagnostics.test.js +2 -2
- package/dist/test/document-symbol.test.js +1 -1
- package/dist/test/fixture-corpus.test.js +4 -4
- package/dist/test/hover.test.js +1 -1
- package/dist/test/lexer.test.js +4 -4
- package/dist/test/local-labels.test.js +1 -1
- package/dist/test/publish-diagnostics.test.js +1 -1
- package/dist/test/semantic-tokens.test.js +2 -1
- package/dist/test/symbols.test.js +1 -1
- package/dist/test/workspace-symbol.test.js +2 -2
- package/dist/test/workspace.test.js +2 -2
- package/package.json +1 -1
- package/src/lsp/completion.ts +15 -11
- package/test/completion.test.ts +151 -151
- package/test/definition-references.test.ts +152 -152
- package/test/diagnostics.test.ts +129 -129
- package/test/document-symbol.test.ts +131 -131
- package/test/fixture-corpus.test.ts +33 -33
- package/test/fixtures/valid/{merlin32-linkscript.asm → merlin32-linkscript.S} +16 -16
- package/test/hover.test.ts +175 -175
- package/test/lexer.test.ts +87 -87
- package/test/local-labels.test.ts +47 -47
- package/test/publish-diagnostics.test.ts +206 -206
- package/test/semantic-tokens.test.ts +128 -128
- package/test/smoke/run-smoke.ps1 +177 -177
- package/test/symbols.test.ts +41 -41
- package/test/workspace-symbol.test.ts +139 -139
- package/test/workspace.test.ts +29 -29
- /package/test/fixtures/invalid/{65816-bank-ops.asm → 65816-bank-ops.S} +0 -0
- /package/test/fixtures/invalid/{65816-long-addressing.asm → 65816-long-addressing.S} +0 -0
- /package/test/fixtures/valid/{merlin32-main-6502.asm → merlin32-main-6502.S} +0 -0
- /package/test/fixtures/valid/{smoke-test.asm → smoke-test.S} +0 -0
package/test/completion.test.ts
CHANGED
|
@@ -1,151 +1,151 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import fs from "node:fs";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { spawn } from "node:child_process";
|
|
5
|
-
|
|
6
|
-
type JsonRpcMessage = {
|
|
7
|
-
id?: number;
|
|
8
|
-
jsonrpc: "2.0";
|
|
9
|
-
result?: unknown;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
function encodeMessage(message: object): string {
|
|
13
|
-
const body = JSON.stringify(message);
|
|
14
|
-
return `Content-Length: ${Buffer.byteLength(body, "utf8")}\r\n\r\n${body}`;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function decodeMessages(streamBuffer: string): { messages: JsonRpcMessage[]; rest: string } {
|
|
18
|
-
const messages: JsonRpcMessage[] = [];
|
|
19
|
-
let buffer = streamBuffer;
|
|
20
|
-
|
|
21
|
-
for (;;) {
|
|
22
|
-
const separator = buffer.indexOf("\r\n\r\n");
|
|
23
|
-
if (separator === -1) {
|
|
24
|
-
return { messages, rest: buffer };
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const header = buffer.slice(0, separator);
|
|
28
|
-
const match = /Content-Length: (\d+)/i.exec(header);
|
|
29
|
-
if (!match) {
|
|
30
|
-
throw new Error(`Missing Content-Length header: ${header}`);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const length = Number(match[1]);
|
|
34
|
-
const body = buffer.slice(separator + 4);
|
|
35
|
-
if (Buffer.byteLength(body, "utf8") < length) {
|
|
36
|
-
return { messages, rest: buffer };
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
messages.push(JSON.parse(body.slice(0, length)) as JsonRpcMessage);
|
|
40
|
-
buffer = body.slice(length);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function positionOf(text: string, needle: string): { line: number; character: number } {
|
|
45
|
-
const index = text.indexOf(needle);
|
|
46
|
-
assert.notEqual(index, -1, `expected to find ${needle}`);
|
|
47
|
-
const prefix = text.slice(0, index);
|
|
48
|
-
const lines = prefix.split("\n");
|
|
49
|
-
return {
|
|
50
|
-
line: lines.length - 1,
|
|
51
|
-
character: lines.at(-1)?.length ?? 0
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export async function runCompletionTest(): Promise<void> {
|
|
56
|
-
const serverPath = path.resolve(__dirname, "../src/server.js");
|
|
57
|
-
const mainPath = path.resolve(
|
|
58
|
-
process.cwd(),
|
|
59
|
-
"test/fixtures/valid/merlin32-main-6502.
|
|
60
|
-
);
|
|
61
|
-
const mainUri = `file://${mainPath.replace(/\\/g, "/")}`;
|
|
62
|
-
const text = `${fs.readFileSync(mainPath, "utf8")}\n ld\n du\n bpl G`;
|
|
63
|
-
const child = spawn(process.execPath, [serverPath], {
|
|
64
|
-
stdio: ["pipe", "pipe", "pipe"]
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
let stdout = "";
|
|
68
|
-
let nextId = 1;
|
|
69
|
-
const pending = new Map<number, (message: JsonRpcMessage) => void>();
|
|
70
|
-
|
|
71
|
-
child.stdout.setEncoding("utf8");
|
|
72
|
-
child.stdout.on("data", (chunk: string) => {
|
|
73
|
-
stdout += chunk;
|
|
74
|
-
const decoded = decodeMessages(stdout);
|
|
75
|
-
stdout = decoded.rest;
|
|
76
|
-
|
|
77
|
-
for (const message of decoded.messages) {
|
|
78
|
-
if (message.id !== undefined) {
|
|
79
|
-
pending.get(message.id)?.(message);
|
|
80
|
-
pending.delete(message.id);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
function sendRequest(method: string, params: object): Promise<JsonRpcMessage> {
|
|
86
|
-
const id = nextId++;
|
|
87
|
-
child.stdin.write(
|
|
88
|
-
encodeMessage({
|
|
89
|
-
id,
|
|
90
|
-
jsonrpc: "2.0",
|
|
91
|
-
method,
|
|
92
|
-
params
|
|
93
|
-
})
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
return new Promise((resolve) => {
|
|
97
|
-
pending.set(id, resolve);
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function sendNotification(method: string, params: object): void {
|
|
102
|
-
child.stdin.write(
|
|
103
|
-
encodeMessage({
|
|
104
|
-
jsonrpc: "2.0",
|
|
105
|
-
method,
|
|
106
|
-
params
|
|
107
|
-
})
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
try {
|
|
112
|
-
await sendRequest("initialize", {
|
|
113
|
-
capabilities: {},
|
|
114
|
-
processId: process.pid,
|
|
115
|
-
rootUri: `file://${path.resolve(process.cwd()).replace(/\\/g, "/")}`
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
sendNotification("initialized", {});
|
|
119
|
-
sendNotification("textDocument/didOpen", {
|
|
120
|
-
textDocument: {
|
|
121
|
-
uri: mainUri,
|
|
122
|
-
languageId: "asm",
|
|
123
|
-
version: 1,
|
|
124
|
-
text
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
const opcodeResponse = await sendRequest("textDocument/completion", {
|
|
129
|
-
textDocument: { uri: mainUri },
|
|
130
|
-
position: positionOf(text, " ld")
|
|
131
|
-
});
|
|
132
|
-
const opcodeItems = opcodeResponse.result as Array<{ label: string }>;
|
|
133
|
-
assert.equal(opcodeItems.some((item) => item.label === "lda"), true);
|
|
134
|
-
|
|
135
|
-
const directiveResponse = await sendRequest("textDocument/completion", {
|
|
136
|
-
textDocument: { uri: mainUri },
|
|
137
|
-
position: positionOf(text, " du")
|
|
138
|
-
});
|
|
139
|
-
const directiveItems = directiveResponse.result as Array<{ label: string }>;
|
|
140
|
-
assert.equal(directiveItems.some((item) => item.label === "dum"), true);
|
|
141
|
-
|
|
142
|
-
const symbolResponse = await sendRequest("textDocument/completion", {
|
|
143
|
-
textDocument: { uri: mainUri },
|
|
144
|
-
position: positionOf(text, " bpl G")
|
|
145
|
-
});
|
|
146
|
-
const symbolItems = symbolResponse.result as Array<{ label: string }>;
|
|
147
|
-
assert.equal(symbolItems.some((item) => item.label === "GetKey"), true);
|
|
148
|
-
} finally {
|
|
149
|
-
child.kill();
|
|
150
|
-
}
|
|
151
|
-
}
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
|
|
6
|
+
type JsonRpcMessage = {
|
|
7
|
+
id?: number;
|
|
8
|
+
jsonrpc: "2.0";
|
|
9
|
+
result?: unknown;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
function encodeMessage(message: object): string {
|
|
13
|
+
const body = JSON.stringify(message);
|
|
14
|
+
return `Content-Length: ${Buffer.byteLength(body, "utf8")}\r\n\r\n${body}`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function decodeMessages(streamBuffer: string): { messages: JsonRpcMessage[]; rest: string } {
|
|
18
|
+
const messages: JsonRpcMessage[] = [];
|
|
19
|
+
let buffer = streamBuffer;
|
|
20
|
+
|
|
21
|
+
for (;;) {
|
|
22
|
+
const separator = buffer.indexOf("\r\n\r\n");
|
|
23
|
+
if (separator === -1) {
|
|
24
|
+
return { messages, rest: buffer };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const header = buffer.slice(0, separator);
|
|
28
|
+
const match = /Content-Length: (\d+)/i.exec(header);
|
|
29
|
+
if (!match) {
|
|
30
|
+
throw new Error(`Missing Content-Length header: ${header}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const length = Number(match[1]);
|
|
34
|
+
const body = buffer.slice(separator + 4);
|
|
35
|
+
if (Buffer.byteLength(body, "utf8") < length) {
|
|
36
|
+
return { messages, rest: buffer };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
messages.push(JSON.parse(body.slice(0, length)) as JsonRpcMessage);
|
|
40
|
+
buffer = body.slice(length);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function positionOf(text: string, needle: string): { line: number; character: number } {
|
|
45
|
+
const index = text.indexOf(needle);
|
|
46
|
+
assert.notEqual(index, -1, `expected to find ${needle}`);
|
|
47
|
+
const prefix = text.slice(0, index);
|
|
48
|
+
const lines = prefix.split("\n");
|
|
49
|
+
return {
|
|
50
|
+
line: lines.length - 1,
|
|
51
|
+
character: lines.at(-1)?.length ?? 0
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function runCompletionTest(): Promise<void> {
|
|
56
|
+
const serverPath = path.resolve(__dirname, "../src/server.js");
|
|
57
|
+
const mainPath = path.resolve(
|
|
58
|
+
process.cwd(),
|
|
59
|
+
"test/fixtures/valid/merlin32-main-6502.S"
|
|
60
|
+
);
|
|
61
|
+
const mainUri = `file://${mainPath.replace(/\\/g, "/")}`;
|
|
62
|
+
const text = `${fs.readFileSync(mainPath, "utf8")}\n ld\n du\n bpl G`;
|
|
63
|
+
const child = spawn(process.execPath, [serverPath], {
|
|
64
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
let stdout = "";
|
|
68
|
+
let nextId = 1;
|
|
69
|
+
const pending = new Map<number, (message: JsonRpcMessage) => void>();
|
|
70
|
+
|
|
71
|
+
child.stdout.setEncoding("utf8");
|
|
72
|
+
child.stdout.on("data", (chunk: string) => {
|
|
73
|
+
stdout += chunk;
|
|
74
|
+
const decoded = decodeMessages(stdout);
|
|
75
|
+
stdout = decoded.rest;
|
|
76
|
+
|
|
77
|
+
for (const message of decoded.messages) {
|
|
78
|
+
if (message.id !== undefined) {
|
|
79
|
+
pending.get(message.id)?.(message);
|
|
80
|
+
pending.delete(message.id);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
function sendRequest(method: string, params: object): Promise<JsonRpcMessage> {
|
|
86
|
+
const id = nextId++;
|
|
87
|
+
child.stdin.write(
|
|
88
|
+
encodeMessage({
|
|
89
|
+
id,
|
|
90
|
+
jsonrpc: "2.0",
|
|
91
|
+
method,
|
|
92
|
+
params
|
|
93
|
+
})
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
return new Promise((resolve) => {
|
|
97
|
+
pending.set(id, resolve);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function sendNotification(method: string, params: object): void {
|
|
102
|
+
child.stdin.write(
|
|
103
|
+
encodeMessage({
|
|
104
|
+
jsonrpc: "2.0",
|
|
105
|
+
method,
|
|
106
|
+
params
|
|
107
|
+
})
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
await sendRequest("initialize", {
|
|
113
|
+
capabilities: {},
|
|
114
|
+
processId: process.pid,
|
|
115
|
+
rootUri: `file://${path.resolve(process.cwd()).replace(/\\/g, "/")}`
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
sendNotification("initialized", {});
|
|
119
|
+
sendNotification("textDocument/didOpen", {
|
|
120
|
+
textDocument: {
|
|
121
|
+
uri: mainUri,
|
|
122
|
+
languageId: "asm",
|
|
123
|
+
version: 1,
|
|
124
|
+
text
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const opcodeResponse = await sendRequest("textDocument/completion", {
|
|
129
|
+
textDocument: { uri: mainUri },
|
|
130
|
+
position: positionOf(text, " ld")
|
|
131
|
+
});
|
|
132
|
+
const opcodeItems = opcodeResponse.result as Array<{ label: string }>;
|
|
133
|
+
assert.equal(opcodeItems.some((item) => item.label === "lda"), true);
|
|
134
|
+
|
|
135
|
+
const directiveResponse = await sendRequest("textDocument/completion", {
|
|
136
|
+
textDocument: { uri: mainUri },
|
|
137
|
+
position: positionOf(text, " du")
|
|
138
|
+
});
|
|
139
|
+
const directiveItems = directiveResponse.result as Array<{ label: string }>;
|
|
140
|
+
assert.equal(directiveItems.some((item) => item.label === "dum"), true);
|
|
141
|
+
|
|
142
|
+
const symbolResponse = await sendRequest("textDocument/completion", {
|
|
143
|
+
textDocument: { uri: mainUri },
|
|
144
|
+
position: positionOf(text, " bpl G")
|
|
145
|
+
});
|
|
146
|
+
const symbolItems = symbolResponse.result as Array<{ label: string }>;
|
|
147
|
+
assert.equal(symbolItems.some((item) => item.label === "GetKey"), true);
|
|
148
|
+
} finally {
|
|
149
|
+
child.kill();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -1,152 +1,152 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import fs from "node:fs";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { spawn } from "node:child_process";
|
|
5
|
-
|
|
6
|
-
type JsonRpcMessage = {
|
|
7
|
-
id?: number;
|
|
8
|
-
jsonrpc: "2.0";
|
|
9
|
-
result?: unknown;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
function encodeMessage(message: object): string {
|
|
13
|
-
const body = JSON.stringify(message);
|
|
14
|
-
return `Content-Length: ${Buffer.byteLength(body, "utf8")}\r\n\r\n${body}`;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function decodeMessages(streamBuffer: string): { messages: JsonRpcMessage[]; rest: string } {
|
|
18
|
-
const messages: JsonRpcMessage[] = [];
|
|
19
|
-
let buffer = streamBuffer;
|
|
20
|
-
|
|
21
|
-
for (;;) {
|
|
22
|
-
const separator = buffer.indexOf("\r\n\r\n");
|
|
23
|
-
if (separator === -1) {
|
|
24
|
-
return { messages, rest: buffer };
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const header = buffer.slice(0, separator);
|
|
28
|
-
const match = /Content-Length: (\d+)/i.exec(header);
|
|
29
|
-
if (!match) {
|
|
30
|
-
throw new Error(`Missing Content-Length header: ${header}`);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const length = Number(match[1]);
|
|
34
|
-
const body = buffer.slice(separator + 4);
|
|
35
|
-
if (Buffer.byteLength(body, "utf8") < length) {
|
|
36
|
-
return { messages, rest: buffer };
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
messages.push(JSON.parse(body.slice(0, length)) as JsonRpcMessage);
|
|
40
|
-
buffer = body.slice(length);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function positionOf(text: string, needle: string): { line: number; character: number } {
|
|
45
|
-
const index = text.indexOf(needle);
|
|
46
|
-
assert.notEqual(index, -1, `expected to find ${needle}`);
|
|
47
|
-
const prefix = text.slice(0, index);
|
|
48
|
-
const lines = prefix.split("\n");
|
|
49
|
-
return {
|
|
50
|
-
line: lines.length - 1,
|
|
51
|
-
character: lines.at(-1)?.length ?? 0
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export async function runDefinitionReferencesTest(): Promise<void> {
|
|
56
|
-
const serverPath = path.resolve(__dirname, "../src/server.js");
|
|
57
|
-
const mainPath = path.resolve(
|
|
58
|
-
process.cwd(),
|
|
59
|
-
"test/fixtures/valid/merlin32-main-6502.
|
|
60
|
-
);
|
|
61
|
-
const mainUri = `file://${mainPath.replace(/\\/g, "/")}`;
|
|
62
|
-
const text = fs.readFileSync(mainPath, "utf8");
|
|
63
|
-
const child = spawn(process.execPath, [serverPath], {
|
|
64
|
-
stdio: ["pipe", "pipe", "pipe"]
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
let stdout = "";
|
|
68
|
-
let nextId = 1;
|
|
69
|
-
const pending = new Map<number, (message: JsonRpcMessage) => void>();
|
|
70
|
-
|
|
71
|
-
child.stdout.setEncoding("utf8");
|
|
72
|
-
child.stdout.on("data", (chunk: string) => {
|
|
73
|
-
stdout += chunk;
|
|
74
|
-
const decoded = decodeMessages(stdout);
|
|
75
|
-
stdout = decoded.rest;
|
|
76
|
-
|
|
77
|
-
for (const message of decoded.messages) {
|
|
78
|
-
if (message.id !== undefined) {
|
|
79
|
-
pending.get(message.id)?.(message);
|
|
80
|
-
pending.delete(message.id);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
function sendRequest(method: string, params: object): Promise<JsonRpcMessage> {
|
|
86
|
-
const id = nextId++;
|
|
87
|
-
child.stdin.write(
|
|
88
|
-
encodeMessage({
|
|
89
|
-
id,
|
|
90
|
-
jsonrpc: "2.0",
|
|
91
|
-
method,
|
|
92
|
-
params
|
|
93
|
-
})
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
return new Promise((resolve) => {
|
|
97
|
-
pending.set(id, resolve);
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function sendNotification(method: string, params: object): void {
|
|
102
|
-
child.stdin.write(
|
|
103
|
-
encodeMessage({
|
|
104
|
-
jsonrpc: "2.0",
|
|
105
|
-
method,
|
|
106
|
-
params
|
|
107
|
-
})
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
try {
|
|
112
|
-
await sendRequest("initialize", {
|
|
113
|
-
capabilities: {},
|
|
114
|
-
processId: process.pid,
|
|
115
|
-
rootUri: `file://${path.resolve(process.cwd()).replace(/\\/g, "/")}`
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
sendNotification("initialized", {});
|
|
119
|
-
sendNotification("textDocument/didOpen", {
|
|
120
|
-
textDocument: {
|
|
121
|
-
uri: mainUri,
|
|
122
|
-
languageId: "asm",
|
|
123
|
-
version: 1,
|
|
124
|
-
text
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
const definitionResponse = await sendRequest("textDocument/definition", {
|
|
129
|
-
textDocument: { uri: mainUri },
|
|
130
|
-
position: positionOf(text, "bpl GetKey")
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
const definition = definitionResponse.result as { uri: string; range: { start: { line: number } } };
|
|
134
|
-
assert.equal(definition.uri, mainUri);
|
|
135
|
-
assert.equal(definition.range.start.line, 69);
|
|
136
|
-
|
|
137
|
-
const referencesResponse = await sendRequest("textDocument/references", {
|
|
138
|
-
textDocument: { uri: mainUri },
|
|
139
|
-
position: positionOf(text, "GetKey ldx"),
|
|
140
|
-
context: {
|
|
141
|
-
includeDeclaration: true
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
const references = referencesResponse.result as Array<{ uri: string; range: { start: { line: number } } }>;
|
|
146
|
-
assert.equal(Array.isArray(references), true);
|
|
147
|
-
assert.equal(references.some((reference) => reference.uri === mainUri && reference.range.start.line === 69), true);
|
|
148
|
-
assert.equal(references.some((reference) => reference.uri === mainUri && reference.range.start.line === 70), true);
|
|
149
|
-
} finally {
|
|
150
|
-
child.kill();
|
|
151
|
-
}
|
|
152
|
-
}
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
|
|
6
|
+
type JsonRpcMessage = {
|
|
7
|
+
id?: number;
|
|
8
|
+
jsonrpc: "2.0";
|
|
9
|
+
result?: unknown;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
function encodeMessage(message: object): string {
|
|
13
|
+
const body = JSON.stringify(message);
|
|
14
|
+
return `Content-Length: ${Buffer.byteLength(body, "utf8")}\r\n\r\n${body}`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function decodeMessages(streamBuffer: string): { messages: JsonRpcMessage[]; rest: string } {
|
|
18
|
+
const messages: JsonRpcMessage[] = [];
|
|
19
|
+
let buffer = streamBuffer;
|
|
20
|
+
|
|
21
|
+
for (;;) {
|
|
22
|
+
const separator = buffer.indexOf("\r\n\r\n");
|
|
23
|
+
if (separator === -1) {
|
|
24
|
+
return { messages, rest: buffer };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const header = buffer.slice(0, separator);
|
|
28
|
+
const match = /Content-Length: (\d+)/i.exec(header);
|
|
29
|
+
if (!match) {
|
|
30
|
+
throw new Error(`Missing Content-Length header: ${header}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const length = Number(match[1]);
|
|
34
|
+
const body = buffer.slice(separator + 4);
|
|
35
|
+
if (Buffer.byteLength(body, "utf8") < length) {
|
|
36
|
+
return { messages, rest: buffer };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
messages.push(JSON.parse(body.slice(0, length)) as JsonRpcMessage);
|
|
40
|
+
buffer = body.slice(length);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function positionOf(text: string, needle: string): { line: number; character: number } {
|
|
45
|
+
const index = text.indexOf(needle);
|
|
46
|
+
assert.notEqual(index, -1, `expected to find ${needle}`);
|
|
47
|
+
const prefix = text.slice(0, index);
|
|
48
|
+
const lines = prefix.split("\n");
|
|
49
|
+
return {
|
|
50
|
+
line: lines.length - 1,
|
|
51
|
+
character: lines.at(-1)?.length ?? 0
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function runDefinitionReferencesTest(): Promise<void> {
|
|
56
|
+
const serverPath = path.resolve(__dirname, "../src/server.js");
|
|
57
|
+
const mainPath = path.resolve(
|
|
58
|
+
process.cwd(),
|
|
59
|
+
"test/fixtures/valid/merlin32-main-6502.S"
|
|
60
|
+
);
|
|
61
|
+
const mainUri = `file://${mainPath.replace(/\\/g, "/")}`;
|
|
62
|
+
const text = fs.readFileSync(mainPath, "utf8");
|
|
63
|
+
const child = spawn(process.execPath, [serverPath], {
|
|
64
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
let stdout = "";
|
|
68
|
+
let nextId = 1;
|
|
69
|
+
const pending = new Map<number, (message: JsonRpcMessage) => void>();
|
|
70
|
+
|
|
71
|
+
child.stdout.setEncoding("utf8");
|
|
72
|
+
child.stdout.on("data", (chunk: string) => {
|
|
73
|
+
stdout += chunk;
|
|
74
|
+
const decoded = decodeMessages(stdout);
|
|
75
|
+
stdout = decoded.rest;
|
|
76
|
+
|
|
77
|
+
for (const message of decoded.messages) {
|
|
78
|
+
if (message.id !== undefined) {
|
|
79
|
+
pending.get(message.id)?.(message);
|
|
80
|
+
pending.delete(message.id);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
function sendRequest(method: string, params: object): Promise<JsonRpcMessage> {
|
|
86
|
+
const id = nextId++;
|
|
87
|
+
child.stdin.write(
|
|
88
|
+
encodeMessage({
|
|
89
|
+
id,
|
|
90
|
+
jsonrpc: "2.0",
|
|
91
|
+
method,
|
|
92
|
+
params
|
|
93
|
+
})
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
return new Promise((resolve) => {
|
|
97
|
+
pending.set(id, resolve);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function sendNotification(method: string, params: object): void {
|
|
102
|
+
child.stdin.write(
|
|
103
|
+
encodeMessage({
|
|
104
|
+
jsonrpc: "2.0",
|
|
105
|
+
method,
|
|
106
|
+
params
|
|
107
|
+
})
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
await sendRequest("initialize", {
|
|
113
|
+
capabilities: {},
|
|
114
|
+
processId: process.pid,
|
|
115
|
+
rootUri: `file://${path.resolve(process.cwd()).replace(/\\/g, "/")}`
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
sendNotification("initialized", {});
|
|
119
|
+
sendNotification("textDocument/didOpen", {
|
|
120
|
+
textDocument: {
|
|
121
|
+
uri: mainUri,
|
|
122
|
+
languageId: "asm",
|
|
123
|
+
version: 1,
|
|
124
|
+
text
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const definitionResponse = await sendRequest("textDocument/definition", {
|
|
129
|
+
textDocument: { uri: mainUri },
|
|
130
|
+
position: positionOf(text, "bpl GetKey")
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const definition = definitionResponse.result as { uri: string; range: { start: { line: number } } };
|
|
134
|
+
assert.equal(definition.uri, mainUri);
|
|
135
|
+
assert.equal(definition.range.start.line, 69);
|
|
136
|
+
|
|
137
|
+
const referencesResponse = await sendRequest("textDocument/references", {
|
|
138
|
+
textDocument: { uri: mainUri },
|
|
139
|
+
position: positionOf(text, "GetKey ldx"),
|
|
140
|
+
context: {
|
|
141
|
+
includeDeclaration: true
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
const references = referencesResponse.result as Array<{ uri: string; range: { start: { line: number } } }>;
|
|
146
|
+
assert.equal(Array.isArray(references), true);
|
|
147
|
+
assert.equal(references.some((reference) => reference.uri === mainUri && reference.range.start.line === 69), true);
|
|
148
|
+
assert.equal(references.some((reference) => reference.uri === mainUri && reference.range.start.line === 70), true);
|
|
149
|
+
} finally {
|
|
150
|
+
child.kill();
|
|
151
|
+
}
|
|
152
|
+
}
|