44module Ide.Plugin.Cabal.Diagnostics
55( errorDiagnostic
66, warningDiagnostic
7+ , positionFromCabalPosition
78 -- * Re-exports
89, FileDiagnostic
910, Diagnostic (.. )
@@ -15,6 +16,7 @@ import Development.IDE (FileDiagnostic,
1516 ShowDiagnostic (ShowDiag ))
1617import Distribution.Fields (showPError , showPWarning )
1718import qualified Ide.Plugin.Cabal.Parse as Lib
19+ import Ide.PluginUtils (extendNextLine )
1820import Language.LSP.Types (Diagnostic (.. ),
1921 DiagnosticSeverity (.. ),
2022 DiagnosticSource , NormalizedFilePath ,
@@ -40,10 +42,19 @@ warningDiagnostic fp warning@(Lib.PWarning _ pos _) =
4042-- We define the range to be _from_ this position
4143-- _to_ the first column of the next line.
4244toBeginningOfNextLine :: Lib. Position -> Range
43- toBeginningOfNextLine (Lib. Position line column) =
44- Range
45- (Position (fromIntegral line') (fromIntegral col'))
46- (Position (fromIntegral $ line' + 1 ) 0 )
45+ toBeginningOfNextLine cabalPos = extendNextLine $ Range pos pos
46+ where
47+ pos = positionFromCabalPosition cabalPos
48+
49+ -- | Convert a 'Lib.Position' from Cabal to a 'Range' that LSP understands.
50+ --
51+ -- Prefer this function over hand-rolled unpacking/packing, since LSP is zero-based,
52+ -- while Cabal is one-based.
53+ --
54+ -- >>> positionFromCabalPosition $ Lib.Position 1 1
55+ -- Position 0 0
56+ positionFromCabalPosition :: Lib. Position -> Position
57+ positionFromCabalPosition (Lib. Position line column) = Position (fromIntegral line') (fromIntegral col')
4758 where
4859 -- LSP is zero-based, Cabal is one-based
4960 line' = line- 1
0 commit comments