1
0
mirror of https://github.com/golang/go synced 2024-09-30 16:08:36 -06:00

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 <rfindley@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Rob Findley 2020-04-14 15:35:57 -04:00 committed by Robert Findley
parent 912958979a
commit 3bb7580c69
3 changed files with 31 additions and 8 deletions

View File

@ -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 {

View File

@ -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
}

View File

@ -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)