From 3bb7580c690e3532262f690b701672108a60ad01 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Tue, 14 Apr 2020 15:35:57 -0400 Subject: [PATCH] internal/lsp/fake: correctly configure the fake editor Configuration was not being set correctly, because the configuration response was not honoring the configuration section order. Change-Id: I8418535b45e6a24fd8f0605d58cb370e22664f17 Reviewed-on: https://go-review.googlesource.com/c/tools/+/228257 Run-TryBot: Robert Findley Reviewed-by: Rebecca Stambler TryBot-Result: Gobot Gobot --- internal/lsp/fake/client.go | 11 +++++++++-- internal/lsp/fake/editor.go | 14 +++++++++----- internal/lsp/fake/workspace.go | 14 +++++++++++++- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/internal/lsp/fake/client.go b/internal/lsp/fake/client.go index 394718b98c..74aeff8816 100644 --- a/internal/lsp/fake/client.go +++ b/internal/lsp/fake/client.go @@ -78,8 +78,15 @@ func (c *Client) WorkspaceFolders(context.Context) ([]protocol.WorkspaceFolder, return []protocol.WorkspaceFolder{}, nil } -func (c *Client) Configuration(context.Context, *protocol.ParamConfiguration) ([]interface{}, error) { - return []interface{}{c.configuration()}, nil +func (c *Client) Configuration(_ context.Context, p *protocol.ParamConfiguration) ([]interface{}, error) { + results := make([]interface{}, len(p.Items)) + for i, item := range p.Items { + if item.Section != "gopls" { + continue + } + results[i] = c.configuration() + } + return results, nil } func (c *Client) RegisterCapability(context.Context, *protocol.RegistrationParams) error { diff --git a/internal/lsp/fake/editor.go b/internal/lsp/fake/editor.go index 1cd213ae1b..e440ddf642 100644 --- a/internal/lsp/fake/editor.go +++ b/internal/lsp/fake/editor.go @@ -104,11 +104,13 @@ func (e *Editor) Client() *Client { } func (e *Editor) configuration() map[string]interface{} { + env := map[string]interface{}{} + for _, value := range e.ws.GoEnv() { + kv := strings.SplitN(value, "=", 2) + env[kv[0]] = kv[1] + } return map[string]interface{}{ - "env": map[string]interface{}{ - "GOPATH": e.ws.GOPATH(), - "GO111MODULE": "on", - }, + "env": env, } } @@ -117,8 +119,9 @@ func (e *Editor) initialize(ctx context.Context) error { params.ClientInfo.Name = "fakeclient" params.ClientInfo.Version = "v1.0.0" params.RootURI = e.ws.RootURI() + params.Capabilities.Workspace.Configuration = true + // TODO: set client capabilities - // TODO: set client capabilities. params.Trace = "messages" // TODO: support workspace folders. if e.server != nil { @@ -134,6 +137,7 @@ func (e *Editor) initialize(ctx context.Context) error { return fmt.Errorf("initialized: %v", err) } } + // TODO: await initial configuration here, or expect gopls to manage that? return nil } diff --git a/internal/lsp/fake/workspace.go b/internal/lsp/fake/workspace.go index 1388e1a49f..e42f91c763 100644 --- a/internal/lsp/fake/workspace.go +++ b/internal/lsp/fake/workspace.go @@ -123,7 +123,8 @@ func (w *Workspace) ReadFile(path string) (string, error) { return string(b), nil } -// RegexpSearch searches the file +// RegexpSearch searches the file corresponding to path for the first position +// matching re. func (w *Workspace) RegexpSearch(path string, re string) (Pos, error) { content, err := w.ReadFile(path) if err != nil { @@ -150,12 +151,23 @@ func (w *Workspace) RemoveFile(ctx context.Context, path string) error { return nil } +// GoEnv returns the environment variables that should be used for invoking Go +// commands in the workspace. +func (w *Workspace) GoEnv() []string { + return []string{ + "GOPATH=" + w.GOPATH(), + "GO111MODULE=", + "GOSUMDB=off", + } +} + // RunGoCommand executes a go command in the workspace. func (w *Workspace) RunGoCommand(ctx context.Context, verb string, args ...string) error { inv := gocommand.Invocation{ Verb: verb, Args: args, WorkingDir: w.workdir, + Env: w.GoEnv(), } gocmdRunner := &gocommand.Runner{} _, stderr, _, err := gocmdRunner.RunRaw(ctx, inv)