Skip to content

Commit 7177349

Browse files
Jana Chadtfendor
authored andcommitted
Introduce PluginMethod Typeclass hierarchy
The hierarchy looks as follows: PluginMethod (pluginEnabled) | ----------------------------------- | | PluginRequestMethod PluginNotificationMethod
1 parent 205b850 commit 7177349

File tree

2 files changed

+135
-72
lines changed

2 files changed

+135
-72
lines changed

ghcide/src/Development/IDE/Plugin/HLS.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ extensibleNotificationPlugins recorder xs = mempty { P.pluginHandlers = handlers
218218
case cleanPluginInfo of
219219
Left _ -> pure ()
220220
Right pluginInfos -> do
221-
let fs = map snd $ filter (\((_, desc), _) -> pluginEnabled2 m params desc config) (zip pluginInfos fs')
221+
let fs = map snd $ filter (\((_, desc), _) -> pluginEnabled m params desc config) (zip pluginInfos fs')
222222
case nonEmpty fs of
223223
Nothing -> do
224224
logWith recorder Info LogNoEnabledPlugins

hls-plugin-api/src/Ide/Types.hs

Lines changed: 134 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
{-# LANGUAGE TypeFamilies #-}
1818
{-# LANGUAGE UndecidableInstances #-}
1919
{-# LANGUAGE ViewPatterns #-}
20+
{-# LANGUAGE MultiParamTypeClasses #-}
2021

2122
module Ide.Types
2223
where
@@ -165,11 +166,25 @@ defaultConfigDescriptor = ConfigDescriptor True False (mkCustomConfig emptyPrope
165166
-- | Methods that can be handled by plugins.
166167
-- 'ExtraParams' captures any extra data the IDE passes to the handlers for this method
167168
-- Only methods for which we know how to combine responses can be instances of 'PluginMethod'
168-
class HasTracing (MessageParams m) => PluginMethod m where
169+
class HasTracing (MessageParams m) => PluginMethod (k :: MethodType) (m :: Method FromClient k) where
169170

170171
-- | Parse the configuration to check if this plugin is enabled
171-
pluginEnabled :: SMethod m -> MessageParams m -> PluginDescriptor c -> Config -> Bool
172+
pluginEnabled
173+
:: SMethod m
174+
-> MessageParams m
175+
-- ^ Whether a plugin is enabled might depend on the message parameters
176+
-- eg 'pluginFileType' specifies what file extension a plugin is allowed to handle
177+
-> PluginDescriptor c
178+
-> Config
179+
-> Bool
172180

181+
default pluginEnabled :: (HasTextDocument (MessageParams m) doc, HasUri doc Uri)
182+
=> SMethod m -> MessageParams m -> PluginDescriptor c -> Config -> Bool
183+
pluginEnabled _ params desc conf = pluginResponsible uri desc && plcGlobalOn (configForPlugin conf (pluginId desc))
184+
where
185+
uri = params ^. J.textDocument . J.uri
186+
187+
class PluginMethod Request m => PluginRequestMethod (m :: Method FromClient Request) where
173188
-- | How to combine responses from different plugins
174189
combineResponses
175190
:: SMethod m
@@ -182,11 +197,14 @@ class HasTracing (MessageParams m) => PluginMethod m where
182197
=> SMethod m -> Config -> ClientCapabilities -> MessageParams m -> NonEmpty (ResponseResult m) -> ResponseResult m
183198
combineResponses _method _config _caps _params = sconcat
184199

185-
instance PluginMethod TextDocumentCodeAction where
200+
201+
instance PluginMethod Request TextDocumentCodeAction where
186202
pluginEnabled _ msgParams pluginDesc config =
187203
pluginResponsible uri pluginDesc && pluginEnabledConfig plcCodeActionsOn (pluginId pluginDesc) config
188204
where
189205
uri = msgParams ^. J.textDocument . J.uri
206+
207+
instance PluginRequestMethod TextDocumentCodeAction where
190208
combineResponses _method _config (ClientCapabilities _ textDocCaps _ _ _) (CodeActionParams _ _ _ _ context) resps =
191209
fmap compat $ List $ filter wasRequested $ (\(List x) -> x) $ sconcat resps
192210
where
@@ -224,64 +242,129 @@ pluginResponsible uri pluginDesc
224242
where
225243
mfp = uriToFilePath uri
226244

227-
instance PluginMethod TextDocumentDefinition where
245+
instance PluginMethod Request TextDocumentDefinition where
228246
pluginEnabled _ msgParams pluginDesc _ =
229247
pluginResponsible uri pluginDesc
230248
where
231249
uri = msgParams ^. J.textDocument . J.uri
232-
combineResponses _ _ _ _ (x :| _) = x
233250

234-
instance PluginMethod TextDocumentTypeDefinition where
251+
instance PluginMethod Request TextDocumentTypeDefinition where
235252
pluginEnabled _ msgParams pluginDesc _ =
236253
pluginResponsible uri pluginDesc
237254
where
238255
uri = msgParams ^. J.textDocument . J.uri
239-
combineResponses _ _ _ _ (x :| _) = x
240256

241-
instance PluginMethod TextDocumentDocumentHighlight where
257+
instance PluginMethod Request TextDocumentDocumentHighlight where
242258
pluginEnabled _ msgParams pluginDesc _ =
243259
pluginResponsible uri pluginDesc
244260
where
245261
uri = msgParams ^. J.textDocument . J.uri
246262

247-
instance PluginMethod TextDocumentReferences where
263+
instance PluginMethod Request TextDocumentReferences where
248264
pluginEnabled _ msgParams pluginDesc _ =
249265
pluginResponsible uri pluginDesc
250266
where
251267
uri = msgParams ^. J.textDocument . J.uri
252268

253-
instance PluginMethod WorkspaceSymbol where
269+
instance PluginMethod Request WorkspaceSymbol where
254270
pluginEnabled _ _ _ _ = True
255271

256-
instance PluginMethod TextDocumentCodeLens where
272+
instance PluginMethod Request TextDocumentCodeLens where
257273
pluginEnabled _ msgParams pluginDesc config =
258274
pluginResponsible uri pluginDesc
259275
&& pluginEnabledConfig plcCodeLensOn (pluginId pluginDesc) config
260276
where
261277
uri = msgParams ^. J.textDocument . J.uri
262278

263-
instance PluginMethod TextDocumentRename where
279+
instance PluginMethod Request TextDocumentRename where
264280
pluginEnabled _ msgParams pluginDesc config = pluginResponsible uri pluginDesc
265281
&& pluginEnabledConfig plcRenameOn (pluginId pluginDesc) config
266282
where
267283
uri = msgParams ^. J.textDocument . J.uri
268-
instance PluginMethod TextDocumentHover where
284+
instance PluginMethod Request TextDocumentHover where
269285
pluginEnabled _ msgParams pluginDesc config = pluginResponsible uri pluginDesc
270286
&& pluginEnabledConfig plcHoverOn (pluginId pluginDesc) config
271287
where
272288
uri = msgParams ^. J.textDocument . J.uri
289+
290+
instance PluginMethod Request TextDocumentDocumentSymbol where
291+
pluginEnabled _ msgParams pluginDesc config = pluginResponsible uri pluginDesc
292+
&& pluginEnabledConfig plcSymbolsOn (pluginId pluginDesc) config
293+
where
294+
uri = msgParams ^. J.textDocument . J.uri
295+
296+
instance PluginMethod Request TextDocumentCompletion where
297+
pluginEnabled _ msgParams pluginDesc config = pluginResponsible uri pluginDesc
298+
&& pluginEnabledConfig plcCompletionOn (pluginId pluginDesc) config
299+
where
300+
uri = msgParams ^. J.textDocument . J.uri
301+
302+
instance PluginMethod Request TextDocumentFormatting where
303+
pluginEnabled STextDocumentFormatting msgParams pluginDesc conf =
304+
pluginResponsible uri pluginDesc && PluginId (formattingProvider conf) == pid
305+
where
306+
uri = msgParams ^. J.textDocument . J.uri
307+
pid = pluginId pluginDesc
308+
309+
instance PluginMethod Request TextDocumentRangeFormatting where
310+
pluginEnabled _ msgParams pluginDesc conf = pluginResponsible uri pluginDesc
311+
&& PluginId (formattingProvider conf) == pid
312+
where
313+
uri = msgParams ^. J.textDocument . J.uri
314+
pid = pluginId pluginDesc
315+
316+
instance PluginMethod Request TextDocumentPrepareCallHierarchy where
317+
pluginEnabled _ msgParams pluginDesc conf = pluginResponsible uri pluginDesc
318+
&& pluginEnabledConfig plcCallHierarchyOn pid conf
319+
where
320+
uri = msgParams ^. J.textDocument . J.uri
321+
pid = pluginId pluginDesc
322+
323+
instance PluginMethod Request TextDocumentSelectionRange where
324+
pluginEnabled _ msgParams pluginDesc conf = pluginResponsible uri pluginDesc && pluginEnabledConfig plcSelectionRangeOn pid conf
325+
where
326+
uri = msgParams ^. J.textDocument . J.uri
327+
pid = pluginId pluginDesc
328+
329+
instance PluginMethod Request CallHierarchyIncomingCalls where
330+
pluginEnabled _ _ pluginDesc conf = pluginEnabledConfig plcCallHierarchyOn pid conf
331+
where
332+
pid = pluginId pluginDesc
333+
334+
instance PluginMethod Request CallHierarchyOutgoingCalls where
335+
pluginEnabled _ _ pluginDesc conf = pluginEnabledConfig plcCallHierarchyOn pid conf
336+
where
337+
pid = pluginId pluginDesc
338+
339+
instance PluginMethod Request CustomMethod where
340+
pluginEnabled _ _ _ _ = True
341+
342+
---
343+
instance PluginRequestMethod TextDocumentDefinition where
344+
combineResponses _ _ _ _ (x :| _) = x
345+
346+
instance PluginRequestMethod TextDocumentTypeDefinition where
347+
combineResponses _ _ _ _ (x :| _) = x
348+
349+
instance PluginRequestMethod TextDocumentDocumentHighlight where
350+
351+
instance PluginRequestMethod TextDocumentReferences where
352+
353+
instance PluginRequestMethod WorkspaceSymbol where
354+
355+
instance PluginRequestMethod TextDocumentCodeLens where
356+
357+
instance PluginRequestMethod TextDocumentRename where
358+
359+
instance PluginRequestMethod TextDocumentHover where
273360
combineResponses _ _ _ _ (catMaybes . toList -> hs) = h
274361
where
275362
r = listToMaybe $ mapMaybe (^. range) hs
276363
h = case foldMap (^. contents) hs of
277364
HoverContentsMS (List []) -> Nothing
278365
hh -> Just $ Hover hh r
279366

280-
instance PluginMethod TextDocumentDocumentSymbol where
281-
pluginEnabled _ msgParams pluginDesc config = pluginResponsible uri pluginDesc
282-
&& pluginEnabledConfig plcSymbolsOn (pluginId pluginDesc) config
283-
where
284-
uri = msgParams ^. J.textDocument . J.uri
367+
instance PluginRequestMethod TextDocumentDocumentSymbol where
285368
combineResponses _ _ (ClientCapabilities _ tdc _ _ _) params xs = res
286369
where
287370
uri' = params ^. textDocument . uri
@@ -302,11 +385,7 @@ instance PluginMethod TextDocumentDocumentSymbol where
302385
si = SymbolInformation name' (ds ^. kind) Nothing (ds ^. deprecated) loc parent
303386
in [si] <> children'
304387

305-
instance PluginMethod TextDocumentCompletion where
306-
pluginEnabled _ msgParams pluginDesc config = pluginResponsible uri pluginDesc
307-
&& pluginEnabledConfig plcCompletionOn (pluginId pluginDesc) config
308-
where
309-
uri = msgParams ^. J.textDocument . J.uri
388+
instance PluginRequestMethod TextDocumentCompletion where
310389
combineResponses _ conf _ _ (toList -> xs) = snd $ consumeCompletionResponse limit $ combine xs
311390
where
312391
limit = maxCompletions conf
@@ -334,60 +413,36 @@ instance PluginMethod TextDocumentCompletion where
334413
consumeCompletionResponse n (InL (List xx)) =
335414
consumeCompletionResponse n (InR (CompletionList isCompleteResponse (List xx)))
336415

337-
instance PluginMethod TextDocumentFormatting where
338-
pluginEnabled STextDocumentFormatting msgParams pluginDesc conf =
339-
pluginResponsible uri pluginDesc && PluginId (formattingProvider conf) == pid
340-
where
341-
uri = msgParams ^. J.textDocument . J.uri
342-
pid = pluginId pluginDesc
343-
combineResponses _ _ _ _ x = sconcat x
344-
416+
instance PluginRequestMethod TextDocumentFormatting where
417+
combineResponses _ _ _ _ (x :| _) = x
345418

346-
instance PluginMethod TextDocumentRangeFormatting where
347-
pluginEnabled _ msgParams pluginDesc conf = pluginResponsible uri pluginDesc
348-
&& PluginId (formattingProvider conf) == pid
349-
where
350-
uri = msgParams ^. J.textDocument . J.uri
351-
pid = pluginId pluginDesc
419+
instance PluginRequestMethod TextDocumentRangeFormatting where
352420
combineResponses _ _ _ _ (x :| _) = x
353421

354-
instance PluginMethod TextDocumentPrepareCallHierarchy where
355-
pluginEnabled _ msgParams pluginDesc conf = pluginResponsible uri pluginDesc
356-
&& pluginEnabledConfig plcCallHierarchyOn pid conf
357-
where
358-
uri = msgParams ^. J.textDocument . J.uri
359-
pid = pluginId pluginDesc
422+
instance PluginRequestMethod TextDocumentPrepareCallHierarchy where
360423

361-
instance PluginMethod TextDocumentSelectionRange where
362-
pluginEnabled _ _ pluginDesc conf = pluginEnabledConfig plcSelectionRangeOn pid conf
363-
where
364-
pid = pluginId pluginDesc
424+
instance PluginRequestMethod TextDocumentSelectionRange where
365425
combineResponses _ _ _ _ (x :| _) = x
366426

367-
instance PluginMethod CallHierarchyIncomingCalls where
368-
pluginEnabled _ _ pluginDesc conf = pluginEnabledConfig plcCallHierarchyOn pid conf
369-
where
370-
pid = pluginId pluginDesc
427+
instance PluginRequestMethod CallHierarchyIncomingCalls where
371428

372-
instance PluginMethod CallHierarchyOutgoingCalls where
373-
pluginEnabled _ _ pluginDesc conf = pluginEnabledConfig plcCallHierarchyOn pid conf
374-
where
375-
pid = pluginId pluginDesc
429+
instance PluginRequestMethod CallHierarchyOutgoingCalls where
376430

377-
instance PluginMethod CustomMethod where
378-
pluginEnabled _ _ _ _ = True
431+
instance PluginRequestMethod CustomMethod where
379432
combineResponses _ _ _ _ (x :| _) = x
380-
381433
-- ---------------------------------------------------------------------
382434

383-
class HasTracing (MessageParams m) => PluginNotificationMethod (m :: Method FromClient Notification) where
384-
pluginEnabled2 :: SMethod m -> MessageParams m -> PluginDescriptor c -> Config -> Bool
435+
class PluginMethod Notification m => PluginNotificationMethod (m :: Method FromClient Notification) where
436+
437+
438+
instance PluginMethod Notification TextDocumentDidOpen where
439+
440+
instance PluginMethod Notification TextDocumentDidChange where
441+
442+
instance PluginMethod Notification TextDocumentDidSave where
443+
444+
instance PluginMethod Notification TextDocumentDidClose where
385445

386-
default pluginEnabled2 :: (HasTextDocument (MessageParams m) doc, HasUri doc Uri)
387-
=> SMethod m -> MessageParams m -> PluginDescriptor c -> Config -> Bool
388-
pluginEnabled2 _ params desc conf = pluginResponsible uri desc && plcGlobalOn (configForPlugin conf (pluginId desc))
389-
where
390-
uri = params ^. J.textDocument . J.uri
391446

392447
instance PluginNotificationMethod TextDocumentDidOpen where
393448

@@ -397,22 +452,30 @@ instance PluginNotificationMethod TextDocumentDidSave where
397452

398453
instance PluginNotificationMethod TextDocumentDidClose where
399454

455+
instance PluginMethod Notification WorkspaceDidChangeWatchedFiles where
456+
pluginEnabled _ _ desc conf = plcGlobalOn $ configForPlugin conf (pluginId desc)
457+
458+
instance PluginMethod Notification WorkspaceDidChangeWorkspaceFolders where
459+
pluginEnabled _ _ desc conf = plcGlobalOn $ configForPlugin conf (pluginId desc)
460+
461+
instance PluginMethod Notification WorkspaceDidChangeConfiguration where
462+
pluginEnabled _ _ desc conf = plcGlobalOn $ configForPlugin conf (pluginId desc)
463+
464+
instance PluginMethod Notification Initialized where
465+
pluginEnabled _ _ desc conf = plcGlobalOn $ configForPlugin conf (pluginId desc)
466+
400467
instance PluginNotificationMethod WorkspaceDidChangeWatchedFiles where
401-
pluginEnabled2 _ _ desc conf = plcGlobalOn $ configForPlugin conf (pluginId desc)
402468

403469
instance PluginNotificationMethod WorkspaceDidChangeWorkspaceFolders where
404-
pluginEnabled2 _ _ desc conf = plcGlobalOn $ configForPlugin conf (pluginId desc)
405470

406471
instance PluginNotificationMethod WorkspaceDidChangeConfiguration where
407-
pluginEnabled2 _ _ desc conf = plcGlobalOn $ configForPlugin conf (pluginId desc)
408472

409473
instance PluginNotificationMethod Initialized where
410-
pluginEnabled2 _ _ desc conf = plcGlobalOn $ configForPlugin conf (pluginId desc)
411474

412475
-- ---------------------------------------------------------------------
413476

414477
-- | Methods which have a PluginMethod instance
415-
data IdeMethod (m :: Method FromClient Request) = PluginMethod m => IdeMethod (SMethod m)
478+
data IdeMethod (m :: Method FromClient Request) = PluginRequestMethod m => IdeMethod (SMethod m)
416479
instance GEq IdeMethod where
417480
geq (IdeMethod a) (IdeMethod b) = geq a b
418481
instance GCompare IdeMethod where
@@ -458,7 +521,7 @@ type PluginNotificationMethodHandler a m = a -> VFS -> PluginId -> MessageParams
458521

459522
-- | Make a handler for plugins with no extra data
460523
mkPluginHandler
461-
:: PluginMethod m
524+
:: PluginRequestMethod m
462525
=> SClientMethod m
463526
-> PluginMethodHandler ideState m
464527
-> PluginHandlers ideState

0 commit comments

Comments
 (0)