Skip to content

Commit 31fd42e

Browse files
committed
Reason V4 [Stacked Diff 4/n #2619] [Make merlin and rtop respect latest syntax by default]
1 parent 3307d48 commit 31fd42e

File tree

5 files changed

+97
-41
lines changed

5 files changed

+97
-41
lines changed

src/reason-merlin/ocamlmerlin_reason.cppo.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ open Extend_protocol.Reader
33
let () =
44
Reason_config.recoverable := true
55

6+
(* Merlin integration will by default print types according to the package
7+
* version. The reason is that when printing, we don't have original source
8+
* files which include the version attribute. It is often just printing a
9+
* type segment *)
10+
(* Somehow putting print version = 3.8 up here impacts the *parse* behavior!
11+
* How? *)
612
module Reason_reader = struct
713
type t = buffer
814

@@ -44,6 +50,9 @@ module Reason_reader = struct
4450
fun () -> Lazy.force fmt
4551

4652
let pretty_print ppf =
53+
let print_version = Reason_version.latest_version_for_package in
54+
let () = Reason_version.print_version.major <- print_version.major in
55+
let () = Reason_version.print_version.minor <- print_version.minor in
4756
let open Reason_toolchain in
4857
function
4958
| Pretty_core_type x ->

src/reason-parser/reason_oprint.cppo.ml

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,14 @@ let parenthesize_if_neg ppf fmt v isneg =
163163

164164

165165
let print_out_value ppf tree =
166-
let rec print_tree_1 ppf =
167-
function
166+
let rec print_tree_1 ppf outcome =
167+
let tag =
168+
if Reason_version.print_supports Reason_version.HashVariantsColonMethodCallStarClassTypes then
169+
"#"
170+
else
171+
"`"
172+
in
173+
match outcome with
168174
(* for the next few cases, please see context at https://github.com/facebook/reason/pull/1516#issuecomment-337069150 *)
169175
| Oval_constr (name, [Oval_constr ((Oide_ident { printed_name = "()" }), [])]) ->
170176
(* for normal variants, but sugar Foo(()) to Foo() *)
@@ -177,10 +183,10 @@ let print_out_value ppf tree =
177183
(print_tree_list print_tree_1 ",") params
178184
| Oval_variant (name, Some (Oval_constr ((Oide_ident { printed_name = "()" }), []))) ->
179185
(* for polymorphic variants, but sugar `foo(()) to `foo() *)
180-
fprintf ppf "@[<2>`%s()@]" name
186+
fprintf ppf "@[<2>%s%s()@]" tag name
181187
| Oval_variant (name, Some param) ->
182188
(* for polymorphic variants *)
183-
fprintf ppf "@[<2>`%s(%a)@]" name print_constr_param param
189+
fprintf ppf "@[<2>%s%s(%a)@]" tag name print_constr_param param
184190
| tree -> print_simple_tree ppf tree
185191
and print_constr_param ppf = function
186192
| Oval_int i -> parenthesize_if_neg ppf "%i" i (i < 0)
@@ -206,7 +212,13 @@ let print_out_value ppf tree =
206212
| Oval_array tl ->
207213
fprintf ppf "@[<2>[|%a|]@]" (print_tree_list print_tree_1 ",") tl
208214
| Oval_constr (name, []) -> print_ident ppf name
209-
| Oval_variant (name, None) -> fprintf ppf "`%s" name
215+
| Oval_variant (name, None) ->
216+
let opn =
217+
if Reason_version.print_supports
218+
Reason_version.HashVariantsColonMethodCallStarClassTypes then "#"
219+
else "`"
220+
in
221+
fprintf ppf "%s%s" opn name
210222
| Oval_stuff s -> pp_print_string ppf s
211223
| Oval_record fel ->
212224
fprintf ppf "@[<1>{%a}@]" (cautious (print_fields true)) fel
@@ -249,8 +261,12 @@ let rec print_list pr sep ppf =
249261
| [a] -> pr ppf a
250262
| a :: l -> pr ppf a; sep ppf; print_list pr sep ppf l
251263

252-
let pr_present =
253-
print_list (fun ppf s -> fprintf ppf "`%s" s) (fun ppf -> fprintf ppf "@ ")
264+
let pr_present () =
265+
if Reason_version.print_supports
266+
Reason_version.HashVariantsColonMethodCallStarClassTypes then
267+
print_list (fun ppf s -> fprintf ppf "#%s" s) (fun ppf -> fprintf ppf "@ ")
268+
else
269+
print_list (fun ppf s -> fprintf ppf "`%s" s) (fun ppf -> fprintf ppf "@ ")
254270

255271
let pr_vars =
256272
print_list (fun ppf s -> fprintf ppf "'%s" s) (fun ppf -> fprintf ppf "@ ")
@@ -266,8 +282,7 @@ let get_label lbl =
266282
Optional (String.sub lbl 1 @@ String.length lbl - 1)
267283
else Labeled lbl
268284

269-
let rec print_out_type ppf =
270-
function
285+
let rec print_out_type ppf outcome = match outcome with
271286
| Otyp_alias (ty, s) ->
272287
fprintf ppf "@[%a@ as '%s@]" print_out_type ty s
273288
| Otyp_poly (sl, ty) ->
@@ -305,7 +320,7 @@ and print_out_type_1 ~uncurried ppf =
305320
in
306321
pp_open_box ppf 0;
307322
let (args, result) = collect_args [] x in
308-
let should_wrap_with_parens =
323+
let should_wrap =
309324
(* uncurried arguments are always wrapped in parens *)
310325
if uncurried then true
311326
else match args with
@@ -315,10 +330,15 @@ and print_out_type_1 ~uncurried ppf =
315330
| ["", _] -> false
316331
| _ -> true
317332
in
318-
if should_wrap_with_parens then pp_print_string ppf "(";
333+
334+
let opn, close =
335+
if Reason_version.print_supports AngleBracketTypes then "<", ">"
336+
else "(", ")"
337+
in
338+
if should_wrap then pp_print_string ppf opn;
319339
if uncurried then fprintf ppf ".@ ";
320340
print_list print_arg (fun ppf -> fprintf ppf ",@ ") ppf args;
321-
if should_wrap_with_parens then pp_print_string ppf ")";
341+
if should_wrap then pp_print_string ppf close;
322342

323343
pp_print_string ppf " =>";
324344
pp_print_space ppf ();
@@ -442,7 +462,7 @@ and print_simple_out_type ppf =
442462
let print_present ppf =
443463
function
444464
None | Some [] -> ()
445-
| Some l -> fprintf ppf "@;<1 -2>> @[<hov>%a@]" pr_present l
465+
| Some l -> fprintf ppf "@;<1 -2>> @[<hov>%a@]" (pr_present ()) l
446466
in
447467
let print_fields ppf =
448468
function
@@ -490,13 +510,20 @@ and print_row_field ppf (l, opt_amp, tyl) =
490510
let pr_of ppf =
491511
if opt_amp then fprintf ppf " &@ "
492512
else fprintf ppf "" in
513+
let tag =
514+
if Reason_version.print_supports Reason_version.HashVariantsColonMethodCallStarClassTypes then
515+
"#"
516+
else
517+
"`"
518+
in
493519
let parens = match tyl with
494520
| [ (Otyp_tuple _) ] -> false (* tuples already have parentheses *)
495521
(* [< `Ok(string & int) ] ----> string & int
496522
* [< `Ok(string) ] -----> string *)
497523
| _::_ -> true
498524
| _ -> false in
499-
fprintf ppf "@[<hv 2>`%s%t%s%a%s@]"
525+
fprintf ppf "@[<hv 2>%s%s%t%s%a%s@]"
526+
tag
500527
l
501528
pr_of
502529
(if parens then "(" else "")
@@ -516,19 +543,23 @@ and print_out_wrap_type ppf =
516543
| (Otyp_constr (_, _::_)) as ty ->
517544
print_out_type ppf ty
518545
| ty -> print_simple_out_type ppf ty
519-
and print_typargs ppf =
520-
function
521-
[] -> ()
546+
and print_typargs ppf args =
547+
let opn, close =
548+
if Reason_version.print_supports AngleBracketTypes then "<", ">"
549+
else "(", ")"
550+
in
551+
match args with
552+
| [] -> ()
522553
| [ty1] ->
523-
pp_print_string ppf "(";
554+
pp_print_string ppf opn;
524555
print_out_wrap_type ppf ty1;
525-
pp_print_string ppf ")"
556+
pp_print_string ppf close
526557
| tyl ->
527-
pp_print_string ppf "(";
558+
pp_print_string ppf opn;
528559
pp_open_box ppf 1;
529560
print_typlist print_out_wrap_type "," ppf tyl;
530561
pp_close_box ppf ();
531-
pp_print_string ppf ")"
562+
pp_print_string ppf close
532563

533564
let out_type = ref print_out_type
534565

@@ -736,14 +767,19 @@ and print_out_type_decl kwd ppf td =
736767
td.otype_cstrs
737768
in
738769
let type_defined ppf =
770+
let opn, close =
771+
if Reason_version.print_supports AngleBracketTypes then "<", ">" else "(", ")"
772+
in
739773
match td.otype_params with
740774
[] -> pp_print_string ppf td.otype_name
741-
| [param] -> fprintf ppf "@[%s(%a)@]" td.otype_name type_parameter param
775+
| [param] -> fprintf ppf "@[%s%s%a%s@]" td.otype_name opn type_parameter param close
742776
| _ ->
743-
fprintf ppf "@[%s(@[%a@])@]"
777+
fprintf ppf "@[%s%s@[%a@]%s@]"
744778
td.otype_name
779+
opn
745780
(print_list type_parameter (fun ppf -> fprintf ppf ",@ "))
746781
td.otype_params
782+
close
747783
in
748784
let print_manifest ppf =
749785
function

src/refmttype/reason_format_type.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
*)
77

88

9+
let () = Reason_pprint_ast.configure
10+
(* This can be made pluggable in the future. *)
11+
~width:80
12+
~assumeExplicitArity:false
13+
~constructorLists:[]
14+
15+
let print_version = Reason_version.latest_version_for_package
16+
let () = Reason_version.print_version.major <- print_version.major
17+
let () = Reason_version.print_version.minor <- print_version.minor
18+
919
(* No String.split in stdlib... *)
1020
let split str ~by =
1121
let rec split' str ~by accum =

src/refmttype/reason_type_of_ocaml_type.ml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55
* LICENSE file in the root directory of this source tree.
66
*)
77

