Skip to content

Commit 75bdf46

Browse files
committed
Use Regex for getting package dependencies
1 parent 22edd19 commit 75bdf46

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

haskell-language-server.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ library hls-cabal-plugin
263263

264264

265265
build-depends:
266+
, array
266267
, bytestring
267268
, Cabal-syntax >= 3.7
268269
, containers

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
module Ide.Plugin.Cabal.Dependencies (dependencyVersionHints, collectPackageDependencyVersions, dependencyVersionLens) where
55

6+
import Data.Array ((!))
7+
import Data.ByteString (ByteString)
68
import qualified Data.Char as Char
79
import qualified Data.List as List
810
import qualified Data.Maybe as Maybe
@@ -21,6 +23,8 @@ import Language.LSP.Protocol.Types (CodeLens (..), Command (..),
2123
InlayHint (..),
2224
InlayHintLabelPart (InlayHintLabelPart),
2325
Range (..), type (|?) (..))
26+
import Text.Regex.TDFA (Regex, makeRegex,
27+
matchAllText)
2428

2529
dependencyVersionLens :: [Syntax.Field Syntax.Position] -> HscEnv -> [CodeLens]
2630
dependencyVersionLens cabalFields = fmap mkCodeLens . collectPackageDependencyVersions cabalFields
@@ -61,16 +65,13 @@ collectPackageDependencyVersions cabalFields hscEnv = cabalFields >>= collectPac
6165
collectPackageVersions _ = []
6266

6367
fieldLinePackageVersions :: Syntax.FieldLine Syntax.Position -> [(Syntax.Position, Version)]
64-
fieldLinePackageVersions (Syntax.FieldLine pos x) =
65-
let splitted = T.splitOn "," $ Encoding.decodeUtf8Lenient x
66-
calcStartPosition (prev, start) = T.length prev + 1 + start
67-
potentialPkgs = List.foldl' (\a b -> a <> [(b, Maybe.maybe 0 calcStartPosition $ Maybe.listToMaybe $ reverse a)]) [] splitted
68+
fieldLinePackageVersions (Syntax.FieldLine pos line) =
69+
let linePackageNameRegex :: Regex = makeRegex ("(^|,)[[:space:]]*([a-zA-Z-]+)" :: ByteString)
70+
packageNames = (\x -> x ! 2) <$> matchAllText linePackageNameRegex (Encoding.decodeUtf8Lenient line)
6871
versions = do
69-
(pkg', pkgStartOffset) <- potentialPkgs
70-
let pkgName = T.takeWhile (not . Char.isSpace) . T.strip $ pkg'
71-
endOfPackage = T.length pkgName + (T.length $ T.takeWhile Char.isSpace pkg')
72-
version <- Maybe.maybeToList $ lookupPackageVersion $ T.takeWhile (not . Char.isSpace) . T.strip $ pkg'
73-
pure (Syntax.Position (Syntax.positionRow pos) (Syntax.positionCol pos + pkgStartOffset + endOfPackage), version)
72+
(pkgName, (pkgIndex, pkgOffset)) <- packageNames
73+
version <- Maybe.maybeToList $ lookupPackageVersion pkgName
74+
pure (Syntax.Position (Syntax.positionRow pos) (Syntax.positionCol pos + pkgIndex + pkgOffset), version)
7475
in versions
7576

7677
printVersion :: Version -> T.Text

0 commit comments

Comments
 (0)