@@ -33,45 +33,84 @@ module Development.IDE.GHC.ExactPrint
33
33
)
34
34
where
35
35
36
- import BasicTypes (appPrec )
37
- import Control.Applicative (Alternative )
38
- import Control.Monad
39
- import qualified Control.Monad.Fail as Fail
40
- import Control.Monad.IO.Class (MonadIO )
41
- import Control.Monad.Trans.Class
42
- import Control.Monad.Trans.Except
43
- import Control.Monad.Zip
44
- import qualified Data.DList as DL
45
- import Data.Either.Extra (mapLeft )
46
- import Data.Functor.Classes
47
- import Data.Functor.Contravariant
48
- import qualified Data.Text as T
49
- import Development.IDE.Core.RuleTypes
50
- import Development.IDE.Core.Service (runAction )
51
- import Development.IDE.Core.Shake
52
- import Development.IDE.GHC.Compat hiding (parseExpr )
53
- import Development.IDE.Types.Location
54
- import Development.Shake (RuleResult , Rules )
55
- import Development.Shake.Classes
56
- import qualified GHC.Generics as GHC
57
- import Generics.SYB
58
- import Ide.PluginUtils
59
- import Language.Haskell.GHC.ExactPrint
60
- import Language.Haskell.GHC.ExactPrint.Parsers
61
- import Language.LSP.Types
62
- import Language.LSP.Types.Capabilities (ClientCapabilities )
63
- import Outputable (Outputable , ppr , showSDoc )
64
- import Retrie.ExactPrint hiding (parseDecl , parseExpr , parsePattern , parseType )
65
- import Parser (parseIdentifier )
66
- import Data.Traversable (for )
67
- import Data.Foldable (Foldable (fold ))
68
- import Data.Bool (bool )
69
- import Data.Monoid (All (All ), Any (Any ))
70
- import Data.Functor.Compose (Compose (Compose ))
36
+ import BasicTypes (appPrec )
37
+ import Control.Applicative (Alternative )
38
+ import Control.Monad
39
+ import qualified Control.Monad.Fail as Fail
40
+ import Control.Monad.IO.Class (MonadIO )
41
+ import Control.Monad.Trans.Class
42
+ import Control.Monad.Trans.Except
43
+ import Control.Monad.Zip
44
+ import qualified Data.DList as DL
45
+ import Data.Either.Extra (mapLeft )
46
+ import Data.Functor.Classes
47
+ import Data.Functor.Contravariant
48
+ import qualified Data.Text as T
49
+ import Development.IDE.Core.RuleTypes
50
+ import Development.IDE.Core.Service (runAction )
51
+ import Development.IDE.Core.Shake
52
+ import Development.IDE.GHC.Compat hiding (parseExpr )
53
+ import Development.IDE.Types.Location
54
+ import Development.Shake (RuleResult , Rules )
55
+ import Development.Shake.Classes
56
+ import qualified GHC.Generics as GHC
57
+ import Generics.SYB
58
+ import Ide.PluginUtils
59
+ import Language.Haskell.GHC.ExactPrint
60
+ import Language.Haskell.GHC.ExactPrint.Parsers
61
+ import Language.LSP.Types
62
+ import Language.LSP.Types.Capabilities (ClientCapabilities )
63
+ import Outputable (Outputable , ppr ,
64
+ showSDoc )
65
+ -- import Retrie.ExactPrint hiding (parseDecl, parseExpr, parsePattern, parseType)
66
+ import Data.Bool (bool )
67
+ import Data.Foldable (Foldable (fold ))
68
+ import Data.Functor.Compose (Compose (Compose ))
69
+ import Data.Monoid (All (All ), Any (Any ))
70
+ import Data.Traversable (for )
71
+ import Language.Haskell.GHC.ExactPrint.Annotate (Annotate )
72
+ import Parser (parseIdentifier )
71
73
#if __GLASGOW_HASKELL__ == 808
72
- import Control.Arrow
74
+ import Control.Arrow
73
75
#endif
74
76
77
+ import Control.DeepSeq (rwhnf )
78
+
79
+ -- Start stolen from retrie and not compatible with retrie
80
+
81
+ -- | 'Annotated' packages an AST fragment with the annotations necessary to
82
+ -- 'exactPrint' or 'transform' that AST.
83
+ data Annotated ast = Annotated
84
+ { astA :: ast
85
+ -- ^ Examine the actual AST.
86
+ , annsA :: Anns
87
+ -- ^ Annotations generated/consumed by ghc-exactprint
88
+ , seedA :: Int
89
+ -- ^ Name supply used by ghc-exactprint to generate unique locations.
90
+ }
91
+ deriving (Show )
92
+
93
+ -- | Construct an 'Annotated'.
94
+ -- This should really only be used in the parsing functions, hence the scary name.
95
+ -- Don't use this unless you know what you are doing.
96
+ unsafeMkA :: ast -> Anns -> Int -> Annotated ast
97
+ unsafeMkA = Annotated
98
+
99
+ -- | Exactprint an 'Annotated' thing.
100
+ printA :: Annotate ast => Annotated (Located ast ) -> String
101
+ printA (Annotated ast anns _) = exactPrint ast anns
102
+
103
+ -- | Transform an 'Annotated' thing.
104
+ transformA
105
+ :: Monad m => Annotated ast1 -> (ast1 -> TransformT m ast2 ) -> m (Annotated ast2 )
106
+ transformA (Annotated ast anns seed) f = do
107
+ (ast',(anns',seed'),_) <- runTransformFromT seed anns (f ast)
108
+ return $ Annotated ast' anns' seed'
109
+
110
+ instance NFData (Annotated a ) where
111
+ rnf = rwhnf
112
+
113
+ -- End stolen from retrie and not compatible with retrie
75
114
76
115
------------------------------------------------------------------------------
77
116
@@ -233,7 +272,7 @@ graft' needs_space dst val = Graft $ \dflags a -> do
233
272
( mkT $
234
273
\ case
235
274
(L src _ :: Located ast ) | src == dst -> val'
236
- l -> l
275
+ l -> l
237
276
)
238
277
a
239
278
@@ -539,7 +578,7 @@ smallestM q f = fmap snd . go
539
578
Just True -> do
540
579
it@ (r, x') <- gmapMQ go x
541
580
case r of
542
- Any True -> pure it
581
+ Any True -> pure it
543
582
Any False -> fmap (Any True ,) $ f x'
544
583
Just False -> pure (mempty , x)
545
584
@@ -559,9 +598,9 @@ largestM q f = go
559
598
go :: GenericM m
560
599
go x = do
561
600
case q x of
562
- Just True -> f x
601
+ Just True -> f x
563
602
Just False -> pure x
564
- Nothing -> gmapM go x
603
+ Nothing -> gmapM go x
565
604
566
605
newtype MonadicQuery r m a = MonadicQuery
567
606
{ runMonadicQuery :: m (r , a )
0 commit comments