Skip to content

Commit f8a7806

Browse files
committed
Use Range manipulation functions from hls-plugin-api
1 parent 3081e81 commit f8a7806

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Diagnostics.hs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module 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))
1617
import Distribution.Fields (showPError, showPWarning)
1718
import qualified Ide.Plugin.Cabal.Parse as Lib
19+
import Ide.PluginUtils (extendNextLine)
1820
import 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.
4244
toBeginningOfNextLine :: 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

Comments
 (0)