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 <leitzler@gmail.com>
Run-TryBot: Pontus Leitzler <leitzler@gmail.com>
Reviewed-by: Peter Weinberger <pjw@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
diff --git a/internal/lsp/semantic.go b/internal/lsp/semantic.go
index b1707ab..c3ea15c 100644
--- a/internal/lsp/semantic.go
+++ b/internal/lsp/semantic.go
@@ -828,8 +828,12 @@
 	// 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 @@
 			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) {