Skip to content

Commit 244f92e

Browse files
leitzlerstamblerre
authored andcommitted
internal/lsp: do not send semantic tokens that client doesn't support
LSP clients tells gopls which token types it supports using SemanticTokensClientCapabilites, and when gopls ran into an unsupported token type it still did send that token with the first supported type (i.e. encoded token type 0). This change makes gopls omit tokens where the token type isn't one of the client supported ones. Change-Id: Ic3b6eeb56d67e773e75365660d301f5332f3ab44 Reviewed-on: https://go-review.googlesource.com/c/tools/+/358874 Trust: Pontus Leitzler <[email protected]> Run-TryBot: Pontus Leitzler <[email protected]> Reviewed-by: Peter Weinberger <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent c4ead46 commit 244f92e

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

internal/lsp/semantic.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -828,8 +828,12 @@ func (e *encoded) Data() []uint32 {
828828
// each semantic token needs five values
829829
// (see Integer Encoding for Tokens in the LSP spec)
830830
x := make([]uint32, 5*len(e.items))
831+
var j int
831832
for i := 0; i < len(e.items); i++ {
832-
j := 5 * i
833+
typ, ok := typeMap[e.items[i].typeStr]
834+
if !ok {
835+
continue // client doesn't want typeStr
836+
}
833837
if i == 0 {
834838
x[0] = e.items[0].line
835839
} else {
@@ -840,19 +844,16 @@ func (e *encoded) Data() []uint32 {
840844
x[j+1] = e.items[i].start - e.items[i-1].start
841845
}
842846
x[j+2] = e.items[i].len
843-
typ, ok := typeMap[e.items[i].typeStr]
844-
if !ok {
845-
continue // client doesn't want typeStr
846-
}
847847
x[j+3] = uint32(typ)
848848
mask := 0
849849
for _, s := range e.items[i].mods {
850-
// modMpa[s] is 0 if the client doesn't want this modifier
850+
// modMap[s] is 0 if the client doesn't want this modifier
851851
mask |= modMap[s]
852852
}
853853
x[j+4] = uint32(mask)
854+
j += 5
854855
}
855-
return x
856+
return x[:j]
856857
}
857858

858859
func (e *encoded) importSpec(d *ast.ImportSpec) {

0 commit comments

Comments
 (0)