on master help sign in
Tweak hanging logic on HsApp
commitc16fe06df364ac5be94c6a02c5feeb9c447c04e1 authorUtku Demir <me@utdemir.com> dateMon 16 Sep 2019, 17:49 (7 years ago) parent4bed990
3 files changed+44−22
data/examples/declaration/value/function/block-arguments-out.hs+7−10 data/examples/declaration/value/function/record-constructors-out.hs+6−7 src/Ormolu/Printer/Meat/Declaration/Value.hs+31−5
data/examples/declaration/value/function/block-arguments-out.hs+7−10
@@ -1,13 +1,11 @@
11f1 = foo do bar
22
3−f2 =
4− foo do
5− bar
3+f2 = foo do
4+ bar
65
7−f3 =
8− foo case True of
9− True -> bar
10− False -> baz
6+f3 = foo case True of
7+ True -> bar
8+ False -> baz
119
1210f4 = foo let a = 3 in b
1311
@@ -25,6 +23,5 @@ f6 =
2523
2624f7 = foo \x -> y
2725
28−f8 =
29− foo \x ->
30− y
26+f8 = foo \x ->
27+ y
data/examples/declaration/value/function/record-constructors-out.hs+6−7
@@ -15,13 +15,12 @@ aLongVariableName =
1515 aLongRecordFieldName = YetAnotherLongRecordName
1616 { yetAnotherLongRecordFieldName = "a long string"
1717 },
18− aLongRecordFieldName2 =
19− Just YetAnotherLongRecordName
20− { yetAnotherLongRecordFieldName = "a long string",
21− yetAnotherLongRecordFieldName =
22− Just
23− "a long string"
24− },
18+ aLongRecordFieldName2 = Just YetAnotherLongRecordName
19+ { yetAnotherLongRecordFieldName = "a long string",
20+ yetAnotherLongRecordFieldName =
21+ Just
22+ "a long string"
23+ },
2524 aLongRecordFieldName3 = do
2625 foo
2726 bar
src/Ormolu/Printer/Meat/Declaration/Value.hs+31−5
@@ -59,6 +59,7 @@ data Placement
5959 | Hanging -- ^ Expressions that have hanging form
6060 -- should use it and avoid bumping one level
6161 -- of indentation
62+ deriving (Eq)
6263
6364p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
6465p_valDecl = \case
@@ -512,11 +513,34 @@ p_hsExpr = \case
512513 txt "\\case"
513514 breakpoint
514515 inci (p_matchGroup LambdaCase mgroup)
515− HsApp NoExt f x -> sitcc $ do
516− located f p_hsExpr
517− -- Second argument can be a `do` or `case` block with `-XBlockArguments`.
518− placeHanging (exprPlacement (unLoc x)) $
519− located x p_hsExpr
516+ HsApp NoExt f x -> do
517+ -- We hang only the last function argument. In order to do
518+ -- this, we only call 'placeHanging' on the topmost 'HsApp',
519+ -- and then use 'p_withoutHanging' for the descendants.
520+ let p_withoutHanging (HsApp NoExt f' x') = do
521+ case f' of
522+ (L _ (HsApp _ _ _)) -> located f' p_withoutHanging
523+ _ -> located f' p_hsExpr
524+ breakpoint
525+ inci $ located x' p_hsExpr
526+ p_withoutHanging e = p_hsExpr e
527+ -- Only use the hanging placement if the function spans
528+ -- a single line.
529+ placement =
530+ if isOneLineSpan (getLoc f)
531+ then exprPlacement (unLoc x)
532+ else Normal
533+ -- We only sit when the last expression is not hanging.
534+ -- This is to allow:
535+ -- f = foo bar do
536+ -- baz
537+ sit' = if placement == Normal
538+ then sitcc
539+ else id
540+ sit' $ do
541+ useBraces $ located f p_withoutHanging
542+ placeHanging placement $
543+ located x p_hsExpr
520544 HsAppType a e -> do
521545 located e p_hsExpr
522546 breakpoint
@@ -1091,6 +1115,8 @@ exprPlacement = \case
10911115 -- whole block hanging; so that we can use the common @f = foo $ do@
10921116 -- style.
10931117 OpApp NoExt _ _ y -> exprPlacement (unLoc y)
1118+ -- Same thing for function applications (usually with -XBlockArguments)
1119+ HsApp NoExt _ y -> exprPlacement (unLoc y)
10941120 HsProc NoExt (L s _) _ ->
10951121 -- Indentation breaks if pattern is longer than one line and left
10961122 -- hanging. Consequently, only apply hanging when it is safe.