Skip to content

Commit 50dc120

Browse files
committed
Add PluginFileType
1 parent ce378a9 commit 50dc120

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

ghcide/src/Development/IDE/Core/Shake.hs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ import Ide.Plugin.Config
168168
import qualified Ide.PluginUtils as HLS
169169
import Ide.Types (IdePlugins (IdePlugins),
170170
PluginDescriptor (pluginId),
171-
PluginId)
171+
PluginId, SourceFileOrigin(..),
172+
getSourceFileOrigin)
172173
import Language.LSP.Diagnostics
173174
import Language.LSP.Protocol.Message
174175
import Language.LSP.Protocol.Types
@@ -1128,17 +1129,6 @@ defineEarlyCutOffNoFile recorder f = defineEarlyCutoff recorder $ RuleNoDiagnost
11281129
if file == emptyFilePath then do (hash, res) <- f k; return (Just hash, Just res) else
11291130
fail $ "Rule " ++ show k ++ " should always be called with the empty string for a file"
11301131

1131-
data SourceFileOrigin = FromProject | FromDependency
1132-
1133-
getSourceFileOrigin :: NormalizedFilePath -> SourceFileOrigin
1134-
getSourceFileOrigin f =
1135-
case [".hls", "dependencies"] `isInfixOf` (splitDirectories file) of
1136-
True -> FromDependency
1137-
False -> FromProject
1138-
where
1139-
file :: FilePath
1140-
file = fromNormalizedFilePath f
1141-
11421132
defineEarlyCutoff'
11431133
:: forall k v. IdeRule k v
11441134
=> (Maybe Int32 -> [FileDiagnostic] -> Action ()) -- ^ update diagnostics

ghcide/src/Development/IDE/LSP/Notifications.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ descriptor recorder plId = (defaultPluginDescriptor plId) { pluginNotificationHa
158158
-- The ghcide descriptors should come last'ish so that the notification handlers
159159
-- (which restart the Shake build) run after everything else
160160
pluginPriority = ghcideNotificationsPluginPriority
161+
, pluginFileType = PluginFileType [FromProject, FromDependency] defaultPluginFileExtensions
161162
}
162163

163164
ghcideNotificationsPluginPriority :: Natural

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ descriptor plId = (defaultPluginDescriptor plId)
5353
<> mkPluginHandler SMethod_TextDocumentDocumentHighlight (\ide _ DocumentHighlightParams{..} ->
5454
documentHighlight ide TextDocumentPositionParams{..})
5555
<> mkPluginHandler SMethod_TextDocumentReferences (\ide _ params -> references ide params)
56-
<> mkPluginHandler SMethod_WorkspaceSymbol (\ide _ params -> fmap InL <$> wsSymbols ide params),
56+
<> mkPluginHandler SMethod_WorkspaceSymbol (\ide _ params -> fmap InL <$> wsSymbols ide params)
5757

58-
pluginConfigDescriptor = defaultConfigDescriptor
58+
, pluginConfigDescriptor = defaultConfigDescriptor
59+
, pluginFileType = PluginFileType [FromProject, FromDependency] defaultPluginFileExtensions
5960
}
6061

6162
-- ---------------------------------------------------------------------

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
{-# LANGUAGE ViewPatterns #-}
2424
module Ide.Types
2525
( PluginDescriptor(..), defaultPluginDescriptor, defaultCabalPluginDescriptor
26-
, defaultPluginPriority
26+
, defaultPluginPriority, defaultPluginFileExtensions
2727
, IdeCommand(..)
2828
, IdeMethod(..)
2929
, IdeNotification(..)
@@ -36,6 +36,7 @@ module Ide.Types
3636
, FormattingType(..), FormattingMethod, FormattingHandler, mkFormattingHandlers
3737
, HasTracing(..)
3838
, PluginCommand(..), CommandId(..), CommandFunction, mkLspCommand, mkLspCmdId
39+
, PluginFileType(..)
3940
, PluginId(..)
4041
, PluginHandler(..), mkPluginHandler
4142
, PluginHandlers(..)
@@ -44,6 +45,8 @@ module Ide.Types
4445
, PluginNotificationHandler(..), mkPluginNotificationHandler
4546
, PluginNotificationHandlers(..)
4647
, PluginRequestMethod(..)
48+
, SourceFileOrigin(..)
49+
, getSourceFileOrigin
4750
, getProcessID, getPid
4851
, installSigUsr1Handler
4952
, responseError
@@ -76,6 +79,7 @@ import Data.GADT.Compare
7679
import Data.Hashable (Hashable)
7780
import Data.HashMap.Strict (HashMap)
7881
import qualified Data.HashMap.Strict as HashMap
82+
import Data.List (isInfixOf)
7983
import Data.List.Extra (find, sortOn)
8084
import Data.List.NonEmpty (NonEmpty (..), toList)
8185
import qualified Data.Map as Map
@@ -276,23 +280,44 @@ data PluginDescriptor (ideState :: *) =
276280
, pluginNotificationHandlers :: PluginNotificationHandlers ideState
277281
, pluginModifyDynflags :: DynFlagsModifications
278282
, pluginCli :: Maybe (ParserInfo (IdeCommand ideState))
279-
, pluginFileType :: [T.Text]
283+
, pluginFileType :: PluginFileType
280284
-- ^ File extension of the files the plugin is responsible for.
281285
-- The plugin is only allowed to handle files with these extensions.
282286
-- When writing handlers, etc. for this plugin it can be assumed that all handled files are of this type.
283287
-- The file extension must have a leading '.'.
284288
}
285289

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+
286303
-- | Check whether the given plugin descriptor is responsible for the file with the given path.
287304
-- Compares the file extension of the file at the given path with the file extension
288305
-- the plugin is responsible for.
289306
pluginResponsible :: Uri -> PluginDescriptor c -> Bool
290307
pluginResponsible uri pluginDesc
291308
| Just fp <- mfp
292-
, T.pack (takeExtension fp) `elem` pluginFileType pluginDesc = True
309+
, checkFile (pluginFileType pluginDesc) fp = True
293310
| otherwise = False
294311
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
296321

297322
-- | An existential wrapper of 'Properties'
298323
data CustomConfig = forall r. CustomConfig (Properties r)
@@ -835,7 +860,10 @@ defaultPluginDescriptor plId =
835860
mempty
836861
mempty
837862
Nothing
838-
[".hs", ".lhs", ".hs-boot"]
863+
(PluginFileType [FromProject] defaultPluginFileExtensions)
864+
865+
defaultPluginFileExtensions :: [T.Text]
866+
defaultPluginFileExtensions = [".hs", ".lhs", ".hs-boot"]
839867

840868
-- | Set up a plugin descriptor, initialized with default values.
841869
-- This plugin descriptor is prepared for @.cabal@ files and as such,
@@ -855,7 +883,7 @@ defaultCabalPluginDescriptor plId =
855883
mempty
856884
mempty
857885
Nothing
858-
[".cabal"]
886+
(PluginFileType [FromProject] [".cabal"])
859887

860888
newtype CommandId = CommandId T.Text
861889
deriving (Show, Read, Eq, Ord)

0 commit comments

Comments
 (0)