5
5
{-# LANGUAGE RankNTypes #-}
6
6
{-# LANGUAGE ScopedTypeVariables #-}
7
7
8
-
9
8
module Ide.Plugin.SemanticTokens.Query where
10
9
11
- import Data.Either (rights )
12
- import Data.Foldable (fold )
13
- import qualified Data.List as List
14
- import qualified Data.Map as M
15
- import qualified Data.Map as Map
16
- import Data.Maybe ( fromMaybe , listToMaybe ,
17
- mapMaybe )
18
- import qualified Data.Set as S
19
- import qualified Data.Set as Set
20
- import Data.Text ( Text )
21
- import Development.IDE ( realSrcSpanToRange )
10
+ import Data.Either (rights )
11
+ import Data.Foldable (fold )
12
+ import qualified Data.Map as M
13
+ import qualified Data.Map as Map
14
+ import Data.Maybe ( fromMaybe , listToMaybe ,
15
+ mapMaybe )
16
+ import qualified Data.Set as S
17
+ import qualified Data.Set as Set
18
+ import Data.Text ( Text )
19
+ import Development.IDE.Core.PositionMapping ( PositionMapping ,
20
+ toCurrentRange )
22
21
import Development.IDE.GHC.Compat
23
22
import Ide.Plugin.SemanticTokens.Mappings
24
- import Ide.Plugin.SemanticTokens.Types (HieFunMaskKind ,
25
- HsSemanticTokenType ,
26
- NameSemanticMap )
27
- import Ide.Plugin.SemanticTokens.Utils (realSrcSpanToCodePointRange )
23
+ import Ide.Plugin.SemanticTokens.Types (HieFunMaskKind ,
24
+ HsSemanticTokenType ,
25
+ NameSemanticMap )
26
+ import Ide.Plugin.SemanticTokens.Utils (realSrcSpanToCodePointRange )
28
27
import Language.LSP.Protocol.Types
29
- import Language.LSP.VFS (CodePointRange ,
30
- VirtualFile ,
31
- codePointRangeToRange )
32
- import Prelude hiding (span )
28
+ import Language.LSP.VFS (VirtualFile ,
29
+ codePointRangeToRange )
30
+ import Prelude hiding (span )
33
31
34
32
---------------------------------------------------------
35
33
@@ -74,9 +72,9 @@ hieAstSpanNames vf ast =
74
72
then getIds ast
75
73
else M. unionsWith unionNameSet $ map (hieAstSpanNames vf) (nodeChildren ast)
76
74
where
77
- getIds ast' = fromMaybe mempty $ do
78
- range <- codePointRangeToRange vf $ realSrcSpanToCodePointRange $ nodeSpan ast'
79
- return $ M. singleton range (getNodeIds' ast')
75
+ getIds ast' = fromMaybe mempty $ do
76
+ range <- codePointRangeToRange vf $ realSrcSpanToCodePointRange $ nodeSpan ast'
77
+ return $ M. singleton range (getNodeIds' ast')
80
78
getNodeIds' =
81
79
Map. foldl' combineNodeIds mempty
82
80
. Map. filterWithKey (\ k _ -> k == SourceInfo )
@@ -85,25 +83,32 @@ hieAstSpanNames vf ast =
85
83
combineNodeIds :: NameSet -> NodeInfo a -> NameSet
86
84
ad `combineNodeIds` (NodeInfo _ _ bd) = ad `unionNameSet` xs
87
85
where
88
- xs = mkNameSet $ rights $ M. keys $ M. filterWithKey inclusion bd
86
+ xs = mkNameSet $ rights $ M. keys $ M. filterWithKey inclusion bd
89
87
inclusion :: Identifier -> IdentifierDetails a -> Bool
90
88
inclusion a b = not $ exclusion a b
91
89
exclusion :: Identifier -> IdentifierDetails a -> Bool
92
90
exclusion idt IdentifierDetails {identInfo = infos} = case idt of
93
- Left _ -> True
94
- Right name ->
95
- isDerivedOccName (nameOccName name)
96
- || any isEvidenceContext (S. toList infos)
97
-
91
+ Left _ -> True
92
+ Right name ->
93
+ isDerivedOccName (nameOccName name)
94
+ || any isEvidenceContext (S. toList infos)
98
95
99
96
-------------------------------------------------
100
97
101
98
-- * extract semantic tokens from NameSemanticMap
102
99
103
100
-------------------------------------------------
104
101
105
- semanticTokenAbsoluteSemanticTokens :: [(Range , HsSemanticTokenType )] -> Either Text SemanticTokens
106
- semanticTokenAbsoluteSemanticTokens = makeSemanticTokens defaultSemanticTokensLegend . List. sort . map (uncurry toAbsSemanticToken)
102
+ extractSemanticTokensFromNames :: NameSemanticMap -> M. Map Range NameSet -> M. Map Range HsSemanticTokenType
103
+ extractSemanticTokensFromNames nsm rnMap = Map. mapMaybe (foldMap (lookupNameEnv nsm) . nameSetElemsStable) rnMap
104
+
105
+
106
+ rangeSemanticMapSemanticTokens :: PositionMapping -> M. Map Range HsSemanticTokenType -> Either Text SemanticTokens
107
+ rangeSemanticMapSemanticTokens mapping =
108
+ makeSemanticTokens defaultSemanticTokensLegend
109
+ . mapMaybe (\ (range, ty) -> flip toAbsSemanticToken ty <$> range)
110
+ . Map. toAscList
111
+ . M. mapKeys (\ r -> toCurrentRange mapping r)
107
112
where
108
113
toAbsSemanticToken :: Range -> HsSemanticTokenType -> SemanticTokenAbsolute
109
114
toAbsSemanticToken (Range (Position startLine startColumn) (Position _endLine endColumn)) tokenType =
@@ -114,7 +119,3 @@ semanticTokenAbsoluteSemanticTokens = makeSemanticTokens defaultSemanticTokensLe
114
119
(fromIntegral len)
115
120
(toLspTokenType tokenType)
116
121
[]
117
-
118
- extractSemanticTokensFromNames :: NameSemanticMap -> M. Map Range NameSet -> [(Range , HsSemanticTokenType )]
119
- extractSemanticTokensFromNames nsm rnMap = xs
120
- where xs = mapMaybe sequence (Map. toList $ Map. map (foldMap (lookupNameEnv nsm) . nameSetElemsStable) rnMap)
0 commit comments