Skip to content

Commit

Permalink
Better formatting of Pexp_lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro authored and jordwalke committed Apr 12, 2019
1 parent 9a8fba8 commit 46bffd1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
19 changes: 13 additions & 6 deletions formatTest/typeCheckedTests/expected_output/lazy.re
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
let myComputation =
lazy {
lazy({
let tmp = 10;
let tmp2 = 20;
tmp + tmp2;
};
});

type myRecord = {myRecordField: int};

Expand All @@ -14,18 +14,25 @@ let operateOnLazyValue = (lazy {myRecordField}) => {

let result =
operateOnLazyValue(
lazy {myRecordField: 100},
lazy({myRecordField: 100}),
);

type box('a) =
| Box('a);

let lazy thisIsActuallyAPatternMatch = lazy 200;
let lazy thisIsActuallyAPatternMatch = lazy(200);
let tmp: int = thisIsActuallyAPatternMatch;
let (lazy (Box(i)), x) = (
lazy (Box(200)),
lazy(Box(200)),
100,
);
let tmp: int = i;

let myComputation = lazy 200;
let myComputation = lazy(200);

let reallyLoooooooooooooongIdentifierThatSpansMoreThan50Cols = 200;

let foo =
lazy(
reallyLoooooooooooooongIdentifierThatSpansMoreThan50Cols
);
4 changes: 4 additions & 0 deletions formatTest/typeCheckedTests/input/lazy.re
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ let (lazy (Box(i)), x) = (lazy (Box(200)), 100);
let tmp: int = i;

let myComputation = lazy (200);

let reallyLoooooooooooooongIdentifierThatSpansMoreThan50Cols = 200;

let foo = lazy(reallyLoooooooooooooongIdentifierThatSpansMoreThan50Cols)
2 changes: 1 addition & 1 deletion formatTest/unit_tests/expected_output/modules_no_semi.re
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ let test = b => {

assert(true);

lazy true;
lazy(true);

Fun.ignore();
};
19 changes: 14 additions & 5 deletions src/reason-parser/reason_pprint_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1794,20 +1794,20 @@ let partitionFinalWrapping listTester wrapFinalItemSetting x =
let semiTerminated term = makeList [term; atom ";"]

(* postSpace is so that when comments are interleaved, we still use spacing rules. *)
let makeLetSequence letItems =
let makeLetSequence ?(wrap=("{", "}")) letItems =
makeList
~break:Always_rec
~inline:(true, false)
~wrap:("{", "}")
~wrap
~postSpace:true
~sep:(SepFinal (";", ";"))
letItems

let makeLetSequenceSingleLine letItems =
let makeLetSequenceSingleLine ?(wrap=("{", "}")) letItems =
makeList
~break:IfNeed
~inline:(true, false)
~wrap:("{", "}")
~wrap
~preSpace:true
~postSpace:true
~sep:(Sep ";")
Expand Down Expand Up @@ -5796,7 +5796,16 @@ let printer = object(self:'self)
(makeTup [(self#unparseExpr e)]);
)
| Pexp_lazy e ->
Some (label ~space:true (atom "lazy") (self#simplifyUnparseExpr e))
let lazy_atom = atom "lazy" in
let layout_right = match e with
| {pexp_desc = Pexp_let _} ->
makeLetSequence ~wrap:("({", "})") (self#letList e)
| e when isSingleArgParenApplication [e] ->
self#singleArgParenApplication [e]
| _ ->
formatPrecedence (self#unparseExpr e)
in
Some (label lazy_atom layout_right)
| Pexp_poly _ ->
failwith (
"This version of the pretty printer assumes it is impossible to " ^
Expand Down

0 comments on commit 46bffd1

Please sign in to comment.