Skip to content

Commit

Permalink
Improve functor printing. (#2683)
Browse files Browse the repository at this point in the history
* Improve empty functor printing.

* Improve functor application without args printing

* Improve printing

* Remove parentheses around functor argument.

* Add history entry

* Trigger Build

* add test results

* move check to makeList

* change history entry

* Update src/reason-parser/reason_pprint_ast.ml

---------

Co-authored-by: Antonio Nuno Monteiro <anmonteiro@gmail.com>
  • Loading branch information
SanderSpies and anmonteiro authored Apr 19, 2023
1 parent 2dd5bbb commit 82dd9aa
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 26 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 3.9 (unreleased)

- Reduce the amount of parentheses around functor usage. [#2683](https://github.com/reasonml/reason/pull/2683)
- Print module type body on separate line (@SanderSpies) [#2709](https://github.com/reasonml/reason/pull/2709)
- Fix missing patterns around contraint pattern (a pattern with a type annotation).
- Fix top level extension printing
Expand Down
15 changes: 10 additions & 5 deletions src/reason-parser/reason_pprint_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ let makeList
let config =
{ Layout.
listConfigIfCommentsInterleaved; listConfigIfEolCommentsInterleaved;
break; wrap; inline; sep; indent; sepLeft; preSpace; postSpace; pad;
break = if lst = [] then Layout.IfNeed else break; wrap; inline; sep; indent; sepLeft; preSpace; postSpace; pad;
}
in
Layout.Sequence (config, lst)
Expand Down Expand Up @@ -2942,7 +2942,7 @@ let printer = object(self:'self)
| (Ptype_variant lst, scope, None) -> [
privatize scope
[makeList
~break:(if lst == [] then IfNeed else Always_rec)
~break:Always_rec
~postSpace:true
~inline:(true, true)
(self#type_variant_list lst)]
Expand Down Expand Up @@ -5060,7 +5060,7 @@ let printer = object(self:'self)
| { pmod_desc = Pmod_functor(fp, me2) } ->
let firstOne =
match fp with
| Unit -> atom "()"
| Unit -> atom ""
| Named (s, mt') ->
let s = moduleIdent s in
self#module_type (makeList [atom s; atom ":"]) mt'
Expand Down Expand Up @@ -7541,7 +7541,12 @@ let printer = object(self:'self)
in
formatPrecedence (self#module_type letPattern mt)
| Pmod_structure s ->
let wrap = if hug then ("({", "})") else ("{", "}") in
let wrap = if hug then
if s = [] then
("(", ")")
else
("({", "})")
else ("{", "}") in
let items =
groupAndPrint
~xf:self#structure_item
Expand All @@ -7558,7 +7563,7 @@ let printer = object(self:'self)
items
| _ ->
(* For example, functor application will be wrapped. *)
formatPrecedence (self#module_expr x)
formatPrecedence ~wrap:("", "") (self#module_expr x)
method module_expr x =
match x.pmod_desc with
Expand Down
4 changes: 1 addition & 3 deletions test/extensions.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ Format extensions

[%extend open M];

[%extend
module M = {}
];
[%extend module M = {}];

[%extend module type M = {}];

Expand Down
14 changes: 13 additions & 1 deletion test/modules.t/input.re
Original file line number Diff line number Diff line change
Expand Up @@ -487,4 +487,16 @@ module type Event = (module type of {
include (Version2: (module type of Version2));

/* https://github.com/facebook/reason/issues/2608 */
module Functor = (()): (module type of {}) => {};
module Functor = (): (module type of {}) => {};

module Lola1 = () => {
let a = 3;
}

module Lola2 = (C: Cat, D: Dog, L: Lion) => {
let a = 33;
}

module L = Lola1();

module L2 = Lola2(Cat, Dog, Foo);
25 changes: 15 additions & 10 deletions test/modules.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,7 @@ Format modules
);

module ResultFromNonSimpleFunctorArg =
CurriedNoSugar(
(
MakeAModule({})
),
BMod,
);
CurriedNoSugar(MakeAModule(), BMod);

/* TODO: Functor type signatures should more resemble value signatures */
let curriedFunc: (int, int) => int =
Expand Down Expand Up @@ -451,7 +446,7 @@ Format modules
G: (Type) => Type,
X: Type,
) =>
F((G(X)));
F(G(X));
let l: Compose(List)(Maybe)(Char).t = [
Some('a'),
];
Expand Down Expand Up @@ -657,7 +652,17 @@ Format modules
include (Version2: (module type of Version2));

/* https://github.com/facebook/reason/issues/2608 */
module Functor =
(())
: (module type of {}) => {};
module Functor = () : (module type of {}) => {};

module Lola1 = () => {
let a = 3;
};

module Lola2 = (C: Cat, D: Dog, L: Lion) => {
let a = 33;
};

module L = Lola1();

module L2 = Lola2(Cat, Dog, Foo);
/* From http://stackoverflow.com/questions/1986374/ higher-order-type-constructors-and-functors-in-ocaml */
9 changes: 2 additions & 7 deletions test/modules_no_semi.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,7 @@ Format modules no semi
);

module ResultFromNonSimpleFunctorArg =
CurriedNoSugar(
(
MakeAModule({})
),
BMod,
);
CurriedNoSugar(MakeAModule(), BMod);

/* TODO: Functor type signatures should more resemble value signatures */
let curriedFunc: (int, int) => int =
Expand Down Expand Up @@ -451,7 +446,7 @@ Format modules no semi
G: (Type) => Type,
X: Type,
) =>
F((G(X)));
F(G(X));
let l: Compose(List)(Maybe)(Char).t = [
Some('a'),
];
Expand Down

0 comments on commit 82dd9aa

Please sign in to comment.