@@ -41,9 +41,6 @@ data PTokenState = PTokenState
41
41
, columnsInUtf16 :: ! UInt -- the column of the start of the current rope in utf16
42
42
}
43
43
44
- runTokenizer :: (Monad m ) => Tokenizer m a -> PTokenState -> m a
45
- runTokenizer p st = evalStateT p st
46
-
47
44
data SplitResult
48
45
= NoSplit (Text , Range ) -- does not need to split, token text, token range
49
46
| Split (Text , Range , Range ) -- token text, prefix range(module range), token range
@@ -64,34 +61,33 @@ mkPTokenState vf =
64
61
}
65
62
66
63
-- lift a Tokenizer Maybe a to Tokenizer m a,
67
- -- if the Maybe is Nothing, do nothing, recover the state, and return the default value
68
- -- if the Maybe is Just a , do the action, and keep the state, and return a
69
- liftMaybeM :: (Monad m ) => a - > Tokenizer Maybe a -> Tokenizer m a
70
- liftMaybeM a p = do
64
+ -- if the Maybe is Nothing, do nothing, recover the state, and return the mempty value
65
+ -- if the Maybe is Just x , do the action, and keep the state, and return x
66
+ liftMaybeM :: (Monad m , Monoid a ) = > Tokenizer Maybe a -> Tokenizer m a
67
+ liftMaybeM p = do
71
68
st <- get
72
- maybe (return a) (\ (ans, st') -> put st' >> return ans) $ runStateT p st
73
-
74
- computeRangeHsSemanticTokenTypeList :: HsSemanticLookup -> VirtualFile -> HieAST a -> RangeHsSemanticTokenTypes
75
- computeRangeHsSemanticTokenTypeList lookupHsTokenType vf ast =
76
- RangeHsSemanticTokenTypes $ DL. toList $ runIdentity $ runTokenizer (foldAst lookupHsTokenType ast) (mkPTokenState vf)
69
+ maybe (return mempty ) (\ (ans, st') -> put st' >> return ans) $ runStateT p st
77
70
78
71
foldMapM :: (Monad m , Monoid b , Foldable t ) => (a -> m b ) -> t a -> m b
79
72
foldMapM f ta = foldM (\ b a -> mappend b <$> f a) mempty ta
80
73
74
+ computeRangeHsSemanticTokenTypeList :: HsSemanticLookup -> VirtualFile -> HieAST a -> RangeHsSemanticTokenTypes
75
+ computeRangeHsSemanticTokenTypeList lookupHsTokenType vf ast =
76
+ RangeHsSemanticTokenTypes $ DL. toList $ runIdentity $ evalStateT (foldAst lookupHsTokenType ast) (mkPTokenState vf)
81
77
-- | foldAst
82
78
-- visit every leaf node in the ast in depth first order
83
79
foldAst :: (Monad m ) => HsSemanticLookup -> HieAST t -> Tokenizer m (DList (Range , HsSemanticTokenType ))
84
80
foldAst lookupHsTokenType ast = if null (nodeChildren ast)
85
- then liftMaybeM mempty (visitLeafIds lookupHsTokenType ast)
81
+ then liftMaybeM (visitLeafIds lookupHsTokenType ast)
86
82
else foldMapM (foldAst lookupHsTokenType) $ nodeChildren ast
87
83
88
84
visitLeafIds :: HsSemanticLookup -> HieAST t -> Tokenizer Maybe (DList (Range , HsSemanticTokenType ))
89
- visitLeafIds lookupHsTokenType leaf = liftMaybeM mempty $ do
85
+ visitLeafIds lookupHsTokenType leaf = liftMaybeM $ do
90
86
let span = nodeSpan leaf
91
87
(ran, token) <- focusTokenAt leaf
92
88
-- if `focusTokenAt` succeed, we can safely assume we have shift the cursor correctly
93
89
-- we do not need to recover the cursor state, even if the following computation failed
94
- liftMaybeM mempty $ do
90
+ liftMaybeM $ do
95
91
-- only handle the leaf node with single column token
96
92
guard $ srcSpanStartLine span == srcSpanEndLine span
97
93
splitResult <- lift $ splitRangeByText token ran
@@ -104,8 +100,7 @@ visitLeafIds lookupHsTokenType leaf = liftMaybeM mempty $ do
104
100
(Just TModule , _) -> return $ DL. singleton (ran, TModule )
105
101
(Just tokenType, NoSplit (_, tokenRan)) -> return $ DL. singleton (tokenRan, tokenType)
106
102
(Just tokenType, Split (_, ranPrefix, tokenRan)) -> return $ DL. fromList [(ranPrefix, TModule ),(tokenRan, tokenType)]
107
- where
108
- maybeTokenType = foldMap (getIdentifier lookupHsTokenType ranSplit) (M. keys bd)
103
+ where maybeTokenType = foldMap (getIdentifier lookupHsTokenType ranSplit) (M. keys bd)
109
104
110
105
getIdentifier :: HsSemanticLookup -> SplitResult -> Identifier -> Maybe HsSemanticTokenType
111
106
getIdentifier lookupHsTokenType ranSplit idt = do
0 commit comments