Skip to content

Commit 3c6c580

Browse files
committed
check for parse errors and do not format if errors are found
1 parent 8b288aa commit 3c6c580

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

analysis/src/Commands.ml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,20 @@ let rename ~path ~line ~col ~newName =
212212

213213
let format ~path =
214214
if Filename.check_suffix path ".res" then
215-
let {Res_driver.parsetree = structure; comments} =
215+
let {Res_driver.parsetree = structure; comments; diagnostics} =
216216
Res_driver.parsingEngine.parseImplementation ~forPrinter:true
217217
~filename:path
218218
in
219-
Res_printer.printImplementation !Res_cli.ResClflags.width structure comments
219+
if List.length diagnostics > 0 then ""
220+
else
221+
Res_printer.printImplementation !Res_cli.ResClflags.width structure
222+
comments
220223
else if Filename.check_suffix path ".resi" then
221-
let {Res_driver.parsetree = signature; comments} =
224+
let {Res_driver.parsetree = signature; comments; diagnostics} =
222225
Res_driver.parsingEngine.parseInterface ~forPrinter:true ~filename:path
223226
in
224-
Res_printer.printInterface !Res_cli.ResClflags.width signature comments
227+
if List.length diagnostics > 0 then ""
228+
else Res_printer.printInterface !Res_cli.ResClflags.width signature comments
225229
else ""
226230

227231
let test ~path =

server/src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ function format(msg: p.RequestMessage): Array<m.Message> {
466466

467467
// code will always be defined here, even though technically it can be undefined
468468
let code = getOpenedFileContent(params.textDocument.uri);
469-
let formattedResult = utils.formatUsingValidBscNativePath(
469+
let formattedResult = utils.formatCode(
470470
code,
471471
bscNativePath,
472472
extension === c.resiExt

server/src/utils.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ type execResult =
9393
kind: "error";
9494
error: string;
9595
};
96-
export let formatUsingValidBscNativePath = (
96+
export let formatCode = (
9797
code: string,
9898
bscNativePath: p.DocumentUri | null,
9999
isInterface: boolean
@@ -124,6 +124,16 @@ export let formatUsingValidBscNativePath = (
124124
false
125125
);
126126

127+
// The formatter returning an empty string means it couldn't format the
128+
// sources, probably because of errors. In that case, we bail from
129+
// formatting by returning the unformatted content.
130+
if (result === "") {
131+
return {
132+
kind: "success",
133+
result: code,
134+
};
135+
}
136+
127137
return {
128138
kind: "success",
129139
result,

0 commit comments

Comments
 (0)