@@ -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.