Skip to content

Commit 7306239

Browse files
committed
Format
1 parent cc81e91 commit 7306239

File tree

2 files changed

+128
-101
lines changed

2 files changed

+128
-101
lines changed

server/src/constants.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
import * as path from 'path';
1+
import * as path from "path";
22

33
// See https://microsoft.github.io/language-server-protocol/specification Abstract Message
44
// version is fixed to 2.0
5-
export let jsonrpcVersion = '2.0';
6-
export let bscPartialPath = path.join('node_modules', 'bs-platform', process.platform, 'bsc.exe');
5+
export let jsonrpcVersion = "2.0";
6+
export let bscPartialPath = path.join(
7+
"node_modules",
8+
"bs-platform",
9+
process.platform,
10+
"bsc.exe"
11+
);
712
// can't use the native bsb since we might need the watcher -w flag, which is only in the js wrapper
813
// export let bsbPartialPath = path.join('node_modules', 'bs-platform', process.platform, 'bsb.exe');
9-
export let bsbPartialPath = path.join('node_modules', '.bin', 'bsb');
10-
export let bsbLock = '.bsb.lock';
11-
export let bsconfigPartialPath = 'bsconfig.json';
12-
export let compilerLogPartialPath = path.join('lib', 'bs', '.compiler.log');
13-
export let resExt = '.res';
14-
export let resiExt = '.resi';
15-
export let startBuildAction = 'Start Build';
14+
export let bsbPartialPath = path.join("node_modules", ".bin", "bsb");
15+
export let bsbLock = ".bsb.lock";
16+
export let bsconfigPartialPath = "bsconfig.json";
17+
export let compilerLogPartialPath = path.join("lib", "bs", ".compiler.log");
18+
export let resExt = ".res";
19+
export let resiExt = ".resi";
20+
export let startBuildAction = "Start Build";

server/src/utils.ts

Lines changed: 113 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,75 @@
1-
import { Range } from 'vscode-languageserver-textdocument';
2-
import * as c from './constants';
3-
import * as childProcess from 'child_process';
1+
import { Range } from "vscode-languageserver-textdocument";
2+
import * as c from "./constants";
3+
import * as childProcess from "child_process";
44
import * as p from "vscode-languageserver-protocol";
5-
import * as path from 'path';
5+
import * as path from "path";
66
import * as t from "vscode-languageserver-types";
7-
import * as tmp from 'tmp';
8-
import fs from 'fs';
9-
import { report } from 'process';
7+
import * as tmp from "tmp";
8+
import fs from "fs";
109

11-
// TODO: races here
10+
// TODO: races here?
1211
// TODO: this doesn't handle file:/// scheme
13-
export let findProjectRootOfFile = (source: p.DocumentUri): null | p.DocumentUri => {
14-
let dir = path.dirname(source)
12+
export let findProjectRootOfFile = (
13+
source: p.DocumentUri
14+
): null | p.DocumentUri => {
15+
let dir = path.dirname(source);
1516
if (fs.existsSync(path.join(dir, c.bsconfigPartialPath))) {
16-
return dir
17+
return dir;
1718
} else {
1819
if (dir === source) {
1920
// reached top
20-
return null
21+
return null;
2122
} else {
22-
return findProjectRootOfFile(dir)
23+
return findProjectRootOfFile(dir);
2324
}
2425
}
25-
}
26-
27-
type execResult = {
28-
kind: 'success',
29-
result: string
30-
} | {
31-
kind: 'error'
32-
error: string,
3326
};
34-
export let formatUsingValidBscPath = (code: string, bscPath: p.DocumentUri, isInterface: boolean): execResult => {
27+
28+
type execResult =
29+
| {
30+
kind: "success";
31+
result: string;
32+
}
33+
| {
34+
kind: "error";
35+
error: string;
36+
};
37+
export let formatUsingValidBscPath = (
38+
code: string,
39+
bscPath: p.DocumentUri,
40+
isInterface: boolean
41+
): execResult => {
3542
// library cleans up after itself. No need to manually remove temp file
3643
let tmpobj = tmp.fileSync();
3744
let extension = isInterface ? c.resiExt : c.resExt;
3845
let fileToFormat = tmpobj.name + extension;
39-
fs.writeFileSync(fileToFormat, code, { encoding: 'utf-8' });
46+
fs.writeFileSync(fileToFormat, code, { encoding: "utf-8" });
4047
try {
41-
let result = childProcess.execFileSync(bscPath, ['-color', 'never', '-format', fileToFormat], { stdio: 'pipe' })
48+
let result = childProcess.execFileSync(
49+
bscPath,
50+
["-color", "never", "-format", fileToFormat],
51+
{ stdio: "pipe" }
52+
);
4253
return {
43-
kind: 'success',
54+
kind: "success",
4455
result: result.toString(),
45-
}
56+
};
4657
} catch (e) {
4758
return {
48-
kind: 'error',
59+
kind: "error",
4960
error: e.message,
50-
}
61+
};
5162
}
52-
}
63+
};
5364

