33module Ide.Plugin.CabalProject.Diagnostics
44( errorDiagnostic
55, warningDiagnostic
6- , positionFromCabalProjectPosition
6+ , positionFromCabalPosition
77, fatalParseErrorDiagnostic
88 -- * Re-exports
99, FileDiagnostic
@@ -18,6 +18,9 @@ import Development.IDE.Types.Diagnostics (fdLspDiagnosticL,
1818 ideErrorWithSource )
1919import Distribution.Fields (showPError , showPWarning )
2020import qualified Distribution.Parsec as Syntax
21+ import Ide.Plugin.Cabal.Diagnostics (mkDiag ,
22+ positionFromCabalPosition ,
23+ toBeginningOfNextLine )
2124import Ide.PluginUtils (extendNextLine )
2225import Language.LSP.Protocol.Lens (range )
2326import Language.LSP.Protocol.Types (Diagnostic (.. ),
@@ -30,64 +33,18 @@ import Language.LSP.Protocol.Types (Diagnostic (..),
3033-- | Produce a diagnostic for a fatal Cabal parser error.
3134fatalParseErrorDiagnostic :: NormalizedFilePath -> T. Text -> FileDiagnostic
3235fatalParseErrorDiagnostic fp msg =
33- mkDiag fp " cabal" DiagnosticSeverity_Error (toBeginningOfNextLine Syntax. zeroPos) msg
36+ mkDiag fp " cabal-project " DiagnosticSeverity_Error (toBeginningOfNextLine Syntax. zeroPos) msg
3437
3538-- | Produce a diagnostic from a Cabal parser error
3639errorDiagnostic :: NormalizedFilePath -> Syntax. PError -> FileDiagnostic
3740errorDiagnostic fp err@ (Syntax. PError pos _) =
38- mkDiag fp " cabal" DiagnosticSeverity_Error (toBeginningOfNextLine pos) msg
41+ mkDiag fp " cabal-project " DiagnosticSeverity_Error (toBeginningOfNextLine pos) msg
3942 where
4043 msg = T. pack $ showPError (fromNormalizedFilePath fp) err
4144
4245-- | Produce a diagnostic from a Cabal parser warning
4346warningDiagnostic :: NormalizedFilePath -> Syntax. PWarning -> FileDiagnostic
4447warningDiagnostic fp warning@ (Syntax. PWarning _ pos _) =
45- mkDiag fp " cabal" DiagnosticSeverity_Warning (toBeginningOfNextLine pos) msg
48+ mkDiag fp " cabal-project " DiagnosticSeverity_Warning (toBeginningOfNextLine pos) msg
4649 where
4750 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