Skip to content

Commit 9337f25

Browse files
dylan-thinnesJaro Reinders
authored and
Jaro Reinders
committed
Attach reasons into data field of LSP Diagnostic instead of code field
Had to move `attachReason` between modules to achieve this, which is fine because it was never exported from its own module.
1 parent 1cb1325 commit 9337f25

File tree

5 files changed

+42
-30
lines changed

5 files changed

+42
-30
lines changed

ghcide/ghcide.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ library
8585
, hls-plugin-api == 2.9.0.1
8686
, implicit-hie >= 0.1.4.0 && < 0.1.5
8787
, lens
88+
, lens-aeson
8889
, list-t
8990
, lsp ^>=2.7
9091
, lsp-types ^>=2.3

ghcide/src/Development/IDE/GHC/Warnings.hs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ module Development.IDE.GHC.Warnings(withWarnings) where
77

88
import Control.Concurrent.Strict
99
import Control.Lens (over)
10-
import Data.List
1110
import qualified Data.Text as T
1211

1312
import Development.IDE.GHC.Compat
14-
import Development.IDE.GHC.Error
1513
import Development.IDE.Types.Diagnostics
16-
import Language.LSP.Protocol.Types (type (|?) (..))
14+
import Development.IDE.GHC.Error
1715

1816
{-
1917
NOTE on withWarnings and its dangers
@@ -59,24 +57,3 @@ withWarnings diagSource action = do
5957
res <- action $ \env -> putLogHook (newLogger env) env
6058
warns <- readVar warnings
6159
return (reverse $ concat warns, res)
62-
63-
#if MIN_VERSION_ghc(9,3,0)
64-
attachReason :: Maybe DiagnosticReason -> Diagnostic -> Diagnostic
65-
attachReason Nothing d = d
66-
attachReason (Just wr) d = d{_code = InR <$> showReason wr}
67-
where
68-
showReason = \case
69-
WarningWithFlag flag -> showFlag flag
70-
_ -> Nothing
71-
#else
72-
attachReason :: WarnReason -> Diagnostic -> Diagnostic
73-
attachReason wr d = d{_code = InR <$> showReason wr}
74-
where
75-
showReason = \case
76-
NoReason -> Nothing
77-
Reason flag -> showFlag flag
78-
ErrReason flag -> showFlag =<< flag
79-
#endif
80-
81-
showFlag :: WarningFlag -> Maybe T.Text
82-
showFlag flag = ("-W" <>) . T.pack . flagSpecName <$> find ((== flag) . flagSpecFlag) wWarningFlags

ghcide/src/Development/IDE/Types/Diagnostics.hs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,24 @@ module Development.IDE.Types.Diagnostics (
1919
ideErrorFromLspDiag,
2020
showDiagnostics,
2121
showDiagnosticsColored,
22-
IdeResultNoDiagnosticsEarlyCutoff) where
22+
IdeResultNoDiagnosticsEarlyCutoff,
23+
attachReason,
24+
attachedReason) where
2325

