internal/lsp: fix incorrect line and start of semantic tokens

Current implementation returns incorrect result when some of the items are skipped, because positions could be relative to a skipped item. Instead, each position must be relative to the previous item added to the result.

Change-Id: I3c1a68d37bf0c9cfc1bccfe6f76c25b536224293
GitHub-Last-Rev: 6eedc7cbcc27f47ed5742b340f4438291ab70863
GitHub-Pull-Request: golang/tools#376
Reviewed-on: https://go-review.googlesource.com/c/tools/+/396715
Run-TryBot: Peter Weinberger <pjw@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Peter Weinberger <pjw@google.com>
Trust: Peter Weinberger <pjw@google.com>
Trust: Suzy Mueller <suzmue@golang.org>
This commit is contained in:
hiroebe 2022-03-30 17:03:58 +00:00 committed by Peter Weinberger
parent 41787c4fe7
commit cda13e227d
1 changed files with 6 additions and 4 deletions

View File

@ -829,19 +829,20 @@ func (e *encoded) Data() []uint32 {
// (see Integer Encoding for Tokens in the LSP spec)
x := make([]uint32, 5*len(e.items))
var j int
var last semItem
for i := 0; i < len(e.items); i++ {
typ, ok := typeMap[e.items[i].typeStr]
if !ok {
continue // client doesn't want typeStr
}
if i == 0 {
if j == 0 {
x[0] = e.items[0].line
} else {
x[j] = e.items[i].line - e.items[i-1].line
x[j] = e.items[i].line - last.line
}
x[j+1] = e.items[i].start
if i > 0 && e.items[i].line == e.items[i-1].line {
x[j+1] = e.items[i].start - e.items[i-1].start
if j > 0 && x[j] == 0 {
x[j+1] = e.items[i].start - last.start
}
x[j+2] = e.items[i].len
x[j+3] = uint32(typ)
@ -852,6 +853,7 @@ func (e *encoded) Data() []uint32 {
}
x[j+4] = uint32(mask)
j += 5
last = e.items[i]
}
return x[:j]
}