Skip to content

Commit 5da8462

Browse files
committed
cleanup
1 parent 332d29a commit 5da8462

File tree

1 file changed

+12
-17
lines changed
  • plugins/hls-semantic-tokens-plugin/src/Ide/Plugin/SemanticTokens

1 file changed

+12
-17
lines changed

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

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ data PTokenState = PTokenState
4141
, columnsInUtf16 :: !UInt -- the column of the start of the current rope in utf16
4242
}
4343

44-
runTokenizer :: (Monad m) => Tokenizer m a -> PTokenState -> m a
45-
runTokenizer p st = evalStateT p st
46-
4744
data SplitResult
4845
= NoSplit (Text, Range) -- does not need to split, token text, token range
4946
| Split (Text, Range, Range) -- token text, prefix range(module range), token range
@@ -64,34 +61,33 @@ mkPTokenState vf =
6461
}
6562

6663
-- 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
7168
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
7770

7871
foldMapM :: (Monad m, Monoid b, Foldable t) => (a -> m b) -> t a -> m b
7972
foldMapM f ta = foldM (\b a -> mappend b <$> f a) mempty ta
8073

74+
computeRangeHsSemanticTokenTypeList :: HsSemanticLookup -> VirtualFile -> HieAST a -> RangeHsSemanticTokenTypes
75+
computeRangeHsSemanticTokenTypeList lookupHsTokenType vf ast =
76+
RangeHsSemanticTokenTypes $ DL.toList $ runIdentity $ evalStateT (foldAst lookupHsTokenType ast) (mkPTokenState vf)
8177
-- | foldAst
8278
-- visit every leaf node in the ast in depth first order
8379
foldAst :: (Monad m) => HsSemanticLookup -> HieAST t -> Tokenizer m (DList (Range, HsSemanticTokenType))
8480
foldAst lookupHsTokenType ast = if null (nodeChildren ast)
85-
then liftMaybeM mempty (visitLeafIds lookupHsTokenType ast)
81+
then liftMaybeM (visitLeafIds lookupHsTokenType ast)
8682
else foldMapM (foldAst lookupHsTokenType) $ nodeChildren ast
8783

8884
visitLeafIds :: HsSemanticLookup -> HieAST t -> Tokenizer Maybe (DList (Range, HsSemanticTokenType))
89-
visitLeafIds lookupHsTokenType leaf = liftMaybeM mempty $ do
85+
visitLeafIds lookupHsTokenType leaf = liftMaybeM $ do
9086
let span = nodeSpan leaf
9187
(ran, token) <- focusTokenAt leaf
9288
-- if `focusTokenAt` succeed, we can safely assume we have shift the cursor correctly
9389
-- we do not need to recover the cursor state, even if the following computation failed
94-
liftMaybeM mempty $ do
90+
liftMaybeM $ do
9591
-- only handle the leaf node with single column token
9692
guard $ srcSpanStartLine span == srcSpanEndLine span
9793
splitResult <- lift $ splitRangeByText token ran
@@ -104,8 +100,7 @@ visitLeafIds lookupHsTokenType leaf = liftMaybeM mempty $ do
104100
(Just TModule, _) -> return $ DL.singleton (ran, TModule)
105101
(Just tokenType, NoSplit (_, tokenRan)) -> return $ DL.singleton (tokenRan, tokenType)
106102
(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)
109104

110105
getIdentifier :: HsSemanticLookup -> SplitResult -> Identifier -> Maybe HsSemanticTokenType
111106
getIdentifier lookupHsTokenType ranSplit idt = do

0 commit comments

Comments
 (0)