Skip to content

Commit

Permalink
lsp: Recognise new token type for function name (#1233)
Browse files Browse the repository at this point in the history
* lsp: Recognise new token type for function name

* handlers: Add test w/ new function semantic token
  • Loading branch information
radeksimko authored Apr 6, 2023
1 parent 61f5de9 commit 369d561
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/syntax-highlighting.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Fallback types are chosen based on meaningful semantic mapping and default theme
| `hcl-traversalStep` | `variable` |
| `hcl-typeCapsule` | `function` |
| `hcl-typePrimitive` | `keyword` |
| `hcl-functionName` | `function` |

#### Token Modifiers

Expand Down
120 changes: 120 additions & 0 deletions internal/langserver/handlers/semantic_tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,123 @@ func TestVarsSemanticTokensFull(t *testing.T) {
}
}`)
}

func TestVarsSemanticTokensFull_functionToken(t *testing.T) {
tmpDir := TempDir(t)
InitPluginCache(t, tmpDir.Path())

var testSchema tfjson.ProviderSchemas
err := json.Unmarshal([]byte(testModuleSchemaOutput), &testSchema)
if err != nil {
t.Fatal(err)
}

ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}
wc := walker.NewWalkerCollector()

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
tmpDir.Path(): {
{
Method: "Version",
Repeatability: 1,
Arguments: []interface{}{
mock.AnythingOfType(""),
},
ReturnArguments: []interface{}{
version.Must(version.NewVersion("1.0.0")),
nil,
nil,
},
},
{
Method: "GetExecPath",
Repeatability: 1,
ReturnArguments: []interface{}{
"",
},
},
{
Method: "ProviderSchemas",
Repeatability: 1,
Arguments: []interface{}{
mock.AnythingOfType(""),
},
ReturnArguments: []interface{}{
&testSchema,
nil,
},
},
},
},
},
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()

ls.Call(t, &langserver.CallRequest{
Method: "initialize",
ReqParams: fmt.Sprintf(`{
"capabilities": {
"textDocument": {
"semanticTokens": {
"tokenTypes": [
"type",
"property",
"string",
"function"
],
"tokenModifiers": [
"defaultLibrary",
"deprecated"
],
"requests": {
"full": true
}
}
}
},
"rootUri": %q,
"processId": 12345
}`, tmpDir.URI)})
waitForWalkerPath(t, ss, wc, tmpDir)
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
})
ls.Call(t, &langserver.CallRequest{
Method: "textDocument/didOpen",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"version": 0,
"languageId": "terraform",
"text": "locals {\n foo = abs(-42)\n}\n",
"uri": "%s/locals.tf"
}
}`, tmpDir.URI)})
waitForAllJobs(t, ss)

ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/semanticTokens/full",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"uri": "%s/locals.tf"
}
}`, tmpDir.URI)}, `{
"jsonrpc": "2.0",
"id": 3,
"result": {
"data": [
0,0,6,3,0,
1,2,3,1,0,
0,6,3,0,0
]
}
}`)
}
3 changes: 3 additions & 0 deletions internal/lsp/token_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ func (te *TokenEncoder) resolveTokenType(token lang.SemanticToken) (semtok.Token
case lang.TokenTypePrimitive:
return te.firstSupportedTokenType(
semtok.TokenType(lang.TokenTypePrimitive), semtok.TokenTypeKeyword)
case lang.TokenFunctionName:
return te.firstSupportedTokenType(
semtok.TokenType(lang.TokenFunctionName), semtok.TokenTypeFunction)
}

return "", false
Expand Down

0 comments on commit 369d561

Please sign in to comment.