on master help sign in
Fix the “overindentation” issues with function applications
commitd02483a8ab64c658015466693cce38c3c2d55db5 authormrkkrp <mark.karpov@tweag.io> dateFri 4 Oct 2019, 20:08 (7 years ago) parent096c8d7
6 files changed+40−31
data/examples/declaration/value/function/infix/hanging-out.hs+3−3 data/examples/declaration/value/function/overindentation-out.hs+9 data/examples/declaration/value/function/overindentation.hs+9 src/Ormolu/Parser/CommentStream.hs+1−1 src/Ormolu/Printer/Internal.hs+5−5 src/Ormolu/Printer/Meat/Declaration/Value.hs+13−22
data/examples/declaration/value/function/infix/hanging-out.hs+3−3
@@ -4,9 +4,9 @@ f = unFoo . foo bar baz 3 $ do
44
55g = unFoo
66 . foo
7− bar
8− baz
9− 3
7+ bar
8+ baz
9+ 3
1010 $ do
1111 act
1212 ret
data/examples/declaration/value/function/overindentation-out.hsnew file+9
@@ -0,0 +1,9 @@
1+reallyincrediblyLongName = f
2+ a
3+ A
4+ { reallyincrediblyLongName = f
5+ a
6+ A
7+ { reallyincrediblyLongName
8+ }
9+ }
data/examples/declaration/value/function/overindentation.hsnew file+9
@@ -0,0 +1,9 @@
1+reallyincrediblyLongName = f
2+ a
3+ A
4+ { reallyincrediblyLongName = f
5+ a
6+ A
7+ { reallyincrediblyLongName
8+ }
9+ }
src/Ormolu/Parser/CommentStream.hs+1−1
@@ -63,7 +63,7 @@ mkCommentStream extraComments pstate =
6363 extraComments
6464 ++ mapMaybe (liftMaybe . fmap unAnnotationComment) (GHC.comment_q pstate)
6565 ++ concatMap (mapMaybe (liftMaybe . fmap unAnnotationComment) . snd)
66− (GHC.annotations_comments pstate)
66+ (GHC.annotations_comments pstate)
6767
6868-- | Test whether a 'Comment' looks like a Haddock following a definition,
6969-- i.e. something starting with @-- ^@.
src/Ormolu/Printer/Internal.hs+5−5
@@ -431,11 +431,11 @@ popComment f = R $ do
431431 then
432432 Just x
433433 <$ modify
434− ( \sc ->
435− sc
436− { scCommentStream = CommentStream xs
437− }
438− )
434+ ( \sc ->
435+ sc
436+ { scCommentStream = CommentStream xs
437+ }
438+ )
439439 else return Nothing
440440
441441-- | Get the first enclosing 'RealSrcSpan' that satisfies given predicate.
src/Ormolu/Printer/Meat/Declaration/Value.hs+13−22
@@ -245,7 +245,7 @@ p_match' placer render style isInfix strictness m_pats m_grhss = do
245245 Nothing -> blockPlacement placer grhssGRHSs
246246 Just spn ->
247247 if isOneLineSpan
248− (mkSrcSpan spn (srcSpanStart grhssSpan))
248+ (mkSrcSpan spn (srcSpanStart grhssSpan))
249249 then blockPlacement placer grhssGRHSs
250250 else Normal
251251 p_body = do
@@ -374,7 +374,7 @@ p_stmt' placer render = \case
374374 case f of
375375 L l' x ->
376376 if isOneLineSpan
377− (mkSrcSpan (srcSpanEnd (getLoc l)) (srcSpanStart l'))
377+ (mkSrcSpan (srcSpanEnd (getLoc l)) (srcSpanStart l'))
378378 then placer x
379379 else Normal
380380 switchLayout [getLoc l, getLoc f] $
@@ -525,9 +525,9 @@ p_hsExpr' s = \case
525525 breakpoint
526526 inci (p_matchGroup LambdaCase mgroup)
527527 HsApp NoExt f x -> do
528− -- We hang only the last function argument. In order to do
529− -- this, we only call 'placeHanging' on the topmost 'HsApp',
530− -- and then use 'p_withoutHanging' for the descendants.
528+ -- We hang only the last function argument. In order to do this, we only
529+ -- call 'placeHanging' on the topmost 'HsApp', and then use
530+ -- 'p_withoutHanging' for the descendants.
531531 let p_withoutHanging (HsApp NoExt f' x') = do
532532 case f' of
533533 L _ (HsApp _ _ _) -> located f' p_withoutHanging
@@ -535,24 +535,15 @@ p_hsExpr' s = \case
535535 breakpoint
536536 inci $ located x' p_hsExpr
537537 p_withoutHanging e = p_hsExpr e
538− -- Only use the hanging placement if the function spans
539− -- a single line.
538+ -- Only use the hanging placement if the function spans a single
539+ -- line.
540540 placement =
541541 if isOneLineSpan (getLoc f)
542542 then exprPlacement (unLoc x)
543543 else Normal
544− -- We only sit when the last expression is not hanging.
545− -- This is to allow:
546− -- f = foo bar do
547− -- baz
548− sit' =
549− if placement == Normal
550− then sitcc
551− else id
552− sit' $ do
553− useBraces $ located f p_withoutHanging
554− placeHanging placement $
555− located x p_hsExpr
544+ useBraces (located f p_withoutHanging)
545+ placeHanging placement $
546+ located x p_hsExpr
556547 HsAppType a e -> do
557548 located e p_hsExpr
558549 breakpoint
@@ -620,8 +611,8 @@ p_hsExpr' s = \case
620611 p_seqBody =
621612 sitcc
622613 . sep
623− (comma >> breakpoint)
624− (located' (sitcc . p_stmt))
614+ (comma >> breakpoint)
615+ (located' (sitcc . p_stmt))
625616 stmts = init xs
626617 yield = last xs
627618 lists = foldr (liftAppend . gatherStmt) [] stmts
@@ -1218,7 +1209,7 @@ p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
12181209 -- hanging placement.
12191210 let placement =
12201211 if isOneLineSpan
1221− (mkSrcSpan (srcSpanStart (opTreeLoc x)) (srcSpanStart (opTreeLoc y)))
1212+ (mkSrcSpan (srcSpanStart (opTreeLoc x)) (srcSpanStart (opTreeLoc y)))
12221213 then case y of
12231214 OpNode (L _ n) -> exprPlacement n
12241215 _ -> Normal