on master help sign in
Tweak hanging logic on HsApp
c16fe06Utku Demir
data/examples/declaration/value/function/block-arguments-out.hs+7−10
@@ -1,13 +1,11 @@
1f1 = foo do bar
1f1 = foo do bar
2
2
3−f2 =
4− foo do
3+f2 = foo do
5− bar
4+ bar
6
5
7−f3 =
8− foo case True of
6+f3 = foo case True of
9− True -> bar
7+ True -> bar
10− False -> baz
8+ False -> baz
11
9
12f4 = foo let a = 3 in b
10f4 = foo let a = 3 in b
13
11
@@ -25,6 +23,5 @@ f6 =
25
23
26f7 = foo \x -> y
24f7 = foo \x -> y
27
25
28−f8 =
29− foo \x ->
26+f8 = foo \x ->
30− y
27+ y
data/examples/declaration/value/function/record-constructors-out.hs+6−7
@@ -15,13 +15,12 @@ aLongVariableName =
15 aLongRecordFieldName = YetAnotherLongRecordName
15 aLongRecordFieldName = YetAnotherLongRecordName
16 { yetAnotherLongRecordFieldName = "a long string"
16 { yetAnotherLongRecordFieldName = "a long string"
17 },
17 },
18− aLongRecordFieldName2 =
19− Just YetAnotherLongRecordName
18+ aLongRecordFieldName2 = Just YetAnotherLongRecordName
20− { yetAnotherLongRecordFieldName = "a long string",
19+ { yetAnotherLongRecordFieldName = "a long string",
21− yetAnotherLongRecordFieldName =
20+ yetAnotherLongRecordFieldName =
22− Just
21+ Just
23− "a long string"
22+ "a long string"
24− },
23+ },
25 aLongRecordFieldName3 = do
24 aLongRecordFieldName3 = do
26 foo
25 foo
27 bar
26 bar
src/Ormolu/Printer/Meat/Declaration/Value.hs+31−5
@@ -59,6 +59,7 @@ data Placement
59 | Hanging -- ^ Expressions that have hanging form
59 | Hanging -- ^ Expressions that have hanging form
60 -- should use it and avoid bumping one level
60 -- should use it and avoid bumping one level
61 -- of indentation
61 -- of indentation
62+ deriving (Eq)
62
63
63p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
64p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
64p_valDecl = \case
65p_valDecl = \case
@@ -512,11 +513,34 @@ p_hsExpr = \case
512 txt "\\case"
513 txt "\\case"
513 breakpoint
514 breakpoint
514 inci (p_matchGroup LambdaCase mgroup)
515 inci (p_matchGroup LambdaCase mgroup)
515− HsApp NoExt f x -> sitcc $ do
516+ HsApp NoExt f x -> do
516− located f p_hsExpr
517+ -- We hang only the last function argument. In order to do
517− -- Second argument can be a `do` or `case` block with `-XBlockArguments`.
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)
518− placeHanging (exprPlacement (unLoc x)) $
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 $
519− located x p_hsExpr
543+ located x p_hsExpr
520 HsAppType a e -> do
544 HsAppType a e -> do
521 located e p_hsExpr
545 located e p_hsExpr
522 breakpoint
546 breakpoint
@@ -1091,6 +1115,8 @@ exprPlacement = \case
1091 -- whole block hanging; so that we can use the common @f = foo $ do@
1115 -- whole block hanging; so that we can use the common @f = foo $ do@
1092 -- style.
1116 -- style.
1093 OpApp NoExt _ _ y -> exprPlacement (unLoc y)
1117 OpApp NoExt _ _ y -> exprPlacement (unLoc y)
1118+ -- Same thing for function applications (usually with -XBlockArguments)
1119+ HsApp NoExt _ y -> exprPlacement (unLoc y)
1094 HsProc NoExt (L s _) _ ->
1120 HsProc NoExt (L s _) _ ->
1095 -- Indentation breaks if pattern is longer than one line and left
1121 -- Indentation breaks if pattern is longer than one line and left
1096 -- hanging. Consequently, only apply hanging when it is safe.
1122 -- hanging. Consequently, only apply hanging when it is safe.