Skip to content

Commit a27a625

Browse files
committed
runDumpCommand
1 parent 7306239 commit a27a625

File tree

2 files changed

+70
-13
lines changed

2 files changed

+70
-13
lines changed

server/src/dumpCommand.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { fileURLToPath } from "url";
2+
import { RequestMessage } from "vscode-languageserver";
3+
import * as utils from "./utils";
4+
import * as path from "path";
5+
import { exec } from "child_process";
6+
7+
export function runDumpCommand(
8+
msg: RequestMessage,
9+
onResult: (
10+
result: { hover?: string; definition?: { uri?: string; range: any } } | null
11+
) => void
12+
) {
13+
let filePath = fileURLToPath(msg.params.textDocument.uri);
14+
let projectRootPath = utils.findProjectRootOfFile(filePath);
15+
if (projectRootPath == null) {
16+
onResult(null);
17+
} else {
18+
// Currently assumes the dump command is "bin.exe" at the project root.
19+
let commandPath = path.join(projectRootPath, "bin.exe");
20+
let command =
21+
commandPath +
22+
" dump " +
23+
filePath +
24+
":" +
25+
msg.params.position.line +
26+
":" +
27+
msg.params.position.character;
28+
exec(command, { cwd: projectRootPath }, function (_error, _stdout, stderr) {
29+
let result = JSON.parse(stderr);
30+
if (result && result[0]) {
31+
onResult(result[0]);
32+
} else {
33+
onResult(null);
34+
}
35+
});
36+
}
37+
}

server/src/server.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import * as chokidar from "chokidar";
2020
import { assert } from "console";
2121
import { fileURLToPath } from "url";
2222
import { ChildProcess } from "child_process";
23+
import { runDumpCommand } from "./dumpCommand";
24+
2325

2426
// https://microsoft.github.io/language-server-protocol/specification#initialize
2527
// According to the spec, there could be requests before the 'initialize' request. Link in comment tells how to handle them.
@@ -313,32 +315,50 @@ process.on("message", (msg: m.Message) => {
313315
process.send!(response);
314316
}
315317
} else if (msg.method === p.HoverRequest.method) {
316-
let dummyHoverResponse: m.ResponseMessage = {
318+
let emptyHoverResponse: m.ResponseMessage = {
317319
jsonrpc: c.jsonrpcVersion,
318320
id: msg.id,
319321
// type result = Hover | null
320322
// type Hover = {contents: MarkedString | MarkedString[] | MarkupContent, range?: Range}
321-
result: { contents: "Time to go for a 20k run!" },
323+
result: null,
322324
};
323-
324-
process.send!(dummyHoverResponse);
325+
runDumpCommand(msg, (result) => {
326+
if (result && result.hover) {
327+
let hoverResponse: m.ResponseMessage = {
328+
...emptyHoverResponse,
329+
result: {
330+
contents: result.hover,
331+
},
332+
};
333+
process.send!(hoverResponse);
334+
} else {
335+
process.send!(emptyHoverResponse);
336+
}
337+
});
325338
} else if (msg.method === p.DefinitionRequest.method) {
326339
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition
327-
let dummyDefinitionResponse: m.ResponseMessage = {
340+
let emptyDefinitionResponse: m.ResponseMessage = {
328341
jsonrpc: c.jsonrpcVersion,
329342
id: msg.id,
330343
// result should be: Location | Array<Location> | Array<LocationLink> | null
331-
result: {
332-
uri: msg.params.textDocument.uri,
333-
range: {
334-
start: { line: 2, character: 4 },
335-
end: { line: 2, character: 12 },
336-
},
337-
},
344+
result: null,
338345
// error: code and message set in case an exception happens during the definition request.
339346
};
340347

341-
process.send!(dummyDefinitionResponse);
348+
runDumpCommand(msg, (result) => {
349+
if (result && result.definition) {
350+
let definitionResponse: m.ResponseMessage = {
351+
...emptyDefinitionResponse,
352+
result: {
353+
uri: result.definition.uri || msg.params.textDocument.uri,
354+
range: result.definition.range,
355+
},
356+
};
357+
process.send!(definitionResponse);
358+
} else {
359+
process.send!(emptyDefinitionResponse);
360+
}
361+
});
342362
} else if (msg.method === p.DocumentFormattingRequest.method) {
343363
// technically, a formatting failure should reply with the error. Sadly
344364
// the LSP alert box for these error replies sucks (e.g. doesn't actually

0 commit comments

Comments
 (0)