Skip to content

Commit 1fd122e

Browse files
committed
revert cacheLookup
1 parent 5126c75 commit 1fd122e

File tree

4 files changed

+22
-57
lines changed

4 files changed

+22
-57
lines changed

plugins/hls-semantic-tokens-plugin/src/Ide/Plugin/SemanticTokens/Tokenize.hs

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33

44
module Ide.Plugin.SemanticTokens.Tokenize (computeRangeHsSemanticTokenTypeList) where
55

6-
import Control.Lens (Identity (Identity, runIdentity))
6+
import Control.Lens (Identity (runIdentity))
77
import Control.Monad (foldM, guard)
88
import Control.Monad.State.Strict (MonadState (get),
99
MonadTrans (lift),
10-
evalStateT, gets, mapStateT,
11-
modify', put)
10+
evalStateT, modify, put)
1211
import Control.Monad.Trans.State.Strict (StateT, runStateT)
1312
import Data.Char (isAlphaNum)
1413
import Data.DList (DList)
@@ -32,25 +31,13 @@ import Prelude hiding (length, span)
3231

3332
type Tokenizer m a = StateT PTokenState m a
3433
type HsSemanticLookup = Identifier -> Maybe HsSemanticTokenType
35-
type CachedHsSemanticLookup m = Identifier -> Tokenizer m (Maybe HsSemanticTokenType)
36-
37-
cacheLookup :: (Monad m) => HsSemanticLookup -> CachedHsSemanticLookup m
38-
cacheLookup _ (Left _) = return $ Just TModule
39-
cacheLookup lk idt@(Right n) = do
40-
ne <- gets semanticLookupCache
41-
case lookupNameEnv ne n of
42-
Nothing -> do
43-
let hsSemanticTy = lk idt
44-
modify' (\x -> x{ semanticLookupCache= extendNameEnv ne n hsSemanticTy })
45-
return hsSemanticTy
46-
Just x -> return x
34+
4735

4836
data PTokenState = PTokenState
4937
{
50-
rope :: !Rope -- the remains of rope we are working on
51-
, cursor :: !Char.Position -- the cursor position of the current rope to the start of the original file in code point position
52-
, columnsInUtf16 :: !UInt -- the column of the start of the current rope in utf16
53-
, semanticLookupCache :: !(NameEnv (Maybe HsSemanticTokenType)) -- the cache for semantic lookup result of the current file
38+
rope :: !Rope -- the remains of rope we are working on
39+
, cursor :: !Char.Position -- the cursor position of the current rope to the start of the original file in code point position
40+
, columnsInUtf16 :: !UInt -- the column of the start of the current rope in utf16
5441
}
5542

5643
data SplitResult
@@ -62,14 +49,14 @@ getSplitTokenText :: SplitResult -> Text
6249
getSplitTokenText (NoSplit (t, _)) = t
6350
getSplitTokenText (Split (t, _, _)) = t
6451

52+
6553
mkPTokenState :: VirtualFile -> PTokenState
6654
mkPTokenState vf =
6755
PTokenState
6856
{
6957
rope = vf._file_text,
7058
cursor = Char.Position 0 0,
71-
columnsInUtf16 = 0,
72-
semanticLookupCache = emptyNameEnv
59+
columnsInUtf16 = 0
7360
}
7461

7562
-- lift a Tokenizer Maybe a to Tokenizer m a,
@@ -85,15 +72,15 @@ foldMapM f ta = foldM (\b a -> mappend b <$> f a) mempty ta
8572

8673
computeRangeHsSemanticTokenTypeList :: HsSemanticLookup -> VirtualFile -> HieAST a -> RangeHsSemanticTokenTypes
8774
computeRangeHsSemanticTokenTypeList lookupHsTokenType vf ast =
88-
RangeHsSemanticTokenTypes $ DL.toList $ runIdentity $ evalStateT (foldAst (cacheLookup lookupHsTokenType) ast) (mkPTokenState vf)
75+
RangeHsSemanticTokenTypes $ DL.toList $ runIdentity $ evalStateT (foldAst lookupHsTokenType ast) (mkPTokenState vf)
8976
-- | foldAst
9077
-- visit every leaf node in the ast in depth first order
91-
foldAst :: (Monad m) => CachedHsSemanticLookup Identity -> HieAST t -> Tokenizer m (DList (Range, HsSemanticTokenType))
78+
foldAst :: (Monad m) => HsSemanticLookup -> HieAST t -> Tokenizer m (DList (Range, HsSemanticTokenType))
9279
foldAst lookupHsTokenType ast = if null (nodeChildren ast)
93-
then visitLeafIds lookupHsTokenType ast
80+
then liftMaybeM (visitLeafIds lookupHsTokenType ast)
9481
else foldMapM (foldAst lookupHsTokenType) $ nodeChildren ast
9582

