on master help sign in
Preserve the name of the expression bracket
e598b72Utku Demir
data/examples/declaration/splice/bracket-out.hs+1−1
@@ -1,7 +1,7 @@
1{-# LANGUAGE TemplateHaskell #-}
1{-# LANGUAGE TemplateHaskell #-}
2
2
3foo =
3foo =
4− [e|
4+ [|
5 foo bar
5 foo bar
6 |]
6 |]
7
7
data/examples/declaration/splice/splice-decl-out.hs+1−1
@@ -10,7 +10,7 @@ $$foo
10
10
11foo bar
11foo bar
12
12
13−[e|booya|]
13+[|booya|]
14
14
15-- TemplateHaskell allows Q () at the top level
15-- TemplateHaskell allows Q () at the top level
16do
16do
src/Ormolu/Printer/Combinators.hs+3
@@ -10,6 +10,8 @@ module Ormolu.Printer.Combinators
10 ( -- * The 'R' monad
10 ( -- * The 'R' monad
11 R
11 R
12 , runR
12 , runR
13+ , getAnns
14+ , getEnclosingSpan
13 -- * Combinators
15 -- * Combinators
14 -- ** Basic
16 -- ** Basic
15 , txt
17 , txt
@@ -25,6 +27,7 @@ module Ormolu.Printer.Combinators
25 -- ** Formatting lists
27 -- ** Formatting lists
26 , sep
28 , sep
27 , sepSemi
29 , sepSemi
30+ , canUseBraces
28 , useBraces
31 , useBraces
29 , dontUseBraces
32 , dontUseBraces
30 -- ** Wrapping
33 -- ** Wrapping
src/Ormolu/Printer/Comments.hs+4−1
@@ -8,6 +8,7 @@ module Ormolu.Printer.Comments
8 ( spitPrecedingComments
8 ( spitPrecedingComments
9 , spitFollowingComments
9 , spitFollowingComments
10 , spitRemainingComments
10 , spitRemainingComments
11+ , isNewlineModified
11 , hasMoreComments
12 , hasMoreComments
12 )
13 )
13where
14where
@@ -77,7 +78,9 @@ spitFollowingComment
77 -> R (Maybe RealSrcSpan) -- ^ Location of this comment
78 -> R (Maybe RealSrcSpan) -- ^ Location of this comment
78spitFollowingComment (L ref a) mlastSpn = do
79spitFollowingComment (L ref a) mlastSpn = do
79 mnSpn <- nextEltSpan
80 mnSpn <- nextEltSpan
81+ -- Get first enclosing span that is not equal to reference span, i.e. it's
82+ -- truly something enclosing the AST element.
80− meSpn <- getEnclosingSpan ref
83+ meSpn <- getEnclosingSpan (/= ref)
81 newlineModified <- isNewlineModified
84 newlineModified <- isNewlineModified
82 i <- getIndent
85 i <- getIndent
83 withPoppedComment (commentFollowsElt ref mnSpn meSpn mlastSpn) $ \l comment ->
86 withPoppedComment (commentFollowsElt ref mnSpn meSpn mlastSpn) $ \l comment ->
src/Ormolu/Printer/Internal.hs+7−5
@@ -76,7 +76,7 @@ data RC = RC
76 , rcDebug :: Bool
76 , rcDebug :: Bool
77 -- ^ Whether to print debugging info as we go
77 -- ^ Whether to print debugging info as we go
78 , rcEnclosingSpans :: [RealSrcSpan]
78 , rcEnclosingSpans :: [RealSrcSpan]
79− -- ^ Span of enclosing element of AST
79+ -- ^ Spans of enclosing elements of AST
80 , rcAnns :: Anns
80 , rcAnns :: Anns
81 -- ^ Collection of annotations
81 -- ^ Collection of annotations
82 , rcCanUseBraces :: Bool
82 , rcCanUseBraces :: Bool
@@ -326,11 +326,13 @@ setIndent i m' = do
326 R (local modRC m)
326 R (local modRC m)
327 traceR "set_indent_after" Nothing
327 traceR "set_indent_after" Nothing
328
328
329−-- | Get 'RealSrcSpan' of enclosing span for given referencne span.
329+-- | Get the first enclosing 'RealSrcSpan' that satisfies given predicate.
330
330
331+getEnclosingSpan
332+ :: (RealSrcSpan -> Bool) -- ^ Predicate to use
331−getEnclosingSpan :: RealSrcSpan -> R (Maybe RealSrcSpan)
333+ -> R (Maybe RealSrcSpan)
332−getEnclosingSpan r =
334+getEnclosingSpan f =
333− listToMaybe . dropWhile (== r) <$> R (asks rcEnclosingSpans)
335+ listToMaybe . filter f <$> R (asks rcEnclosingSpans)
334
336
335-- | Set 'RealSrcSpan' of enclosing span for the given computation.
337-- | Set 'RealSrcSpan' of enclosing span for the given computation.
336
338
src/Ormolu/Printer/Meat/Common.hs−1
@@ -20,7 +20,6 @@ import GHC hiding (GhcPs, IE)
20import Name (nameStableString)
20import Name (nameStableString)
21import OccName (OccName (..))
21import OccName (OccName (..))
22import Ormolu.Printer.Combinators
22import Ormolu.Printer.Combinators
23−import Ormolu.Printer.Internal (getAnns)
24import RdrName (RdrName (..))
23import RdrName (RdrName (..))
25
24
26-- | Data and type family style.
25-- | Data and type family style.
src/Ormolu/Printer/Meat/Declaration/Value.hs+15−2
@@ -10,7 +10,6 @@ module Ormolu.Printer.Meat.Declaration.Value
10 )
10 )
11where
11where
12
12
13−import Ormolu.Printer.Internal
14import Bag (bagToList)
13import Bag (bagToList)
15import BasicTypes
14import BasicTypes
16import Control.Monad
15import Control.Monad
@@ -847,7 +846,12 @@ p_hsSpliceTH isTyped expr = \case
847
846
848p_hsBracket :: HsBracket GhcPs -> R ()
847p_hsBracket :: HsBracket GhcPs -> R ()
849p_hsBracket = \case
848p_hsBracket = \case
849+ ExpBr NoExt expr -> do
850+ anns <- getEnclosingAnns
851+ let name = case anns of
852+ AnnOpenEQ:_ -> ""
853+ _ -> "e"
850− ExpBr NoExt expr -> quote "e" (located expr p_hsExpr)
854+ quote name (located expr p_hsExpr)
851 PatBr NoExt pat -> quote "p" (located pat p_pat)
855 PatBr NoExt pat -> quote "p" (located pat p_pat)
852 DecBrL NoExt decls -> quote "d" (p_hsDecls Free decls)
856 DecBrL NoExt decls -> quote "d" (p_hsDecls Free decls)
853 DecBrG NoExt _ -> notImplemented "DecBrG" -- result of renamer
857 DecBrG NoExt _ -> notImplemented "DecBrG" -- result of renamer
@@ -967,3 +971,12 @@ withGuards = any (checkOne . unLoc)
967 checkOne :: GRHS GhcPs (Located body) -> Bool
971 checkOne :: GRHS GhcPs (Located body) -> Bool
968 checkOne (GRHS NoExt [] _) = False
972 checkOne (GRHS NoExt [] _) = False
969 checkOne _ = True
973 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
16import Ormolu.Parser.Pragma
16import Ormolu.Parser.Pragma
17import Ormolu.Printer.Combinators
17import Ormolu.Printer.Combinators
18import Ormolu.Printer.Comments
18import Ormolu.Printer.Comments
19−import Ormolu.Printer.Internal (isNewlineModified)
20import Ormolu.Printer.Meat.Common
19import Ormolu.Printer.Meat.Common
21import Ormolu.Printer.Meat.Declaration
20import Ormolu.Printer.Meat.Declaration
22import Ormolu.Printer.Meat.Declaration.Warning
21import Ormolu.Printer.Meat.Declaration.Warning