10
10
11
11
module Ide.Plugin.ExplicitImports
12
12
( descriptor
13
+ , descriptorForModules
13
14
, extractMinimalImports
14
15
, within
15
16
) where
@@ -45,7 +46,14 @@ importCommandId = "ImportLensCommand"
45
46
46
47
-- | The "main" function of a plugin
47
48
descriptor :: PluginId -> PluginDescriptor IdeState
48
- descriptor plId =
49
+ descriptor = descriptorForModules (/= moduleName pRELUDE)
50
+
51
+ descriptorForModules
52
+ :: (ModuleName -> Bool )
53
+ -- ^ Predicate to select modules that will be annotated
54
+ -> PluginId
55
+ -> PluginDescriptor IdeState
56
+ descriptorForModules pred plId =
49
57
(defaultPluginDescriptor plId)
50
58
{
51
59
-- This plugin provides a command handler
@@ -54,9 +62,9 @@ descriptor plId =
54
62
pluginRules = minimalImportsRule,
55
63
pluginHandlers = mconcat
56
64
[ -- This plugin provides code lenses
57
- mkPluginHandler STextDocumentCodeLens lensProvider
65
+ mkPluginHandler STextDocumentCodeLens $ lensProvider pred
58
66
-- This plugin provides code actions
59
- , mkPluginHandler STextDocumentCodeAction codeActionProvider
67
+ , mkPluginHandler STextDocumentCodeAction $ codeActionProvider pred
60
68
]
61
69
}
62
70
@@ -87,8 +95,9 @@ runImportCommand _state (ImportCommandParams edit) = do
87
95
-- the provider should produce one code lens associated to the import statement:
88
96
--
89
97
-- > import Data.List (intercalate, sortBy)
90
- lensProvider :: PluginMethodHandler IdeState TextDocumentCodeLens
98
+ lensProvider :: ( ModuleName -> Bool ) -> PluginMethodHandler IdeState TextDocumentCodeLens
91
99
lensProvider
100
+ pred
92
101
state -- ghcide state, used to retrieve typechecking artifacts
93
102
pId -- plugin Id
94
103
CodeLensParams {_textDocument = TextDocumentIdentifier {_uri}}
@@ -105,7 +114,7 @@ lensProvider
105
114
sequence
106
115
[ generateLens pId _uri edit
107
116
| (imp, Just minImport) <- minImports,
108
- Just edit <- [mkExplicitEdit posMapping imp minImport]
117
+ Just edit <- [mkExplicitEdit pred posMapping imp minImport]
109
118
]
110
119
return $ Right (List $ catMaybes commands)
111
120
_ ->
@@ -115,8 +124,8 @@ lensProvider
115
124
116
125
-- | If there are any implicit imports, provide one code action to turn them all
117
126
-- into explicit imports.
118
- codeActionProvider :: PluginMethodHandler IdeState TextDocumentCodeAction
119
- codeActionProvider ideState _pId (CodeActionParams _ _ docId range _context)
127
+ codeActionProvider :: ( ModuleName -> Bool ) -> PluginMethodHandler IdeState TextDocumentCodeAction
128
+ codeActionProvider pred ideState _pId (CodeActionParams _ _ docId range _context)
120
129
| TextDocumentIdentifier {_uri} <- docId,
121
130
Just nfp <- uriToNormalizedFilePath $ toNormalizedUri _uri = liftIO $
122
131
do
@@ -135,7 +144,7 @@ codeActionProvider ideState _pId (CodeActionParams _ _ docId range _context)
135
144
[ e
136
145
| (imp, Just explicit) <-
137
146
maybe [] getMinimalImportsResult minImports,
138
- Just e <- [mkExplicitEdit zeroMapping imp explicit]
147
+ Just e <- [mkExplicitEdit pred zeroMapping imp explicit]
139
148
]
140
149
caExplicitImports = InR CodeAction {.. }
141
150
_title = " Make all imports explicit"
@@ -219,16 +228,16 @@ extractMinimalImports (Just hsc) (Just TcModuleResult {..}) = do
219
228
return (imports, minimalImports)
220
229
extractMinimalImports _ _ = return ([] , Nothing )
221
230
222
- mkExplicitEdit :: PositionMapping -> LImportDecl pass -> T. Text -> Maybe TextEdit
223
- mkExplicitEdit posMapping (L src imp) explicit
231
+ mkExplicitEdit :: ( ModuleName -> Bool ) -> PositionMapping -> LImportDecl pass -> T. Text -> Maybe TextEdit
232
+ mkExplicitEdit pred posMapping (L src imp) explicit
224
233
-- Explicit import list case
225
234
| ImportDecl {ideclHiding = Just (False , _)} <- imp =
226
235
Nothing
227
236
| not (isQualifiedImport imp),
228
237
RealSrcSpan l <- src,
229
238
L _ mn <- ideclName imp,
230
239
-- (almost) no one wants to see an explicit import list for Prelude
231
- mn /= moduleName pRELUDE ,
240
+ pred mn ,
232
241
Just rng <- toCurrentRange posMapping $ realSrcSpanToRange l =
233
242
Just $ TextEdit rng explicit
234
243
| otherwise =
0 commit comments