on master help sign in
Preserve the name of the expression bracket

Both `[e|...|]` and `[||]` mean the same thing, and instead of sticking to a single style, we can just preserve what user wrote.

commite598b72be52fdc651c57395281970a99bd6e2d04 authorUtku Demir <me@utdemir.com> dateFri 16 Aug 2019, 14:19 (7 years ago) parenta3559a2
8 files changed+31−12
data/examples/declaration/splice/bracket-out.hs+1−1 data/examples/declaration/splice/splice-decl-out.hs+1−1 src/Ormolu/Printer/Combinators.hs+3 src/Ormolu/Printer/Comments.hs+4−1 src/Ormolu/Printer/Internal.hs+7−5 src/Ormolu/Printer/Meat/Common.hs−1 src/Ormolu/Printer/Meat/Declaration/Value.hs+15−2 src/Ormolu/Printer/Meat/Module.hs−1
data/examples/declaration/splice/bracket-out.hs+1−1
@@ -1,7 +1,7 @@
11{-# LANGUAGE TemplateHaskell #-}
22
33foo =
4− [e|
4+ [|
55 foo bar
66 |]
77
data/examples/declaration/splice/splice-decl-out.hs+1−1
@@ -10,7 +10,7 @@ $$foo
1010
1111foo bar
1212
13−[e|booya|]
13+[|booya|]
1414
1515-- TemplateHaskell allows Q () at the top level
1616do
src/Ormolu/Printer/Combinators.hs+3
@@ -10,6 +10,8 @@ module Ormolu.Printer.Combinators
1010 ( -- * The 'R' monad
1111 R
1212 , runR
13+ , getAnns
14+ , getEnclosingSpan
1315 -- * Combinators
1416 -- ** Basic
1517 , txt
@@ -25,6 +27,7 @@ module Ormolu.Printer.Combinators
2527 -- ** Formatting lists
2628 , sep
2729 , sepSemi
30+ , canUseBraces
2831 , useBraces
2932 , dontUseBraces
3033 -- ** Wrapping
src/Ormolu/Printer/Comments.hs+4−1
@@ -8,6 +8,7 @@ module Ormolu.Printer.Comments
88 ( spitPrecedingComments
99 , spitFollowingComments
1010 , spitRemainingComments
11+ , isNewlineModified
1112 , hasMoreComments
1213 )
1314where
@@ -77,7 +78,9 @@ spitFollowingComment
7778 -> R (Maybe RealSrcSpan) -- ^ Location of this comment
7879spitFollowingComment (L ref a) mlastSpn = do
7980 mnSpn <- nextEltSpan
80− meSpn <- getEnclosingSpan ref
81+ -- Get first enclosing span that is not equal to reference span, i.e. it's
82+ -- truly something enclosing the AST element.
83+ meSpn <- getEnclosingSpan (/= ref)
8184 newlineModified <- isNewlineModified
8285 i <- getIndent
8386 withPoppedComment (commentFollowsElt ref mnSpn meSpn mlastSpn) $ \l comment ->
src/Ormolu/Printer/Internal.hs+7−5
@@ -76,7 +76,7 @@ data RC = RC
7676 , rcDebug :: Bool
7777 -- ^ Whether to print debugging info as we go
7878 , rcEnclosingSpans :: [RealSrcSpan]
79− -- ^ Span of enclosing element of AST
79+ -- ^ Spans of enclosing elements of AST
8080 , rcAnns :: Anns
8181 -- ^ Collection of annotations
8282 , rcCanUseBraces :: Bool
@@ -326,11 +326,13 @@ setIndent i m' = do
326326 R (local modRC m)
327327 traceR "set_indent_after" Nothing
328328
329−-- | Get 'RealSrcSpan' of enclosing span for given referencne span.
329+-- | Get the first enclosing 'RealSrcSpan' that satisfies given predicate.
330330
331−getEnclosingSpan :: RealSrcSpan -> R (Maybe RealSrcSpan)
332−getEnclosingSpan r =
333− listToMaybe . dropWhile (== r) <$> R (asks rcEnclosingSpans)
331+getEnclosingSpan
332+ :: (RealSrcSpan -> Bool) -- ^ Predicate to use
333+ -> R (Maybe RealSrcSpan)
334+getEnclosingSpan f =
335+ listToMaybe . filter f <$> R (asks rcEnclosingSpans)
334336
335337-- | Set 'RealSrcSpan' of enclosing span for the given computation.
336338
src/Ormolu/Printer/Meat/Common.hs−1
@@ -20,7 +20,6 @@ import GHC hiding (GhcPs, IE)
2020import Name (nameStableString)
2121import OccName (OccName (..))
2222import Ormolu.Printer.Combinators
23−import Ormolu.Printer.Internal (getAnns)
2423import RdrName (RdrName (..))
2524
2625-- | Data and type family style.
src/Ormolu/Printer/Meat/Declaration/Value.hs+15−2
@@ -10,7 +10,6 @@ module Ormolu.Printer.Meat.Declaration.Value
1010 )
1111where
1212
13−import Ormolu.Printer.Internal
1413import Bag (bagToList)
1514import BasicTypes
1615import Control.Monad
@@ -847,7 +846,12 @@ p_hsSpliceTH isTyped expr = \case
847846
848847p_hsBracket :: HsBracket GhcPs -> R ()
849848p_hsBracket = \case
850− ExpBr NoExt expr -> quote "e" (located expr p_hsExpr)
849+ ExpBr NoExt expr -> do
850+ anns <- getEnclosingAnns
851+ let name = case anns of
852+ AnnOpenEQ:_ -> ""
853+ _ -> "e"
854+ quote name (located expr p_hsExpr)
851855 PatBr NoExt pat -> quote "p" (located pat p_pat)
852856 DecBrL NoExt decls -> quote "d" (p_hsDecls Free decls)
853857 DecBrG NoExt _ -> notImplemented "DecBrG" -- result of renamer
@@ -967,3 +971,12 @@ withGuards = any (checkOne . unLoc)
967971 checkOne :: GRHS GhcPs (Located body) -> Bool
968972 checkOne (GRHS NoExt [] _) = False
969973 checkOne _ = True
974+
975+-- | Get annotations for the enclosing element.
976+
977+getEnclosingAnns :: R [AnnKeywordId]
978+getEnclosingAnns = do
979+ e <- getEnclosingSpan (const True)
980+ case e of
981+ Nothing -> return []
982+ Just e' -> getAnns (RealSrcSpan e')
src/Ormolu/Printer/Meat/Module.hs−1
@@ -16,7 +16,6 @@ import Ormolu.Imports
1616import Ormolu.Parser.Pragma
1717import Ormolu.Printer.Combinators
1818import Ormolu.Printer.Comments
19−import Ormolu.Printer.Internal (isNewlineModified)
2019import Ormolu.Printer.Meat.Common
2120import Ormolu.Printer.Meat.Declaration
2221import Ormolu.Printer.Meat.Declaration.Warning