3
3
module Ide.Plugin.CabalProject.Diagnostics
4
4
( errorDiagnostic
5
5
, warningDiagnostic
6
- , positionFromCabalProjectPosition
6
+ , positionFromCabalPosition
7
7
, fatalParseErrorDiagnostic
8
8
-- * Re-exports
9
9
, FileDiagnostic
@@ -18,6 +18,9 @@ import Development.IDE.Types.Diagnostics (fdLspDiagnosticL,
18
18
ideErrorWithSource )
19
19
import Distribution.Fields (showPError , showPWarning )
20
20
import qualified Distribution.Parsec as Syntax
21
+ import Ide.Plugin.Cabal.Diagnostics (mkDiag ,
22
+ positionFromCabalPosition ,
23
+ toBeginningOfNextLine )
21
24
import Ide.PluginUtils (extendNextLine )
22
25
import Language.LSP.Protocol.Lens (range )
23
26
import Language.LSP.Protocol.Types (Diagnostic (.. ),
@@ -30,64 +33,18 @@ import Language.LSP.Protocol.Types (Diagnostic (..),
30
33
-- | Produce a diagnostic for a fatal Cabal parser error.
31
34
fatalParseErrorDiagnostic :: NormalizedFilePath -> T. Text -> FileDiagnostic
32
35
fatalParseErrorDiagnostic fp msg =
33
- mkDiag fp " cabal" DiagnosticSeverity_Error (toBeginningOfNextLine Syntax. zeroPos) msg
36
+ mkDiag fp " cabal-project " DiagnosticSeverity_Error (toBeginningOfNextLine Syntax. zeroPos) msg
34
37
35
38
-- | Produce a diagnostic from a Cabal parser error
36
39
errorDiagnostic :: NormalizedFilePath -> Syntax. PError -> FileDiagnostic
37
40
errorDiagnostic fp err@ (Syntax. PError pos _) =
38
- mkDiag fp " cabal" DiagnosticSeverity_Error (toBeginningOfNextLine pos) msg
41
+ mkDiag fp " cabal-project " DiagnosticSeverity_Error (toBeginningOfNextLine pos) msg
39
42
where
40
43
msg = T. pack $ showPError (fromNormalizedFilePath fp) err
41
44
42
45
-- | Produce a diagnostic from a Cabal parser warning
43
46
warningDiagnostic :: NormalizedFilePath -> Syntax. PWarning -> FileDiagnostic
44
47
warningDiagnostic fp warning@ (Syntax. PWarning _ pos _) =
45
- mkDiag fp " cabal" DiagnosticSeverity_Warning (toBeginningOfNextLine pos) msg
48
+ mkDiag fp " cabal-project " DiagnosticSeverity_Warning (toBeginningOfNextLine pos) msg
46
49
where
47
50
msg = T. pack $ showPWarning (fromNormalizedFilePath fp) warning
48
-
49
- -- | The Cabal parser does not output a _range_ for a warning/error,
50
- -- only a single source code 'Lib.Position'.
51
- -- We define the range to be _from_ this position
52
- -- _to_ the first column of the next line.
53
- toBeginningOfNextLine :: Syntax. Position -> Range
54
- toBeginningOfNextLine cabalPos = extendNextLine $ Range pos pos
55
- where
56
- pos = positionFromCabalProjectPosition cabalPos
57
-
58
- -- | Convert a 'Lib.Position' from Cabal to a 'Range' that LSP understands.
59
- --
60
- -- Prefer this function over hand-rolled unpacking/packing, since LSP is zero-based,
61
- -- while Cabal is one-based.
62
- --
63
- -- >>> positionFromCabalPosition $ Lib.Position 1 1
64
- -- Position 0 0
65
- positionFromCabalProjectPosition :: Syntax. Position -> Position
66
- positionFromCabalProjectPosition (Syntax. Position line column) = Position (fromIntegral line') (fromIntegral col')
67
- where
68
- -- LSP is zero-based, Cabal is one-based
69
- -- Cabal can return line 0 for errors in the first line
70
- line' = if line <= 0 then 0 else line- 1
71
- col' = if column <= 0 then 0 else column- 1
72
-
73
- -- | Create a 'FileDiagnostic'
74
- mkDiag
75
- :: NormalizedFilePath
76
- -- ^ Cabal file path
77
- -> T. Text
78
- -- ^ Where does the diagnostic come from?
79
- -> DiagnosticSeverity
80
- -- ^ Severity
81
- -> Range
82
- -- ^ Which source code range should the editor highlight?
83
- -> T. Text
84
- -- ^ The message displayed by the editor
85
- -> FileDiagnostic
86
- mkDiag file diagSource sev loc msg =
87
- ideErrorWithSource
88
- (Just diagSource)
89
- (Just sev)
90
- file
91
- msg
92
- Nothing
93
- & fdLspDiagnosticL . range .~ loc
0 commit comments