|
1 | 1 | import { fileURLToPath } from "url";
|
2 | 2 | import { RequestMessage } from "vscode-languageserver";
|
3 |
| -import { CompletionItem } from "vscode-languageserver-protocol"; |
| 3 | +import { CompletionItem, Hover, Location } from "vscode-languageserver-protocol"; |
4 | 4 | import * as utils from "./utils";
|
5 | 5 | import * as path from "path";
|
6 | 6 | import { execFileSync } from "child_process";
|
7 | 7 | import fs from "fs";
|
8 | 8 |
|
9 |
| -let binaryPath = path.join( |
| 9 | +let binariesFolder = path.join( |
10 | 10 | path.dirname(__dirname),
|
11 |
| - process.platform, |
12 |
| - "rescript-editor-support.exe" |
| 11 | + "analysis_binaries" |
| 12 | +) |
| 13 | + |
| 14 | +// For local development and CI tests |
| 15 | +let currentPlatformBinaryPath = path.join( |
| 16 | + binariesFolder, |
| 17 | + "current-platform.exe" |
| 18 | +); |
| 19 | +// Platform-specific production binaries manually downloaded from CI |
| 20 | +let productionBinaryPath = path.join( |
| 21 | + binariesFolder, |
| 22 | + process.platform + ".exe" |
13 | 23 | );
|
14 | 24 |
|
15 |
| -export let binaryExists = fs.existsSync(binaryPath); |
| 25 | +let findBinary = () => { |
| 26 | + if (fs.existsSync(currentPlatformBinaryPath)) { |
| 27 | + return currentPlatformBinaryPath |
| 28 | + } else if (fs.existsSync(productionBinaryPath)) { |
| 29 | + return productionBinaryPath |
| 30 | + } else { |
| 31 | + return null |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// export let binaryExists = fs.existsSync(binaryPath); |
| 36 | + |
| 37 | +// let findExecutable = (uri: string) => { |
| 38 | +// let filePath = fileURLToPath(uri); |
| 39 | +// let projectRootPath = utils.findProjectRootOfFile(filePath); |
| 40 | +// if (projectRootPath == null || !binaryExists) { |
| 41 | +// return null; |
| 42 | +// } else { |
| 43 | +// return { |
| 44 | +// binaryPath: binaryPath, |
| 45 | +// filePath: filePath, |
| 46 | +// cwd: projectRootPath, |
| 47 | +// }; |
| 48 | +// } |
| 49 | +// }; |
| 50 | + |
| 51 | +// type dumpCommandResult = { |
| 52 | +// hover?: string; |
| 53 | +// definition?: { uri?: string; range: any }; |
| 54 | +// }; |
| 55 | +// export function runDumpCommand(msg: RequestMessage): dumpCommandResult | null { |
| 56 | +// let executable = findExecutable(msg.params.textDocument.uri); |
| 57 | +// if (executable == null) { |
| 58 | +// return null; |
| 59 | +// } |
| 60 | + |
| 61 | +// let command = |
| 62 | +// executable.filePath + |
| 63 | +// ":" + |
| 64 | +// msg.params.position.line + |
| 65 | +// ":" + |
| 66 | +// msg.params.position.character; |
| 67 | + |
| 68 | +// try { |
| 69 | +// let stdout = execFileSync(executable.binaryPath, ["dump", command], { |
| 70 | +// cwd: executable.cwd, |
| 71 | +// }); |
| 72 | +// let parsed = JSON.parse(stdout.toString()); |
| 73 | +// if (parsed && parsed[0]) { |
| 74 | +// return parsed[0]; |
| 75 | +// } else { |
| 76 | +// return null; |
| 77 | +// } |
| 78 | +// } catch (error) { |
| 79 | +// // TODO: @cristianoc any exception possible? |
| 80 | +// return null; |
| 81 | +// } |
| 82 | +// } |
16 | 83 |
|
17 |
| -let findExecutable = (uri: string) => { |
18 |
| - let filePath = fileURLToPath(uri); |
| 84 | +export function runCompletionCommand( |
| 85 | + msg: RequestMessage, |
| 86 | + code: string |
| 87 | +): CompletionItem[] | null { |
| 88 | + let filePath = fileURLToPath(msg.params.textDocument.uri) |
19 | 89 | let projectRootPath = utils.findProjectRootOfFile(filePath);
|
20 |
| - if (projectRootPath == null || !binaryExists) { |
| 90 | + let binaryPath = findBinary(); |
| 91 | + if (binaryPath == null || projectRootPath == null) { |
21 | 92 | return null;
|
22 |
| - } else { |
23 |
| - return { |
24 |
| - binaryPath: binaryPath, |
25 |
| - filePath: filePath, |
26 |
| - cwd: projectRootPath, |
27 |
| - }; |
28 | 93 | }
|
29 |
| -}; |
| 94 | + let tmpname = utils.createFileInTempDir(); |
| 95 | + fs.writeFileSync(tmpname, code, { encoding: "utf-8" }); |
30 | 96 |
|
31 |
| -type dumpCommandResult = { |
32 |
| - hover?: string; |
33 |
| - definition?: { uri?: string; range: any }; |
34 |
| -}; |
35 |
| -export function runDumpCommand(msg: RequestMessage): dumpCommandResult | null { |
36 |
| - let executable = findExecutable(msg.params.textDocument.uri); |
37 |
| - if (executable == null) { |
| 97 | + try { |
| 98 | + let stdout = execFileSync( |
| 99 | + binaryPath, |
| 100 | + ["complete", filePath, msg.params.position.line, msg.params.position.character, tmpname], |
| 101 | + { cwd: projectRootPath } |
| 102 | + ); |
| 103 | + return JSON.parse(stdout.toString()); |
| 104 | + } catch (error) { |
| 105 | + // TODO: @cristianoc any exception possible? |
38 | 106 | return null;
|
| 107 | + } finally { |
| 108 | + fs.unlink(tmpname, () => null); |
39 | 109 | }
|
| 110 | +} |
40 | 111 |
|
41 |
| - let command = |
42 |
| - executable.filePath + |
43 |
| - ":" + |
44 |
| - msg.params.position.line + |
45 |
| - ":" + |
46 |
| - msg.params.position.character; |
| 112 | +export function runHoverCommand( |
| 113 | + msg: RequestMessage, |
| 114 | +): Hover | null { |
| 115 | + let filePath = fileURLToPath(msg.params.textDocument.uri) |
| 116 | + let projectRootPath = utils.findProjectRootOfFile(filePath); |
| 117 | + let binaryPath = findBinary(); |
| 118 | + if (binaryPath == null || projectRootPath == null) { |
| 119 | + return null; |
| 120 | + } |
47 | 121 |
|
48 | 122 | try {
|
49 |
| - let stdout = execFileSync(executable.binaryPath, ["dump", command], { |
50 |
| - cwd: executable.cwd, |
51 |
| - }); |
52 |
| - let parsed = JSON.parse(stdout.toString()); |
53 |
| - if (parsed && parsed[0]) { |
54 |
| - return parsed[0]; |
55 |
| - } else { |
56 |
| - return null; |
57 |
| - } |
| 123 | + let stdout = execFileSync( |
| 124 | + binaryPath, |
| 125 | + ["hover", filePath, msg.params.position.line, msg.params.position.character], |
| 126 | + { cwd: projectRootPath } |
| 127 | + ); |
| 128 | + return JSON.parse(stdout.toString()); |
58 | 129 | } catch (error) {
|
59 | 130 | // TODO: @cristianoc any exception possible?
|
60 | 131 | return null;
|
61 | 132 | }
|
62 | 133 | }
|
63 | 134 |
|
64 |
| -// TODO: the result will never be null soon when the updated binary syncs |
65 |
| -export function runCompletionCommand( |
| 135 | +export function runDefinitionCommand( |
66 | 136 | msg: RequestMessage,
|
67 |
| - code: string |
68 |
| -): CompletionItem[] | null { |
69 |
| - let executable = findExecutable(msg.params.textDocument.uri); |
70 |
| - if (executable == null) { |
| 137 | +): Location | null { |
| 138 | + let filePath = fileURLToPath(msg.params.textDocument.uri) |
| 139 | + let projectRootPath = utils.findProjectRootOfFile(filePath); |
| 140 | + let binaryPath = findBinary(); |
| 141 | + if (binaryPath == null || projectRootPath == null) { |
71 | 142 | return null;
|
72 | 143 | }
|
73 |
| - let tmpname = utils.createFileInTempDir(); |
74 |
| - fs.writeFileSync(tmpname, code, { encoding: "utf-8" }); |
75 |
| - |
76 |
| - let command = |
77 |
| - executable.filePath + |
78 |
| - ":" + |
79 |
| - msg.params.position.line + |
80 |
| - ":" + |
81 |
| - msg.params.position.character; |
82 | 144 |
|
83 | 145 | try {
|
84 | 146 | let stdout = execFileSync(
|
85 |
| - executable.binaryPath, |
86 |
| - ["complete", command, tmpname], |
87 |
| - { cwd: executable.cwd } |
| 147 | + binaryPath, |
| 148 | + ["definition", filePath, msg.params.position.line, msg.params.position.character], |
| 149 | + { cwd: projectRootPath } |
88 | 150 | );
|
89 |
| - let parsed = JSON.parse(stdout.toString()); |
90 |
| - if (parsed && parsed[0]) { |
91 |
| - return parsed; |
92 |
| - } else { |
93 |
| - return null; |
94 |
| - } |
| 151 | + return JSON.parse(stdout.toString()); |
95 | 152 | } catch (error) {
|
96 | 153 | // TODO: @cristianoc any exception possible?
|
97 | 154 | return null;
|
98 |
| - } finally { |
99 |
| - fs.unlink(tmpname, () => null); |
100 | 155 | }
|
101 | 156 | }
|
0 commit comments