@@ -22,11 +22,14 @@ import Data.Either (fromRight,
22
22
import Data.Functor ((<&>) )
23
23
import Data.IORef.Extra
24
24
import qualified Data.Map as Map
25
- import Data.Maybe (fromMaybe )
25
+ import Data.Maybe (fromMaybe ,
26
+ maybeToList )
26
27
import qualified Data.Text as T
27
28
import qualified Data.Text.Utf16.Rope.Mixed as Rope
28
29
import Development.IDE hiding
29
30
(pluginHandlers )
31
+ import Development.IDE.Core.PluginUtils (activeDiagnosticsInRange ,
32
+ uriToNormalizedFilePathE )
30
33
import Development.IDE.Core.Shake
31
34
import Development.IDE.GHC.Compat
32
35
import Development.IDE.GHC.ExactPrint
@@ -53,38 +56,42 @@ type GhcideCodeAction = ExceptT PluginError (ReaderT CodeActionArgs IO) GhcideCo
53
56
-------------------------------------------------------------------------------------------------
54
57
55
58
runGhcideCodeAction :: IdeState -> MessageParams Method_TextDocumentCodeAction -> GhcideCodeAction -> HandlerM Config GhcideCodeActionResult
56
- runGhcideCodeAction state (CodeActionParams _ _ (TextDocumentIdentifier uri) _range CodeActionContext {_diagnostics = diags}) codeAction = do
57
- let mbFile = toNormalizedFilePath' <$> uriToFilePath uri
58
- runRule key = runAction (" GhcideCodeActions." <> show key) state $ runMaybeT $ MaybeT (pure mbFile) >>= MaybeT . use key
59
- caaGhcSession <- onceIO $ runRule GhcSession
60
- caaExportsMap <-
61
- onceIO $
62
- caaGhcSession >>= \ case
63
- Just env -> do
64
- pkgExports <- envPackageExports env
65
- localExports <- readTVarIO (exportsMap $ shakeExtras state)
66
- pure $ localExports <> pkgExports
67
- _ -> pure mempty
68
- caaIdeOptions <- onceIO $ runAction " GhcideCodeActions.getIdeOptions" state getIdeOptions
69
- caaParsedModule <- onceIO $ runRule GetParsedModuleWithComments
70
- caaContents <-
71
- onceIO $
72
- runRule GetFileContents <&> \ case
73
- Just (_, mbContents) -> fmap Rope. toText mbContents
74
- Nothing -> Nothing
75
- caaDf <- onceIO $ fmap (ms_hspp_opts . pm_mod_summary) <$> caaParsedModule
76
- caaAnnSource <- onceIO $ runRule GetAnnotatedParsedSource
77
- caaTmr <- onceIO $ runRule TypeCheck
78
- caaHar <- onceIO $ runRule GetHieAst
79
- caaBindings <- onceIO $ runRule GetBindings
80
- caaGblSigs <- onceIO $ runRule GetGlobalBindingTypeSigs
81
- results <- liftIO $
82
- sequence
83
- [ runReaderT (runExceptT codeAction) CodeActionArgs {.. }
84
- | caaDiagnostic <- diags
85
- ]
86
- let (_errs, successes) = partitionEithers results
87
- pure $ concat successes
59
+ runGhcideCodeAction state (CodeActionParams _ _ (TextDocumentIdentifier uri) _range _) codeAction
60
+ | Just nfp <- toNormalizedFilePath' <$> uriToFilePath uri = do
61
+ let runRule key = runAction (" GhcideCodeActions." <> show key) state $ runMaybeT $ MaybeT (pure (Just nfp)) >>= MaybeT . use key
62
+ caaGhcSession <- onceIO $ runRule GhcSession
63
+ caaExportsMap <-
64
+ onceIO $
65
+ caaGhcSession >>= \ case
66
+ Just env -> do
67
+ pkgExports <- envPackageExports env
68
+ localExports <- readTVarIO (exportsMap $ shakeExtras state)
69
+ pure $ localExports <> pkgExports
70
+ _ -> pure mempty
71
+ caaIdeOptions <- onceIO $ runAction " GhcideCodeActions.getIdeOptions" state getIdeOptions
72
+ caaParsedModule <- onceIO $ runRule GetParsedModuleWithComments
73
+ caaContents <-
74
+ onceIO $
75
+ runRule GetFileContents <&> \ case
76
+ Just (_, mbContents) -> fmap Rope. toText mbContents
77
+ Nothing -> Nothing
78
+ caaDf <- onceIO $ fmap (ms_hspp_opts . pm_mod_summary) <$> caaParsedModule
79
+ caaAnnSource <- onceIO $ runRule GetAnnotatedParsedSource
80
+ caaTmr <- onceIO $ runRule TypeCheck
81
+ caaHar <- onceIO $ runRule GetHieAst
82
+ caaBindings <- onceIO $ runRule GetBindings
83
+ caaGblSigs <- onceIO $ runRule GetGlobalBindingTypeSigs
84
+ diags <- concat . maybeToList <$> activeDiagnosticsInRange (shakeExtras state) nfp _range
85
+ results <- liftIO $
86
+ sequence
87
+ [
88
+ runReaderT (runExceptT codeAction) CodeActionArgs {.. }
89
+ | caaDiagnostic <- diags
90
+ ]
91
+ let (_errs, successes) = partitionEithers results
92
+ pure $ concat successes
93
+ | otherwise = pure []
94
+
88
95
89
96
mkCA :: T. Text -> Maybe CodeActionKind -> Maybe Bool -> [Diagnostic ] -> WorkspaceEdit -> (Command |? CodeAction )
90
97
mkCA title kind isPreferred diags edit =
@@ -145,7 +152,7 @@ data CodeActionArgs = CodeActionArgs
145
152
caaHar :: IO (Maybe HieAstResult ),
146
153
caaBindings :: IO (Maybe Bindings ),
147
154
caaGblSigs :: IO (Maybe GlobalBindingTypeSigsResult ),
148
- caaDiagnostic :: Diagnostic
155
+ caaDiagnostic :: FileDiagnostic
149
156
}
150
157
151
158
-- | There's no concurrency in each provider,
@@ -223,6 +230,9 @@ instance ToCodeAction r => ToCodeAction (IdeOptions -> r) where
223
230
toCodeAction = toCodeAction3 caaIdeOptions
224
231
225
232
instance ToCodeAction r => ToCodeAction (Diagnostic -> r ) where
233
+ toCodeAction f = ExceptT . ReaderT $ \ caa@ CodeActionArgs {caaDiagnostic = x} -> flip runReaderT caa . runExceptT . toCodeAction $ f (fdLspDiagnostic x)
234
+
235
+ instance ToCodeAction r => ToCodeAction (FileDiagnostic -> r ) where
226
236
toCodeAction f = ExceptT . ReaderT $ \ caa@ CodeActionArgs {caaDiagnostic = x} -> flip runReaderT caa . runExceptT . toCodeAction $ f x
227
237
228
238
instance ToCodeAction r => ToCodeAction (Maybe ParsedModule -> r ) where
0 commit comments