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