1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:44:43 -07:00

internal/lsp: make snippets private fields

Change-Id: I35d883196c7c3b35e14b49c0f5c779a91e72ce42
Reviewed-on: https://go-review.googlesource.com/c/tools/+/177177
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Rebecca Stambler 2019-05-14 14:15:18 -04:00
parent 7d7faa4812
commit 2196cb7019
4 changed files with 25 additions and 25 deletions

View File

@ -65,11 +65,7 @@ func toProtocolCompletionItems(candidates []source.CompletionItem, prefix string
}
insertText := candidate.InsertText
if insertTextFormat == protocol.SnippetTextFormat {
if usePlaceholders && candidate.PlaceholderSnippet != nil {
insertText = candidate.PlaceholderSnippet.String()
} else if candidate.Snippet != nil {
insertText = candidate.Snippet.String()
}
insertText = candidate.Snippet(usePlaceholders)
}
item := protocol.CompletionItem{
Label: candidate.Label,

View File

@ -44,7 +44,7 @@ type CompletionItem struct {
//
// foo(${1:})
//
Snippet *snippet.Builder
plainSnippet *snippet.Builder
// PlaceholderSnippet is the LSP snippet for the completion ite, containing
// placeholders. The LSP specification contains details about LSP snippets.
@ -56,7 +56,21 @@ type CompletionItem struct {
//
// foo(${1:a int}, ${2: b int}, ${3: c int})
//
PlaceholderSnippet *snippet.Builder
placeholderSnippet *snippet.Builder
}
// Snippet is a convenience function that determines the snippet that should be
// used for an item, depending on if the callee wants placeholders or not.
func (i *CompletionItem) Snippet(usePlaceholders bool) string {
if usePlaceholders {
if i.placeholderSnippet != nil {
return i.placeholderSnippet.String()
}
}
if i.plainSnippet != nil {
return i.plainSnippet.String()
}
return i.InsertText
}
type CompletionItemKind int

View File

@ -81,8 +81,8 @@ func (c *completer) item(obj types.Object, score float64) CompletionItem {
Detail: detail,
Kind: kind,
Score: score,
Snippet: plainSnippet,
PlaceholderSnippet: placeholderSnippet,
plainSnippet: plainSnippet,
placeholderSnippet: placeholderSnippet,
}
}
@ -126,7 +126,7 @@ func (c *completer) formatBuiltin(obj types.Object, score float64) CompletionIte
params, _ := c.formatFieldList(decl.Type.Params)
results, writeResultParens := c.formatFieldList(decl.Type.Results)
item.Label, item.Detail = formatFunction(obj.Name(), params, results, writeResultParens)
item.Snippet, item.PlaceholderSnippet = c.functionCallSnippets(obj.Name(), params)
item.plainSnippet, item.placeholderSnippet = c.functionCallSnippets(obj.Name(), params)
case *types.TypeName:
if types.IsInterface(obj.Type()) {
item.Kind = InterfaceCompletionItem

View File

@ -173,32 +173,22 @@ func (r *runner) checkCompletionSnippets(ctx context.Context, t *testing.T, data
}
wantCompletion := items[want.CompletionItem]
var gotItem *source.CompletionItem
var got *source.CompletionItem
for _, item := range list {
if item.Label == wantCompletion.Label {
gotItem = &item
got = &item
break
}
}
if gotItem == nil {
if got == nil {
t.Fatalf("%s: couldn't find completion matching %q", src.URI(), wantCompletion.Label)
}
var expected string
expected := want.PlainSnippet
if usePlaceholders {
expected = want.PlaceholderSnippet
} else {
expected = want.PlainSnippet
}
insertText := gotItem.InsertText
if usePlaceholders && gotItem.PlaceholderSnippet != nil {
insertText = gotItem.PlaceholderSnippet.String()
} else if gotItem.Snippet != nil {
insertText = gotItem.Snippet.String()
}
if expected != insertText {
if insertText := got.Snippet(usePlaceholders); expected != insertText {
t.Errorf("%s: expected snippet %q, got %q", src, expected, insertText)
}
}