11{-# LANGUAGE OverloadedRecordDot #-}
22{-# LANGUAGE OverloadedStrings #-}
3+ {-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
4+ {-# HLINT ignore "Avoid restricted function" #-}
35
46module Ide.Plugin.SemanticTokens.Tokenize (computeRangeHsSemanticTokenTypeList ) where
57
6- import Control.Lens (Identity (runIdentity ))
8+ import Control.Lens (Identity (Identity , runIdentity ))
79import Control.Monad (foldM , guard )
810import Control.Monad.State.Strict (MonadState (get ),
911 MonadTrans (lift ),
10- evalStateT , gets , modify' ,
11- put )
12+ evalStateT , gets , mapStateT ,
13+ modify' , put )
1214import Control.Monad.Trans.State.Strict (StateT , runStateT )
1315import Data.Char (isAlphaNum )
1416import Data.DList (DList )
@@ -83,20 +85,21 @@ liftMaybeM p = do
8385 st <- get
8486 maybe (return mempty ) (\ (ans, st') -> put st' >> return ans) $ runStateT p st
8587
88+
8689foldMapM :: (Monad m , Monoid b , Foldable t ) => (a -> m b ) -> t a -> m b
8790foldMapM f ta = foldM (\ b a -> mappend b <$> f a) mempty ta
8891
8992computeRangeHsSemanticTokenTypeList :: HsSemanticLookup -> VirtualFile -> HieAST a -> RangeHsSemanticTokenTypes
9093computeRangeHsSemanticTokenTypeList lookupHsTokenType vf ast =
91- RangeHsSemanticTokenTypes $ DL. toList $ runIdentity $ evalStateT (foldAst lookupHsTokenType ast) (mkPTokenState vf)
94+ RangeHsSemanticTokenTypes $ DL. toList $ runIdentity $ evalStateT (foldAst (cacheLookup lookupHsTokenType) ast) (mkPTokenState vf)
9295-- | foldAst
9396-- visit every leaf node in the ast in depth first order
94- foldAst :: (Monad m ) => HsSemanticLookup -> HieAST t -> Tokenizer m (DList (Range , HsSemanticTokenType ))
97+ foldAst :: (Monad m ) => CachedHsSemanticLookup Identity -> HieAST t -> Tokenizer m (DList (Range , HsSemanticTokenType ))
9598foldAst lookupHsTokenType ast = if null (nodeChildren ast)
9699 then visitLeafIds lookupHsTokenType ast
97100 else foldMapM (foldAst lookupHsTokenType) $ nodeChildren ast
98101
99- visitLeafIds :: (Monad m ) => HsSemanticLookup -> HieAST t -> Tokenizer m (DList (Range , HsSemanticTokenType ))
102+ visitLeafIds :: (Monad m ) => CachedHsSemanticLookup Identity -> HieAST t -> Tokenizer m (DList (Range , HsSemanticTokenType ))
100103visitLeafIds lookupHsTokenType leaf = liftMaybeM $ do
101104 let span = nodeSpan leaf
102105 (ran, token) <- focusTokenAt leaf
@@ -106,20 +109,23 @@ visitLeafIds lookupHsTokenType leaf = liftMaybeM $ do
106109 -- only handle the leaf node with single column token
107110 guard $ srcSpanStartLine span == srcSpanEndLine span
108111 splitResult <- lift $ splitRangeByText token ran
109- foldMapM (combineNodeIds lookupHsTokenType ran splitResult) $ Map. filterWithKey (\ k _ -> k == SourceInfo ) $ getSourcedNodeInfo $ sourcedNodeInfo leaf
112+ mapStateT hoistIdMaybe
113+ $ foldMapM (combineNodeIds lookupHsTokenType ran splitResult)
114+ $ Map. filterWithKey (\ k _ -> k == SourceInfo ) $ getSourcedNodeInfo $ sourcedNodeInfo leaf
110115 where
111- combineNodeIds :: (Monad m ) => HsSemanticLookup -> Range -> SplitResult -> NodeInfo a -> Tokenizer m (DList (Range , HsSemanticTokenType ))
116+ hoistIdMaybe :: Identity (a , s ) -> Maybe (a , s )
117+ hoistIdMaybe (Identity x) = Just x
118+ combineNodeIds :: CachedHsSemanticLookup Identity -> Range -> SplitResult -> NodeInfo a -> Tokenizer Identity (DList (Range , HsSemanticTokenType ))
112119 combineNodeIds lookupHsTokenType ran ranSplit (NodeInfo _ _ bd) = do
113- maybeTokenType <- foldMapM (cacheLookup $ lookupIdentifier lookupHsTokenType ranSplit) (M. keys bd)
120+ maybeTokenType <- foldMapM (maybe ( return Nothing ) lookupHsTokenType . getIdentifier ranSplit) (M. keys bd)
114121 case (maybeTokenType, ranSplit) of
115122 (Nothing , _) -> return mempty
116123 (Just TModule , _) -> return $ DL. singleton (ran, TModule )
117124 (Just tokenType, NoSplit (_, tokenRan)) -> return $ DL. singleton (tokenRan, tokenType)
118125 (Just tokenType, Split (_, ranPrefix, tokenRan)) -> return $ DL. fromList [(ranPrefix, TModule ),(tokenRan, tokenType)]
119- lookupIdentifier :: HsSemanticLookup -> SplitResult -> HsSemanticLookup
120- lookupIdentifier lookupHsTokenType ranSplit idt = do
126+ getIdentifier ranSplit idt = do
121127 case idt of
122- Left _moduleName -> Just TModule
128+ Left _moduleName -> Just idt
123129 Right name -> do
124130 occStr <- T. pack <$> case (occNameString . nameOccName) name of
125131 -- the generated selector name with {-# LANGUAGE DuplicateRecordFields #-}
@@ -129,7 +135,7 @@ visitLeafIds lookupHsTokenType leaf = liftMaybeM $ do
129135 c : ' :' : _ | isAlphaNum c -> Nothing
130136 ns -> Just ns
131137 guard $ getSplitTokenText ranSplit == occStr
132- lookupHsTokenType idt
138+ return idt
133139
134140
135141focusTokenAt ::
0 commit comments