mirror of
https://github.com/golang/go
synced 2024-10-01 04:18:33 -06:00
5091d647ee
Code generation has been unified, so that tsprotocol.go and tsserver.go are produced by the same program. tsprotocol.go is about 900 lines shorter, partly from removing boilerplate comments that golint no longer requires. (And partly by generating fewer unneeded types.) The choice made for a union type is commented with the set of types. There is no Go equivalent for union types, but making themn all interface{} would replace type checking at unmarshalling with checking runtime conversions. Intersection types (A&B) are sometimes embedded (struct{A;B;}, and sometimes expanded, as they have to be if A and B have fields with the same names. There are fewer embedded structs, which had been verbose and confusing to initialize. They have been replaced by types whose names end in Gn. Essentially all the generated *structs have been removed. This makes no difference in what the client sends, and the server may send a {} where it previously might have sent nothing. The benefit is that some nil tests can be removed. Thus 'omitempty' in json tags is just documentation that the element is optional in the protocol. The files that generate this code will be submitted later, but soon. Change-Id: I52b997d9c58de3d733fc8c6ce061e47ce2bdb100 Reviewed-on: https://go-review.googlesource.com/c/tools/+/207598 Run-TryBot: Peter Weinberger <pjw@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
125 lines
3.2 KiB
Go
125 lines
3.2 KiB
Go
// 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"
|
||
"time"
|
||
|
||
"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 {
|
||
if len(args) != 1 {
|
||
return tool.CommandLineErrorf("fix expects 1 argument")
|
||
}
|
||
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
|
||
}
|
||
|
||
// Wait for diagnostics results
|
||
select {
|
||
case <-file.hasDiagnostics:
|
||
case <-time.After(30 * time.Second):
|
||
return errors.Errorf("timed out waiting for results from %v", file.uri)
|
||
}
|
||
|
||
file.diagnosticsMu.Lock()
|
||
defer file.diagnosticsMu.Unlock()
|
||
|
||
p := protocol.CodeActionParams{
|
||
TextDocument: protocol.TextDocumentIdentifier{
|
||
URI: protocol.NewURI(uri),
|
||
},
|
||
Context: protocol.CodeActionContext{
|
||
Only: []protocol.CodeActionKind{protocol.QuickFix},
|
||
Diagnostics: file.diagnostics,
|
||
},
|
||
}
|
||
actions, err := conn.CodeAction(ctx, &p)
|
||
if err != nil {
|
||
return errors.Errorf("%v: %v", from, err)
|
||
}
|
||
var edits []protocol.TextEdit
|
||
v, ok := actions.([]protocol.CodeAction)
|
||
if !ok {
|
||
return errors.Errorf("expected CodeAction, got %T", actions)
|
||
}
|
||
for _, a := range v {
|
||
if !a.IsPreferred && !s.All {
|
||
continue
|
||
}
|
||
for _, c := range a.Edit.DocumentChanges {
|
||
if c.TextDocument.URI == string(uri) {
|
||
edits = append(edits, c.Edits...)
|
||
}
|
||
}
|
||
}
|
||
|
||
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
|
||
}
|