2426
import Control.DeepSeq
2527
import Control.Lens
28+
import qualified Data.Aeson as JSON
29+
import qualified Data.Aeson.Lens as JSON
2630
import Data.ByteString (ByteString)
31+
import Data.List
2732
import Data.Maybe as Maybe
2833
import qualified Data.Text as T
29-
import Development.IDE.GHC.Compat (GhcMessage, MsgEnvelope)
34+
import Development.IDE.GHC.Compat (GhcMessage, MsgEnvelope, WarningFlag, wWarningFlags, flagSpecFlag, flagSpecName)
3035
import Development.IDE.Types.Location
3136
import GHC.Generics
32-
import GHC.Types.Error (diagnosticCode, DiagnosticCode (..), errMsgDiagnostic)
37+
import GHC.Types.Error (diagnosticCode, DiagnosticCode (..), errMsgDiagnostic, DiagnosticReason(..), diagnosticReason)
3338
import Language.LSP.Diagnostics
39+
import Language.LSP.Protocol.Lens (data_)
3440
import Language.LSP.Protocol.Types as LSP
3541
import Prettyprinter
3642
import Prettyprinter.Render.Terminal (Color (..), color)
@@ -69,7 +75,7 @@ ideErrorFromLspDiag lspDiag fdFilePath origMsg =
6975
case origMsg of
7076
Nothing -> NoStructuredMessage
7177
Just msg -> SomeStructuredMessage msg
72-
fdLspDiagnostic = lspDiag
78+
fdLspDiagnostic = (attachReason (fmap (diagnosticReason . errMsgDiagnostic) origMsg) lspDiag)
7379
#if MIN_VERSION_ghc(9,6,1)
7480
{ _code = fmap ghcCodeToLspCode . diagnosticCode . errMsgDiagnostic =<< origMsg
7581
}
@@ -85,6 +91,30 @@ ideErrorFromLspDiag lspDiag fdFilePath origMsg =
8591
in
8692
FileDiagnostic {..}
8793

94+
attachedReason :: Traversal' Diagnostic (Maybe JSON.Value)
95+
attachedReason = data_ . non (JSON.object []) . JSON.atKey "attachedReason"
96+
97+
#if MIN_VERSION_ghc(9,3,0)
98+
attachReason :: Maybe DiagnosticReason -> Diagnostic -> Diagnostic
99+
attachReason Nothing = id
100+
attachReason (Just wr) = attachedReason .~ fmap JSON.toJSON (showReason wr)
101+
where
102+
showReason = \case
103+
WarningWithFlag flag -> showFlag flag
104+
_ -> Nothing
105+
#else
106+
attachReason :: WarnReason -> Diagnostic -> Diagnostic
107+
attachReason wr = attachedReason .~ fmap JSON.toJSON (showReason wr)
108+
where
109+
showReason = \case
110+
NoReason -> Nothing
111+
Reason flag -> showFlag flag
112+
ErrReason flag -> showFlag =<< flag
113+
#endif
114+
115+
showFlag :: WarningFlag -> Maybe T.Text
116+
showFlag flag = ("-W" <>) . T.pack . flagSpecName <$> find ((== flag) . flagSpecFlag) wWarningFlags
117+
88118
ideErrorWithSource
89119
:: Maybe T.Text
90120
-> Maybe DiagnosticSeverity

haskell-language-server.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,11 +912,13 @@ library hls-pragmas-plugin
912912
hs-source-dirs: plugins/hls-pragmas-plugin/src
913913
build-depends:
914914
, base >=4.12 && <5
915+
, aeson
915916
, extra
916917
, fuzzy
917918
, ghcide == 2.9.0.1
918919
, hls-plugin-api == 2.9.0.1
919920
, lens
921+
, lens-aeson
920922
, lsp
921923
, text
922924
, transformers

plugins/hls-pragmas-plugin/src/Ide/Plugin/Pragmas.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Ide.Plugin.Pragmas
1515
, AppearWhere(..)
1616
) where
1717

18+
import qualified Data.Aeson as JSON
1819
import Control.Lens hiding (List)
1920
import Control.Monad.IO.Class (MonadIO (liftIO))
2021
import Data.Char (isAlphaNum)
@@ -120,8 +121,9 @@ suggest dflags diag =
120121
-- ---------------------------------------------------------------------
121122

122123
suggestDisableWarning :: Diagnostic -> [PragmaEdit]
123-
suggestDisableWarning Diagnostic {_code}
124-
| Just (LSP.InR (T.stripPrefix "-W" -> Just w)) <- _code
124+
suggestDisableWarning diagnostic
125+
| Just (Just (JSON.String attachedReason)) <- diagnostic ^? attachedReason
126+
, Just w <- T.stripPrefix "-W" attachedReason
125127
, w `notElem` warningBlacklist =
126128
pure ("Disable \"" <> w <> "\" warnings", OptGHC w)
127129
| otherwise = []

0 commit comments

Comments
 (0)