23
23
{-# LANGUAGE ViewPatterns #-}
24
24
module Ide.Types
25
25
( PluginDescriptor (.. ), defaultPluginDescriptor, defaultCabalPluginDescriptor
26
- , defaultPluginPriority
26
+ , defaultPluginPriority, defaultPluginFileExtensions
27
27
, IdeCommand (.. )
28
28
, IdeMethod (.. )
29
29
, IdeNotification (.. )
@@ -36,6 +36,7 @@ module Ide.Types
36
36
, FormattingType (.. ), FormattingMethod , FormattingHandler , mkFormattingHandlers
37
37
, HasTracing (.. )
38
38
, PluginCommand (.. ), CommandId (.. ), CommandFunction , mkLspCommand, mkLspCmdId
39
+ , PluginFileType (.. )
39
40
, PluginId (.. )
40
41
, PluginHandler (.. ), mkPluginHandler
41
42
, PluginHandlers (.. )
@@ -44,6 +45,8 @@ module Ide.Types
44
45
, PluginNotificationHandler (.. ), mkPluginNotificationHandler
45
46
, PluginNotificationHandlers (.. )
46
47
, PluginRequestMethod (.. )
48
+ , SourceFileOrigin (.. )
49
+ , getSourceFileOrigin
47
50
, getProcessID, getPid
48
51
, installSigUsr1Handler
49
52
, responseError
@@ -76,6 +79,7 @@ import Data.GADT.Compare
76
79
import Data.Hashable (Hashable )
77
80
import Data.HashMap.Strict (HashMap )
78
81
import qualified Data.HashMap.Strict as HashMap
82
+ import Data.List (isInfixOf )
79
83
import Data.List.Extra (find , sortOn )
80
84
import Data.List.NonEmpty (NonEmpty (.. ), toList )
81
85
import qualified Data.Map as Map
@@ -276,23 +280,44 @@ data PluginDescriptor (ideState :: *) =
276
280
, pluginNotificationHandlers :: PluginNotificationHandlers ideState
277
281
, pluginModifyDynflags :: DynFlagsModifications
278
282
, pluginCli :: Maybe (ParserInfo (IdeCommand ideState ))
279
- , pluginFileType :: [ T. Text ]
283
+ , pluginFileType :: PluginFileType
280
284
-- ^ File extension of the files the plugin is responsible for.
281
285
-- The plugin is only allowed to handle files with these extensions.
282
286
-- When writing handlers, etc. for this plugin it can be assumed that all handled files are of this type.
283
287
-- The file extension must have a leading '.'.
284
288
}
285
289
290
+ data PluginFileType = PluginFileType [SourceFileOrigin ] [T. Text ]
291
+
292
+ data SourceFileOrigin = FromProject | FromDependency deriving Eq
293
+
294
+ getSourceFileOrigin :: NormalizedFilePath -> SourceFileOrigin
295
+ getSourceFileOrigin f =
296
+ case [" .hls" , " dependencies" ] `isInfixOf` (splitDirectories file) of
297
+ True -> FromDependency
298
+ False -> FromProject
299
+ where
300
+ file :: FilePath
301
+ file = fromNormalizedFilePath f
302
+
286
303
-- | Check whether the given plugin descriptor is responsible for the file with the given path.
287
304
-- Compares the file extension of the file at the given path with the file extension
288
305
-- the plugin is responsible for.
289
306
pluginResponsible :: Uri -> PluginDescriptor c -> Bool
290
307
pluginResponsible uri pluginDesc
291
308
| Just fp <- mfp
292
- , T. pack (takeExtension fp) `elem` pluginFileType pluginDesc = True
309
+ , checkFile ( pluginFileType pluginDesc) fp = True
293
310
| otherwise = False
294
311
where
295
- mfp = uriToFilePath uri
312
+ checkFile :: PluginFileType -> NormalizedFilePath -> Bool
313
+ checkFile (PluginFileType validOrigins validExtensions) fp =
314
+ getSourceFileOrigin fp `elem` validOrigins
315
+ &&
316
+ getExtension fp `elem` validExtensions
317
+ getExtension :: NormalizedFilePath -> T. Text
318
+ getExtension = T. pack . takeExtension . fromNormalizedFilePath
319
+ mfp :: Maybe NormalizedFilePath
320
+ mfp = uriToNormalizedFilePath $ toNormalizedUri uri
296
321
297
322
-- | An existential wrapper of 'Properties'
298
323
data CustomConfig = forall r . CustomConfig (Properties r )
@@ -835,7 +860,10 @@ defaultPluginDescriptor plId =
835
860
mempty
836
861
mempty
837
862
Nothing
838
- [" .hs" , " .lhs" , " .hs-boot" ]
863
+ (PluginFileType [FromProject ] defaultPluginFileExtensions)
864
+
865
+ defaultPluginFileExtensions :: [T. Text ]
866
+ defaultPluginFileExtensions = [" .hs" , " .lhs" , " .hs-boot" ]
839
867
840
868
-- | Set up a plugin descriptor, initialized with default values.
841
869
-- This plugin descriptor is prepared for @.cabal@ files and as such,
@@ -855,7 +883,7 @@ defaultCabalPluginDescriptor plId =
855
883
mempty
856
884
mempty
857
885
Nothing
858
- [ " .cabal" ]
886
+ ( PluginFileType [ FromProject ] [ " .cabal" ])
859
887
860
888
newtype CommandId = CommandId T. Text
861
889
deriving (Show , Read , Eq , Ord )
0 commit comments