Skip to content

Commit f5fad79

Browse files
committed
improve position handling
1 parent 961d714 commit f5fad79

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import Language.LSP.Protocol.Types
8080
import Language.LSP.VFS qualified as VFS
8181
import Text.Regex.TDFA
8282
import System.FilePath ((</>))
83+
import Debug.Trace
8384

8485
data Log
8586
= LogModificationTime NormalizedFilePath FileVersion
@@ -444,6 +445,7 @@ lens state _plId clp = do
444445

445446
nfp <- getNormalizedFilePathE uri
446447
cabalFields <- runActionE "cabal.cabal-lens" state $ useE ParseCabalFields nfp
448+
447449
let positionedDeps = concatMap parseDeps cabalFields
448450

449451
let rfp = rootDir state
@@ -460,7 +462,8 @@ lens state _plId clp = do
460462
where
461463
getCodeLens :: Positioned SimpleDependency -> CodeLens
462464
getCodeLens (Positioned pos (Dependency _ v)) =
463-
let cPos = Types.cabalPositionToLSPPosition pos in CodeLens
465+
let cPos = Types.cabalPositionToLSPPosition pos
466+
in CodeLens
464467
{ _range = Range cPos cPos
465468
, _command = Just $ mkActionlessCommand v
466469
, _data_ = Nothing

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,11 @@ type PkgName = T.Text
185185
type PkgVersion = T.Text
186186

187187
data SimpleDependency = Dependency PkgName PkgVersion
188+
deriving Show
188189

189190
-- | Represents some element that has an associated position in a file
190191
data Positioned a = Positioned Syntax.Position a
192+
deriving Show
191193

192194
data DependencyInstances = DependencyInstances
193195
{ installPlan :: [DependencyInstance] }
@@ -197,7 +199,7 @@ data DependencyInstances = DependencyInstances
197199
data DependencyInstance = DependencyInstance
198200
{ _pkgName :: PkgName
199201
, _pkgVersion :: PkgVersion
200-
, _componentName :: T.Text
202+
, _pkgType :: T.Text
201203
} -- missing some unneeded fields
202204
deriving (Show, Generic)
203205

@@ -207,8 +209,8 @@ instance A.FromJSON DependencyInstance where
207209
parseJSON = A.withObject "InstallPlan" $ \obj -> do
208210
pkgName <- obj .: "pkg-name"
209211
pkgVersion <- obj .: "pkg-version"
210-
cmpName <- obj .: "component-name"
211-
return $ DependencyInstance pkgName pkgVersion cmpName
212+
pkgType <- obj .: "type"
213+
return $ DependencyInstance pkgName pkgVersion pkgType
212214

213215
instance A.FromJSON DependencyInstances where
214216
parseJSON = A.withObject "PlanJson" $ \obj -> do

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module Ide.Plugin.Cabal.Dependencies (
44
DependencyInstance(..),
55
DependencyInstances(..),
66
parseDeps,
7-
planJsonPath
7+
planJsonPath,
8+
packageRegex
89
) where
910

1011
import Distribution.Fields qualified as Syntax
@@ -14,18 +15,20 @@ import Data.Text.Encoding qualified as Encoding
1415
import Data.Text qualified as T
1516
import System.FilePath ((</>), (<.>))
1617

17-
import Text.Regex.TDFA ((=~), AllTextMatches (getAllTextMatches))
18+
import Text.Regex.TDFA ((=~), AllTextMatches (getAllTextMatches), AllMatches(getAllMatches))
1819
import Data.ByteString (ByteString)
1920

2021
import Ide.Plugin.Cabal.Completion.Types
21-
22+
import Debug.Trace
23+
import Data.Tuple.Extra (dupe)
24+
2225
planJsonPath :: FilePath
2326
planJsonPath = "dist-newstyle" </> "cache" </> "plan" <.> "json" -- hard coded for now
24-
27+
2528
-- | Parses a Field that may contain dependencies
2629
parseDeps :: Syntax.Field Syntax.Position -> [Positioned PkgName]
2730
parseDeps (Syntax.Field (Syntax.Name _ "build-depends") fls) = concatMap mkPosDeps fls
28-
parseDeps (Syntax.Section _ _ fls) = concatMap parseDeps fls
31+
parseDeps (Syntax.Section _ _ fls) = concatMap parseDeps fls
2932
parseDeps _ = []
3033

3134
-- | Matches valid Cabal dependency names
@@ -35,7 +38,13 @@ packageRegex = "[a-zA-Z0-9_-]+" -- not sure if this is correct
3538
-- | Parses a single FieldLine of Cabal dependencies. Returns a list since a single line may
3639
-- contain multiple dependencies.
3740
mkPosDeps :: Syntax.FieldLine Syntax.Position -> [Positioned PkgName]
38-
mkPosDeps (Syntax.FieldLine pos dep) = map (\n -> Positioned pos n) $ getPackageNames dep
39-
where
41+
mkPosDeps (Syntax.FieldLine pos dep) = zipWith
42+
(\n (o, _) -> Positioned (Syntax.Position (Syntax.positionRow pos) (Syntax.positionCol pos + o + 1)) n)
43+
(getPackageNames dep)
44+
(getPackageNameOffsets dep)
45+
where
4046
getPackageNames :: ByteString -> [T.Text]
4147
getPackageNames dep = getAllTextMatches (Encoding.decodeUtf8Lenient dep =~ packageRegex)
48+
49+
getPackageNameOffsets :: ByteString -> [(Int, Int)]
50+
getPackageNameOffsets dep = getAllMatches (Encoding.decodeUtf8Lenient dep =~ packageRegex)

0 commit comments

Comments
 (0)