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