on master help sign in
Adjust when we put parentheses around TH-quoted names
commit004c8d025612e92feffb398fb094de48b5ee1944 authormrkkrp <mark.karpov@tweag.io> dateSun 21 Jul 2019, 22:58 (7 years ago) parentd61b910
4 files changed+38−12
data/examples/declaration/splice/quotes-out.hs+6 data/examples/declaration/splice/quotes.hs+6 src/Ormolu/Printer/Meat/Common.hs+23−11 src/Ormolu/Printer/Meat/Declaration/Value.hs+3−1
data/examples/declaration/splice/quotes-out.hs+6
@@ -5,3 +5,9 @@ bar = 'foo
55baz = ''(:#)
66
77baz' = ''(Foo.Bar.:#)
8+
9+unit = ''()
10+
11+list = ''[]
12+
13+quolified = ''Semigroup.Option
data/examples/declaration/splice/quotes.hs+6
@@ -5,3 +5,9 @@ bar = 'foo
55baz = ''(:#)
66
77baz' = ''(Foo.Bar.:#)
8+
9+unit = ''()
10+
11+list = ''[]
12+
13+quolified = ''Semigroup.Option
src/Ormolu/Printer/Meat/Common.hs+23−11
@@ -8,6 +8,7 @@ module Ormolu.Printer.Meat.Common
88 , p_hsmodName
99 , p_ieWrappedName
1010 , p_rdrName
11+ , doesNotNeedExtraParens
1112 , p_qualName
1213 , p_ieWildcard
1314 , p_infixDefHelper
@@ -63,12 +64,12 @@ p_rdrName l@(L spn _) = located l $ \x -> do
6364 txt "'"
6465 y
6566 else id
66− (m, avoidParens) =
67+ m =
6768 case x of
6869 Unqual occName ->
69− (atom occName, False)
70+ atom occName
7071 Qual mname occName ->
71− (p_qualName mname occName, False)
72+ p_qualName mname occName
7273 Orig _ occName ->
7374 -- NOTE This is used when GHC generates code that will be fed
7475 -- into the renamer (e.g. from deriving clauses), but where we
@@ -77,19 +78,30 @@ p_rdrName l@(L spn _) = located l $ \x -> do
7778 --
7879 -- My current understanding is that the provided module name
7980 -- serves no purpose for us and can be safely ignored.
80− (atom occName, False)
81+ atom occName
8182 Exact name ->
82− -- NOTE I'm not sure this "stable string" is stable enough, but
83− -- it looks like this is the most robust way to tell if we're
84− -- looking at exactly this piece of built-in syntax.
85− ( atom name
86− , "$ghc-prim$GHC.Tuple$" `isPrefixOf` nameStableString name
87− )
83+ atom name
8884 m' = backticksWrapper (singleQuoteWrapper m)
89− if avoidParens
85+ if doesNotNeedExtraParens x
9086 then m'
9187 else parensWrapper m'
9288
89+-- | Whether given name should not have parentheses around it. This is used
90+-- to detect e.g. tuples for which annotations will indicate parentheses,
91+-- but the parentheses are already part of the symbol, so no extra layer of
92+-- parentheses should be added. It also detects the [] literal.
93+
94+doesNotNeedExtraParens :: RdrName -> Bool
95+doesNotNeedExtraParens = \case
96+ Exact name ->
97+ let s = nameStableString name
98+ -- NOTE I'm not sure this "stable string" is stable enough, but it looks
99+ -- like this is the most robust way to tell if we're looking at exactly
100+ -- this piece of built-in syntax.
101+ in ("$ghc-prim$GHC.Tuple$" `isPrefixOf` s) ||
102+ ("$ghc-prim$GHC.Types$[]" `isPrefixOf` s)
103+ _ -> False
104+
93105p_qualName :: ModuleName -> OccName -> R ()
94106p_qualName mname occName = do
95107 atom mname
src/Ormolu/Printer/Meat/Declaration/Value.hs+3−1
@@ -25,6 +25,7 @@ import Ormolu.Printer.Meat.Declaration.Signature
2525import Ormolu.Printer.Meat.Type
2626import Ormolu.Utils
2727import Outputable (Outputable (..))
28+import RdrName (rdrNameOcc)
2829import SrcLoc (combineSrcSpans, isOneLineSpan)
2930import qualified Data.List.NonEmpty as NE
3031import {-# SOURCE #-} Ormolu.Printer.Meat.Declaration
@@ -810,7 +811,8 @@ p_hsBracket = \case
810811 -- turn makes it impossible to detect if there are parentheses around
811812 -- it, etc. So we have to add parentheses manually assuming they are
812813 -- necessary for all operators.
813− let isOperator = any isPunctuation (showOutputable name)
814+ let isOperator = any isPunctuation (showOutputable (rdrNameOcc name))
815+ && not (doesNotNeedExtraParens name)
814816 wrapper = if isOperator then parens else id
815817 wrapper $ p_rdrName (noLoc name)
816818 TExpBr NoExt expr -> do