@@ -14,53 +14,37 @@ module Ide.Plugin.Tactic
14
14
) where
15
15
16
16
import Bag (listToBag , bagToList )
17
- import Control.Arrow
18
17
import Control.Monad
19
18
import Control.Monad.Trans
20
19
import Control.Monad.Trans.Maybe
21
20
import Data.Aeson
22
21
import Data.Bool (bool )
23
- import Data.Coerce
24
22
import Data.Data (Data )
25
- import Data.Functor ((<&>) )
26
23
import Data.Generics.Aliases (mkQ )
27
24
import Data.Generics.Schemes (everything )
28
- import Data.Map (Map )
29
- import qualified Data.Map as M
30
25
import Data.Maybe
31
26
import Data.Monoid
32
- import qualified Data.Set as S
33
27
import qualified Data.Text as T
34
28
import Data.Traversable
35
- import Development.IDE.Core.PositionMapping
36
- import Development.IDE.Core.RuleTypes
37
- import Development.IDE.Core.Service (runAction )
38
- import Development.IDE.Core.Shake (useWithStale , IdeState (.. ))
29
+ import Development.IDE.Core.Shake (IdeState (.. ))
39
30
import Development.IDE.GHC.Compat
40
- import Development.IDE.GHC.Error (realSrcSpanToRange )
41
31
import Development.IDE.GHC.ExactPrint
42
- import Development.IDE.Spans.LocalBindings (Bindings , getDefiningBindings )
43
- import Development.Shake (RuleResult , Action )
44
- import qualified FastString
32
+ import Development.Shake.Classes
45
33
import Ide.Plugin.Tactic.CaseSplit
46
- import Ide.Plugin.Tactic.Context
47
34
import Ide.Plugin.Tactic.GHC
48
- import Ide.Plugin.Tactic.Judgements
35
+ import Ide.Plugin.Tactic.LanguageServer
49
36
import Ide.Plugin.Tactic.LanguageServer.TacticProviders
50
37
import Ide.Plugin.Tactic.Range
51
38
import Ide.Plugin.Tactic.Tactics
52
39
import Ide.Plugin.Tactic.TestTypes
53
40
import Ide.Plugin.Tactic.Types
54
41
import Ide.Types
55
42
import Language.LSP.Server
56
- import Language.LSP.Types.Capabilities
57
43
import Language.LSP.Types
44
+ import Language.LSP.Types.Capabilities
58
45
import OccName
59
46
import Prelude hiding (span )
60
- import SrcLoc (containsSpan )
61
47
import System.Timeout
62
- import TcRnTypes (tcg_binds )
63
- import Development.Shake.Classes
64
48
65
49
66
50
descriptor :: PluginId -> PluginDescriptor IdeState
@@ -86,20 +70,6 @@ tcCommandName :: TacticCommand -> T.Text
86
70
tcCommandName = T. pack . show
87
71
88
72
89
- runIde :: IdeState -> Action a -> IO a
90
- runIde state = runAction " tactic" state
91
-
92
- runStaleIde
93
- :: forall a r
94
- . ( r ~ RuleResult a
95
- , Eq a , Hashable a , Binary a , Show a , Typeable a , NFData a
96
- , Show r , Typeable r , NFData r
97
- )
98
- => IdeState
99
- -> NormalizedFilePath
100
- -> a
101
- -> MaybeT IO (r , PositionMapping )
102
- runStaleIde state nfp a = MaybeT $ runIde state $ useWithStale a nfp
103
73
104
74
105
75
codeActionProvider :: PluginMethodHandler IdeState TextDocumentCodeAction
@@ -119,90 +89,6 @@ codeActionProvider state plId (CodeActionParams _ _ (TextDocumentIdentifier uri)
119
89
codeActionProvider _ _ _ = pure $ Right $ List []
120
90
121
91
122
-
123
- ------------------------------------------------------------------------------
124
- -- | Find the last typechecked module, and find the most specific span, as well
125
- -- as the judgement at the given range.
126
- judgementForHole
127
- :: IdeState
128
- -> NormalizedFilePath
129
- -> Range
130
- -> MaybeT IO (Range , Judgement , Context , DynFlags )
131
- judgementForHole state nfp range = do
132
- (asts, amapping) <- runStaleIde state nfp GetHieAst
133
- case asts of
134
- HAR _ _ _ _ (HieFromDisk _) -> fail " Need a fresh hie file"
135
- HAR _ hf _ _ HieFresh -> do
136
- (binds, _) <- runStaleIde state nfp GetBindings
137
- (tcmod, _) <- runStaleIde state nfp TypeCheck
138
- (rss, g) <- liftMaybe $ getSpanAndTypeAtHole amapping range hf
139
- resulting_range <- liftMaybe $ toCurrentRange amapping $ realSrcSpanToRange rss
140
- let (jdg, ctx) = mkJudgementAndContext g binds rss tcmod
141
- dflags <- getIdeDynflags state nfp
142
- pure (resulting_range, jdg, ctx, dflags)
143
-
144
-
145
- getIdeDynflags
146
- :: IdeState
147
- -> NormalizedFilePath
148
- -> MaybeT IO DynFlags
149
- getIdeDynflags state nfp = do
150
- -- Ok to use the stale 'ModIface', since all we need is its 'DynFlags'
151
- -- which don't change very often.
152
- ((modsum,_), _) <- runStaleIde state nfp GetModSummaryWithoutTimestamps
153
- pure $ ms_hspp_opts modsum
154
-
155
-
156
- getSpanAndTypeAtHole
157
- :: PositionMapping
158
- -> Range
159
- -> HieASTs b
160
- -> Maybe (Span , b )
161
- getSpanAndTypeAtHole amapping range hf = do
162
- range' <- fromCurrentRange amapping range
163
- join $ listToMaybe $ M. elems $ flip M. mapWithKey (getAsts hf) $ \ fs ast ->
164
- case selectSmallestContaining (rangeToRealSrcSpan (FastString. unpackFS fs) range') ast of
165
- Nothing -> Nothing
166
- Just ast' -> do
167
- let info = nodeInfo ast'
168
- ty <- listToMaybe $ nodeType info
169
- guard $ (" HsUnboundVar" ," HsExpr" ) `S.member` nodeAnnotations info
170
- pure (nodeSpan ast', ty)
171
-
172
-
173
- mkJudgementAndContext
174
- :: Type
175
- -> Bindings
176
- -> RealSrcSpan
177
- -> TcModuleResult
178
- -> (Judgement , Context )
179
- mkJudgementAndContext g binds rss tcmod = do
180
- let tcg = tmrTypechecked tcmod
181
- tcs = tcg_binds tcg
182
- ctx = mkContext
183
- (mapMaybe (sequenceA . (occName *** coerce))
184
- $ getDefiningBindings binds rss)
185
- tcg
186
- top_provs = getRhsPosVals rss tcs
187
- local_hy = spliceProvenance top_provs
188
- $ hypothesisFromBindings rss binds
189
- cls_hy = contextMethodHypothesis ctx
190
- in ( mkFirstJudgement
191
- (local_hy <> cls_hy)
192
- (isRhsHole rss tcs)
193
- g
194
- , ctx
195
- )
196
-
197
- spliceProvenance
198
- :: Map OccName Provenance
199
- -> Hypothesis a
200
- -> Hypothesis a
201
- spliceProvenance provs x =
202
- Hypothesis $ flip fmap (unHypothesis x) $ \ hi ->
203
- overProvenance (maybe id const $ M. lookup (hi_name hi) provs) hi
204
-
205
-
206
92
tacticCmd :: (OccName -> TacticsM () ) -> CommandFunction IdeState TacticParams
207
93
tacticCmd tac state (TacticParams uri range var_name)
208
94
| Just nfp <- uriToNormalizedFilePath $ toNormalizedUri uri = do
@@ -355,46 +241,16 @@ fromMaybeT :: Functor m => a -> MaybeT m a -> m a
355
241
fromMaybeT def = fmap (fromMaybe def) . runMaybeT
356
242
357
243
358
- liftMaybe :: Monad m => Maybe a -> MaybeT m a
359
- liftMaybe a = MaybeT $ pure a
360
244
361
245
362
- ------------------------------------------------------------------------------
363
- -- | Is this hole immediately to the right of an equals sign?
364
- isRhsHole :: RealSrcSpan -> TypecheckedSource -> Bool
365
- isRhsHole rss tcs = everything (||) (mkQ False $ \ case
366
- TopLevelRHS _ _ (L (RealSrcSpan span ) _) -> containsSpan rss span
367
- _ -> False
368
- ) tcs
369
-
370
-
371
- ------------------------------------------------------------------------------
372
- -- | Compute top-level position vals of a function
373
- getRhsPosVals :: RealSrcSpan -> TypecheckedSource -> Map OccName Provenance
374
- getRhsPosVals rss tcs
375
- = M. fromList
376
- $ join
377
- $ maybeToList
378
- $ getFirst
379
- $ everything (<>) (mkQ mempty $ \ case
380
- TopLevelRHS name ps
381
- (L (RealSrcSpan span ) -- body with no guards and a single defn
382
- (HsVar _ (L _ hole)))
383
- | containsSpan rss span -- which contains our span
384
- , isHole $ occName hole -- and the span is a hole
385
- -> First $ do
386
- patnames <- traverse getPatName ps
387
- pure $ zip patnames $ [0 .. ] <&> TopLevelArgPrv name
388
- _ -> mempty
389
- ) tcs
390
-
391
246
392
247
locateBiggest :: (Data r , Data a ) => SrcSpan -> a -> Maybe r
393
248
locateBiggest ss x = getFirst $ everything (<>)
394
249
( mkQ mempty $ \ case
395
250
L span r | ss `isSubspanOf` span -> pure r
396
251
_ -> mempty
397
- )x
252
+ ) x
253
+
398
254
399
255
locateFirst :: (Data r , Data a ) => a -> Maybe r
400
256
locateFirst x = getFirst $ everything (<>)
0 commit comments