2019-10-29 02:38:47 -06:00
|
|
|
|
// Copyright 2019 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 cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/diff"
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
|
"golang.org/x/tools/internal/tool"
|
|
|
|
|
errors "golang.org/x/xerrors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// suggestedfix implements the fix verb for gopls.
|
|
|
|
|
type suggestedfix struct {
|
|
|
|
|
Diff bool `flag:"d" help:"display diffs instead of rewriting files"`
|
|
|
|
|
Write bool `flag:"w" help:"write result to (source) file instead of stdout"`
|
|
|
|
|
All bool `flag:"a" help:"apply all fixes, not just preferred fixes"`
|
|
|
|
|
|
|
|
|
|
app *Application
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *suggestedfix) Name() string { return "fix" }
|
|
|
|
|
func (s *suggestedfix) Usage() string { return "<filename>" }
|
|
|
|
|
func (s *suggestedfix) ShortHelp() string { return "apply suggested fixes" }
|
|
|
|
|
func (s *suggestedfix) DetailedHelp(f *flag.FlagSet) {
|
|
|
|
|
fmt.Fprintf(f.Output(), `
|
|
|
|
|
Example: apply suggested fixes for this file:
|
|
|
|
|
|
|
|
|
|
$ gopls fix -w internal/lsp/cmd/check.go
|
|
|
|
|
|
|
|
|
|
gopls fix flags are:
|
|
|
|
|
`)
|
|
|
|
|
f.PrintDefaults()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run performs diagnostic checks on the file specified and either;
|
|
|
|
|
// - if -w is specified, updates the file in place;
|
|
|
|
|
// - if -d is specified, prints out unified diffs of the changes; or
|
|
|
|
|
// - otherwise, prints the new versions to stdout.
|
|
|
|
|
func (s *suggestedfix) Run(ctx context.Context, args ...string) error {
|
2020-03-05 07:42:47 -07:00
|
|
|
|
if len(args) < 1 {
|
|
|
|
|
return tool.CommandLineErrorf("fix expects at least 1 argument")
|
2019-10-29 02:38:47 -06:00
|
|
|
|
}
|
|
|
|
|
conn, err := s.app.connect(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer conn.terminate(ctx)
|
|
|
|
|
|
|
|
|
|
from := span.Parse(args[0])
|
|
|
|
|
uri := from.URI()
|
|
|
|
|
file := conn.AddFile(ctx, uri)
|
|
|
|
|
if file.err != nil {
|
|
|
|
|
return file.err
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-10 15:54:47 -07:00
|
|
|
|
if err := conn.diagnoseFiles(ctx, []span.URI{uri}); err != nil {
|
|
|
|
|
return err
|
2019-10-29 02:38:47 -06:00
|
|
|
|
}
|
2020-01-11 21:59:57 -07:00
|
|
|
|
conn.Client.filesMu.Lock()
|
|
|
|
|
defer conn.Client.filesMu.Unlock()
|
2019-10-29 02:38:47 -06:00
|
|
|
|
|
2020-03-05 07:42:47 -07:00
|
|
|
|
codeActionKinds := []protocol.CodeActionKind{protocol.QuickFix}
|
|
|
|
|
if len(args) > 1 {
|
|
|
|
|
codeActionKinds = []protocol.CodeActionKind{}
|
|
|
|
|
for _, k := range args[1:] {
|
|
|
|
|
codeActionKinds = append(codeActionKinds, protocol.CodeActionKind(k))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-29 02:38:47 -06:00
|
|
|
|
p := protocol.CodeActionParams{
|
|
|
|
|
TextDocument: protocol.TextDocumentIdentifier{
|
2020-02-12 14:36:46 -07:00
|
|
|
|
URI: protocol.URIFromSpanURI(uri),
|
2019-10-29 02:38:47 -06:00
|
|
|
|
},
|
|
|
|
|
Context: protocol.CodeActionContext{
|
2020-03-05 07:42:47 -07:00
|
|
|
|
Only: codeActionKinds,
|
2019-10-29 02:38:47 -06:00
|
|
|
|
Diagnostics: file.diagnostics,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
actions, err := conn.CodeAction(ctx, &p)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Errorf("%v: %v", from, err)
|
|
|
|
|
}
|
|
|
|
|
var edits []protocol.TextEdit
|
2019-11-19 06:42:45 -07:00
|
|
|
|
for _, a := range actions {
|
2019-11-12 15:58:37 -07:00
|
|
|
|
if !a.IsPreferred && !s.All {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2020-03-05 07:42:47 -07:00
|
|
|
|
if !from.HasPosition() {
|
|
|
|
|
for _, c := range a.Edit.DocumentChanges {
|
|
|
|
|
if fileURI(c.TextDocument.URI) == uri {
|
|
|
|
|
edits = append(edits, c.Edits...)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// If the span passed in has a position, then we need to find
|
|
|
|
|
// the codeaction that has the same range as the passed in span.
|
|
|
|
|
for _, diag := range a.Diagnostics {
|
|
|
|
|
spn, err := file.mapper.RangeSpan(diag.Range)
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if span.ComparePoint(from.Start(), spn.Start()) == 0 {
|
|
|
|
|
for _, c := range a.Edit.DocumentChanges {
|
|
|
|
|
if fileURI(c.TextDocument.URI) == uri {
|
|
|
|
|
edits = append(edits, c.Edits...)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break
|
2019-11-12 15:58:37 -07:00
|
|
|
|
}
|
2019-10-29 02:38:47 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sedits, err := source.FromProtocolEdits(file.mapper, edits)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Errorf("%v: %v", edits, err)
|
|
|
|
|
}
|
|
|
|
|
newContent := diff.ApplyEdits(string(file.mapper.Content), sedits)
|
|
|
|
|
|
|
|
|
|
filename := file.uri.Filename()
|
|
|
|
|
switch {
|
|
|
|
|
case s.Write:
|
|
|
|
|
if len(edits) > 0 {
|
|
|
|
|
ioutil.WriteFile(filename, []byte(newContent), 0644)
|
|
|
|
|
}
|
|
|
|
|
case s.Diff:
|
|
|
|
|
diffs := diff.ToUnified(filename+".orig", filename, string(file.mapper.Content), sedits)
|
|
|
|
|
fmt.Print(diffs)
|
|
|
|
|
default:
|
|
|
|
|
fmt.Print(string(newContent))
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|