Skip to content

Commit e9aab3c

Browse files
committed
Don't produce diagnostics if plugin is turned off
1 parent ade0e85 commit e9aab3c

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal.hs

+28-24
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ instance Pretty Log where
8181
descriptor :: Recorder (WithPriority Log) -> PluginId -> PluginDescriptor IdeState
8282
descriptor recorder plId =
8383
(defaultCabalPluginDescriptor plId "Provides a variety of IDE features in cabal files")
84-
{ pluginRules = cabalRules recorder
84+
{ pluginRules = cabalRules recorder plId
8585
, pluginHandlers =
8686
mconcat
8787
[ mkPluginHandler LSP.SMethod_TextDocumentCodeAction licenseSuggestCodeAction
@@ -139,31 +139,35 @@ restartCabalShakeSession shakeExtras vfs file actionMsg = do
139139
-- Plugin Rules
140140
-- ----------------------------------------------------------------
141141

142-
cabalRules :: Recorder (WithPriority Log) -> Rules ()
143-
cabalRules recorder = do
142+
cabalRules :: Recorder (WithPriority Log) -> PluginId -> Rules ()
143+
cabalRules recorder plId = do
144144
-- Make sure we initialise the cabal files-of-interest.
145145
ofInterestRules recorder
146146
-- Rule to produce diagnostics for cabal files.
147-
define (cmapWithPrio LogShake recorder) $ \Types.ParseCabal file -> do
148-
-- whenever this key is marked as dirty (e.g., when a user writes stuff to it),
149-
-- we rerun this rule because this rule *depends* on GetModificationTime.
150-
(t, mCabalSource) <- use_ GetFileContents file
151-
log' Debug $ LogModificationTime file t
152-
contents <- case mCabalSource of
153-
Just sources ->
154-
pure $ Encoding.encodeUtf8 sources
155-
Nothing -> do
156-
liftIO $ BS.readFile $ fromNormalizedFilePath file
147+
define (cmapWithPrio LogShake recorder) $ \Types.GetCabalDiagnostics file -> do
148+
config <- getPluginConfigAction plId
149+
if not (plcGlobalOn config && plcDiagnosticsOn config)
150+
then pure ([], Nothing)
151+
else do
152+
-- whenever this key is marked as dirty (e.g., when a user writes stuff to it),
153+
-- we rerun this rule because this rule *depends* on GetModificationTime.
154+
(t, mCabalSource) <- use_ GetFileContents file
155+
log' Debug $ LogModificationTime file t
156+
contents <- case mCabalSource of
157+
Just sources ->
158+
pure $ Encoding.encodeUtf8 sources
159+
Nothing -> do
160+
liftIO $ BS.readFile $ fromNormalizedFilePath file
157161

158-
(pWarnings, pm) <- liftIO $ Parse.parseCabalFileContents contents
159-
let warningDiags = fmap (Diagnostics.warningDiagnostic file) pWarnings
160-
case pm of
161-
Left (_cabalVersion, pErrorNE) -> do
162-
let errorDiags = NE.toList $ NE.map (Diagnostics.errorDiagnostic file) pErrorNE
163-
allDiags = errorDiags <> warningDiags
164-
pure (allDiags, Nothing)
165-
Right gpd -> do
166-
pure (warningDiags, Just gpd)
162+
(pWarnings, pm) <- liftIO $ Parse.parseCabalFileContents contents
163+
let warningDiags = fmap (Diagnostics.warningDiagnostic file) pWarnings
164+
case pm of
165+
Left (_cabalVersion, pErrorNE) -> do
166+
let errorDiags = NE.toList $ NE.map (Diagnostics.errorDiagnostic file) pErrorNE
167+
allDiags = errorDiags <> warningDiags
168+
pure (allDiags, Nothing)
169+
Right gpd -> do
170+
pure (warningDiags, Just gpd)
167171

168172
action $ do
169173
-- Run the cabal kick. This code always runs when 'shakeRestart' is run.
@@ -183,7 +187,7 @@ function invocation.
183187
kick :: Action ()
184188
kick = do
185189
files <- HashMap.keys <$> getCabalFilesOfInterestUntracked
186-
void $ uses Types.ParseCabal files
190+
void $ uses Types.GetCabalDiagnostics files
187191

188192
-- ----------------------------------------------------------------
189193
-- Code Actions
@@ -292,7 +296,7 @@ completion recorder ide _ complParams = do
292296
let completer = Completions.contextToCompleter ctx
293297
let completerData = CompleterTypes.CompleterData
294298
{ getLatestGPD = do
295-
mGPD <- runIdeAction "cabal-plugin.modulesCompleter.gpd" (shakeExtras ide) $ useWithStaleFast Types.ParseCabal $ toNormalizedFilePath fp
299+
mGPD <- runIdeAction "cabal-plugin.modulesCompleter.gpd" (shakeExtras ide) $ useWithStaleFast Types.GetCabalDiagnostics $ toNormalizedFilePath fp
296300
pure $ fmap fst mGPD
297301
, cabalPrefixInfo = prefInfo
298302
, stanzaName =

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Types.hs

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ instance Pretty Log where
3737
LogUseWithStaleFastNoResult -> "Package description couldn't be read"
3838
LogMapLookUpOfKnownKeyFailed key -> "Lookup of key in map failed even though it should exist" <+> pretty key
3939

40-
type instance RuleResult ParseCabal = Parse.GenericPackageDescription
40+
type instance RuleResult GetCabalDiagnostics = Parse.GenericPackageDescription
4141

42-
data ParseCabal = ParseCabal
42+
data GetCabalDiagnostics = GetCabalDiagnostics
4343
deriving (Eq, Show, Typeable, Generic)
4444

45-
instance Hashable ParseCabal
45+
instance Hashable GetCabalDiagnostics
4646

47-
instance NFData ParseCabal
47+
instance NFData GetCabalDiagnostics
4848

4949
-- | The context a cursor can be in within a cabal file.
5050
--

0 commit comments

Comments
 (0)