@@ -39,6 +39,7 @@ module Ide.Types
39
39
, PluginNotificationHandlers (.. )
40
40
, PluginRequestMethod (.. )
41
41
, getProcessID, getPid
42
+ , getVirtualFileFromVFS
42
43
, installSigUsr1Handler
43
44
, lookupCommandProvider
44
45
, ResolveFunction
@@ -94,13 +95,13 @@ import Ide.Plugin.Properties
94
95
import qualified Language.LSP.Protocol.Lens as L
95
96
import Language.LSP.Protocol.Message
96
97
import Language.LSP.Protocol.Types
98
+ import qualified Language.LSP.Protocol.Types as J
97
99
import Language.LSP.Server
98
100
import Language.LSP.VFS
99
101
import Numeric.Natural
100
102
import OpenTelemetry.Eventlog
101
103
import Options.Applicative (ParserInfo )
102
104
import Prettyprinter as PP
103
- import System.FilePath
104
105
import System.IO.Unsafe
105
106
import Text.Regex.TDFA.Text ()
106
107
import UnliftIO (MonadUnliftIO )
@@ -323,7 +324,7 @@ data PluginDescriptor (ideState :: Type) =
323
324
, pluginNotificationHandlers :: PluginNotificationHandlers ideState
324
325
, pluginModifyDynflags :: DynFlagsModifications
325
326
, pluginCli :: Maybe (ParserInfo (IdeCommand ideState ))
326
- , pluginFileType :: [T. Text ]
327
+ , pluginLanguageIds :: [J. LanguageKind ]
327
328
-- ^ File extension of the files the plugin is responsible for.
328
329
-- The plugin is only allowed to handle files with these extensions.
329
330
-- When writing handlers, etc. for this plugin it can be assumed that all handled files are of this type.
@@ -416,14 +417,18 @@ pluginResolverResponsible _ _ = DoesNotHandleRequest $ NotResolveOwner "(unable
416
417
-- We are passing the msgParams here even though we only need the URI URI here.
417
418
-- If in the future we need to be able to provide only an URI it can be
418
419
-- separated again.
419
- pluginSupportsFileType :: (L. HasTextDocument m doc , L. HasUri doc Uri ) => m -> PluginDescriptor c -> HandleRequestResult
420
- pluginSupportsFileType msgParams pluginDesc =
421
- case mfp of
422
- Just fp | T. pack (takeExtension fp) `elem` pluginFileType pluginDesc -> HandlesRequest
423
- _ -> DoesNotHandleRequest $ DoesNotSupportFileType (maybe " (unable to determine file type)" (T. pack . takeExtension) mfp )
420
+ pluginSupportsFileType :: (L. HasTextDocument m doc , L. HasUri doc Uri ) => VFS -> m -> PluginDescriptor c -> HandleRequestResult
421
+ pluginSupportsFileType ( VFS vfs) msgParams pluginDesc =
422
+ case languageKindM of
423
+ Just languageKind | languageKind `elem` pluginLanguageIds pluginDesc -> HandlesRequest
424
+ _ -> DoesNotHandleRequest $ DoesNotSupportFileType (maybe " (unable to determine file type)" (T. pack . show ) languageKindM )
424
425
where
425
- mfp = uriToFilePath uri
426
- uri = msgParams ^. L. textDocument . L. uri
426
+ mVFE = getVirtualFileFromVFSIncludingClosed (VFS vfs) uri
427
+ uri = toNormalizedUri $ msgParams ^. L. textDocument . L. uri
428
+ languageKindM =
429
+ case mVFE of
430
+ Just x -> virtualFileEntryLanguageKind x
431
+ _ -> Nothing
427
432
428
433
-- | Methods that can be handled by plugins.
429
434
-- 'ExtraParams' captures any extra data the IDE passes to the handlers for this method
@@ -452,7 +457,9 @@ class HasTracing (MessageParams m) => PluginMethod (k :: MessageKind) (m :: Meth
452
457
--
453
458
-- But there is no use to split it up into two different methods for now.
454
459
handlesRequest
455
- :: SMethod m
460
+ :: VFS
461
+ -- ^ The virtual file system, contains the language kind of the file.
462
+ -> SMethod m
456
463
-- ^ Method type.
457
464
-> MessageParams m
458
465
-- ^ Whether a plugin is enabled might depend on the message parameters
@@ -468,24 +475,24 @@ class HasTracing (MessageParams m) => PluginMethod (k :: MessageKind) (m :: Meth
468
475
-- with the given parameters?
469
476
470
477
default handlesRequest :: (L. HasTextDocument (MessageParams m ) doc , L. HasUri doc Uri )
471
- => SMethod m -> MessageParams m -> PluginDescriptor c -> Config -> HandleRequestResult
472
- handlesRequest _ params desc conf =
473
- pluginEnabledGlobally desc conf <> pluginSupportsFileType params desc
478
+ => VFS -> SMethod m -> MessageParams m -> PluginDescriptor c -> Config -> HandleRequestResult
479
+ handlesRequest vfs _ params desc conf =
480
+ pluginEnabledGlobally desc conf <> pluginSupportsFileType vfs params desc
474
481
475
482
-- | Check if a plugin is enabled, if one of it's specific config's is enabled,
476
483
-- and if it supports the file
477
484
pluginEnabledWithFeature :: (L. HasTextDocument (MessageParams m ) doc , L. HasUri doc Uri )
478
- => (PluginConfig -> Bool ) -> SMethod m -> MessageParams m
485
+ => (PluginConfig -> Bool ) -> VFS -> SMethod m -> MessageParams m
479
486
-> PluginDescriptor c -> Config -> HandleRequestResult
480
- pluginEnabledWithFeature feature _ msgParams pluginDesc config =
487
+ pluginEnabledWithFeature feature vfs _ msgParams pluginDesc config =
481
488
pluginEnabledGlobally pluginDesc config
482
489
<> pluginFeatureEnabled feature pluginDesc config
483
- <> pluginSupportsFileType msgParams pluginDesc
490
+ <> pluginSupportsFileType vfs msgParams pluginDesc
484
491
485
492
-- | Check if a plugin is enabled, if one of it's specific configs is enabled,
486
493
-- and if it's the plugin responsible for a resolve request.
487
- pluginEnabledResolve :: L. HasData_ s (Maybe Value ) => (PluginConfig -> Bool ) -> p -> s -> PluginDescriptor c -> Config -> HandleRequestResult
488
- pluginEnabledResolve feature _ msgParams pluginDesc config =
494
+ pluginEnabledResolve :: L. HasData_ s (Maybe Value ) => (PluginConfig -> Bool ) -> VFS -> p -> s -> PluginDescriptor c -> Config -> HandleRequestResult
495
+ pluginEnabledResolve feature _ _ msgParams pluginDesc config =
489
496
pluginEnabledGlobally pluginDesc config
490
497
<> pluginFeatureEnabled feature pluginDesc config
491
498
<> pluginResolverResponsible msgParams pluginDesc
@@ -498,23 +505,23 @@ instance PluginMethod Request Method_CodeActionResolve where
498
505
handlesRequest = pluginEnabledResolve plcCodeActionsOn
499
506
500
507
instance PluginMethod Request Method_TextDocumentDefinition where
501
- handlesRequest _ msgParams pluginDesc _ = pluginSupportsFileType msgParams pluginDesc
508
+ handlesRequest vfs _ msgParams pluginDesc _ = pluginSupportsFileType vfs msgParams pluginDesc
502
509
503
510
instance PluginMethod Request Method_TextDocumentTypeDefinition where
504
- handlesRequest _ msgParams pluginDesc _ = pluginSupportsFileType msgParams pluginDesc
511
+ handlesRequest vfs _ msgParams pluginDesc _ = pluginSupportsFileType vfs msgParams pluginDesc
505
512
506
513
instance PluginMethod Request Method_TextDocumentImplementation where
507
- handlesRequest _ msgParams pluginDesc _ = pluginSupportsFileType msgParams pluginDesc
514
+ handlesRequest vfs _ msgParams pluginDesc _ = pluginSupportsFileType vfs msgParams pluginDesc
508
515
509
516
instance PluginMethod Request Method_TextDocumentDocumentHighlight where
510
- handlesRequest _ msgParams pluginDesc _ = pluginSupportsFileType msgParams pluginDesc
517
+ handlesRequest vfs _ msgParams pluginDesc _ = pluginSupportsFileType vfs msgParams pluginDesc
511
518
512
519
instance PluginMethod Request Method_TextDocumentReferences where
513
- handlesRequest _ msgParams pluginDesc _ = pluginSupportsFileType msgParams pluginDesc
520
+ handlesRequest vfs _ msgParams pluginDesc _ = pluginSupportsFileType vfs msgParams pluginDesc
514
521
515
522
instance PluginMethod Request Method_WorkspaceSymbol where
516
523
-- Unconditionally enabled, but should it really be?
517
- handlesRequest _ _ _ _ = HandlesRequest
524
+ handlesRequest _ _ _ _ _ = HandlesRequest
518
525
519
526
instance PluginMethod Request Method_TextDocumentInlayHint where
520
527
handlesRequest = pluginEnabledWithFeature plcInlayHintsOn
@@ -549,22 +556,22 @@ instance PluginMethod Request Method_TextDocumentCompletion where
549
556
handlesRequest = pluginEnabledWithFeature plcCompletionOn
550
557
551
558
instance PluginMethod Request Method_TextDocumentFormatting where
552
- handlesRequest _ msgParams pluginDesc conf =
559
+ handlesRequest vfs _ msgParams pluginDesc conf =
553
560
(if PluginId (formattingProvider conf) == pid
554
561
|| PluginId (cabalFormattingProvider conf) == pid
555
562
then HandlesRequest
556
563
else DoesNotHandleRequest (NotFormattingProvider (formattingProvider conf)) )
557
- <> pluginSupportsFileType msgParams pluginDesc
564
+ <> pluginSupportsFileType vfs msgParams pluginDesc
558
565
where
559
566
pid = pluginId pluginDesc
560
567
561
568
instance PluginMethod Request Method_TextDocumentRangeFormatting where
562
- handlesRequest _ msgParams pluginDesc conf =
569
+ handlesRequest vfs _ msgParams pluginDesc conf =
563
570
(if PluginId (formattingProvider conf) == pid
564
571
|| PluginId (cabalFormattingProvider conf) == pid
565
572
then HandlesRequest
566
573
else DoesNotHandleRequest (NotFormattingProvider (formattingProvider conf)))
567
- <> pluginSupportsFileType msgParams pluginDesc
574
+ <> pluginSupportsFileType vfs msgParams pluginDesc
568
575
where
569
576
pid = pluginId pluginDesc
570
577
@@ -585,21 +592,21 @@ instance PluginMethod Request Method_TextDocumentFoldingRange where
585
592
586
593
instance PluginMethod Request Method_CallHierarchyIncomingCalls where
587
594
-- This method has no URI parameter, thus no call to 'pluginResponsible'
588
- handlesRequest _ _ pluginDesc conf =
595
+ handlesRequest _ _ _ pluginDesc conf =
589
596
pluginEnabledGlobally pluginDesc conf
590
597
<> pluginFeatureEnabled plcCallHierarchyOn pluginDesc conf
591
598
592
599
instance PluginMethod Request Method_CallHierarchyOutgoingCalls where
593
600
-- This method has no URI parameter, thus no call to 'pluginResponsible'
594
- handlesRequest _ _ pluginDesc conf =
601
+ handlesRequest _ _ _ pluginDesc conf =
595
602
pluginEnabledGlobally pluginDesc conf
596
603
<> pluginFeatureEnabled plcCallHierarchyOn pluginDesc conf
597
604
598
605
instance PluginMethod Request Method_WorkspaceExecuteCommand where
599
- handlesRequest _ _ _ _= HandlesRequest
606
+ handlesRequest _ _ _ _ _ = HandlesRequest
600
607
601
608
instance PluginMethod Request (Method_CustomMethod m ) where
602
- handlesRequest _ _ _ _ = HandlesRequest
609
+ handlesRequest _ _ _ _ _ = HandlesRequest
603
610
604
611
-- Plugin Notifications
605
612
@@ -613,19 +620,19 @@ instance PluginMethod Notification Method_TextDocumentDidClose where
613
620
614
621
instance PluginMethod Notification Method_WorkspaceDidChangeWatchedFiles where
615
622
-- This method has no URI parameter, thus no call to 'pluginResponsible'.
616
- handlesRequest _ _ desc conf = pluginEnabledGlobally desc conf
623
+ handlesRequest _ _ _ desc conf = pluginEnabledGlobally desc conf
617
624
618
625
instance PluginMethod Notification Method_WorkspaceDidChangeWorkspaceFolders where
619
626
-- This method has no URI parameter, thus no call to 'pluginResponsible'.
620
- handlesRequest _ _ desc conf = pluginEnabledGlobally desc conf
627
+ handlesRequest _ _ _ desc conf = pluginEnabledGlobally desc conf
621
628
622
629
instance PluginMethod Notification Method_WorkspaceDidChangeConfiguration where
623
630
-- This method has no URI parameter, thus no call to 'pluginResponsible'.
624
- handlesRequest _ _ desc conf = pluginEnabledGlobally desc conf
631
+ handlesRequest _ _ _ desc conf = pluginEnabledGlobally desc conf
625
632
626
633
instance PluginMethod Notification Method_Initialized where
627
634
-- This method has no URI parameter, thus no call to 'pluginResponsible'.
628
- handlesRequest _ _ desc conf = pluginEnabledGlobally desc conf
635
+ handlesRequest _ _ _ desc conf = pluginEnabledGlobally desc conf
629
636
630
637
631
638
-- ---------------------------------------------------------------------
@@ -1054,7 +1061,7 @@ defaultPluginDescriptor plId desc =
1054
1061
mempty
1055
1062
mempty
1056
1063
Nothing
1057
- [" .hs " , " .lhs " , " .hs-boot " ]
1064
+ [J. LanguageKind_Haskell , J. LanguageKind_Custom " literate haskell " ]
1058
1065
1059
1066
-- | Set up a plugin descriptor, initialized with default values.
1060
1067
-- This plugin descriptor is prepared for @.cabal@ files and as such,
@@ -1075,7 +1082,7 @@ defaultCabalPluginDescriptor plId desc =
1075
1082
mempty
1076
1083
mempty
1077
1084
Nothing
1078
- [" . cabal" ]
1085
+ [J. LanguageKind_Custom " cabal" ]
1079
1086
1080
1087
newtype CommandId = CommandId T. Text
1081
1088
deriving (Show , Read , Eq , Ord )
@@ -1251,6 +1258,20 @@ mkLspCmdId pid (PluginId plid) (CommandId cid)
1251
1258
getPid :: IO T. Text
1252
1259
getPid = T. pack . show <$> getProcessID
1253
1260
1261
+ getVirtualFileFromVFS :: VFS -> NormalizedUri -> Maybe VirtualFile
1262
+ getVirtualFileFromVFS (VFS vfs) uri =
1263
+ case Map. lookup uri vfs of
1264
+ Just (Open x) -> Just x
1265
+ Just (Closed _) -> Nothing
1266
+ Nothing -> Nothing
1267
+
1268
+ getVirtualFileFromVFSIncludingClosed :: VFS -> NormalizedUri -> Maybe VirtualFileEntry
1269
+ getVirtualFileFromVFSIncludingClosed (VFS vfs) uri =
1270
+ case Map. lookup uri vfs of
1271
+ Just x -> Just x
1272
+ Nothing -> Nothing
1273
+
1274
+
1254
1275
getProcessID :: IO Int
1255
1276
installSigUsr1Handler :: IO () -> IO ()
1256
1277
0 commit comments