8-
9-
let () = Reason_pprint_ast.configure
10-
(* This can be made pluggable in the future. *)
11-
~width:80
12-
~assumeExplicitArity:false
13-
~constructorLists:[]
14-
158
let reasonFormatter = Reason_pprint_ast.createFormatter ()
169

1710
(* "Why would you ever pass in some of these to print into Reason?"

src/rtop/rtop.ml

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,30 @@ let () = try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH") with | Not
44

55
let () = UTop.require ["reason.easy_format"; "reason";]
66

7+
let print_version = Reason_version.latest_version_for_package
8+
let () = Reason_version.cli_arg_parse_version.major <- print_version.major
9+
let () = Reason_version.cli_arg_parse_version.minor <- print_version.minor
10+
let () = Reason_version.print_version.major <- print_version.major
11+
let () = Reason_version.print_version.minor <- print_version.minor
12+
713
let () = Reason_toploop.main ()
814

915
let () = Reason_utop.init_reason ()
1016

1117
let () = print_string
12-
"
13-
___ _______ ________ _ __
14-
/ _ \\/ __/ _ | / __/ __ \\/ |/ /
15-
/ , _/ _// __ |_\\ \\/ /_/ / /
16-
/_/|_/___/_/ |_/___/\\____/_/|_/
18+
{|
19+
_ __ ___ __ _ ___ ___ _ __
20+
| '__/ _ \/ _` / __|/ _ \| '_ \
21+
| | | __/ (_| \__ \ (_) | | | |
22+
|_| \___|\__,_|___/\___/|_| |_|
23+
24+
(syntax version 3.8)
1725

18-
Execute statements/let bindings. Hit <enter> after the semicolon. Ctrl-d to quit.
26+
Semicolon submits statements. Ctrl-d to quit.
1927

20-
> let myVar = \"Hello Reason!\";
21-
> let myList: list(string) = [\"first\", \"second\"];
22-
> #use \"./src/myFile.re\"; /* loads the file into here */
23-
"
28+
> let myVar = "Hello Reason!";
29+
> let myList: list<string> = ["first", "second"];
30+
> #use "./src/myFile.re"; /* loads the file into here */
31+
|}
2432

25-
let () = UTop_main.main ()
33+
let () = UTop_main.main ()

0 commit comments

Comments
 (0)