mirror of
https://github.com/golang/go
synced 2024-11-18 13:14:47 -07:00
4025ed8474
This change moves the suggested fixes logic for fillstruct out of the analysis and into internal/lsp/source. This logic is then used as part of a new fillstruct command. This command is returned along with the code action results, to be executed only when the user accepts the code action. This led to a number of changes to testing. The suggested fix tests in internal/lsp doesn't support executing commands, so we skip them. The suggested fix tests in internal/lsp/source are changed to call fillstruct directly. A new regtest is added to check the command execution, which led to a few regtest changes. Also, remove the `go mod tidy` code action, as it's made redundant by the existence of the suggested fixes coming from internal/lsp/mod. Change-Id: I35ca0aff1ace8f0097fe7cb57232997facb516a4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/241983 Reviewed-by: Heschi Kreinick <heschi@google.com>
69 lines
1.5 KiB
Go
69 lines
1.5 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 regtest
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"golang.org/x/tools/internal/lsp"
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
"golang.org/x/tools/internal/lsp/tests"
|
|
)
|
|
|
|
// A basic test for fillstruct, now that it uses a command.
|
|
func TestFillStruct(t *testing.T) {
|
|
const basic = `
|
|
-- go.mod --
|
|
module mod.com
|
|
|
|
go 1.14
|
|
-- main.go --
|
|
package main
|
|
|
|
import "go/types"
|
|
|
|
func Foo() {
|
|
_ = types.Info{}
|
|
}
|
|
`
|
|
runner.Run(t, basic, func(t *testing.T, env *Env) {
|
|
env.Await(
|
|
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
|
|
)
|
|
env.OpenFile("main.go")
|
|
if err := env.Editor.RefactorRewrite(env.Ctx, "main.go", &protocol.Range{
|
|
Start: protocol.Position{
|
|
Line: 5,
|
|
Character: 16,
|
|
},
|
|
End: protocol.Position{
|
|
Line: 5,
|
|
Character: 16,
|
|
},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := `package main
|
|
|
|
import "go/types"
|
|
|
|
func Foo() {
|
|
_ = types.Info{
|
|
Types: map[ast.Expr]types.TypeAndValue{},
|
|
Defs: map[*ast.Ident]types.Object{},
|
|
Uses: map[*ast.Ident]types.Object{},
|
|
Implicits: map[ast.Node]types.Object{},
|
|
Selections: map[*ast.SelectorExpr]*types.Selection{},
|
|
Scopes: map[ast.Node]*types.Scope{},
|
|
InitOrder: []*types.Initializer{},
|
|
}
|
|
}
|
|
`
|
|
if got := env.Editor.BufferText("main.go"); got != want {
|
|
t.Fatalf("TestFillStruct failed:\n%s", tests.Diff(want, got))
|
|
}
|
|
})
|
|
}
|