internal/lsp: trim address operator from completion filterText

VSCode doesn't like (read: ignores) candidates whose filterText begins
with "&", so trim it off.

I also tweaked "addressed" candidates to include the "&" prefix in the
item label as well so the user can see what they will get.

Change-Id: I85840d036e379a202b72e28c5257807a069ae45d
Reviewed-on: https://go-review.googlesource.com/c/tools/+/212406
Run-TryBot: Muir Manders <muir@mnd.rs>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Muir Manders 2019-12-23 20:06:34 -08:00 committed by Rebecca Stambler
parent 3721262b3e
commit dd894d0a8a
3 changed files with 20 additions and 9 deletions

View File

@ -8,6 +8,7 @@ import (
"context"
"fmt"
"sort"
"strings"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
@ -122,8 +123,12 @@ func toProtocolCompletionItems(candidates []source.CompletionItem, rng protocol.
// This is a hack so that the client sorts completion results in the order
// according to their score. This can be removed upon the resolution of
// https://github.com/Microsoft/language-server-protocol/issues/348.
SortText: fmt.Sprintf("%05d", i),
FilterText: candidate.InsertText,
SortText: fmt.Sprintf("%05d", i),
// Trim address operator (VSCode doesn't like weird characters
// in filterText).
FilterText: strings.TrimLeft(candidate.InsertText, "&"),
Preselect: i == 0,
Documentation: candidate.Documentation,
}

View File

@ -152,6 +152,8 @@ func (c *completer) item(cand candidate) (CompletionItem, error) {
// If there is no selector, just stick the "&" at the start.
insert = "&" + insert
}
label = "&" + label
}
detail = strings.TrimPrefix(detail, "untyped ")

View File

@ -10,13 +10,16 @@ func _() {
b int //@item(addrB, "b", "int", "var")
)
wantsPtr() //@rank(")", addrB, addrA),snippet(")", addrB, "&b", "&b")
&b //@item(addrBRef, "&b", "int", "var")
wantsPtr() //@rank(")", addrBRef, addrA),snippet(")", addrBRef, "&b", "&b")
wantsPtr(&b) //@snippet(")", addrB, "b", "b")
var s foo
s.c //@item(addrDeepC, "s.c", "int", "field")
wantsPtr() //@snippet(")", addrDeepC, "&s.c", "&s.c")
wantsPtr(s) //@snippet(")", addrDeepC, "&s.c", "&s.c")
&s.c //@item(addrDeepCRef, "&s.c", "int", "field")
wantsPtr() //@snippet(")", addrDeepCRef, "&s.c", "&s.c")
wantsPtr(s) //@snippet(")", addrDeepCRef, "&s.c", "&s.c")
wantsPtr(&s) //@snippet(")", addrDeepC, "s.c", "s.c")
// don't add "&" in item (it gets added as an additional edit)
@ -32,9 +35,10 @@ func _() {
getFoo().c //@item(addrGetFooC, "getFoo().c", "int", "field")
// addressable
getFoo().ptr().c //@item(addrGetFooPtrC, "getFoo().ptr().c", "int", "field")
getFoo().ptr().c //@item(addrGetFooPtrC, "getFoo().ptr().c", "int", "field")
&getFoo().ptr().c //@item(addrGetFooPtrCRef, "&getFoo().ptr().c", "int", "field")
wantsPtr() //@rank(addrGetFooPtrC, addrGetFooC),snippet(")", addrGetFooPtrC, "&getFoo().ptr().c", "&getFoo().ptr().c")
wantsPtr() //@rank(addrGetFooPtrCRef, addrGetFooC),snippet(")", addrGetFooPtrCRef, "&getFoo().ptr().c", "&getFoo().ptr().c")
wantsPtr(&g) //@rank(addrGetFooPtrC, addrGetFooC),snippet(")", addrGetFooPtrC, "getFoo().ptr().c", "getFoo().ptr().c")
}
@ -45,8 +49,8 @@ type nested struct {
func _() {
getNested := func() nested { return nested{} }
getNested().f.c //@item(addrNestedC, "getNested().f.c", "int", "field")
getNested().f.ptr().c //@item(addrNestedPtrC, "getNested().f.ptr().c", "int", "field")
getNested().f.c //@item(addrNestedC, "getNested().f.c", "int", "field")
&getNested().f.ptr().c //@item(addrNestedPtrC, "&getNested().f.ptr().c", "int", "field")
// addrNestedC is not addressable, so rank lower
wantsPtr(getNestedfc) //@fuzzy(")", addrNestedPtrC, addrNestedC)