54-
export let runBsbWatcherUsingValidBsbPath = (bsbPath: p.DocumentUri, projectRootPath: p.DocumentUri) => {
55-
let process = childProcess.execFile(bsbPath, ['-w'], { cwd: projectRootPath })
56-
return process
65+
export let runBsbWatcherUsingValidBsbPath = (
66+
bsbPath: p.DocumentUri,
67+
projectRootPath: p.DocumentUri
68+
) => {
69+
let process = childProcess.execFile(bsbPath, ["-w"], {
70+
cwd: projectRootPath,
71+
});
72+
return process;
5773
// try {
5874
// let result = childProcess.execFileSync(bsbPath, [], { stdio: 'pipe', cwd: projectRootPath })
5975
// return {
@@ -66,7 +82,7 @@ export let runBsbWatcherUsingValidBsbPath = (bsbPath: p.DocumentUri, projectRoot
6682
// error: e.message,
6783
// }
6884
// }
69-
}
85+
};
7086

7187
export let parseDiagnosticLocation = (location: string): Range => {
7288
// example output location:
@@ -76,34 +92,39 @@ export let parseDiagnosticLocation = (location: string): Range => {
7692

7793
// language-server position is 0-based. Ours is 1-based. Don't forget to convert
7894
// also, our end character is inclusive. Language-server's is exclusive
79-
let isRange = location.indexOf('-') >= 0
95+
let isRange = location.indexOf("-") >= 0;
8096
if (isRange) {
81-
let [from, to] = location.split('-')
82-
let [fromLine, fromChar] = from.split(':')
83-
let isSingleLine = to.indexOf(':') >= 0
84-
let [toLine, toChar] = isSingleLine ? to.split(':') : [fromLine, to]
97+
let [from, to] = location.split("-");
98+
let [fromLine, fromChar] = from.split(":");
99+
let isSingleLine = to.indexOf(":") >= 0;
100+
let [toLine, toChar] = isSingleLine ? to.split(":") : [fromLine, to];
85101
return {
86-
start: { line: parseInt(fromLine) - 1, character: parseInt(fromChar) - 1 },
102+
start: {
103+
line: parseInt(fromLine) - 1,
104+
character: parseInt(fromChar) - 1,
105+
},
87106
end: { line: parseInt(toLine) - 1, character: parseInt(toChar) },
88-
}
107+
};
89108
} else {
90-
let [line, char] = location.split(':')
91-
let start = { line: parseInt(line) - 1, character: parseInt(char) }
109+
let [line, char] = location.split(":");
110+
let start = { line: parseInt(line) - 1, character: parseInt(char) };
92111
return {
93112
start: start,
94113
end: start,
95-
}
114+
};
96115
}
97-
}
116+
};
98117

99118
type filesDiagnostics = {
100119
[key: string]: p.Diagnostic[];
101-
}
120+
};
102121
type parsedCompilerLogResult = {
103-
done: boolean,
104-
result: filesDiagnostics,
105-
}
106-
export let parseCompilerLogOutput = (content: string): parsedCompilerLogResult => {
122+
done: boolean;
123+
result: filesDiagnostics;
124+
};
125+
export let parseCompilerLogOutput = (
126+
content: string
127+
): parsedCompilerLogResult => {
107128
/* example .compiler.log file content that we're gonna parse:
108129
109130
#Start(1600519680823)
@@ -145,27 +166,27 @@ export let parseCompilerLogOutput = (content: string): parsedCompilerLogResult =
145166
*/
146167

147168
type parsedDiagnostic = {
148-
code: number | undefined,
149-
severity: t.DiagnosticSeverity,
150-
tag: t.DiagnosticTag | undefined,
151-
content: string[]
152-
}
169+
code: number | undefined;
170+
severity: t.DiagnosticSeverity;
171+
tag: t.DiagnosticTag | undefined;
172+
content: string[];
173+
};
153174
let parsedDiagnostics: parsedDiagnostic[] = [];
154-
let lines = content.split('\n');
175+
let lines = content.split("\n");
155176
let done = false;
156177

