Skip to content

Commit 472e141

Browse files
committed
modify the lspTokenReverseMap to take semantic config
1 parent 0726b35 commit 472e141

File tree

2 files changed

+45
-40
lines changed

2 files changed

+45
-40
lines changed

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,15 @@ toLspTokenType conf tk = case tk of
4848
TRecField -> runIdentity $ stRecField conf
4949
TPatternSyn -> runIdentity $ stPatternSyn conf
5050

51-
lspTokenReverseMap :: Map.Map SemanticTokenTypes HsSemanticTokenType
52-
lspTokenReverseMap = Map.fromList $ map (\x -> (toLspTokenType def x, x)) $ enumFrom minBound
51+
lspTokenReverseMap :: SemanticTokensConfig -> Map.Map SemanticTokenTypes HsSemanticTokenType
52+
lspTokenReverseMap config
53+
| length xs /= Map.size mr = error "lspTokenReverseMap: token type mapping is not bijection"
54+
| otherwise = mr
55+
where xs = enumFrom minBound
56+
mr = Map.fromList $ map (\x -> (toLspTokenType config x, x)) xs
5357

54-
fromLspTokenType :: SemanticTokenTypes -> Maybe HsSemanticTokenType
55-
fromLspTokenType tk = Map.lookup tk lspTokenReverseMap
58+
fromLspTokenType :: SemanticTokensConfig -> SemanticTokenTypes -> Maybe HsSemanticTokenType
59+
fromLspTokenType cf tk = Map.lookup tk (lspTokenReverseMap cf)
5660

5761
-- * 2. Mapping from GHC type and tyThing to semantic token type.
5862

@@ -156,18 +160,19 @@ infoTokenType x = case x of
156160
-- for debug and test.
157161
-- this function is used to recover the original tokens(with token in haskell token type zoon)
158162
-- from the lsp semantic tokens(with token in lsp token type zoon)
159-
-- this use the default token type mapping
160-
recoverSemanticTokens :: VirtualFile -> SemanticTokens -> Either Text [SemanticTokenOriginal HsSemanticTokenType]
161-
recoverSemanticTokens v s = do
163+
-- the `SemanticTokensConfig` used should be a map with bijection property
164+
recoverSemanticTokens :: SemanticTokensConfig -> VirtualFile -> SemanticTokens -> Either Text [SemanticTokenOriginal HsSemanticTokenType]
165+
recoverSemanticTokens config v s = do
162166
tks <- recoverLspSemanticTokens v s
163-
return $ map fromLspTokenTypeStrict tks
167+
return $ map (lspTokenHsToken config) tks
164168

165-
-- | fromLspTokenTypeStrict
169+
-- | lspTokenHsToken
166170
-- for debug and test.
167-
-- use the default token type mapping to convert lsp token type to haskell token type
168-
fromLspTokenTypeStrict :: SemanticTokenOriginal SemanticTokenTypes -> SemanticTokenOriginal HsSemanticTokenType
169-
fromLspTokenTypeStrict (SemanticTokenOriginal tokenType location name) =
170-
case fromLspTokenType tokenType of
171+
-- use the `SemanticTokensConfig` to convert lsp token type to haskell token type
172+
-- the `SemanticTokensConfig` used should be a map with bijection property
173+
lspTokenHsToken :: SemanticTokensConfig -> SemanticTokenOriginal SemanticTokenTypes -> SemanticTokenOriginal HsSemanticTokenType
174+
lspTokenHsToken config (SemanticTokenOriginal tokenType location name) =
175+
case fromLspTokenType config tokenType of
171176
Just t -> SemanticTokenOriginal t location name
172177
Nothing -> error "recoverSemanticTokens: unknown lsp token type"
173178

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,20 @@ goldenWithHaskellAndCapsOutPut config plugin title tree path desc act =
7676
void waitForBuildQueue
7777
act doc
7878

79-
goldenWithSemanticTokens :: TestName -> FilePath -> TestTree
80-
goldenWithSemanticTokens title path =
79+
goldenWithSemanticTokensWithDefaultConfig :: TestName -> FilePath -> TestTree
80+
goldenWithSemanticTokensWithDefaultConfig title path =
8181
goldenWithHaskellAndCapsOutPut
8282
def
8383
semanticTokensPlugin
8484
title
8585
(mkFs $ FS.directProject (path <.> "hs"))
8686
path
8787
"expected"
88-
docSemanticTokensString
88+
(docSemanticTokensString def)
8989

90-
docSemanticTokensString :: TextDocumentIdentifier -> Session String
91-
docSemanticTokensString doc = do
92-
xs <- map fromLspTokenTypeStrict <$> docLspSemanticTokensString doc
90+
docSemanticTokensString :: SemanticTokensConfig-> TextDocumentIdentifier -> Session String
91+
docSemanticTokensString cf doc = do
92+
xs <- map (lspTokenHsToken cf) <$> docLspSemanticTokensString doc
9393
return $ unlines . map show $ xs
9494

