From 244f92ec88242fddef5b34be34f1655c2799c8cc Mon Sep 17 00:00:00 2001 From: Pontus Leitzler Date: Tue, 26 Oct 2021 15:31:50 +0200 Subject: [PATCH] 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 Run-TryBot: Pontus Leitzler Reviewed-by: Peter Weinberger gopls-CI: kokoro TryBot-Result: Go Bot --- internal/lsp/semantic.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/internal/lsp/semantic.go b/internal/lsp/semantic.go index b1707ab3d3..c3ea15c6b1 100644 --- a/internal/lsp/semantic.go +++ b/internal/lsp/semantic.go @@ -828,8 +828,12 @@ func (e *encoded) Data() []uint32 { // each semantic token needs five values // (see Integer Encoding for Tokens in the LSP spec) x := make([]uint32, 5*len(e.items)) + var j int for i := 0; i < len(e.items); i++ { - j := 5 * i + typ, ok := typeMap[e.items[i].typeStr] + if !ok { + continue // client doesn't want typeStr + } if i == 0 { x[0] = e.items[0].line } else { @@ -840,19 +844,16 @@ func (e *encoded) Data() []uint32 { x[j+1] = e.items[i].start - e.items[i-1].start } x[j+2] = e.items[i].len - typ, ok := typeMap[e.items[i].typeStr] - if !ok { - continue // client doesn't want typeStr - } x[j+3] = uint32(typ) mask := 0 for _, s := range e.items[i].mods { - // modMpa[s] is 0 if the client doesn't want this modifier + // modMap[s] is 0 if the client doesn't want this modifier mask |= modMap[s] } x[j+4] = uint32(mask) + j += 5 } - return x + return x[:j] } func (e *encoded) importSpec(d *ast.ImportSpec) {