on master help sign in
Fix backticks and parens in multiline function declarations

Function id obtained through pattern matching on ‘FunBind’ should not be used to print the actual equations because the different ‘RdrNames’ used in the equations may have different “decorations” (such as backticks and paretheses) associated with them. It is necessary to use per-equation names obtained from ‘m_ctxt’ of ‘Match’.

commit5955c514eeb880b5432b9c7e90adb3b7dee0dbf1 authorTeodoro Freund <teodoro.freund@tweag.io> dateTue 6 Aug 2019, 17:02 (7 years ago) parent75869e1
6 files changed+61−7
data/examples/declaration/value/function/backticks-lhs-out.hs+7 data/examples/declaration/value/function/backticks-lhs.hs+7 data/examples/declaration/value/function/parenthesis-lhs-out.hs+9 data/examples/declaration/value/function/parenthesis-lhs.hs+9 data/examples/declaration/value/pattern-synonyms/explicitely-bidirectional-out.hs+1−2 src/Ormolu/Printer/Meat/Declaration/Value.hs+28−5
data/examples/declaration/value/function/backticks-lhs-out.hsnew file+7
@@ -0,0 +1,7 @@
1+x `op1` (Just 0) = True
2+op1 x (Just _) = False
3+op1 x Nothing = undefined
4+
5+op2 1 y = False
6+x `op2` y =
7+ True
data/examples/declaration/value/function/backticks-lhs.hsnew file+7
@@ -0,0 +1,7 @@
1+x `op1` (Just 0) = True
2+op1 x (Just _) = False
3+op1 x Nothing = undefined
4+
5+op2 1 y = False
6+x `op2` y =
7+ True
data/examples/declaration/value/function/parenthesis-lhs-out.hsnew file+9
@@ -0,0 +1,9 @@
1+(!=!) 2 y = 1
2+x !=! y = 2
3+
4+x ?=? [] = 123
5+(?=?) x (_ : []) = 456
6+x ?=? _ = f x x
7+ where
8+ f x 7 = 789
9+ x `f` _ = 101
data/examples/declaration/value/function/parenthesis-lhs.hsnew file+9
@@ -0,0 +1,9 @@
1+(!=!) 2 y = 1
2+x !=! y = 2
3+
4+x ?=? [] = 123
5+(?=?) x (_:[]) = 456
6+x ?=? _ = f x x
7+ where
8+ f x 7 = 789
9+ x `f` _ = 101
data/examples/declaration/value/pattern-synonyms/explicitely-bidirectional-out.hs+1−2
@@ -13,5 +13,4 @@ pattern HeadC' x <-
1313pattern Simple <-
1414 "Simple"
1515 where
16− Simple =
17− "Complicated"
16+ Simple = "Complicated"
src/Ormolu/Printer/Meat/Declaration/Value.hs+28−5
@@ -69,8 +69,7 @@ p_funBind
6969 :: Located RdrName
7070 -> MatchGroup GhcPs (LHsExpr GhcPs)
7171 -> R ()
72−p_funBind name mgroup =
73− p_matchGroup (Function name) mgroup
72+p_funBind name = p_matchGroup (Function name)
7473
7574p_matchGroup
7675 :: MatchGroupStyle
@@ -86,11 +85,35 @@ p_matchGroup'
8685 -> MatchGroup GhcPs (Located body)
8786 -> R ()
8887p_matchGroup' placer pretty style MG {..} =
89− sep newline (located' (\m@Match {..} ->
90− p_match' placer pretty style (isInfixMatch m) (matchStrictness m) m_pats m_grhss))
91− (unLoc mg_alts)
88+ sep newline (located' p_Match) (unLoc mg_alts)
89+ where
90+ p_Match m@Match {..} =
91+ p_match'
92+ placer
93+ pretty
94+ (adjustMatchGroupStyle m style)
95+ (isInfixMatch m)
96+ (matchStrictness m)
97+ m_pats
98+ m_grhss
99+ p_Match _ = notImplemented "XMatch"
92100p_matchGroup' _ _ _ (XMatchGroup NoExt) = notImplemented "XMatchGroup"
93101
102+-- | Function id obtained through pattern matching on 'FunBind' should not
103+-- be used to print the actual equations because the different ‘RdrNames’
104+-- used in the equations may have different “decorations” (such as backticks
105+-- and paretheses) associated with them. It is necessary to use per-equation
106+-- names obtained from 'm_ctxt' of 'Match'. This function replaces function
107+-- name inside of 'Function' accordingly.
108+
109+adjustMatchGroupStyle
110+ :: Match GhcPs body
111+ -> MatchGroupStyle
112+ -> MatchGroupStyle
113+adjustMatchGroupStyle m = \case
114+ Function _ -> (Function . mc_fun . m_ctxt) m
115+ style -> style
116+
94117matchStrictness :: Match id body -> SrcStrictness
95118matchStrictness match =
96119 case m_ctxt match of