9595
docLspSemanticTokensString :: TextDocumentIdentifier -> Session [SemanticTokenOriginal Language.LSP.Protocol.Types.SemanticTokenTypes]
@@ -106,19 +106,19 @@ semanticTokensClassTests :: TestTree
106106
semanticTokensClassTests =
107107
testGroup
108108
"type class"
109-
[ goldenWithSemanticTokens "golden type class" "TClass",
110-
goldenWithSemanticTokens "imported class method InstanceClassMethodBind" "TInstanceClassMethodBind",
111-
goldenWithSemanticTokens "imported class method TInstanceClassMethodUse" "TInstanceClassMethodUse",
112-
goldenWithSemanticTokens "imported deriving" "TClassImportedDeriving"
109+
[ goldenWithSemanticTokensWithDefaultConfig "golden type class" "TClass",
110+
goldenWithSemanticTokensWithDefaultConfig "imported class method InstanceClassMethodBind" "TInstanceClassMethodBind",
111+
goldenWithSemanticTokensWithDefaultConfig "imported class method TInstanceClassMethodUse" "TInstanceClassMethodUse",
112+
goldenWithSemanticTokensWithDefaultConfig "imported deriving" "TClassImportedDeriving"
113113
]
114114

115115
semanticTokensValuePatternTests :: TestTree
116116
semanticTokensValuePatternTests =
117117
testGroup
118118
"value and patterns "
119-
[ goldenWithSemanticTokens "value bind" "TValBind",
120-
goldenWithSemanticTokens "pattern match" "TPatternMatch",
121-
goldenWithSemanticTokens "pattern bind" "TPatternbind"
119+
[ goldenWithSemanticTokensWithDefaultConfig "value bind" "TValBind",
120+
goldenWithSemanticTokensWithDefaultConfig "pattern match" "TPatternMatch",
121+
goldenWithSemanticTokensWithDefaultConfig "pattern bind" "TPatternbind"
122122
]
123123

124124
mkSemanticConfig :: Value -> Config
@@ -187,35 +187,35 @@ semanticTokensTests =
187187
either
188188
(error . show)
189189
(\xs -> liftIO $ xs @?= expect)
190-
$ recoverSemanticTokens vfs tokens
190+
$ recoverSemanticTokens def vfs tokens
191191
return ()
192192
_ -> error "No tokens found"
193193
liftIO $ 1 @?= 1,
194-
goldenWithSemanticTokens "mixed constancy test result generated from one ghc version" "T1",
195-
goldenWithSemanticTokens "pattern bind" "TPatternSyn",
196-
goldenWithSemanticTokens "type family" "TTypefamily",
197-
goldenWithSemanticTokens "TUnicodeSyntax" "TUnicodeSyntax"
194+
goldenWithSemanticTokensWithDefaultConfig "mixed constancy test result generated from one ghc version" "T1",
195+
goldenWithSemanticTokensWithDefaultConfig "pattern bind" "TPatternSyn",
196+
goldenWithSemanticTokensWithDefaultConfig "type family" "TTypefamily",
197+
goldenWithSemanticTokensWithDefaultConfig "TUnicodeSyntax" "TUnicodeSyntax"
198198
]
199199

200200
semanticTokensDataTypeTests :: TestTree
201201
semanticTokensDataTypeTests =
202202
testGroup
203203
"get semantic Tokens"
204-
[ goldenWithSemanticTokens "simple datatype" "TDataType",
205-
goldenWithSemanticTokens "record" "TRecord",
206-
goldenWithSemanticTokens "datatype import" "TDatatypeImported",
207-
goldenWithSemanticTokens "datatype family" "TDataFamily",
208-
goldenWithSemanticTokens "GADT" "TGADT"
204+
[ goldenWithSemanticTokensWithDefaultConfig "simple datatype" "TDataType",
205+
goldenWithSemanticTokensWithDefaultConfig "record" "TRecord",
206+
goldenWithSemanticTokensWithDefaultConfig "datatype import" "TDatatypeImported",
207+
goldenWithSemanticTokensWithDefaultConfig "datatype family" "TDataFamily",
208+
goldenWithSemanticTokensWithDefaultConfig "GADT" "TGADT"
209209
]
210210

211211
semanticTokensFunctionTests :: TestTree
212212
semanticTokensFunctionTests =
213213
testGroup
214214
"get semantic of functions"
215-
[ goldenWithSemanticTokens "functions" "TFunction",
216-
goldenWithSemanticTokens "local functions" "TFunctionLocal",
217-
goldenWithSemanticTokens "function in let binding" "TFunctionLet",
218-
goldenWithSemanticTokens "negative case non-function with constraint" "TNoneFunctionWithConstraint"
215+
[ goldenWithSemanticTokensWithDefaultConfig "functions" "TFunction",
216+
goldenWithSemanticTokensWithDefaultConfig "local functions" "TFunctionLocal",
217+
goldenWithSemanticTokensWithDefaultConfig "function in let binding" "TFunctionLet",
218+
goldenWithSemanticTokensWithDefaultConfig "negative case non-function with constraint" "TNoneFunctionWithConstraint"
219219
]
220220

221221
main :: IO ()

0 commit comments

Comments
 (0)