@@ -39,6 +39,7 @@ module Ide.Types
3939, PluginNotificationHandlers (.. )
4040, PluginRequestMethod (.. )
4141, getProcessID, getPid
42+ , getVirtualFileFromVFS
4243, installSigUsr1Handler
4344, lookupCommandProvider
4445, ResolveFunction
@@ -94,13 +95,13 @@ import Ide.Plugin.Properties
9495import qualified Language.LSP.Protocol.Lens as L
9596import Language.LSP.Protocol.Message
9697import Language.LSP.Protocol.Types
98+ import qualified Language.LSP.Protocol.Types as J
9799import Language.LSP.Server
98100import Language.LSP.VFS
99101import Numeric.Natural
100102import OpenTelemetry.Eventlog
101103import Options.Applicative (ParserInfo )
102104import Prettyprinter as PP
103- import System.FilePath
104105import System.IO.Unsafe
105106import Text.Regex.TDFA.Text ()
106107import UnliftIO (MonadUnliftIO )
@@ -323,7 +324,7 @@ data PluginDescriptor (ideState :: Type) =
323324 , pluginNotificationHandlers :: PluginNotificationHandlers ideState
324325 , pluginModifyDynflags :: DynFlagsModifications
325326 , pluginCli :: Maybe (ParserInfo (IdeCommand ideState ))
326- , pluginFileType :: [T. Text ]
327+ , pluginLanguageIds :: [J. LanguageKind ]
327328 -- ^ File extension of the files the plugin is responsible for.
328329 -- The plugin is only allowed to handle files with these extensions.
329330 -- 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
416417-- We are passing the msgParams here even though we only need the URI URI here.
417418-- If in the future we need to be able to provide only an URI it can be
418419-- 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 )
424425 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
427432
428433-- | Methods that can be handled by plugins.
429434-- '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
452457 --
453458 -- But there is no use to split it up into two different methods for now.
454459 handlesRequest
455- :: SMethod m
460+ :: VFS
461+ -- ^ The virtual file system, contains the language kind of the file.
462+ -> SMethod m
456463 -- ^ Method type.
457464 -> MessageParams m
458465 -- ^ Whether a plugin is enabled might depend on the message parameters
@@ -468,24 +475,24 @@ class HasTracing (MessageParams m) => PluginMethod (k :: MessageKind) (m :: Meth
468475 -- with the given parameters?
469476
470477 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
474481
475482-- | Check if a plugin is enabled, if one of it's specific config's is enabled,
476483-- and if it supports the file
477484pluginEnabledWithFeature :: (L. HasTextDocument (MessageParams m ) doc , L. HasUri doc Uri )
478- => (PluginConfig -> Bool ) -> SMethod m -> MessageParams m
485+ => (PluginConfig -> Bool ) -> VFS -> SMethod m -> MessageParams m
479486 -> PluginDescriptor c -> Config -> HandleRequestResult
480- pluginEnabledWithFeature feature _ msgParams pluginDesc config =
487+ pluginEnabledWithFeature feature vfs _ msgParams pluginDesc config =
481488 pluginEnabledGlobally pluginDesc config
482489 <> pluginFeatureEnabled feature pluginDesc config
483- <> pluginSupportsFileType msgParams pluginDesc
490+ <> pluginSupportsFileType vfs msgParams pluginDesc
484491
485492-- | Check if a plugin is enabled, if one of it's specific configs is enabled,
486493-- 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 =
489496 pluginEnabledGlobally pluginDesc config
490497 <> pluginFeatureEnabled feature pluginDesc config
491498 <> pluginResolverResponsible msgParams pluginDesc
@@ -498,23 +505,23 @@ instance PluginMethod Request Method_CodeActionResolve where
498505 handlesRequest = pluginEnabledResolve plcCodeActionsOn
499506
500507instance PluginMethod Request Method_TextDocumentDefinition where
501- handlesRequest _ msgParams pluginDesc _ = pluginSupportsFileType msgParams pluginDesc
508+ handlesRequest vfs _ msgParams pluginDesc _ = pluginSupportsFileType vfs msgParams pluginDesc
502509
503510instance PluginMethod Request Method_TextDocumentTypeDefinition where
504- handlesRequest _ msgParams pluginDesc _ = pluginSupportsFileType msgParams pluginDesc
511+ handlesRequest vfs _ msgParams pluginDesc _ = pluginSupportsFileType vfs msgParams pluginDesc
505512
506513instance PluginMethod Request Method_TextDocumentImplementation where
507- handlesRequest _ msgParams pluginDesc _ = pluginSupportsFileType msgParams pluginDesc
514+ handlesRequest vfs _ msgParams pluginDesc _ = pluginSupportsFileType vfs msgParams pluginDesc
508515
509516instance PluginMethod Request Method_TextDocumentDocumentHighlight where
510- handlesRequest _ msgParams pluginDesc _ = pluginSupportsFileType msgParams pluginDesc
517+ handlesRequest vfs _ msgParams pluginDesc _ = pluginSupportsFileType vfs msgParams pluginDesc
511518
512519instance PluginMethod Request Method_TextDocumentReferences where
513- handlesRequest _ msgParams pluginDesc _ = pluginSupportsFileType msgParams pluginDesc
520+ handlesRequest vfs _ msgParams pluginDesc _ = pluginSupportsFileType vfs msgParams pluginDesc
514521
515522instance PluginMethod Request Method_WorkspaceSymbol where
516523 -- Unconditionally enabled, but should it really be?
517- handlesRequest _ _ _ _ = HandlesRequest
524+ handlesRequest _ _ _ _ _ = HandlesRequest
518525
519526instance PluginMethod Request Method_TextDocumentInlayHint where
520527 handlesRequest = pluginEnabledWithFeature plcInlayHintsOn
@@ -549,22 +556,22 @@ instance PluginMethod Request Method_TextDocumentCompletion where
549556 handlesRequest = pluginEnabledWithFeature plcCompletionOn
550557
551558instance PluginMethod Request Method_TextDocumentFormatting where
552- handlesRequest _ msgParams pluginDesc conf =
559+ handlesRequest vfs _ msgParams pluginDesc conf =
553560 (if PluginId (formattingProvider conf) == pid
554561 || PluginId (cabalFormattingProvider conf) == pid
555562 then HandlesRequest
556563 else DoesNotHandleRequest (NotFormattingProvider (formattingProvider conf)) )
557- <> pluginSupportsFileType msgParams pluginDesc
564+ <> pluginSupportsFileType vfs msgParams pluginDesc
558565 where
559566 pid = pluginId pluginDesc
560567
561568instance PluginMethod Request Method_TextDocumentRangeFormatting where
562- handlesRequest _ msgParams pluginDesc conf =
569+ handlesRequest vfs _ msgParams pluginDesc conf =
563570 (if PluginId (formattingProvider conf) == pid
564571 || PluginId (cabalFormattingProvider conf) == pid
565572 then HandlesRequest
566573 else DoesNotHandleRequest (NotFormattingProvider (formattingProvider conf)))
567- <> pluginSupportsFileType msgParams pluginDesc
574+ <> pluginSupportsFileType vfs msgParams pluginDesc
568575 where
569576 pid = pluginId pluginDesc
570577
@@ -585,21 +592,21 @@ instance PluginMethod Request Method_TextDocumentFoldingRange where
585592
586593instance PluginMethod Request Method_CallHierarchyIncomingCalls where
587594 -- This method has no URI parameter, thus no call to 'pluginResponsible'
588- handlesRequest _ _ pluginDesc conf =
595+ handlesRequest _ _ _ pluginDesc conf =
589596 pluginEnabledGlobally pluginDesc conf
590597 <> pluginFeatureEnabled plcCallHierarchyOn pluginDesc conf
591598
592599instance PluginMethod Request Method_CallHierarchyOutgoingCalls where
593600 -- This method has no URI parameter, thus no call to 'pluginResponsible'
594- handlesRequest _ _ pluginDesc conf =
601+ handlesRequest _ _ _ pluginDesc conf =
595602 pluginEnabledGlobally pluginDesc conf
596603 <> pluginFeatureEnabled plcCallHierarchyOn pluginDesc conf
597604
598605instance PluginMethod Request Method_WorkspaceExecuteCommand where
599- handlesRequest _ _ _ _= HandlesRequest
606+ handlesRequest _ _ _ _ _ = HandlesRequest
600607
601608instance PluginMethod Request (Method_CustomMethod m ) where
602- handlesRequest _ _ _ _ = HandlesRequest
609+ handlesRequest _ _ _ _ _ = HandlesRequest
603610
604611-- Plugin Notifications
605612
@@ -613,19 +620,19 @@ instance PluginMethod Notification Method_TextDocumentDidClose where
613620
614621instance PluginMethod Notification Method_WorkspaceDidChangeWatchedFiles where
615622 -- 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
617624
618625instance PluginMethod Notification Method_WorkspaceDidChangeWorkspaceFolders where
619626 -- 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
621628
622629instance PluginMethod Notification Method_WorkspaceDidChangeConfiguration where
623630 -- 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
625632
626633instance PluginMethod Notification Method_Initialized where
627634 -- 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
629636
630637
631638-- ---------------------------------------------------------------------
@@ -1054,7 +1061,7 @@ defaultPluginDescriptor plId desc =
10541061 mempty
10551062 mempty
10561063 Nothing
1057- [" .hs " , " .lhs " , " .hs-boot " ]
1064+ [J. LanguageKind_Haskell , J. LanguageKind_Custom " literate haskell " ]
10581065
10591066-- | Set up a plugin descriptor, initialized with default values.
10601067-- This plugin descriptor is prepared for @.cabal@ files and as such,
@@ -1075,7 +1082,7 @@ defaultCabalPluginDescriptor plId desc =
10751082 mempty
10761083 mempty
10771084 Nothing
1078- [" . cabal" ]
1085+ [J. LanguageKind_Custom " cabal" ]
10791086
10801087newtype CommandId = CommandId T. Text
10811088 deriving (Show , Read , Eq , Ord )
@@ -1251,6 +1258,20 @@ mkLspCmdId pid (PluginId plid) (CommandId cid)
12511258getPid :: IO T. Text
12521259getPid = T. pack . show <$> getProcessID
12531260
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+
12541275getProcessID :: IO Int
12551276installSigUsr1Handler :: IO () -> IO ()
12561277
0 commit comments