on master help sign in
Adjust when we put parentheses around TH-quoted names
004c8d0mrkkrp
data/examples/declaration/splice/quotes-out.hs+6
@@ -5,3 +5,9 @@ bar = 'foo
5baz = ''(:#)
5baz = ''(:#)
6
6
7baz' = ''(Foo.Bar.:#)
7baz' = ''(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
5baz = ''(:#)
5baz = ''(:#)
6
6
7baz' = ''(Foo.Bar.:#)
7baz' = ''(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
8 , p_hsmodName
8 , p_hsmodName
9 , p_ieWrappedName
9 , p_ieWrappedName
10 , p_rdrName
10 , p_rdrName
11+ , doesNotNeedExtraParens
11 , p_qualName
12 , p_qualName
12 , p_ieWildcard
13 , p_ieWildcard
13 , p_infixDefHelper
14 , p_infixDefHelper
@@ -63,12 +64,12 @@ p_rdrName l@(L spn _) = located l $ \x -> do
63 txt "'"
64 txt "'"
64 y
65 y
65 else id
66 else id
66− (m, avoidParens) =
67+ m =
67 case x of
68 case x of
68 Unqual occName ->
69 Unqual occName ->
69− (atom occName, False)
70+ atom occName
70 Qual mname occName ->
71 Qual mname occName ->
71− (p_qualName mname occName, False)
72+ p_qualName mname occName
72 Orig _ occName ->
73 Orig _ occName ->
73 -- NOTE This is used when GHC generates code that will be fed
74 -- NOTE This is used when GHC generates code that will be fed
74 -- into the renamer (e.g. from deriving clauses), but where we
75 -- into the renamer (e.g. from deriving clauses), but where we
@@ -77,19 +78,30 @@ p_rdrName l@(L spn _) = located l $ \x -> do
77 --
78 --
78 -- My current understanding is that the provided module name
79 -- My current understanding is that the provided module name
79 -- serves no purpose for us and can be safely ignored.
80 -- serves no purpose for us and can be safely ignored.
80− (atom occName, False)
81+ atom occName
81 Exact name ->
82 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
83+ atom name
86− , "$ghc-prim$GHC.Tuple$" `isPrefixOf` nameStableString name
87− )
88 m' = backticksWrapper (singleQuoteWrapper m)
84 m' = backticksWrapper (singleQuoteWrapper m)
89− if avoidParens
85+ if doesNotNeedExtraParens x
90 then m'
86 then m'
91 else parensWrapper m'
87 else parensWrapper m'
92
88
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+
93p_qualName :: ModuleName -> OccName -> R ()
105p_qualName :: ModuleName -> OccName -> R ()
94p_qualName mname occName = do
106p_qualName mname occName = do
95 atom mname
107 atom mname
src/Ormolu/Printer/Meat/Declaration/Value.hs+3−1
@@ -25,6 +25,7 @@ import Ormolu.Printer.Meat.Declaration.Signature
25import Ormolu.Printer.Meat.Type
25import Ormolu.Printer.Meat.Type
26import Ormolu.Utils
26import Ormolu.Utils
27import Outputable (Outputable (..))
27import Outputable (Outputable (..))
28+import RdrName (rdrNameOcc)
28import SrcLoc (combineSrcSpans, isOneLineSpan)
29import SrcLoc (combineSrcSpans, isOneLineSpan)
29import qualified Data.List.NonEmpty as NE
30import qualified Data.List.NonEmpty as NE
30import {-# SOURCE #-} Ormolu.Printer.Meat.Declaration
31import {-# SOURCE #-} Ormolu.Printer.Meat.Declaration
@@ -810,7 +811,8 @@ p_hsBracket = \case
810 -- turn makes it impossible to detect if there are parentheses around
811 -- turn makes it impossible to detect if there are parentheses around
811 -- it, etc. So we have to add parentheses manually assuming they are
812 -- it, etc. So we have to add parentheses manually assuming they are
812 -- necessary for all operators.
813 -- necessary for all operators.
813− let isOperator = any isPunctuation (showOutputable name)
814+ let isOperator = any isPunctuation (showOutputable (rdrNameOcc name))
815+ && not (doesNotNeedExtraParens name)
814 wrapper = if isOperator then parens else id
816 wrapper = if isOperator then parens else id
815 wrapper $ p_rdrName (noLoc name)
817 wrapper $ p_rdrName (noLoc name)
816 TExpBr NoExt expr -> do
818 TExpBr NoExt expr -> do