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’.
This commit is contained in:
Teodoro Freund 2019-08-06 17:02:36 +02:00 committed by Mark Karpov
parent 75869e1590
commit 5955c514ee
6 changed files with 61 additions and 7 deletions

View File

@ -0,0 +1,7 @@
x `op1` (Just 0) = True
op1 x (Just _) = False
op1 x Nothing = undefined
op2 1 y = False
x `op2` y =
True

View File

@ -0,0 +1,7 @@
x `op1` (Just 0) = True
op1 x (Just _) = False
op1 x Nothing = undefined
op2 1 y = False
x `op2` y =
True

View File

@ -0,0 +1,9 @@
(!=!) 2 y = 1
x !=! y = 2
x ?=? [] = 123
(?=?) x (_ : []) = 456
x ?=? _ = f x x
where
f x 7 = 789
x `f` _ = 101

View File

@ -0,0 +1,9 @@
(!=!) 2 y = 1
x !=! y = 2
x ?=? [] = 123
(?=?) x (_:[]) = 456
x ?=? _ = f x x
where
f x 7 = 789
x `f` _ = 101

View File

@ -13,5 +13,4 @@ pattern HeadC' x <-
pattern Simple <-
"Simple"
where
Simple =
"Complicated"
Simple = "Complicated"

View File

@ -69,8 +69,7 @@ p_funBind
:: Located RdrName
-> MatchGroup GhcPs (LHsExpr GhcPs)
-> R ()
p_funBind name mgroup =
p_matchGroup (Function name) mgroup
p_funBind name = p_matchGroup (Function name)
p_matchGroup
:: MatchGroupStyle
@ -86,11 +85,35 @@ p_matchGroup'
-> MatchGroup GhcPs (Located body)
-> R ()
p_matchGroup' placer pretty style MG {..} =
sep newline (located' (\m@Match {..} ->
p_match' placer pretty style (isInfixMatch m) (matchStrictness m) m_pats m_grhss))
(unLoc mg_alts)
sep newline (located' p_Match) (unLoc mg_alts)
where
p_Match m@Match {..} =
p_match'
placer
pretty
(adjustMatchGroupStyle m style)
(isInfixMatch m)
(matchStrictness m)
m_pats
m_grhss
p_Match _ = notImplemented "XMatch"
p_matchGroup' _ _ _ (XMatchGroup NoExt) = notImplemented "XMatchGroup"
-- | 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'. This function replaces function
-- name inside of 'Function' accordingly.
adjustMatchGroupStyle
:: Match GhcPs body
-> MatchGroupStyle
-> MatchGroupStyle
adjustMatchGroupStyle m = \case
Function _ -> (Function . mc_fun . m_ctxt) m
style -> style
matchStrictness :: Match id body -> SrcStrictness
matchStrictness match =
case m_ctxt match of