1
0
mirror of https://github.com/golang/go synced 2024-11-05 22:56:11 -07:00
go/internal/lsp/cmd/test/suggested_fix.go
Rebecca Stambler 37a045f3b9 internal/lsp: move undeclaredname suggested fix out of analysis
This CL is a follow-up from CL 241983. I didn't realize that the
undeclaredname analysis was also using the go/printer.Fprint trick,
which we decided was both incorrect and inefficient. This CL does
approximately the same things as CL 241983, with a few changes to make
the approach more general.

source.Analyzer now has a field to indicate if its suggested fix needs
to be computed separately, and that is used to determine which
code actions get commands. We also make helper functions to map
analyses to their commands.

I figured out a neater way to test suggested fixes in this CL, so I
reversed the move to source_test back to lsp_test (which was the right
place all along).

Change-Id: I505bf4790481d887edda8b82897e541ec73fb427
Reviewed-on: https://go-review.googlesource.com/c/tools/+/242366
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-07-23 23:54:27 +00:00

36 lines
1.0 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 cmdtest
import (
"fmt"
"testing"
"golang.org/x/tools/internal/lsp/tests"
"golang.org/x/tools/internal/span"
)
func (r *runner) SuggestedFix(t *testing.T, spn span.Span, actionKinds []string) {
uri := spn.URI()
filename := uri.Filename()
args := []string{"fix", "-a", fmt.Sprintf("%s", spn)}
for _, kind := range actionKinds {
if kind == "refactor.rewrite" {
t.Skip("refactor.rewrite is not yet supported on the command line")
}
}
args = append(args, actionKinds...)
got, stderr := r.NormalizeGoplsCmd(t, args...)
if stderr == "ExecuteCommand is not yet supported on the command line" {
t.Skipf(stderr)
}
want := string(r.data.Golden("suggestedfix_"+tests.SpanName(spn), filename, func() ([]byte, error) {
return []byte(got), nil
}))
if want != got {
t.Errorf("suggested fixes failed for %s:\n%s", filename, tests.Diff(want, got))
}
}