2020-03-10 22:49:10 -06:00
|
|
|
// 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.
|
|
|
|
|
2019-09-18 23:21:54 -06:00
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-10 18:17:16 -06:00
|
|
|
"fmt"
|
2020-05-05 12:43:51 -06:00
|
|
|
"io"
|
2019-09-18 23:21:54 -06:00
|
|
|
|
2020-07-15 21:33:55 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
|
|
|
"golang.org/x/tools/internal/lsp/debug/tag"
|
2019-09-18 23:21:54 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2020-06-11 16:25:17 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2020-03-10 22:49:10 -06:00
|
|
|
"golang.org/x/tools/internal/xcontext"
|
2019-09-18 23:21:54 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) executeCommand(ctx context.Context, params *protocol.ExecuteCommandParams) (interface{}, error) {
|
2020-07-10 18:17:16 -06:00
|
|
|
var found bool
|
|
|
|
for _, command := range s.session.Options().SupportedCommands {
|
|
|
|
if command == params.Command {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return nil, fmt.Errorf("unsupported command detected: %s", params.Command)
|
|
|
|
}
|
2020-07-14 18:40:38 -06:00
|
|
|
// Some commands require that all files are saved to disk. If we detect
|
|
|
|
// unsaved files, warn the user instead of running the commands.
|
|
|
|
unsaved := false
|
|
|
|
for _, overlay := range s.session.Overlays() {
|
|
|
|
if !overlay.Saved() {
|
|
|
|
unsaved = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if unsaved {
|
|
|
|
switch params.Command {
|
|
|
|
case source.CommandTest, source.CommandGenerate:
|
2020-05-05 12:43:51 -06:00
|
|
|
return nil, s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
|
|
|
|
Type: protocol.Error,
|
2020-07-14 18:40:38 -06:00
|
|
|
Message: fmt.Sprintf("cannot run command %s: unsaved files in the view", params.Command),
|
2020-05-05 12:43:51 -06:00
|
|
|
})
|
|
|
|
}
|
2020-07-14 18:40:38 -06:00
|
|
|
}
|
|
|
|
switch params.Command {
|
|
|
|
case source.CommandTest:
|
|
|
|
var uri protocol.DocumentURI
|
|
|
|
var funcName string
|
|
|
|
if err := source.DecodeArgs(params.Arguments, &uri, &funcName); err != nil {
|
2020-05-05 12:43:51 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-14 18:40:38 -06:00
|
|
|
snapshot, _, ok, err := s.beginFileRequest(ctx, uri, source.UnknownKind)
|
|
|
|
if !ok {
|
2020-05-05 12:43:51 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-14 18:40:38 -06:00
|
|
|
go s.runGoTest(ctx, snapshot, funcName)
|
2020-05-06 20:54:50 -06:00
|
|
|
case source.CommandGenerate:
|
2020-07-14 18:40:38 -06:00
|
|
|
var uri protocol.DocumentURI
|
|
|
|
var recursive bool
|
|
|
|
if err := source.DecodeArgs(params.Arguments, &uri, &recursive); err != nil {
|
2020-03-10 22:49:10 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-14 18:40:38 -06:00
|
|
|
go s.runGoGenerate(xcontext.Detach(ctx), uri.SpanURI(), recursive)
|
2020-05-14 12:02:48 -06:00
|
|
|
case source.CommandRegenerateCgo:
|
2020-07-14 18:40:38 -06:00
|
|
|
var uri protocol.DocumentURI
|
|
|
|
if err := source.DecodeArgs(params.Arguments, &uri); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 12:02:48 -06:00
|
|
|
mod := source.FileModification{
|
2020-07-14 18:40:38 -06:00
|
|
|
URI: uri.SpanURI(),
|
2020-05-14 12:02:48 -06:00
|
|
|
Action: source.InvalidateMetadata,
|
|
|
|
}
|
|
|
|
_, err := s.didModifyFiles(ctx, []source.FileModification{mod}, FromRegenerateCgo)
|
|
|
|
return nil, err
|
2020-05-28 19:21:29 -06:00
|
|
|
case source.CommandTidy, source.CommandVendor:
|
2020-07-14 18:40:38 -06:00
|
|
|
var uri protocol.DocumentURI
|
|
|
|
if err := source.DecodeArgs(params.Arguments, &uri); err != nil {
|
|
|
|
return nil, err
|
2019-09-18 23:21:54 -06:00
|
|
|
}
|
2020-05-28 19:21:29 -06:00
|
|
|
// The flow for `go mod tidy` and `go mod vendor` is almost identical,
|
|
|
|
// so we combine them into one case for convenience.
|
2020-07-14 18:40:38 -06:00
|
|
|
a := "tidy"
|
2020-05-28 19:21:29 -06:00
|
|
|
if params.Command == source.CommandVendor {
|
2020-07-14 18:40:38 -06:00
|
|
|
a = "vendor"
|
2019-09-18 23:21:54 -06:00
|
|
|
}
|
2020-07-14 18:40:38 -06:00
|
|
|
err := s.directGoModCommand(ctx, uri, "mod", []string{a}...)
|
2020-05-28 19:21:29 -06:00
|
|
|
return nil, err
|
2020-05-06 20:54:50 -06:00
|
|
|
case source.CommandUpgradeDependency:
|
2020-07-14 18:40:38 -06:00
|
|
|
var uri protocol.DocumentURI
|
|
|
|
var deps []string
|
|
|
|
if err := source.DecodeArgs(params.Arguments, &uri, &deps); err != nil {
|
|
|
|
return nil, err
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
2020-07-14 18:40:38 -06:00
|
|
|
err := s.directGoModCommand(ctx, uri, "get", deps...)
|
2020-05-28 19:21:29 -06:00
|
|
|
return nil, err
|
2020-07-10 18:02:44 -06:00
|
|
|
case source.CommandFillStruct:
|
2020-07-14 18:40:38 -06:00
|
|
|
var uri protocol.DocumentURI
|
|
|
|
var rng protocol.Range
|
|
|
|
if err := source.DecodeArgs(params.Arguments, &uri, &rng); err != nil {
|
2020-07-10 18:02:44 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-14 18:40:38 -06:00
|
|
|
snapshot, fh, ok, err := s.beginFileRequest(ctx, uri, source.Go)
|
2020-07-10 18:02:44 -06:00
|
|
|
if !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-14 18:40:38 -06:00
|
|
|
edits, err := source.FillStruct(ctx, snapshot, fh, rng)
|
2020-07-10 18:02:44 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r, err := s.client.ApplyEdit(ctx, &protocol.ApplyWorkspaceEditParams{
|
|
|
|
Edit: protocol.WorkspaceEdit{
|
|
|
|
DocumentChanges: edits,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !r.Applied {
|
|
|
|
return nil, s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
|
|
|
|
Type: protocol.Error,
|
|
|
|
Message: fmt.Sprintf("fillstruct failed: %v", r.FailureReason),
|
|
|
|
})
|
|
|
|
}
|
2020-07-14 18:40:38 -06:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown command: %s", params.Command)
|
2019-09-18 23:21:54 -06:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-03-10 22:49:10 -06:00
|
|
|
|
2020-06-21 21:21:15 -06:00
|
|
|
func (s *Server) directGoModCommand(ctx context.Context, uri protocol.DocumentURI, verb string, args ...string) error {
|
2020-05-28 19:21:29 -06:00
|
|
|
view, err := s.session.ViewOf(uri.SpanURI())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-21 21:21:15 -06:00
|
|
|
return view.Snapshot().RunGoCommandDirect(ctx, verb, args)
|
2020-05-28 19:21:29 -06:00
|
|
|
}
|
|
|
|
|
2020-07-14 18:40:38 -06:00
|
|
|
func (s *Server) runGoTest(ctx context.Context, snapshot source.Snapshot, funcName string) error {
|
2020-05-05 12:43:51 -06:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-06-11 16:25:17 -06:00
|
|
|
ew := &eventWriter{ctx: ctx, operation: "test"}
|
2020-07-15 21:33:55 -06:00
|
|
|
msg := fmt.Sprintf("testing %s", funcName)
|
|
|
|
wc := s.newProgressWriter(ctx, "test", msg, msg, cancel)
|
2020-05-05 12:43:51 -06:00
|
|
|
defer wc.Close()
|
|
|
|
|
|
|
|
messageType := protocol.Info
|
|
|
|
message := "test passed"
|
2020-06-11 16:25:17 -06:00
|
|
|
stderr := io.MultiWriter(ew, wc)
|
2020-06-12 12:57:48 -06:00
|
|
|
|
2020-06-11 16:25:17 -06:00
|
|
|
if err := snapshot.RunGoCommandPiped(ctx, "test", []string{"-run", funcName}, ew, stderr); err != nil {
|
2020-06-12 12:57:48 -06:00
|
|
|
if errors.Is(err, context.Canceled) {
|
2020-06-11 16:25:17 -06:00
|
|
|
return err
|
2020-05-05 12:43:51 -06:00
|
|
|
}
|
2020-06-12 12:57:48 -06:00
|
|
|
messageType = protocol.Error
|
|
|
|
message = "test failed"
|
2020-05-05 12:43:51 -06:00
|
|
|
}
|
2020-06-12 12:57:48 -06:00
|
|
|
return s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
|
2020-05-05 12:43:51 -06:00
|
|
|
Type: messageType,
|
|
|
|
Message: message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-15 21:33:55 -06:00
|
|
|
// GenerateWorkDoneTitle is the title used in progress reporting for go
|
|
|
|
// generate commands. It is exported for testing purposes.
|
|
|
|
const GenerateWorkDoneTitle = "generate"
|
|
|
|
|
2020-07-14 18:40:38 -06:00
|
|
|
func (s *Server) runGoGenerate(ctx context.Context, uri span.URI, recursive bool) error {
|
2020-07-15 21:33:55 -06:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
er := &eventWriter{ctx: ctx, operation: "generate"}
|
|
|
|
wc := s.newProgressWriter(ctx, GenerateWorkDoneTitle, "running go generate", "started go generate, check logs for progress", cancel)
|
|
|
|
defer wc.Close()
|
|
|
|
args := []string{"-x"}
|
|
|
|
if recursive {
|
|
|
|
args = append(args, "./...")
|
|
|
|
}
|
|
|
|
|
|
|
|
stderr := io.MultiWriter(er, wc)
|
|
|
|
view, err := s.session.ViewOf(uri)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
snapshot := view.Snapshot()
|
|
|
|
if err := snapshot.RunGoCommandPiped(ctx, "generate", args, er, stderr); err != nil {
|
|
|
|
if errors.Is(err, context.Canceled) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
event.Error(ctx, "generate: command error", err, tag.Directory.Of(uri.Filename()))
|
|
|
|
return s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
|
|
|
|
Type: protocol.Error,
|
|
|
|
Message: "go generate exited with an error, check gopls logs",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|