mirror of
https://github.com/golang/go
synced 2024-11-18 15:04:44 -07:00
cb8d9cd245
Some code lenses may be undesirable for certain users or editors -- for example a code lens that runs tests, when VSCode already supports this functionality outside of the LSP. To handle such situations, support configuring code lenses via a new 'codelens' gopls option. Add support for code lens in regtests, and use this to test the new configuration. To achieve this, thread through a new 'EditorConfig' type that configures the fake editor's LSP session. It made sense to move the test Env overlay onto this config object as well. While looking at them, document some types in source.Options. Change-Id: I961077422a273829c5cbd83c3b87fae29f77eeda Reviewed-on: https://go-review.googlesource.com/c/tools/+/232680 Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
88 lines
2.7 KiB
Go
88 lines
2.7 KiB
Go
// Copyright 2020 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package lsp
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"golang.org/x/tools/internal/gocommand"
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
"golang.org/x/tools/internal/packagesinternal"
|
|
"golang.org/x/tools/internal/xcontext"
|
|
errors "golang.org/x/xerrors"
|
|
)
|
|
|
|
func (s *Server) executeCommand(ctx context.Context, params *protocol.ExecuteCommandParams) (interface{}, error) {
|
|
switch params.Command {
|
|
case source.CommandGenerate:
|
|
dir, recursive, err := getGenerateRequest(params.Arguments)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
go s.runGenerate(xcontext.Detach(ctx), dir, recursive)
|
|
case source.CommandTidy:
|
|
if len(params.Arguments) == 0 || len(params.Arguments) > 1 {
|
|
return nil, errors.Errorf("expected one file URI for call to `go mod tidy`, got %v", params.Arguments)
|
|
}
|
|
uri := protocol.DocumentURI(params.Arguments[0].(string))
|
|
snapshot, _, ok, err := s.beginFileRequest(uri, source.Mod)
|
|
if !ok {
|
|
return nil, err
|
|
}
|
|
cfg := snapshot.Config(ctx)
|
|
// Run go.mod tidy on the view.
|
|
inv := gocommand.Invocation{
|
|
Verb: "mod",
|
|
Args: []string{"tidy"},
|
|
Env: cfg.Env,
|
|
WorkingDir: snapshot.View().Folder().Filename(),
|
|
}
|
|
gocmdRunner := packagesinternal.GetGoCmdRunner(cfg)
|
|
if _, err := gocmdRunner.Run(ctx, inv); err != nil {
|
|
return nil, err
|
|
}
|
|
case source.CommandUpgradeDependency:
|
|
if len(params.Arguments) < 2 {
|
|
return nil, errors.Errorf("expected one file URI and one dependency for call to `go get`, got %v", params.Arguments)
|
|
}
|
|
uri := protocol.DocumentURI(params.Arguments[0].(string))
|
|
deps := params.Arguments[1].(string)
|
|
snapshot, _, ok, err := s.beginFileRequest(uri, source.UnknownKind)
|
|
if !ok {
|
|
return nil, err
|
|
}
|
|
cfg := snapshot.Config(ctx)
|
|
// Run "go get" on the dependency to upgrade it to the latest version.
|
|
inv := gocommand.Invocation{
|
|
Verb: "get",
|
|
Args: strings.Split(deps, " "),
|
|
Env: cfg.Env,
|
|
WorkingDir: snapshot.View().Folder().Filename(),
|
|
}
|
|
gocmdRunner := packagesinternal.GetGoCmdRunner(cfg)
|
|
if _, err := gocmdRunner.Run(ctx, inv); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func getGenerateRequest(args []interface{}) (string, bool, error) {
|
|
if len(args) != 2 {
|
|
return "", false, errors.Errorf("expected exactly 2 arguments but got %d", len(args))
|
|
}
|
|
dir, ok := args[0].(string)
|
|
if !ok {
|
|
return "", false, errors.Errorf("expected dir to be a string value but got %T", args[0])
|
|
}
|
|
recursive, ok := args[1].(bool)
|
|
if !ok {
|
|
return "", false, errors.Errorf("expected recursive to be a boolean but got %T", args[1])
|
|
}
|
|
return dir, recursive, nil
|
|
}
|