1
0
mirror of https://github.com/golang/go synced 2024-11-18 19:24:39 -07:00

internal/lsp: change CompletionItem.{Command,TextEdit} to pointers

This is a continuation of the discussion on
https://github.com/microsoft/vscode-go/issues/2920. Add corresponding
checks to internal/lsp/cmd/capabilities_test.go.

Change-Id: I51af05dee9e7ecea0e40733dd4c5ca3dfb8f4dd8
Reviewed-on: https://go-review.googlesource.com/c/tools/+/209859
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Rebecca Stambler 2019-12-04 13:36:18 -05:00
parent 4981f6b3ad
commit 1d943b0903
4 changed files with 39 additions and 5 deletions

View File

@ -114,6 +114,40 @@ func TestCapabilities(t *testing.T) {
}); err != nil { }); err != nil {
t.Fatal(err) t.Fatal(err)
} }
// Send a completion request to validate expected types.
list, err := c.Server.Completion(ctx, &protocol.CompletionParams{
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
TextDocument: protocol.TextDocumentIdentifier{
URI: uri,
},
Position: protocol.Position{
Line: 0,
Character: 28,
},
},
})
if err != nil {
t.Fatal(err)
}
for _, item := range list.Items {
// We expect the "editor.action.triggerParameterHints" command for functions and methods.
if item.Kind == protocol.MethodCompletion || item.Kind == protocol.FunctionCompletion {
continue
}
// All other completion items should have nil commands.
// An empty command will be treated as a command with the name '' by VS Code.
// This causes VS Code to report errors to users about invalid commands.
if item.Command != nil {
t.Errorf("unexpected command for non-function completion item")
}
// The item's TextEdit must be a pointer, as VS Code considers TextEdits
// that don't contain the cursor position to be invalid.
var textEdit interface{} = item.TextEdit
if _, ok := textEdit.(*protocol.TextEdit); !ok {
t.Errorf("textEdit is not a *protocol.TextEdit, instead it is %T", textEdit)
}
}
} }
func validateCapabilities(result *protocol.InitializeResult) error { func validateCapabilities(result *protocol.InitializeResult) error {

View File

@ -114,7 +114,7 @@ func toProtocolCompletionItems(candidates []source.CompletionItem, rng protocol.
Label: candidate.Label, Label: candidate.Label,
Detail: candidate.Detail, Detail: candidate.Detail,
Kind: candidate.Kind, Kind: candidate.Kind,
TextEdit: protocol.TextEdit{ TextEdit: &protocol.TextEdit{
NewText: insertText, NewText: insertText,
Range: rng, Range: rng,
}, },
@ -133,7 +133,7 @@ func toProtocolCompletionItems(candidates []source.CompletionItem, rng protocol.
// since we show return types as well. // since we show return types as well.
switch item.Kind { switch item.Kind {
case protocol.FunctionCompletion, protocol.MethodCompletion: case protocol.FunctionCompletion, protocol.MethodCompletion:
item.Command = protocol.Command{ item.Command = &protocol.Command{
Command: "editor.action.triggerParameterHints", Command: "editor.action.triggerParameterHints",
} }
} }

View File

@ -552,7 +552,7 @@ type CompletionItem struct {
* *Note:* The text edit's range must be a [single line] and it must contain the position * *Note:* The text edit's range must be a [single line] and it must contain the position
* at which completion has been requested. * at which completion has been requested.
*/ */
TextEdit TextEdit `json:"textEdit,omitempty"` TextEdit *TextEdit `json:"textEdit,omitempty"`
/** /**
* An optional array of additional [text edits](#TextEdit) that are applied when * An optional array of additional [text edits](#TextEdit) that are applied when
* selecting this completion. Edits must not overlap (including the same insert position) * selecting this completion. Edits must not overlap (including the same insert position)
@ -574,7 +574,7 @@ type CompletionItem struct {
* additional modifications to the current document should be described with the * additional modifications to the current document should be described with the
* [additionalTextEdits](#CompletionItem.additionalTextEdits)-property. * [additionalTextEdits](#CompletionItem.additionalTextEdits)-property.
*/ */
Command Command `json:"command,omitempty"` Command *Command `json:"command,omitempty"`
/** /**
* An data entry field that is preserved on a completion item between * An data entry field that is preserved on a completion item between
* a [CompletionRequest](#CompletionRequest) and a [CompletionResolveRequest] * a [CompletionRequest](#CompletionRequest) and a [CompletionResolveRequest]

View File

@ -26,7 +26,7 @@ func ToProtocolCompletionItem(item source.CompletionItem) protocol.CompletionIte
Detail: item.Detail, Detail: item.Detail,
Documentation: item.Documentation, Documentation: item.Documentation,
InsertText: item.InsertText, InsertText: item.InsertText,
TextEdit: protocol.TextEdit{ TextEdit: &protocol.TextEdit{
NewText: item.Snippet(), NewText: item.Snippet(),
}, },
// Negate score so best score has lowest sort text like real API. // Negate score so best score has lowest sort text like real API.