Skip to content

Commit 2164729

Browse files
committed
remove some redundancies between cabal and cabal-project plugin
1 parent ba5216d commit 2164729

File tree

4 files changed

+12
-69
lines changed

4 files changed

+12
-69
lines changed

haskell-language-server.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,8 @@ library hls-cabal-project-plugin
369369
, pretty
370370
, cabal-install
371371
, cabal-install-solver
372+
, haskell-language-server:hls-cabal-plugin
373+
372374

373375
hs-source-dirs: plugins/hls-cabal-project-plugin/src
374376

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module Ide.Plugin.Cabal.Diagnostics
55
, warningDiagnostic
66
, positionFromCabalPosition
77
, fatalParseErrorDiagnostic
8+
, toBeginningOfNextLine
9+
, mkDiag
810
-- * Re-exports
911
, FileDiagnostic
1012
, Diagnostic(..)

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

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module 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)
1919
import Distribution.Fields (showPError, showPWarning)
2020
import qualified Distribution.Parsec as Syntax
21+
import Ide.Plugin.Cabal.Diagnostics (mkDiag,
22+
positionFromCabalPosition,
23+
toBeginningOfNextLine)
2124
import Ide.PluginUtils (extendNextLine)
2225
import Language.LSP.Protocol.Lens (range)
2326
import 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.
3134
fatalParseErrorDiagnostic :: NormalizedFilePath -> T.Text -> FileDiagnostic
3235
fatalParseErrorDiagnostic 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
3639
errorDiagnostic :: NormalizedFilePath -> Syntax.PError -> FileDiagnostic
3740
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
3942
where
4043
msg = T.pack $ showPError (fromNormalizedFilePath fp) err
4144

4245
-- | Produce a diagnostic from a Cabal parser warning
4346
warningDiagnostic :: NormalizedFilePath -> Syntax.PWarning -> FileDiagnostic
4447
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
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

plugins/hls-cabal-project-plugin/src/Ide/Plugin/CabalProject/Orphans.hs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,7 @@ import qualified Distribution.Solver.Types.ProjectConfigPath as PCPath
1313
import GHC.Generics (Generic)
1414

1515
import qualified Distribution.Client.ProjectConfig.Types as PC
16-
17-
-- ----------------------------------------------------------------
18-
-- Cabal-syntax orphan instances we need sometimes
19-
-- ----------------------------------------------------------------
20-
21-
instance NFData (Field Position) where
22-
rnf (Field name fieldLines) = rnf name `seq` rnf fieldLines
23-
rnf (Section name sectionArgs fields) = rnf name `seq` rnf sectionArgs `seq` rnf fields
24-
25-
instance NFData (Name Position) where
26-
rnf (Name ann fName) = rnf ann `seq` rnf fName
27-
28-
instance NFData (FieldLine Position) where
29-
rnf (FieldLine ann bs) = rnf ann `seq` rnf bs
30-
31-
instance NFData (SectionArg Position) where
32-
rnf (SecArgName ann bs) = rnf ann `seq` rnf bs
33-
rnf (SecArgStr ann bs) = rnf ann `seq` rnf bs
34-
rnf (SecArgOther ann bs) = rnf ann `seq` rnf bs
16+
import Ide.Plugin.Cabal.Orphans ()
3517

3618
-- Project Config Orphans
3719

0 commit comments

Comments
 (0)