on master help sign in
Fix formatting of applications where function is a complex expression

Usually we want to bump indentation for arguments for the sake of readability. However, when the function itself is a do-block or case expression it is not a good idea. It seems to be safe to always bump indentation when the function expression is parenthesised.

commit5b9861b57fd74d2cc65d0fb201329f5bd08fee9b authormrkkrp <mark.karpov@tweag.io> dateFri 29 Nov 2019, 13:53 (7 years ago) parent5ca9205
5 files changed+47−2
data/examples/declaration/value/function/application-out.hs → data/examples/declaration/value/function/application-0-out.hs data/examples/declaration/value/function/application.hs → data/examples/declaration/value/function/application-0.hs data/examples/declaration/value/function/application-1-out.hs+19 data/examples/declaration/value/function/application-1.hs+14 src/Ormolu/Printer/Meat/Declaration/Value.hs+14−2
data/examples/declaration/value/function/application-out.hs → data/examples/declaration/value/function/application-0-out.hsrenamed

The content is the same.

data/examples/declaration/value/function/application.hs → data/examples/declaration/value/function/application-0.hsrenamed

The content is the same.

data/examples/declaration/value/function/application-1-out.hsnew file+19
@@ -0,0 +1,19 @@
1+main =
2+ do
3+ x
4+ y
5+ z
6+
7+main =
8+ case foo of
9+ x -> a
10+ foo
11+ a
12+ b
13+
14+main =
15+ do
16+ if x then y else z
17+ foo
18+ a
19+ b
data/examples/declaration/value/function/application-1.hsnew file+14
@@ -0,0 +1,14 @@
1+main =
2+ do
3+ x
4+ y
5+ z
6+
7+main =
8+ case foo of
9+ x -> a
10+ foo a b
11+
12+main = do
13+ if x then y else z
14+ foo a b
src/Ormolu/Printer/Meat/Declaration/Value.hs+14−2
@@ -553,11 +553,23 @@ p_hsExpr' s = \case
553553 -- one.
554554 case placement of
555555 Normal -> do
556+ let -- Usually we want to bump indentation for arguments for the
557+ -- sake of readability. However, when the function itself is a
558+ -- do-block or case expression it is not a good idea. It seems
559+ -- to be safe to always bump indentation when the function
560+ -- expression is parenthesised.
561+ indent =
562+ case func of
563+ L _ (HsPar NoExt _) -> inci
564+ L spn _ ->
565+ if isOneLineSpan spn
566+ then inci
567+ else id
556568 useBraces $ do
557569 located func (p_hsExpr' s)
558570 breakpoint
559− inci $ sep breakpoint (located' p_hsExpr) initp
560− inci $ do
571+ indent $ sep breakpoint (located' p_hsExpr) initp
572+ indent $ do
561573 unless (null initp) breakpoint
562574 located lastp p_hsExpr
563575 Hanging -> do