96-
visitLeafIds :: (Monad m) => CachedHsSemanticLookup Identity -> HieAST t -> Tokenizer m (DList (Range, HsSemanticTokenType))
83+
visitLeafIds :: HsSemanticLookup -> HieAST t -> Tokenizer Maybe (DList (Range, HsSemanticTokenType))
9784
visitLeafIds lookupHsTokenType leaf = liftMaybeM $ do
9885
let span = nodeSpan leaf
9986
(ran, token) <- focusTokenAt leaf
@@ -103,23 +90,21 @@ visitLeafIds lookupHsTokenType leaf = liftMaybeM $ do
10390
-- only handle the leaf node with single column token
10491
guard $ srcSpanStartLine span == srcSpanEndLine span
10592
splitResult <- lift $ splitRangeByText token ran
106-
mapStateT hoistIdMaybe
107-
$ foldMapM (combineNodeIds lookupHsTokenType ran splitResult)
108-
$ Map.filterWithKey (\k _ -> k == SourceInfo) $ getSourcedNodeInfo $ sourcedNodeInfo leaf
93+
foldMapM (combineNodeIds lookupHsTokenType ran splitResult) $ Map.filterWithKey (\k _ -> k == SourceInfo) $ getSourcedNodeInfo $ sourcedNodeInfo leaf
10994
where
110-
hoistIdMaybe :: Identity (a, s) -> Maybe (a, s)
111-
hoistIdMaybe (Identity x) = Just x
112-
combineNodeIds :: CachedHsSemanticLookup Identity -> Range -> SplitResult -> NodeInfo a -> Tokenizer Identity (DList (Range, HsSemanticTokenType))
113-
combineNodeIds lookupHsTokenType ran ranSplit (NodeInfo _ _ bd) = do
114-
maybeTokenType <- foldMapM (maybe (return Nothing) lookupHsTokenType . getIdentifier ranSplit) (M.keys bd)
95+
combineNodeIds :: (Monad m) => HsSemanticLookup -> Range -> SplitResult -> NodeInfo a -> Tokenizer m (DList (Range, HsSemanticTokenType))
96+
combineNodeIds lookupHsTokenType ran ranSplit (NodeInfo _ _ bd) =
11597
case (maybeTokenType, ranSplit) of
11698
(Nothing, _) -> return mempty
11799
(Just TModule, _) -> return $ DL.singleton (ran, TModule)
118100
(Just tokenType, NoSplit (_, tokenRan)) -> return $ DL.singleton (tokenRan, tokenType)
119101
(Just tokenType, Split (_, ranPrefix, tokenRan)) -> return $ DL.fromList [(ranPrefix, TModule),(tokenRan, tokenType)]
120-
getIdentifier ranSplit idt = do
102+
where maybeTokenType = foldMap (getIdentifier lookupHsTokenType ranSplit) (M.keys bd)
103+
104+
getIdentifier :: HsSemanticLookup -> SplitResult -> Identifier -> Maybe HsSemanticTokenType
105+
getIdentifier lookupHsTokenType ranSplit idt = do
121106
case idt of
122-
Left _moduleName -> Just idt
107+
Left _moduleName -> Just TModule
123108
Right name -> do
124109
occStr <- T.pack <$> case (occNameString . nameOccName) name of
125110
-- the generated selector name with {-# LANGUAGE DuplicateRecordFields #-}
@@ -129,7 +114,7 @@ visitLeafIds lookupHsTokenType leaf = liftMaybeM $ do
129114
c : ':' : _ | isAlphaNum c -> Nothing
130115
ns -> Just ns
131116
guard $ getSplitTokenText ranSplit == occStr
132-
return idt
117+
lookupHsTokenType idt
133118

134119

135120
focusTokenAt ::
@@ -153,7 +138,7 @@ focusTokenAt leaf = do
153138
let nce = newColumn ncs token
154139
-- compute the new range for utf16, tuning the columns is enough
155140
let ran = codePointRangeToRangeWith ncs nce $ realSrcSpanToCodePointRange span
156-
modify' $ \s -> s {columnsInUtf16 = nce, rope = remains, cursor = tokenEndPos}
141+
modify $ \s -> s {columnsInUtf16 = nce, rope = remains, cursor = tokenEndPos}
157142
return (ran, token)
158143
where
159144
srcSpanCharPositions :: RealSrcSpan -> (Char.Position, Char.Position)

plugins/hls-semantic-tokens-plugin/test/SemanticTokensTest.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ semanticTokensDataTypeTests =
270270
"get semantic Tokens"
271271
[ goldenWithSemanticTokensWithDefaultConfig "simple datatype" "TDataType",
272272
goldenWithSemanticTokensWithDefaultConfig "record" "TRecord",
273-
goldenWithSemanticTokensWithDefaultConfig "TRecordWildCards" "TRecordWildCards",
274273
goldenWithSemanticTokensWithDefaultConfig "record With DuplicateRecordFields" "TRecordDuplicateRecordFields",
275274
goldenWithSemanticTokensWithDefaultConfig "datatype import" "TDatatypeImported",
276275
goldenWithSemanticTokensWithDefaultConfig "datatype family" "TDataFamily",

plugins/hls-semantic-tokens-plugin/test/testdata/TRecordWildCards.expected

Lines changed: 0 additions & 12 deletions
This file was deleted.

plugins/hls-semantic-tokens-plugin/test/testdata/TRecordWildCards.hs

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)