2019-11-21 21:42:27 -07:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp"
|
|
|
|
"golang.org/x/tools/internal/lsp/cache"
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
errors "golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestCapabilities does some minimal validation of the server's adherence to the LSP.
|
|
|
|
// The checks in the test are added as changes are made and errors noticed.
|
|
|
|
func TestCapabilities(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "fake")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tmpFile := filepath.Join(tmpDir, "fake.go")
|
|
|
|
if err := ioutil.WriteFile(tmpFile, []byte(""), 0775); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte(`module fake`), 0775); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
app := New("gopls-test", tmpDir, os.Environ(), nil)
|
|
|
|
c := newConnection(app)
|
|
|
|
ctx := context.Background()
|
|
|
|
defer c.terminate(ctx)
|
|
|
|
|
|
|
|
params := &protocol.ParamInitialize{}
|
|
|
|
params.RootURI = string(span.FileURI(c.Client.app.wd))
|
|
|
|
params.Capabilities.Workspace.Configuration = true
|
|
|
|
|
|
|
|
// Send an initialize request to the server.
|
internal/lsp: refactor LSP server instantiation
Previously, the process of instantiating and running the LSP server was
sharded across the lsp, protocol, and cmd packages, and this resulted in
some APIs that are hard to work with. For example, it's hard to guess
the difference between lsp.NewClientServer, lsp.NewServer,
protocol.NewServer (which returns a client), and protocol.NewClient
(which returns a server).
This change reorganizes Server instantiation as follows:
+ The lsp.Server is now purely an implementation of the protocol.Server
interface. It is no longer responsible for installing itself into the
jsonrpc2 Stream, nor for running itself.
+ A new package 'lsprpc' is added, to implement the logic of binding an
incoming connection to an LSP server session. This is put in a
separate package for lack of a clear home: it didn't really
philosophically belong in any of the lsp, cmd, or protocol packages.
We can perhaps move it to cmd in the future, but I'd like to keep it
as a separate package while I develop request forwarding.
simplified import graph:
jsonrpc2 ⭠ lsprpc ⭠ cmd
⭩ ⭦
lsp (t.b.d. client tests)
⭩ ⭨
protocol source
+ The jsonrpc2 package is extended to have a minimal API for running a
'StreamServer': something analogous to an HTTP server that listens
for new connections and delegates to a handler (but we couldn't use
the word 'Handler' for this delegate as it was already taken).
After these changes, I hope that the concerns of "serving the LSP",
"serving jsonrpc2", and "installing the LSP on jsonrpc2" are more
logically organized, though one legitimate criticism is that the word
'Server' is still heavily overloaded.
This change prepares a subsequent change which hijacks the jsonrpc2
connection when forwarding messages to a shared gopls instance.
To test this change, the following improvements are made:
+ A servertest package is added to make it easier to run a test against
an in-process jsonrpc2 server. For now, this uses TCP but it could
easily be modified to use io.Pipe.
+ cmd tests are updated to use the servertest package. Unfortunately it
wasn't yet possible to eliminate the concept of `remote=internal` in
favor of just using multiple sessions, because view initialization
involves calling both `go env` and `packages.Load`, which slow down
session startup significantly. See also golang.org/issue/35968.
Instead, the syntax for `-remote=internal` is modified to be
`-remote=internal@127.0.0.1:12345`.
+ An additional test for request cancellation is added for the
sessionserver package. This test uncovered a bug: when calling
Canceller.Cancel, we were using id rather than &id, which resulted in
incorrect json serialization (as only the pointer receiver implements
the json.Marshaller interface).
Updates golang/go#34111
Change-Id: I75c219df634348cdf53a9e57839b98588311a9ef
Reviewed-on: https://go-review.googlesource.com/c/tools/+/215742
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-01-21 17:34:50 -07:00
|
|
|
c.Server = lsp.NewServer(cache.New(app.options).NewSession(), c.Client)
|
2019-11-21 21:42:27 -07:00
|
|
|
result, err := c.Server.Initialize(ctx, params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Validate initialization result.
|
|
|
|
if err := validateCapabilities(result); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
// Complete initialization of server.
|
|
|
|
if err := c.Server.Initialized(ctx, &protocol.InitializedParams{}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the file on the server side.
|
|
|
|
uri := protocol.NewURI(span.FileURI(tmpFile))
|
|
|
|
if err := c.Server.DidOpen(ctx, &protocol.DidOpenTextDocumentParams{
|
|
|
|
TextDocument: protocol.TextDocumentItem{
|
|
|
|
URI: uri,
|
|
|
|
LanguageID: "go",
|
|
|
|
Version: 1,
|
|
|
|
Text: `package main; func main() {};`,
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are sending a full text change, the change.Range must be nil.
|
|
|
|
// It is not enough for the Change to be empty, as that is ambiguous.
|
|
|
|
if err := c.Server.DidChange(ctx, &protocol.DidChangeTextDocumentParams{
|
|
|
|
TextDocument: protocol.VersionedTextDocumentIdentifier{
|
|
|
|
TextDocumentIdentifier: protocol.TextDocumentIdentifier{
|
|
|
|
URI: uri,
|
|
|
|
},
|
|
|
|
Version: 2,
|
|
|
|
},
|
|
|
|
ContentChanges: []protocol.TextDocumentContentChangeEvent{
|
|
|
|
{
|
|
|
|
Range: nil,
|
2019-11-27 12:14:59 -07:00
|
|
|
Text: `package main; func main() { fmt.Println("") }`,
|
2019-11-21 21:42:27 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-27 12:14:59 -07:00
|
|
|
|
|
|
|
// Send a code action request to validate expected types.
|
|
|
|
actions, err := c.Server.CodeAction(ctx, &protocol.CodeActionParams{
|
|
|
|
TextDocument: protocol.TextDocumentIdentifier{
|
|
|
|
URI: uri,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, action := range actions {
|
|
|
|
// Validate that an empty command is sent along with import organization responses.
|
|
|
|
if action.Kind == protocol.SourceOrganizeImports && action.Command != nil {
|
|
|
|
t.Errorf("unexpected command for import organization")
|
|
|
|
}
|
|
|
|
}
|
2019-12-10 10:45:31 -07:00
|
|
|
|
|
|
|
if err := c.Server.DidSave(ctx, &protocol.DidSaveTextDocumentParams{
|
|
|
|
TextDocument: protocol.VersionedTextDocumentIdentifier{
|
|
|
|
Version: 2,
|
|
|
|
TextDocumentIdentifier: protocol.TextDocumentIdentifier{
|
|
|
|
URI: uri,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// LSP specifies that a file can be saved with optional text, so this field must be nil.
|
|
|
|
Text: nil,
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-12-04 11:36:18 -07:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 21:42:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func validateCapabilities(result *protocol.InitializeResult) error {
|
|
|
|
// If the client sends "false" for RenameProvider.PrepareSupport,
|
|
|
|
// the server must respond with a boolean.
|
|
|
|
if v, ok := result.Capabilities.RenameProvider.(bool); !ok {
|
|
|
|
return errors.Errorf("RenameProvider must be a boolean if PrepareSupport is false (got %T)", v)
|
|
|
|
}
|
|
|
|
// The same goes for CodeActionKind.ValueSet.
|
|
|
|
if v, ok := result.Capabilities.CodeActionProvider.(bool); !ok {
|
|
|
|
return errors.Errorf("CodeActionSupport must be a boolean if CodeActionKind.ValueSet has length 0 (got %T)", v)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|