on master help sign in
Fix parentheses around TH-quoted operators
commitde1b33788def660e550d1153f159fee944e452ec authormrkkrp <mark.karpov@tweag.io> dateSun 21 Jul 2019, 12:09 (7 years ago) parentab4885f
3 files changed+14−1
data/examples/declaration/splice/quotes-out.hs+2 data/examples/declaration/splice/quotes.hs+2 src/Ormolu/Printer/Meat/Declaration/Value.hs+10−1
data/examples/declaration/splice/quotes-out.hs+2
@@ -1,3 +1,5 @@
11foo = ''R
22
33bar = 'foo
4+
5+baz = ''(:#)
data/examples/declaration/splice/quotes.hs+2
@@ -1,3 +1,5 @@
11foo = ''R
22
33bar = 'foo
4+
5+baz = ''(:#)
src/Ormolu/Printer/Meat/Declaration/Value.hs+10−1
@@ -14,6 +14,7 @@ import Bag (bagToList)
1414import BasicTypes
1515import Control.Monad
1616import Data.Bool (bool)
17+import Data.Char (isAlphaNum)
1718import Data.Data hiding (Infix, Prefix)
1819import Data.List (intersperse, sortOn)
1920import Data.Text (Text)
@@ -803,7 +804,15 @@ p_hsBracket = \case
803804 TypBr NoExt ty -> quote "t" (located ty p_hsType)
804805 VarBr NoExt isSingleQuote name -> do
805806 txt (bool "''" "'" isSingleQuote)
806− p_rdrName (noLoc name)
807+ -- HACK As you can see we use 'noLoc' here to be able to pass name into
808+ -- 'p_rdrName' since the latter expects a "located" thing. The problem
809+ -- is that 'VarBr' doesn't provide us with location of the name. This in
810+ -- 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+ -- necessary for all operators.
813+ let isOperator = not (any isAlphaNum (showOutputable name))
814+ wrapper = if isOperator then parens else id
815+ wrapper $ p_rdrName (noLoc name)
807816 TExpBr NoExt expr -> do
808817 txt "[||"
809818 breakpoint'