Skip to content

Commit 51763bb

Browse files
committed
refactor
1 parent 816017e commit 51763bb

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ import Ide.Plugin.Cabal.Completion.Types
5858
( ParseCabalCommonSections (ParseCabalCommonSections),
5959
ParseCabalFields (..),
6060
ParseCabalFile (..),
61-
ParsePlanJson (..), BuildDependencyVersionMapping (..), Versioned(..),
61+
ParsePlanJson (..),
62+
BuildDependencyVersionMapping (..),
63+
Positioned(..),
64+
SimpleDependency(..)
6265
)
6366
import Ide.Plugin.Cabal.Completion.Types qualified as Types
6467
import Ide.Plugin.Cabal.Definition (gotoDefinition)
@@ -76,7 +79,6 @@ import Language.LSP.Protocol.Message qualified as LSP
7679
import Language.LSP.Protocol.Types
7780
import Language.LSP.VFS qualified as VFS
7881
import Text.Regex.TDFA
79-
import Debug.Trace
8082
import System.FilePath ((</>))
8183

8284
data Log
@@ -449,15 +451,15 @@ lens state _plId clp = do
449451
planDeps <- runActionE "cabal.cabal-lens" state $ useE BuildDependencyVersionMapping planJson
450452

451453
let lenses = Maybe.mapMaybe
452-
(\p@(PositionedDependency _ name) -> getCodeLens . Versioned p <$> Map.lookup name planDeps)
453-
positionedDeps
454+
(\(Positioned pos name) -> getCodeLens . Positioned pos . Dependency name <$> Map.lookup name planDeps)
455+
positionedDeps
454456

455457
pure $ InL lenses
456458
else
457459
pure $ InL []
458460
where
459-
getCodeLens :: Versioned PositionedDependency -> CodeLens
460-
getCodeLens (Versioned (PositionedDependency pos _) v) =
461+
getCodeLens :: Positioned SimpleDependency -> CodeLens
462+
getCodeLens (Positioned pos (Dependency _ v)) =
461463
let cPos = Types.cabalPositionToLSPPosition pos in CodeLens
462464
{ _range = Range cPos cPos
463465
, _command = Just $ mkActionlessCommand v
@@ -490,16 +492,16 @@ hint state _plId clp =
490492
let planJson = toNormalizedFilePath $ rfp </> planJsonPath
491493
planDeps <- runActionE "cabal.cabal-lens" state $ useE BuildDependencyVersionMapping planJson
492494

493-
let hints = Maybe.mapMaybe
494-
(\p@(PositionedDependency _ name) -> getInlayHint . Versioned p <$> Map.lookup name planDeps)
495+
let lenses = Maybe.mapMaybe
496+
(\(Positioned pos name) -> getInlayHint . Positioned pos . Dependency name <$> Map.lookup name planDeps)
495497
positionedDeps
496498

497-
pure $ InL hints
499+
pure $ InL lenses
498500
else
499501
pure $ InL []
500502
where
501-
getInlayHint :: Versioned PositionedDependency -> InlayHint
502-
getInlayHint (Versioned (PositionedDependency pos _) v) = InlayHint
503+
getInlayHint :: Positioned SimpleDependency -> InlayHint
504+
getInlayHint (Positioned pos (Dependency _ v)) = InlayHint
503505
{ _position = Types.cabalPositionToLSPPosition pos
504506
, _label = InL v
505507
, _kind = Nothing

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,9 @@ data Apostrophe = Surrounded | LeftSide
184184
type PkgName = T.Text
185185
type PkgVersion = T.Text
186186

187-
data PositionedDependency = PositionedDependency Syntax.Position PkgName
188-
deriving Show
189-
190-
data Versioned a = Versioned a PkgVersion
187+
data SimpleDependency = Dependency PkgName PkgVersion
188+
189+
data Positioned a = Positioned Syntax.Position a
191190

192191
data DependencyInstances = DependencyInstances
193192
{ installPlan :: [DependencyInstance] }

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
module Ide.Plugin.Cabal.Dependencies (
44
DependencyInstance(..),
55
DependencyInstances(..),
6-
PositionedDependency(..),
76
parseDeps,
87
planJsonPath
98
) where
@@ -24,7 +23,7 @@ planJsonPath :: FilePath
2423
planJsonPath = "dist-newstyle" </> "cache" </> "plan" <.> "json" -- hard coded for now
2524

2625
-- | Parses a Field that may contain dependencies
27-
parseDeps :: Syntax.Field Syntax.Position -> [PositionedDependency]
26+
parseDeps :: Syntax.Field Syntax.Position -> [Positioned PkgName]
2827
parseDeps (Syntax.Field (Syntax.Name _ "build-depends") fls) = concatMap mkPosDeps fls
2928
parseDeps (Syntax.Section _ _ fls) = concatMap parseDeps fls
3029
parseDeps _ = []
@@ -35,8 +34,8 @@ packageRegex = "[a-zA-Z0-9_-]+"
3534

3635
-- | Parses a single FieldLine of Cabal dependencies. Returns a list since a single line may
3736
-- contain multiple dependencies.
38-
mkPosDeps :: Syntax.FieldLine Syntax.Position -> [PositionedDependency]
39-
mkPosDeps (Syntax.FieldLine pos dep) = map (PositionedDependency pos) $ getPackageNames dep
37+
mkPosDeps :: Syntax.FieldLine Syntax.Position -> [Positioned PkgName]
38+
mkPosDeps (Syntax.FieldLine pos dep) = map (\n -> Positioned pos n) $ getPackageNames dep
4039
where
4140
getPackageNames :: ByteString -> [T.Text]
4241
getPackageNames dep = getAllTextMatches (Encoding.decodeUtf8Lenient dep =~ packageRegex)

0 commit comments

Comments
 (0)