@@ -19,18 +19,24 @@ module Development.IDE.Types.Diagnostics (
19
19
ideErrorFromLspDiag ,
20
20
showDiagnostics ,
21
21
showDiagnosticsColored ,
22
- IdeResultNoDiagnosticsEarlyCutoff ) where
22
+ IdeResultNoDiagnosticsEarlyCutoff ,
23
+ attachReason ,
24
+ attachedReason ) where
23
25
24
26
import Control.DeepSeq
25
27
import Control.Lens
28
+ import qualified Data.Aeson as JSON
29
+ import qualified Data.Aeson.Lens as JSON
26
30
import Data.ByteString (ByteString )
31
+ import Data.List
27
32
import Data.Maybe as Maybe
28
33
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 )
30
35
import Development.IDE.Types.Location
31
36
import GHC.Generics
32
- import GHC.Types.Error (diagnosticCode , DiagnosticCode (.. ), errMsgDiagnostic )
37
+ import GHC.Types.Error (diagnosticCode , DiagnosticCode (.. ), errMsgDiagnostic , DiagnosticReason ( .. ), diagnosticReason )
33
38
import Language.LSP.Diagnostics
39
+ import Language.LSP.Protocol.Lens (data_ )
34
40
import Language.LSP.Protocol.Types as LSP
35
41
import Prettyprinter
36
42
import Prettyprinter.Render.Terminal (Color (.. ), color )
@@ -69,7 +75,7 @@ ideErrorFromLspDiag lspDiag fdFilePath origMsg =
69
75
case origMsg of
70
76
Nothing -> NoStructuredMessage
71
77
Just msg -> SomeStructuredMessage msg
72
- fdLspDiagnostic = lspDiag
78
+ fdLspDiagnostic = (attachReason ( fmap (diagnosticReason . errMsgDiagnostic) origMsg) lspDiag)
73
79
#if MIN_VERSION_ghc(9,6,1)
74
80
{ _code = fmap ghcCodeToLspCode . diagnosticCode . errMsgDiagnostic =<< origMsg
75
81
}
@@ -85,6 +91,30 @@ ideErrorFromLspDiag lspDiag fdFilePath origMsg =
85
91
in
86
92
FileDiagnostic {.. }
87
93
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
+
88
118
ideErrorWithSource
89
119
:: Maybe T. Text
90
120
-> Maybe DiagnosticSeverity
0 commit comments