|
1 | 1 | let defaultPrintWidth = 100
|
2 | 2 |
|
| 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 | + |
3 | 50 | (* print res files to res syntax *)
|
4 | 51 | let printRes ~ignoreParseErrors ~isInterface ~filename =
|
| 52 | + getUncurriedAlwaysFromBsconfig ~filename; |
5 | 53 | if isInterface then (
|
6 | 54 | let parseResult =
|
7 | 55 | Res_driver.parsingEngine.parseInterface ~forPrinter:true ~filename
|
|
0 commit comments