6
6
module Development.IDE.GHC.Warnings (withWarnings ) where
7
7
8
8
import Control.Concurrent.Strict
9
+ import Control.Lens (over )
10
+ import Data.List
9
11
import qualified Data.Text as T
10
12
11
13
import Development.IDE.GHC.Compat
12
14
import Development.IDE.GHC.Error
13
15
import Development.IDE.Types.Diagnostics
16
+ import Language.LSP.Protocol.Types (type (|? ) (.. ))
14
17
15
18
16
19
-- | Take a GHC monadic action (e.g. @typecheckModule pm@ for some
@@ -27,9 +30,30 @@ withWarnings diagSource action = do
27
30
warnings <- newVar []
28
31
let newAction :: DynFlags -> LogActionCompat
29
32
newAction dynFlags logFlags wr _ loc prUnqual msg = do
30
- let wr_d = map (wr,) $ diagFromSDocErrMsg diagSource dynFlags (mkWarnMsg dynFlags wr logFlags loc prUnqual msg)
33
+ let wr_d = map (( wr,) . over fdLspDiagnosticL (attachReason wr) ) $ diagFromSDocErrMsg diagSource dynFlags (mkWarnMsg dynFlags wr logFlags loc prUnqual msg)
31
34
modifyVar_ warnings $ return . (wr_d: )
32
35
newLogger env = pushLogHook (const (logActionCompat (newAction (hsc_dflags env)))) (hsc_logger env)
33
36
res <- action $ \ env -> putLogHook (newLogger env) env
34
37
warns <- readVar warnings
35
38
return (reverse $ concat warns, res)
39
+
40
+ #if MIN_VERSION_ghc(9,3,0)
41
+ attachReason :: Maybe DiagnosticReason -> Diagnostic -> Diagnostic
42
+ attachReason Nothing d = d
43
+ attachReason (Just wr) d = d{_code = InR <$> showReason wr}
44
+ where
45
+ showReason = \ case
46
+ WarningWithFlag flag -> showFlag flag
47
+ _ -> Nothing
48
+ #else
49
+ attachReason :: WarnReason -> Diagnostic -> Diagnostic
50
+ attachReason wr d = d{_code = InR <$> showReason wr}
51
+ where
52
+ showReason = \ case
53
+ NoReason -> Nothing
54
+ Reason flag -> showFlag flag
55
+ ErrReason flag -> showFlag =<< flag
56
+ #endif
57
+
58
+ showFlag :: WarningFlag -> Maybe T. Text
59
+ showFlag flag = (" -W" <> ) . T. pack . flagSpecName <$> find ((== flag) . flagSpecFlag) wWarningFlags
0 commit comments