|
// Asks `tsc --lsp` for the definition and the source definition of `greet` in |
|
// main.foo (content-mapped) and in control.ts (same text). The TypeScript 7 VS Code |
|
// extension sends `custom/textDocument/sourceDefinition` for Go to Source Definition. |
|
import { spawn } from "node:child_process"; |
|
import { readFileSync } from "node:fs"; |
|
import { fileURLToPath, pathToFileURL } from "node:url"; |
|
|
|
const root = fileURLToPath(new URL(".", import.meta.url)); |
|
const tsc = fileURLToPath(new URL("./node_modules/typescript/bin/tsc", import.meta.url)); |
|
const server = spawn(process.execPath, [tsc, "--lsp", "--stdio"], { cwd: root, stdio: ["pipe", "pipe", "inherit"] }); |
|
|
|
let nextId = 1; |
|
const pending = new Map(); |
|
let buffered = Buffer.alloc(0); |
|
const send = (message) => { |
|
const body = Buffer.from(JSON.stringify({ jsonrpc: "2.0", ...message })); |
|
server.stdin.write(`Content-Length: ${body.length}\r\n\r\n`); |
|
server.stdin.write(body); |
|
}; |
|
const request = (method, params) => |
|
new Promise((resolve) => { |
|
const id = nextId++; |
|
pending.set(id, resolve); |
|
send({ id, method, params }); |
|
}); |
|
server.stdout.on("data", (chunk) => { |
|
buffered = Buffer.concat([buffered, chunk]); |
|
while (true) { |
|
const headerEnd = buffered.indexOf("\r\n\r\n"); |
|
if (headerEnd < 0) return; |
|
const length = Number(/Content-Length: (\d+)/i.exec(buffered.subarray(0, headerEnd).toString())[1]); |
|
if (buffered.length < headerEnd + 4 + length) return; |
|
const message = JSON.parse(buffered.subarray(headerEnd + 4, headerEnd + 4 + length).toString()); |
|
buffered = buffered.subarray(headerEnd + 4 + length); |
|
if (message.method && message.id !== undefined) { |
|
// Server-to-client requests: configuration, capability registration, progress. |
|
const result = message.method === "workspace/configuration" ? message.params.items.map(() => null) : null; |
|
send({ id: message.id, result }); |
|
} else if (message.id !== undefined && pending.has(message.id)) { |
|
pending.get(message.id)(message.result ?? message.error); |
|
pending.delete(message.id); |
|
} |
|
} |
|
}); |
|
|
|
const uri = (file) => pathToFileURL(`${root}${file}`).href; |
|
await request("initialize", { |
|
processId: process.pid, |
|
rootUri: pathToFileURL(root).href, |
|
workspaceFolders: [{ uri: pathToFileURL(root).href, name: "repro" }], |
|
capabilities: { |
|
workspace: { configuration: true, workspaceFolders: true }, |
|
textDocument: { |
|
synchronization: { dynamicRegistration: true }, |
|
diagnostic: { dynamicRegistration: true }, |
|
}, |
|
}, |
|
initializationOptions: { runExternalCode: true }, |
|
}); |
|
send({ method: "initialized", params: {} }); |
|
|
|
const show = (result) => |
|
(Array.isArray(result) ? result : result ? [result] : []) |
|
.map((l) => { |
|
const range = l.range ?? l.targetSelectionRange; |
|
return `${(l.uri ?? l.targetUri).slice(pathToFileURL(root).href.length)}:${range.start.line + 1}:${range.start.character + 1}`; |
|
}) |
|
.join(", ") || JSON.stringify(result); |
|
|
|
for (const [file, languageId] of [["main.foo", "plaintext"], ["control.ts", "typescript"]]) { |
|
const text = readFileSync(`${root}${file}`, "utf8"); |
|
send({ method: "textDocument/didOpen", params: { textDocument: { uri: uri(file), languageId, version: 1, text } } }); |
|
// Wait until the project (and the mapper) has loaded the file. |
|
await request("textDocument/diagnostic", { textDocument: { uri: uri(file) } }); |
|
const line = text.split("\n").findIndex((l) => l.includes("greet(")); |
|
const params = { textDocument: { uri: uri(file) }, position: { line, character: text.split("\n")[line].indexOf("greet(") + 1 } }; |
|
console.log(`${file} definition: ${show(await request("textDocument/definition", params))}`); |
|
console.log(`${file} source definition: ${show(await request("custom/textDocument/sourceDefinition", params))}`); |
|
} |
|
|
|
await request("shutdown"); |
|
send({ method: "exit" }); |