From 82dd9aa90b06cecf8db6fe59a120653f483b1199 Mon Sep 17 00:00:00 2001 From: Sander Date: Wed, 19 Apr 2023 18:49:16 +0200 Subject: [PATCH] Improve functor printing. (#2683) * 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 --- HISTORY.md | 1 + src/reason-parser/reason_pprint_ast.ml | 15 ++++++++++----- test/extensions.t/run.t | 4 +--- test/modules.t/input.re | 14 +++++++++++++- test/modules.t/run.t | 25 +++++++++++++++---------- test/modules_no_semi.t/run.t | 9 ++------- 6 files changed, 42 insertions(+), 26 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 4e1bff80f..f3437477a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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 diff --git a/src/reason-parser/reason_pprint_ast.ml b/src/reason-parser/reason_pprint_ast.ml index b0880d702..5d7d005ef 100644 --- a/src/reason-parser/reason_pprint_ast.ml +++ b/src/reason-parser/reason_pprint_ast.ml @@ -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) @@ -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)] @@ -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' @@ -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 @@ -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 diff --git a/test/extensions.t/run.t b/test/extensions.t/run.t index 2dc98766b..459bfe2e4 100644 --- a/test/extensions.t/run.t +++ b/test/extensions.t/run.t @@ -4,9 +4,7 @@ Format extensions [%extend open M]; - [%extend - module M = {} - ]; + [%extend module M = {}]; [%extend module type M = {}]; diff --git a/test/modules.t/input.re b/test/modules.t/input.re index 1786ababd..cf46b48f6 100644 --- a/test/modules.t/input.re +++ b/test/modules.t/input.re @@ -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); \ No newline at end of file diff --git a/test/modules.t/run.t b/test/modules.t/run.t index 27c94f76d..a23b89346 100644 --- a/test/modules.t/run.t +++ b/test/modules.t/run.t @@ -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 = @@ -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'), ]; @@ -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 */ diff --git a/test/modules_no_semi.t/run.t b/test/modules_no_semi.t/run.t index 6f5887008..91dc96c67 100644 --- a/test/modules_no_semi.t/run.t +++ b/test/modules_no_semi.t/run.t @@ -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 = @@ -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'), ];