Skip to content

Commit e505902

Browse files
committed
Fix request failed when there's a white space in the project path.
Fixes #78
1 parent adf7486 commit e505902

File tree

3 files changed

+106
-5
lines changed

3 files changed

+106
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## master
2+
3+
- Fix request failed when there's a white space in the project path.
4+
15
## 1.0.5
26

37
Features:

RescriptEditorSupport.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
import fs from "fs";
7+
8+
let binaryPath = path.join(
9+
path.dirname(__dirname),
10+
process.platform,
11+
"rescript-editor-support.exe"
12+
);
13+
14+
export let binaryExists = fs.existsSync(binaryPath);
15+
16+
let findExecutable = (uri: string) => {
17+
let filePath = fileURLToPath(uri);
18+
let projectRootPath = utils.findProjectRootOfFile(filePath);
19+
if (projectRootPath == null || !binaryExists) {
20+
return null;
21+
} else {
22+
return {
23+
binaryPathQuoted: '"' + binaryPath + '"', // path could have white space
24+
filePathQuoted: '"' + filePath + '"',
25+
cwd: projectRootPath,
26+
};
27+
}
28+
};
29+
30+
export function runDumpCommand(
31+
msg: RequestMessage,
32+
onResult: (
33+
result: { hover?: string; definition?: { uri?: string; range: any } } | null
34+
) => void
35+
) {
36+
let executable = findExecutable(msg.params.textDocument.uri);
37+
if (executable == null) {
38+
onResult(null);
39+
} else {
40+
let command =
41+
executable.binaryPathQuoted +
42+
" dump " +
43+
executable.filePathQuoted +
44+
":" +
45+
msg.params.position.line +
46+
":" +
47+
msg.params.position.character;
48+
exec(command, { cwd: executable.cwd }, function (_error, stdout, _stderr) {
49+
let result = JSON.parse(stdout);
50+
if (result && result[0]) {
51+
onResult(result[0]);
52+
} else {
53+
onResult(null);
54+
}
55+
});
56+
}
57+
}
58+
59+
export function runCompletionCommand(
60+
msg: RequestMessage,
61+
code: string,
62+
onResult: (result: [{ label: string }] | null) => void
63+
) {
64+
let executable = findExecutable(msg.params.textDocument.uri);
65+
if (executable == null) {
66+
onResult(null);
67+
} else {
68+
let tmpname = utils.createFileInTempDir();
69+
fs.writeFileSync(tmpname, code, { encoding: "utf-8" });
70+
71+
let command =
72+
executable.binaryPathQuoted +
73+
" complete " +
74+
executable.filePathQuoted +
75+
":" +
76+
msg.params.position.line +
77+
":" +
78+
msg.params.position.character +
79+
" " +
80+
tmpname;
81+
82+
exec(command, { cwd: executable.cwd }, function (_error, stdout, _stderr) {
83+
// async close is fine. We don't use this file name again
84+
fs.unlink(tmpname, () => null);
85+
let result = JSON.parse(stdout);
86+
if (result && result[0]) {
87+
onResult(result);
88+
} else {
89+
onResult(null);
90+
}
91+
});
92+
}
93+
}

server/src/RescriptEditorSupport.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ let findExecutable = (uri: string) => {
1919
if (projectRootPath == null || !binaryExists) {
2020
return null;
2121
} else {
22-
return { binaryPath, filePath, cwd: projectRootPath };
22+
return {
23+
binaryPathQuoted: '"' + binaryPath + '"', // path could have white space
24+
filePathQuoted: '"' + filePath + '"',
25+
cwd: projectRootPath,
26+
};
2327
}
2428
};
2529

@@ -34,9 +38,9 @@ export function runDumpCommand(
3438
onResult(null);
3539
} else {
3640
let command =
37-
executable.binaryPath +
41+
executable.binaryPathQuoted +
3842
" dump " +
39-
executable.filePath +
43+
executable.filePathQuoted +
4044
":" +
4145
msg.params.position.line +
4246
":" +
@@ -65,9 +69,9 @@ export function runCompletionCommand(
6569
fs.writeFileSync(tmpname, code, { encoding: "utf-8" });
6670

6771
let command =
68-
executable.binaryPath +
72+
executable.binaryPathQuoted +
6973
" complete " +
70-
executable.filePath +
74+
executable.filePathQuoted +
7175
":" +
7276
msg.params.position.line +
7377
":" +

0 commit comments

Comments
 (0)