157178
for (let i = 0; i < lines.length; i++) {
158179
let line = lines[i];
159-
if (line.startsWith(' We\'ve found a bug for you!')) {
180+
if (line.startsWith(" We've found a bug for you!")) {
160181
parsedDiagnostics.push({
161182
code: undefined,
162183
severity: t.DiagnosticSeverity.Error,
163184
tag: undefined,
164-
content: []
165-
})
166-
} else if (line.startsWith(' Warning number ')) {
167-
let warningNumber = parseInt(line.slice(' Warning number '.length))
168-
let tag: t.DiagnosticTag | undefined = undefined
185+
content: [],
186+
});
187+
} else if (line.startsWith(" Warning number ")) {
188+
let warningNumber = parseInt(line.slice(" Warning number ".length));
189+
let tag: t.DiagnosticTag | undefined = undefined;
169190
switch (warningNumber) {
170191
case 11:
171192
case 20:
@@ -183,60 +204,61 @@ export let parseCompilerLogOutput = (content: string): parsedCompilerLogResult =
183204
case 66:
184205
case 67:
185206
case 101:
186-
tag = t.DiagnosticTag.Unnecessary
207+
tag = t.DiagnosticTag.Unnecessary;
187208
break;
188209
case 3:
189-
tag = t.DiagnosticTag.Deprecated
210+
tag = t.DiagnosticTag.Deprecated;
190211
break;
191212
}
192213
parsedDiagnostics.push({
193214
code: Number.isNaN(warningNumber) ? undefined : warningNumber,
194215
severity: t.DiagnosticSeverity.Warning,
195216
tag: tag,
196-
content: []
197-
})
198-
} else if (line.startsWith(' Syntax error!')) {
217+
content: [],
218+
});
219+
} else if (line.startsWith(" Syntax error!")) {
199220
parsedDiagnostics.push({
200221
code: undefined,
201222
severity: t.DiagnosticSeverity.Error,
202223
tag: undefined,
203-
content: []
204-
})
205-
} else if (line.startsWith('#Done(')) {
206-
done = true
224+
content: [],
225+
});
226+
} else if (line.startsWith("#Done(")) {
227+
done = true;
207228
} else if (/^ +[0-9]+ /.test(line)) {
208229
// code display. Swallow
209-
} else if (line.startsWith(' ')) {
210-
parsedDiagnostics[parsedDiagnostics.length - 1].content.push(line)
230+
} else if (line.startsWith(" ")) {
231+
parsedDiagnostics[parsedDiagnostics.length - 1].content.push(line);
211232
}
212233
}
213234

214-
let result: filesDiagnostics = {}
215-
parsedDiagnostics.forEach(parsedDiagnostic => {
216-
let [fileAndLocation, ...diagnosticMessage] = parsedDiagnostic.content
217-
let locationSeparator = fileAndLocation.indexOf(':')
218-
let file = fileAndLocation.substring(2, locationSeparator)
219-
let location = fileAndLocation.substring(locationSeparator + 1)
235+
let result: filesDiagnostics = {};
236+
parsedDiagnostics.forEach((parsedDiagnostic) => {
237+
let [fileAndLocation, ...diagnosticMessage] = parsedDiagnostic.content;
238+
let locationSeparator = fileAndLocation.indexOf(":");
239+
let file = fileAndLocation.substring(2, locationSeparator);
240+
let location = fileAndLocation.substring(locationSeparator + 1);
220241
if (result[file] == null) {
221-
result[file] = []
242+
result[file] = [];
222243
}
223-
let cleanedUpDiagnostic = diagnosticMessage
224-
.map(line => {
225-
// remove the spaces in front
226-
return line.slice(2)
227-
})
228-
.join('\n')
229-
// remove start and end whitespaces/newlines
230-
.trim() + '\n';
244+
let cleanedUpDiagnostic =
245+
diagnosticMessage
246+
.map((line) => {
247+
// remove the spaces in front
248+
return line.slice(2);
249+
})
250+
.join("\n")
251+
// remove start and end whitespaces/newlines
252+
.trim() + "\n";
231253
result[file].push({
232254
severity: parsedDiagnostic.severity,
233255
tags: parsedDiagnostic.tag === undefined ? [] : [parsedDiagnostic.tag],
234256
code: parsedDiagnostic.code,
235257
range: parseDiagnosticLocation(location),
236258
source: "ReScript",
237259
message: cleanedUpDiagnostic,
238-
})
239-
})
260+
});
261+
});
240262

241-
return { done, result }
242-
}
263+
return { done, result };
264+
};

0 commit comments

Comments
 (0)