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 insertText := candidate.InsertText
if insertTextFormat == protocol.SnippetTextFormat { if insertTextFormat == protocol.SnippetTextFormat {
if usePlaceholders && candidate.PlaceholderSnippet != nil { insertText = candidate.Snippet(usePlaceholders)
insertText = candidate.PlaceholderSnippet.String()
} else if candidate.Snippet != nil {
insertText = candidate.Snippet.String()
}
} }
item := protocol.CompletionItem{ item := protocol.CompletionItem{
Label: candidate.Label, Label: candidate.Label,

View File

@ -44,7 +44,7 @@ type CompletionItem struct {
// //
// foo(${1:}) // foo(${1:})
// //
Snippet *snippet.Builder plainSnippet *snippet.Builder
// PlaceholderSnippet is the LSP snippet for the completion ite, containing // PlaceholderSnippet is the LSP snippet for the completion ite, containing
// placeholders. The LSP specification contains details about LSP snippets. // 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}) // 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 type CompletionItemKind int

View File

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

View File

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