Skip to content

Commit a85adb1

Browse files
committed
Try to determine uncurried-always mode in the formatter.
Project config is not used in the formatter normally. This attempts a best-effort way to find bsconfig.json and see if the mode is set. The editor currently calls this on a temp file, so it tries to find bsconfig from the path of `bsc.exe` used, by looking outside node_modules. A more robust method should be provided later on, but this should be OK to start experimenting.
1 parent de7f6d5 commit a85adb1

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

res_syntax/src/res_multi_printer.ml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,55 @@
11
let defaultPrintWidth = 100
22

3+
(* Determine if the file is in uncurried-always mode by looking for
4+
the fist ancestor .bsconfig and see if it contains "uncurry": "default" *)
5+
let getUncurriedAlwaysFromBsconfig ~filename =
6+
let rec findBsconfig ~dir =
7+
let bsconfig = Filename.concat dir "bsconfig.json" in
8+
if Sys.file_exists bsconfig then Some (Res_io.readFile ~filename:bsconfig)
9+
else
10+
let parent = Filename.dirname dir in
11+
if parent = dir then None else findBsconfig ~dir:parent
12+
in
13+
let rec findFromNodeModules ~dir =
14+
let parent = Filename.dirname dir in
15+
if Filename.basename dir = "node_modules" then
16+
let bsconfig = Filename.concat parent "bsconfig.json" in
17+
if Sys.file_exists bsconfig then Some (Res_io.readFile ~filename:bsconfig)
18+
else None
19+
else if parent = dir then None
20+
else findFromNodeModules ~dir:parent
21+
in
22+
let dir =
23+
if Filename.is_relative filename then
24+
Filename.dirname (Filename.concat (Sys.getcwd ()) filename)
25+
else Filename.dirname filename
26+
in
27+
let bsconfig () =
28+
match findBsconfig ~dir with
29+
| None ->
30+
(* The editor calls format on a temporary file. So bsconfig can't be found.
31+
This looks outside the node_modules containing the bsc binary *)
32+
let dir = Filename.dirname Sys.argv.(0) in
33+
findFromNodeModules ~dir
34+
| x -> x
35+
in
36+
match bsconfig () with
37+
| exception _ -> ()
38+
| None -> ()
39+
| Some bsconfig ->
40+
let lines = bsconfig |> String.split_on_char '\n' in
41+
let uncurriedAlways =
42+
lines
43+
|> List.exists (fun line ->
44+
let words = line |> String.split_on_char '\"' in
45+
words |> List.exists (fun word -> word = "uncurried")
46+
&& words |> List.exists (fun word -> word = "always"))
47+
in
48+
if uncurriedAlways then Res_uncurried.init := Always
49+
350
(* print res files to res syntax *)
451
let printRes ~ignoreParseErrors ~isInterface ~filename =
52+
getUncurriedAlwaysFromBsconfig ~filename;
553
if isInterface then (
654
let parseResult =
755
Res_driver.parsingEngine.parseInterface ~forPrinter:true ~filename

0 commit comments

Comments
 (0)