1
1
{-# LANGUAGE DuplicateRecordFields #-}
2
2
{-# LANGUAGE OverloadedStrings #-}
3
3
4
- module Ide.Plugin.Cabal.Dependencies (dependencyVersionHints , collectPackageDependencyVersions , dependencyVersionLens , collectPackageDependencyVersions' , printVersion ) where
4
+ module Ide.Plugin.Cabal.Dependencies (dependencyVersionHints , collectPackageDependencyVersions , dependencyVersionLens , printVersion ) where
5
5
6
+ import Control.Lens ((^.) )
6
7
import Data.Array ((!) )
7
8
import Data.ByteString (ByteString )
8
9
import Data.List
@@ -18,39 +19,39 @@ import Development.IDE.GHC.Compat (HscEnv, filterUniqMap,
18
19
import qualified Distribution.Fields as Syntax
19
20
import qualified Distribution.Parsec.Position as Syntax
20
21
import qualified Ide.Plugin.Cabal.Completion.Types as Types
22
+ import qualified Language.LSP.Protocol.Lens as JL
21
23
import Language.LSP.Protocol.Types (CodeLens (.. ), Command (.. ),
22
24
InlayHint (.. ), Range (.. ),
23
25
type (|? ) (.. ))
24
26
import Text.Regex.TDFA (Regex , makeRegex ,
25
27
matchAllText )
26
28
27
29
dependencyVersionLens :: [Syntax. Field Syntax. Position ] -> HscEnv -> [CodeLens ]
28
- dependencyVersionLens cabalFields = (>>= foo) . groupBy (\ (Syntax. Position line1 _, _, _) (Syntax. Position line2 _, _, _) -> line1 == line2 ) . collectPackageDependencyVersions cabalFields
30
+ dependencyVersionLens cabalFields = (>>= foo) . groupBy (\ (a,_, _) (b,_,_) -> (a ^. JL. start . JL. line) == (b ^. JL. start . JL. line) ) . collectPackageDependencyVersions cabalFields
29
31
where
30
- foo :: [(Syntax. Position , T. Text , Version )] -> [CodeLens ]
32
+ foo :: [(Range , T. Text , Version )] -> [CodeLens ]
31
33
foo [] = []
32
34
foo [single] = [mkCodeLens False single]
33
35
foo multi = mkCodeLens True <$> multi
34
36
35
- mkCodeLens :: Bool -> (Syntax. Position , T. Text , Version ) -> CodeLens
36
- mkCodeLens includePkgName (pos, pkgName, dependencyVersion) =
37
- let cPos = Types. cabalPositionToLSPPosition pos
38
- dependencyText =
37
+ mkCodeLens :: Bool -> (Range , T. Text , Version ) -> CodeLens
38
+ mkCodeLens includePkgName (range, pkgName, dependencyVersion) =
39
+ let dependencyText =
39
40
if includePkgName
40
41
then pkgName <> " (" <> printVersion dependencyVersion <> " )"
41
42
else printVersion dependencyVersion
42
43
command = Command dependencyText mempty Nothing
43
44
in CodeLens
44
- { _range = Range cPos cPos
45
+ { _range = range
45
46
, _command = Just command
46
47
, _data_ = Nothing }
47
48
48
49
dependencyVersionHints :: [Syntax. Field Syntax. Position ] -> HscEnv -> [InlayHint ]
49
50
dependencyVersionHints cabalFields = fmap mkHint . collectPackageDependencyVersions cabalFields
50
51
where
51
- mkHint :: (Syntax. Position , T. Text , Version ) -> InlayHint
52
- mkHint (pos, _, dependencyVersion) =
53
- InlayHint { _position = Types. cabalPositionToLSPPosition pos
52
+ mkHint :: (Range , T. Text , Version ) -> InlayHint
53
+ mkHint (Range _ pos, _, dependencyVersion) =
54
+ InlayHint { _position = pos
54
55
, _label = InL $ " (" <> printVersion dependencyVersion <> " )"
55
56
, _kind = Nothing
56
57
, _textEdits = Nothing
@@ -60,28 +61,8 @@ dependencyVersionHints cabalFields = fmap mkHint . collectPackageDependencyVersi
60
61
, _data_ = Nothing
61
62
}
62
63
63
- collectPackageDependencyVersions :: [Syntax. Field Syntax. Position ] -> HscEnv -> [(Syntax. Position , T. Text , Version )]
64
+ collectPackageDependencyVersions :: [Syntax. Field Syntax. Position ] -> HscEnv -> [(Range , T. Text , Version )]
64
65
collectPackageDependencyVersions cabalFields hscEnv = cabalFields >>= collectPackageVersions
65
- where
66
- lookupPackageVersion pkgName = Maybe. listToMaybe $ nonDetEltsUniqMap $ fmap unitPackageVersion $ filterUniqMap ((==) (T. unpack pkgName) . unitPackageNameString) $ getUnitInfoMap hscEnv
67
-
68
- collectPackageVersions :: Syntax. Field Syntax. Position -> [(Syntax. Position , T. Text , Version )]
69
- collectPackageVersions (Syntax. Field (Syntax. Name _ " build-depends" ) pos) = concatMap fieldLinePackageVersions pos
70
- collectPackageVersions (Syntax. Section _ _ fields) = concatMap collectPackageVersions fields
71
- collectPackageVersions _ = []
72
-
73
- fieldLinePackageVersions :: Syntax. FieldLine Syntax. Position -> [(Syntax. Position , T. Text , Version )]
74
- fieldLinePackageVersions (Syntax. FieldLine pos line) =
75
- let linePackageNameRegex :: Regex = makeRegex (" (^|,)[[:space:]]*([a-zA-Z-]+)" :: ByteString )
76
- packageNames = (\ x -> x ! 2 ) <$> matchAllText linePackageNameRegex (Encoding. decodeUtf8Lenient line)
77
- versions = do
78
- (pkgName, (pkgIndex, pkgOffset)) <- packageNames
79
- version <- Maybe. maybeToList $ lookupPackageVersion pkgName
80
- pure (Syntax. Position (Syntax. positionRow pos) (Syntax. positionCol pos + pkgIndex + pkgOffset), pkgName, version)
81
- in versions
82
-
83
- collectPackageDependencyVersions' :: [Syntax. Field Syntax. Position ] -> HscEnv -> [(Range , T. Text , Version )]
84
- collectPackageDependencyVersions' cabalFields hscEnv = cabalFields >>= collectPackageVersions
85
66
where
86
67
lookupPackageVersion pkgName = Maybe. listToMaybe $ nonDetEltsUniqMap $ fmap unitPackageVersion $ filterUniqMap ((==) (T. unpack pkgName) . unitPackageNameString) $ getUnitInfoMap hscEnv
87
68
0 commit comments