diff --git a/go/analysis/internal/checker/checker.go b/go/analysis/internal/checker/checker.go index 33ca77a06c9..8efb89f6bd5 100644 --- a/go/analysis/internal/checker/checker.go +++ b/go/analysis/internal/checker/checker.go @@ -485,11 +485,11 @@ func diff3Conflict(path string, xlabel, ylabel string, xedits, yedits []diff.Edi } oldlabel, old := "base", string(contents) - xdiff, err := diff.ToUnified(oldlabel, xlabel, old, xedits) + xdiff, err := diff.ToUnified(oldlabel, xlabel, old, xedits, diff.DefaultContextLines) if err != nil { return err } - ydiff, err := diff.ToUnified(oldlabel, ylabel, old, yedits) + ydiff, err := diff.ToUnified(oldlabel, ylabel, old, yedits, diff.DefaultContextLines) if err != nil { return err } diff --git a/gopls/internal/lsp/analysis/fillstruct/fillstruct.go b/gopls/internal/lsp/analysis/fillstruct/fillstruct.go index 6d145cf3304..3b87ce5b0f9 100644 --- a/gopls/internal/lsp/analysis/fillstruct/fillstruct.go +++ b/gopls/internal/lsp/analysis/fillstruct/fillstruct.go @@ -357,7 +357,7 @@ func populateValue(f *ast.File, pkg *types.Package, typ types.Type) ast.Expr { case u.Kind() == types.UnsafePointer: return ast.NewIdent("nil") default: - panic("unknown basic type") + panic(fmt.Sprintf("unknown basic type %v", u)) } case *types.Map: diff --git a/gopls/internal/lsp/cmd/cmd.go b/gopls/internal/lsp/cmd/cmd.go index d3d4069a412..714a5bf4baa 100644 --- a/gopls/internal/lsp/cmd/cmd.go +++ b/gopls/internal/lsp/cmd/cmd.go @@ -610,7 +610,7 @@ func applyTextEdits(mapper *protocol.Mapper, edits []protocol.TextEdit, flags *E } if flags.Diff { - unified, err := diff.ToUnified(filename+".orig", filename, string(mapper.Content), renameEdits) + unified, err := diff.ToUnified(filename+".orig", filename, string(mapper.Content), renameEdits, diff.DefaultContextLines) if err != nil { return err } diff --git a/gopls/internal/lsp/regtest/marker.go b/gopls/internal/lsp/regtest/marker.go index a7dee5e15f1..d07c6d070e3 100644 --- a/gopls/internal/lsp/regtest/marker.go +++ b/gopls/internal/lsp/regtest/marker.go @@ -36,6 +36,7 @@ import ( "golang.org/x/tools/gopls/internal/lsp/source" "golang.org/x/tools/gopls/internal/lsp/tests" "golang.org/x/tools/gopls/internal/lsp/tests/compare" + "golang.org/x/tools/internal/diff" "golang.org/x/tools/internal/jsonrpc2" "golang.org/x/tools/internal/jsonrpc2/servertest" "golang.org/x/tools/internal/testenv" @@ -152,12 +153,17 @@ var update = flag.Bool("update", false, "if set, update test data during marker // completion candidate produced at the given location with provided label // results in the given golden state. // -// - codeaction(kind, start, end, golden): specifies a codeaction to request +// - codeaction(start, end, kind, golden): specifies a code action to request // for the given range. To support multi-line ranges, the range is defined // to be between start.Start and end.End. The golden directory contains // changed file content after the code action is applied. // -// - codeactionerr(kind, start, end, wantError): specifies a codeaction that +// - codeactionedit(range, kind, golden): a shorter form of codeaction. +// Invokes a code action of the given kind for the given in-line range, and +// compares the resulting formatted unified *edits* (notably, not the full +// file content) with the golden directory. +// +// - codeactionerr(start, end, kind, wantError): specifies a codeaction that // fails with an error that matches the expectation. // // - codelens(location, title): specifies that a codelens is expected at the @@ -243,6 +249,9 @@ var update = flag.Bool("update", false, "if set, update test data during marker // to have exactly one associated code action of the specified kind. // This action is executed for its editing effects on the source files. // Like rename, the golden directory contains the expected transformed files. +// TODO(rfindley): we probably only need 'suggestedfix' for quick-fixes. All +// other actions should use codeaction markers. In that case, we can remove +// the 'kind' parameter. // // - rank(location, ...completionItem): executes a textDocument/completion // request at the given location, and verifies that each expected @@ -708,6 +717,7 @@ var valueMarkerFuncs = map[string]func(marker){ var actionMarkerFuncs = map[string]func(marker){ "acceptcompletion": actionMarkerFunc(acceptCompletionMarker), "codeaction": actionMarkerFunc(codeActionMarker), + "codeactionedit": actionMarkerFunc(codeActionEditMarker), "codeactionerr": actionMarkerFunc(codeActionErrMarker), "codelenses": actionMarkerFunc(codeLensesMarker), "complete": actionMarkerFunc(completeMarker), @@ -1420,6 +1430,41 @@ func checkChangedFiles(mark marker, changed map[string][]byte, golden *Golden) { } } +// checkDiffs computes unified diffs for each changed file, and compares with +// the diff content stored in the given golden directory. +func checkDiffs(mark marker, changed map[string][]byte, golden *Golden) { + diffs := make(map[string]string) + for name, after := range changed { + before := mark.run.env.FileContent(name) + edits := diff.Strings(before, string(after)) + d, err := diff.ToUnified("before", "after", before, edits, 0) + if err != nil { + // Can't happen: edits are consistent. + log.Fatalf("internal error in diff.ToUnified: %v", err) + } + diffs[name] = d + } + // Check changed files match expectations. + for filename, got := range diffs { + if want, ok := golden.Get(mark.run.env.T, filename, []byte(got)); !ok { + mark.errorf("%s: unexpected change to file %s; got diff:\n%s", + mark.note.Name, filename, got) + + } else if got != string(want) { + mark.errorf("%s: wrong diff for %s:\n\ngot:\n%s\n\nwant:\n%s\n", + mark.note.Name, filename, got, want) + } + } + // Report unmet expectations. + for filename := range golden.data { + if _, ok := changed[filename]; !ok { + want, _ := golden.Get(mark.run.env.T, filename, nil) + mark.errorf("%s: missing change to file %s; want:\n%s", + mark.note.Name, filename, want) + } + } +} + // ---- marker functions ---- // TODO(rfindley): consolidate documentation of these markers. They are already @@ -1887,7 +1932,7 @@ func applyDocumentChanges(env *Env, changes []protocol.DocumentChanges, fileChan return nil } -func codeActionMarker(mark marker, actionKind string, start, end protocol.Location, golden *Golden) { +func codeActionMarker(mark marker, start, end protocol.Location, actionKind string, g *Golden) { // Request the range from start.Start to end.End. loc := start loc.Range.End = end.Range.End @@ -1900,10 +1945,20 @@ func codeActionMarker(mark marker, actionKind string, start, end protocol.Locati } // Check the file state. - checkChangedFiles(mark, changed, golden) + checkChangedFiles(mark, changed, g) } -func codeActionErrMarker(mark marker, actionKind string, start, end protocol.Location, wantErr wantError) { +func codeActionEditMarker(mark marker, loc protocol.Location, actionKind string, g *Golden) { + changed, err := codeAction(mark.run.env, loc.URI, loc.Range, actionKind, nil) + if err != nil { + mark.errorf("codeAction failed: %v", err) + return + } + + checkDiffs(mark, changed, g) +} + +func codeActionErrMarker(mark marker, start, end protocol.Location, actionKind string, wantErr wantError) { loc := start loc.Range.End = end.Range.End _, err := codeAction(mark.run.env, loc.URI, loc.Range, actionKind, nil) @@ -2008,6 +2063,21 @@ func suggestedfixMarker(mark marker, loc protocol.Location, re *regexp.Regexp, a // applied. Currently, this function does not support code actions that return // edits directly; it only supports code action commands. func codeAction(env *Env, uri protocol.DocumentURI, rng protocol.Range, actionKind string, diag *protocol.Diagnostic) (map[string][]byte, error) { + changes, err := codeActionChanges(env, uri, rng, actionKind, diag) + if err != nil { + return nil, err + } + fileChanges := make(map[string][]byte) + if err := applyDocumentChanges(env, changes, fileChanges); err != nil { + return nil, fmt.Errorf("applying document changes: %v", err) + } + return fileChanges, nil +} + +// codeActionChanges executes a textDocument/codeAction request for the +// specified location and kind, and captures the resulting document changes. +// If diag is non-nil, it is used as the code action context. +func codeActionChanges(env *Env, uri protocol.DocumentURI, rng protocol.Range, actionKind string, diag *protocol.Diagnostic) ([]protocol.DocumentChanges, error) { // Request all code actions that apply to the diagnostic. // (The protocol supports filtering using Context.Only={actionKind} // but we can give a better error if we don't filter.) @@ -2047,20 +2117,19 @@ func codeAction(env *Env, uri protocol.DocumentURI, rng protocol.Range, actionKi // Spec: // "If a code action provides an edit and a command, first the edit is // executed and then the command." - fileChanges := make(map[string][]byte) // An action may specify an edit and/or a command, to be // applied in that order. But since applyDocumentChanges(env, // action.Edit.DocumentChanges) doesn't compose, for now we - // assert that all commands used in the @suggestedfix tests - // return only a command. + // assert that actions return one or the other. if action.Edit != nil { if action.Edit.Changes != nil { env.T.Errorf("internal error: discarding unexpected CodeAction{Kind=%s, Title=%q}.Edit.Changes", action.Kind, action.Title) } if action.Edit.DocumentChanges != nil { - if err := applyDocumentChanges(env, action.Edit.DocumentChanges, fileChanges); err != nil { - return nil, fmt.Errorf("applying document changes: %v", err) + if action.Command != nil { + env.T.Errorf("internal error: discarding unexpected CodeAction{Kind=%s, Title=%q}.Command", action.Kind, action.Title) } + return action.Edit.DocumentChanges, nil } } @@ -2085,13 +2154,10 @@ func codeAction(env *Env, uri protocol.DocumentURI, rng protocol.Range, actionKi }); err != nil { return nil, err } - - if err := applyDocumentChanges(env, env.Awaiter.takeDocumentChanges(), fileChanges); err != nil { - return nil, fmt.Errorf("applying document changes from command: %v", err) - } + return env.Awaiter.takeDocumentChanges(), nil } - return fileChanges, nil + return nil, nil } // TODO(adonovan): suggestedfixerr diff --git a/gopls/internal/lsp/regtest/wrappers.go b/gopls/internal/lsp/regtest/wrappers.go index 0220d30d390..a2c6a1eea7e 100644 --- a/gopls/internal/lsp/regtest/wrappers.go +++ b/gopls/internal/lsp/regtest/wrappers.go @@ -114,6 +114,18 @@ func (e *Env) SetBufferContent(name string, content string) { } } +// ReadFile returns the file content for name that applies to the current +// editing session: if the file is open, it returns its buffer content, +// otherwise it returns on disk content. +func (e *Env) FileContent(name string) string { + e.T.Helper() + text, ok := e.Editor.BufferText(name) + if ok { + return text + } + return e.ReadWorkspaceFile(name) +} + // RegexpSearch returns the starting position of the first match for re in the // buffer specified by name, calling t.Fatal on any error. It first searches // for the position in open buffers, then in workspace files. diff --git a/gopls/internal/lsp/testdata/fillstruct/a.go b/gopls/internal/lsp/testdata/fillstruct/a.go deleted file mode 100644 index e1add2d4713..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/a.go +++ /dev/null @@ -1,27 +0,0 @@ -package fillstruct - -import ( - "golang.org/lsptests/fillstruct/data" -) - -type basicStruct struct { - foo int -} - -var _ = basicStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type twoArgStruct struct { - foo int - bar string -} - -var _ = twoArgStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type nestedStruct struct { - bar string - basic basicStruct -} - -var _ = nestedStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -var _ = data.B{} //@suggestedfix("}", "refactor.rewrite", "Fill") diff --git a/gopls/internal/lsp/testdata/fillstruct/a.go.golden b/gopls/internal/lsp/testdata/fillstruct/a.go.golden deleted file mode 100644 index ca1db04ead8..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/a.go.golden +++ /dev/null @@ -1,126 +0,0 @@ --- suggestedfix_a_11_21 -- -package fillstruct - -import ( - "golang.org/lsptests/fillstruct/data" -) - -type basicStruct struct { - foo int -} - -var _ = basicStruct{ - foo: 0, -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type twoArgStruct struct { - foo int - bar string -} - -var _ = twoArgStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type nestedStruct struct { - bar string - basic basicStruct -} - -var _ = nestedStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -var _ = data.B{} //@suggestedfix("}", "refactor.rewrite", "Fill") - --- suggestedfix_a_18_22 -- -package fillstruct - -import ( - "golang.org/lsptests/fillstruct/data" -) - -type basicStruct struct { - foo int -} - -var _ = basicStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type twoArgStruct struct { - foo int - bar string -} - -var _ = twoArgStruct{ - foo: 0, - bar: "", -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type nestedStruct struct { - bar string - basic basicStruct -} - -var _ = nestedStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -var _ = data.B{} //@suggestedfix("}", "refactor.rewrite", "Fill") - --- suggestedfix_a_25_22 -- -package fillstruct - -import ( - "golang.org/lsptests/fillstruct/data" -) - -type basicStruct struct { - foo int -} - -var _ = basicStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type twoArgStruct struct { - foo int - bar string -} - -var _ = twoArgStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type nestedStruct struct { - bar string - basic basicStruct -} - -var _ = nestedStruct{ - bar: "", - basic: basicStruct{}, -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -var _ = data.B{} //@suggestedfix("}", "refactor.rewrite", "Fill") - --- suggestedfix_a_27_16 -- -package fillstruct - -import ( - "golang.org/lsptests/fillstruct/data" -) - -type basicStruct struct { - foo int -} - -var _ = basicStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type twoArgStruct struct { - foo int - bar string -} - -var _ = twoArgStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type nestedStruct struct { - bar string - basic basicStruct -} - -var _ = nestedStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -var _ = data.B{ - ExportedInt: 0, -} //@suggestedfix("}", "refactor.rewrite", "Fill") - diff --git a/gopls/internal/lsp/testdata/fillstruct/a2.go b/gopls/internal/lsp/testdata/fillstruct/a2.go deleted file mode 100644 index b5e30a84f1e..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/a2.go +++ /dev/null @@ -1,29 +0,0 @@ -package fillstruct - -type typedStruct struct { - m map[string]int - s []int - c chan int - c1 <-chan int - a [2]string -} - -var _ = typedStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type funStruct struct { - fn func(i int) int -} - -var _ = funStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type funStructCompex struct { - fn func(i int, s string) (string, int) -} - -var _ = funStructCompex{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type funStructEmpty struct { - fn func() -} - -var _ = funStructEmpty{} //@suggestedfix("}", "refactor.rewrite", "Fill") diff --git a/gopls/internal/lsp/testdata/fillstruct/a2.go.golden b/gopls/internal/lsp/testdata/fillstruct/a2.go.golden deleted file mode 100644 index 2eca3e349a1..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/a2.go.golden +++ /dev/null @@ -1,139 +0,0 @@ --- suggestedfix_a2_11_21 -- -package fillstruct - -type typedStruct struct { - m map[string]int - s []int - c chan int - c1 <-chan int - a [2]string -} - -var _ = typedStruct{ - m: map[string]int{}, - s: []int{}, - c: make(chan int), - c1: make(<-chan int), - a: [2]string{}, -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type funStruct struct { - fn func(i int) int -} - -var _ = funStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type funStructCompex struct { - fn func(i int, s string) (string, int) -} - -var _ = funStructCompex{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type funStructEmpty struct { - fn func() -} - -var _ = funStructEmpty{} //@suggestedfix("}", "refactor.rewrite", "Fill") - --- suggestedfix_a2_17_19 -- -package fillstruct - -type typedStruct struct { - m map[string]int - s []int - c chan int - c1 <-chan int - a [2]string -} - -var _ = typedStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type funStruct struct { - fn func(i int) int -} - -var _ = funStruct{ - fn: func(i int) int { - }, -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type funStructCompex struct { - fn func(i int, s string) (string, int) -} - -var _ = funStructCompex{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type funStructEmpty struct { - fn func() -} - -var _ = funStructEmpty{} //@suggestedfix("}", "refactor.rewrite", "Fill") - --- suggestedfix_a2_23_25 -- -package fillstruct - -type typedStruct struct { - m map[string]int - s []int - c chan int - c1 <-chan int - a [2]string -} - -var _ = typedStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type funStruct struct { - fn func(i int) int -} - -var _ = funStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type funStructCompex struct { - fn func(i int, s string) (string, int) -} - -var _ = funStructCompex{ - fn: func(i int, s string) (string, int) { - }, -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type funStructEmpty struct { - fn func() -} - -var _ = funStructEmpty{} //@suggestedfix("}", "refactor.rewrite", "Fill") - --- suggestedfix_a2_29_24 -- -package fillstruct - -type typedStruct struct { - m map[string]int - s []int - c chan int - c1 <-chan int - a [2]string -} - -var _ = typedStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type funStruct struct { - fn func(i int) int -} - -var _ = funStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type funStructCompex struct { - fn func(i int, s string) (string, int) -} - -var _ = funStructCompex{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type funStructEmpty struct { - fn func() -} - -var _ = funStructEmpty{ - fn: func() { - }, -} //@suggestedfix("}", "refactor.rewrite", "Fill") - diff --git a/gopls/internal/lsp/testdata/fillstruct/a3.go b/gopls/internal/lsp/testdata/fillstruct/a3.go deleted file mode 100644 index 59cd9fa28b5..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/a3.go +++ /dev/null @@ -1,42 +0,0 @@ -package fillstruct - -import ( - "go/ast" - "go/token" -) - -type Foo struct { - A int -} - -type Bar struct { - X *Foo - Y *Foo -} - -var _ = Bar{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type importedStruct struct { - m map[*ast.CompositeLit]ast.Field - s []ast.BadExpr - a [3]token.Token - c chan ast.EmptyStmt - fn func(ast_decl ast.DeclStmt) ast.Ellipsis - st ast.CompositeLit -} - -var _ = importedStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type pointerBuiltinStruct struct { - b *bool - s *string - i *int -} - -var _ = pointerBuiltinStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -var _ = []ast.BasicLit{ - {}, //@suggestedfix("}", "refactor.rewrite", "Fill") -} - -var _ = []ast.BasicLit{{}} //@suggestedfix("}", "refactor.rewrite", "Fill") diff --git a/gopls/internal/lsp/testdata/fillstruct/a3.go.golden b/gopls/internal/lsp/testdata/fillstruct/a3.go.golden deleted file mode 100644 index a7c7baa8d27..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/a3.go.golden +++ /dev/null @@ -1,243 +0,0 @@ --- suggestedfix_a3_17_13 -- -package fillstruct - -import ( - "go/ast" - "go/token" -) - -type Foo struct { - A int -} - -type Bar struct { - X *Foo - Y *Foo -} - -var _ = Bar{ - X: &Foo{}, - Y: &Foo{}, -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type importedStruct struct { - m map[*ast.CompositeLit]ast.Field - s []ast.BadExpr - a [3]token.Token - c chan ast.EmptyStmt - fn func(ast_decl ast.DeclStmt) ast.Ellipsis - st ast.CompositeLit -} - -var _ = importedStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type pointerBuiltinStruct struct { - b *bool - s *string - i *int -} - -var _ = pointerBuiltinStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -var _ = []ast.BasicLit{ - {}, //@suggestedfix("}", "refactor.rewrite", "Fill") -} - -var _ = []ast.BasicLit{{}} //@suggestedfix("}", "refactor.rewrite", "Fill") - --- suggestedfix_a3_28_24 -- -package fillstruct - -import ( - "go/ast" - "go/token" -) - -type Foo struct { - A int -} - -type Bar struct { - X *Foo - Y *Foo -} - -var _ = Bar{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type importedStruct struct { - m map[*ast.CompositeLit]ast.Field - s []ast.BadExpr - a [3]token.Token - c chan ast.EmptyStmt - fn func(ast_decl ast.DeclStmt) ast.Ellipsis - st ast.CompositeLit -} - -var _ = importedStruct{ - m: map[*ast.CompositeLit]ast.Field{}, - s: []ast.BadExpr{}, - a: [3]token.Token{}, - c: make(chan ast.EmptyStmt), - fn: func(ast_decl ast.DeclStmt) ast.Ellipsis { - }, - st: ast.CompositeLit{}, -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type pointerBuiltinStruct struct { - b *bool - s *string - i *int -} - -var _ = pointerBuiltinStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -var _ = []ast.BasicLit{ - {}, //@suggestedfix("}", "refactor.rewrite", "Fill") -} - -var _ = []ast.BasicLit{{}} //@suggestedfix("}", "refactor.rewrite", "Fill") - --- suggestedfix_a3_36_30 -- -package fillstruct - -import ( - "go/ast" - "go/token" -) - -type Foo struct { - A int -} - -type Bar struct { - X *Foo - Y *Foo -} - -var _ = Bar{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type importedStruct struct { - m map[*ast.CompositeLit]ast.Field - s []ast.BadExpr - a [3]token.Token - c chan ast.EmptyStmt - fn func(ast_decl ast.DeclStmt) ast.Ellipsis - st ast.CompositeLit -} - -var _ = importedStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type pointerBuiltinStruct struct { - b *bool - s *string - i *int -} - -var _ = pointerBuiltinStruct{ - b: new(bool), - s: new(string), - i: new(int), -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -var _ = []ast.BasicLit{ - {}, //@suggestedfix("}", "refactor.rewrite", "Fill") -} - -var _ = []ast.BasicLit{{}} //@suggestedfix("}", "refactor.rewrite", "Fill") - --- suggestedfix_a3_39_3 -- -package fillstruct - -import ( - "go/ast" - "go/token" -) - -type Foo struct { - A int -} - -type Bar struct { - X *Foo - Y *Foo -} - -var _ = Bar{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type importedStruct struct { - m map[*ast.CompositeLit]ast.Field - s []ast.BadExpr - a [3]token.Token - c chan ast.EmptyStmt - fn func(ast_decl ast.DeclStmt) ast.Ellipsis - st ast.CompositeLit -} - -var _ = importedStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type pointerBuiltinStruct struct { - b *bool - s *string - i *int -} - -var _ = pointerBuiltinStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -var _ = []ast.BasicLit{ - { - ValuePos: 0, - Kind: 0, - Value: "", - }, //@suggestedfix("}", "refactor.rewrite", "Fill") -} - -var _ = []ast.BasicLit{{}} //@suggestedfix("}", "refactor.rewrite", "Fill") - --- suggestedfix_a3_42_25 -- -package fillstruct - -import ( - "go/ast" - "go/token" -) - -type Foo struct { - A int -} - -type Bar struct { - X *Foo - Y *Foo -} - -var _ = Bar{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type importedStruct struct { - m map[*ast.CompositeLit]ast.Field - s []ast.BadExpr - a [3]token.Token - c chan ast.EmptyStmt - fn func(ast_decl ast.DeclStmt) ast.Ellipsis - st ast.CompositeLit -} - -var _ = importedStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type pointerBuiltinStruct struct { - b *bool - s *string - i *int -} - -var _ = pointerBuiltinStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -var _ = []ast.BasicLit{ - {}, //@suggestedfix("}", "refactor.rewrite", "Fill") -} - -var _ = []ast.BasicLit{{ - ValuePos: 0, - Kind: 0, - Value: "", -}} //@suggestedfix("}", "refactor.rewrite", "Fill") - diff --git a/gopls/internal/lsp/testdata/fillstruct/a4.go b/gopls/internal/lsp/testdata/fillstruct/a4.go deleted file mode 100644 index 5f52a55fa72..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/a4.go +++ /dev/null @@ -1,39 +0,0 @@ -package fillstruct - -import "go/ast" - -type iStruct struct { - X int -} - -type sStruct struct { - str string -} - -type multiFill struct { - num int - strin string - arr []int -} - -type assignStruct struct { - n ast.Node -} - -func fill() { - var x int - var _ = iStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - - var s string - var _ = sStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - - var n int - _ = []int{} - if true { - arr := []int{1, 2} - } - var _ = multiFill{} //@suggestedfix("}", "refactor.rewrite", "Fill") - - var node *ast.CompositeLit - var _ = assignStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") -} diff --git a/gopls/internal/lsp/testdata/fillstruct/a4.go.golden b/gopls/internal/lsp/testdata/fillstruct/a4.go.golden deleted file mode 100644 index b1e376f05f1..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/a4.go.golden +++ /dev/null @@ -1,174 +0,0 @@ --- suggestedfix_a4_25_18 -- -package fillstruct - -import "go/ast" - -type iStruct struct { - X int -} - -type sStruct struct { - str string -} - -type multiFill struct { - num int - strin string - arr []int -} - -type assignStruct struct { - n ast.Node -} - -func fill() { - var x int - var _ = iStruct{ - X: x, - } //@suggestedfix("}", "refactor.rewrite", "Fill") - - var s string - var _ = sStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - - var n int - _ = []int{} - if true { - arr := []int{1, 2} - } - var _ = multiFill{} //@suggestedfix("}", "refactor.rewrite", "Fill") - - var node *ast.CompositeLit - var _ = assignStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") -} - --- suggestedfix_a4_28_18 -- -package fillstruct - -import "go/ast" - -type iStruct struct { - X int -} - -type sStruct struct { - str string -} - -type multiFill struct { - num int - strin string - arr []int -} - -type assignStruct struct { - n ast.Node -} - -func fill() { - var x int - var _ = iStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - - var s string - var _ = sStruct{ - str: s, - } //@suggestedfix("}", "refactor.rewrite", "Fill") - - var n int - _ = []int{} - if true { - arr := []int{1, 2} - } - var _ = multiFill{} //@suggestedfix("}", "refactor.rewrite", "Fill") - - var node *ast.CompositeLit - var _ = assignStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") -} - --- suggestedfix_a4_35_20 -- -package fillstruct - -import "go/ast" - -type iStruct struct { - X int -} - -type sStruct struct { - str string -} - -type multiFill struct { - num int - strin string - arr []int -} - -type assignStruct struct { - n ast.Node -} - -func fill() { - var x int - var _ = iStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - - var s string - var _ = sStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - - var n int - _ = []int{} - if true { - arr := []int{1, 2} - } - var _ = multiFill{ - num: n, - strin: s, - arr: []int{}, - } //@suggestedfix("}", "refactor.rewrite", "Fill") - - var node *ast.CompositeLit - var _ = assignStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") -} - --- suggestedfix_a4_38_23 -- -package fillstruct - -import "go/ast" - -type iStruct struct { - X int -} - -type sStruct struct { - str string -} - -type multiFill struct { - num int - strin string - arr []int -} - -type assignStruct struct { - n ast.Node -} - -func fill() { - var x int - var _ = iStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - - var s string - var _ = sStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") - - var n int - _ = []int{} - if true { - arr := []int{1, 2} - } - var _ = multiFill{} //@suggestedfix("}", "refactor.rewrite", "Fill") - - var node *ast.CompositeLit - var _ = assignStruct{ - n: node, - } //@suggestedfix("}", "refactor.rewrite", "Fill") -} - diff --git a/gopls/internal/lsp/testdata/fillstruct/data/a.go b/gopls/internal/lsp/testdata/fillstruct/data/a.go deleted file mode 100644 index 7ca37736bd1..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/data/a.go +++ /dev/null @@ -1,6 +0,0 @@ -package data - -type B struct { - ExportedInt int - unexportedInt int -} diff --git a/gopls/internal/lsp/testdata/fillstruct/fill_struct.go b/gopls/internal/lsp/testdata/fillstruct/fill_struct.go deleted file mode 100644 index 3da904741d0..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/fill_struct.go +++ /dev/null @@ -1,26 +0,0 @@ -package fillstruct - -type StructA struct { - unexportedIntField int - ExportedIntField int - MapA map[int]string - Array []int - StructB -} - -type StructA2 struct { - B *StructB -} - -type StructA3 struct { - B StructB -} - -func fill() { - a := StructA{} //@suggestedfix("}", "refactor.rewrite", "Fill") - b := StructA2{} //@suggestedfix("}", "refactor.rewrite", "Fill") - c := StructA3{} //@suggestedfix("}", "refactor.rewrite", "Fill") - if true { - _ = StructA3{} //@suggestedfix("}", "refactor.rewrite", "Fill") - } -} diff --git a/gopls/internal/lsp/testdata/fillstruct/fill_struct.go.golden b/gopls/internal/lsp/testdata/fillstruct/fill_struct.go.golden deleted file mode 100644 index de01a40f052..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/fill_struct.go.golden +++ /dev/null @@ -1,124 +0,0 @@ --- suggestedfix_fill_struct_20_15 -- -package fillstruct - -type StructA struct { - unexportedIntField int - ExportedIntField int - MapA map[int]string - Array []int - StructB -} - -type StructA2 struct { - B *StructB -} - -type StructA3 struct { - B StructB -} - -func fill() { - a := StructA{ - unexportedIntField: 0, - ExportedIntField: 0, - MapA: map[int]string{}, - Array: []int{}, - StructB: StructB{}, - } //@suggestedfix("}", "refactor.rewrite", "Fill") - b := StructA2{} //@suggestedfix("}", "refactor.rewrite", "Fill") - c := StructA3{} //@suggestedfix("}", "refactor.rewrite", "Fill") - if true { - _ = StructA3{} //@suggestedfix("}", "refactor.rewrite", "Fill") - } -} - --- suggestedfix_fill_struct_21_16 -- -package fillstruct - -type StructA struct { - unexportedIntField int - ExportedIntField int - MapA map[int]string - Array []int - StructB -} - -type StructA2 struct { - B *StructB -} - -type StructA3 struct { - B StructB -} - -func fill() { - a := StructA{} //@suggestedfix("}", "refactor.rewrite", "Fill") - b := StructA2{ - B: &StructB{}, - } //@suggestedfix("}", "refactor.rewrite", "Fill") - c := StructA3{} //@suggestedfix("}", "refactor.rewrite", "Fill") - if true { - _ = StructA3{} //@suggestedfix("}", "refactor.rewrite", "Fill") - } -} - --- suggestedfix_fill_struct_22_16 -- -package fillstruct - -type StructA struct { - unexportedIntField int - ExportedIntField int - MapA map[int]string - Array []int - StructB -} - -type StructA2 struct { - B *StructB -} - -type StructA3 struct { - B StructB -} - -func fill() { - a := StructA{} //@suggestedfix("}", "refactor.rewrite", "Fill") - b := StructA2{} //@suggestedfix("}", "refactor.rewrite", "Fill") - c := StructA3{ - B: StructB{}, - } //@suggestedfix("}", "refactor.rewrite", "Fill") - if true { - _ = StructA3{} //@suggestedfix("}", "refactor.rewrite", "Fill") - } -} - --- suggestedfix_fill_struct_24_16 -- -package fillstruct - -type StructA struct { - unexportedIntField int - ExportedIntField int - MapA map[int]string - Array []int - StructB -} - -type StructA2 struct { - B *StructB -} - -type StructA3 struct { - B StructB -} - -func fill() { - a := StructA{} //@suggestedfix("}", "refactor.rewrite", "Fill") - b := StructA2{} //@suggestedfix("}", "refactor.rewrite", "Fill") - c := StructA3{} //@suggestedfix("}", "refactor.rewrite", "Fill") - if true { - _ = StructA3{ - B: StructB{}, - } //@suggestedfix("}", "refactor.rewrite", "Fill") - } -} - diff --git a/gopls/internal/lsp/testdata/fillstruct/fill_struct_anon.go b/gopls/internal/lsp/testdata/fillstruct/fill_struct_anon.go deleted file mode 100644 index 2c099a80ea7..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/fill_struct_anon.go +++ /dev/null @@ -1,14 +0,0 @@ -package fillstruct - -type StructAnon struct { - a struct{} - b map[string]interface{} - c map[string]struct { - d int - e bool - } -} - -func fill() { - _ := StructAnon{} //@suggestedfix("}", "refactor.rewrite", "Fill") -} diff --git a/gopls/internal/lsp/testdata/fillstruct/fill_struct_anon.go.golden b/gopls/internal/lsp/testdata/fillstruct/fill_struct_anon.go.golden deleted file mode 100644 index 7cc9ac23d02..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/fill_struct_anon.go.golden +++ /dev/null @@ -1,20 +0,0 @@ --- suggestedfix_fill_struct_anon_13_18 -- -package fillstruct - -type StructAnon struct { - a struct{} - b map[string]interface{} - c map[string]struct { - d int - e bool - } -} - -func fill() { - _ := StructAnon{ - a: struct{}{}, - b: map[string]interface{}{}, - c: map[string]struct{d int; e bool}{}, - } //@suggestedfix("}", "refactor.rewrite", "Fill") -} - diff --git a/gopls/internal/lsp/testdata/fillstruct/fill_struct_nested.go b/gopls/internal/lsp/testdata/fillstruct/fill_struct_nested.go deleted file mode 100644 index ab7be5a7b58..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/fill_struct_nested.go +++ /dev/null @@ -1,15 +0,0 @@ -package fillstruct - -type StructB struct { - StructC -} - -type StructC struct { - unexportedInt int -} - -func nested() { - c := StructB{ - StructC: StructC{}, //@suggestedfix("}", "refactor.rewrite", "Fill") - } -} diff --git a/gopls/internal/lsp/testdata/fillstruct/fill_struct_nested.go.golden b/gopls/internal/lsp/testdata/fillstruct/fill_struct_nested.go.golden deleted file mode 100644 index c902ee7f12b..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/fill_struct_nested.go.golden +++ /dev/null @@ -1,19 +0,0 @@ --- suggestedfix_fill_struct_nested_13_20 -- -package fillstruct - -type StructB struct { - StructC -} - -type StructC struct { - unexportedInt int -} - -func nested() { - c := StructB{ - StructC: StructC{ - unexportedInt: 0, - }, //@suggestedfix("}", "refactor.rewrite", "Fill") - } -} - diff --git a/gopls/internal/lsp/testdata/fillstruct/fill_struct_package.go b/gopls/internal/lsp/testdata/fillstruct/fill_struct_package.go deleted file mode 100644 index ef35627c8ea..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/fill_struct_package.go +++ /dev/null @@ -1,12 +0,0 @@ -package fillstruct - -import ( - h2 "net/http" - - "golang.org/lsptests/fillstruct/data" -) - -func unexported() { - a := data.B{} //@suggestedfix("}", "refactor.rewrite", "Fill") - _ = h2.Client{} //@suggestedfix("}", "refactor.rewrite", "Fill") -} diff --git a/gopls/internal/lsp/testdata/fillstruct/fill_struct_package.go.golden b/gopls/internal/lsp/testdata/fillstruct/fill_struct_package.go.golden deleted file mode 100644 index 0cdbfc820ba..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/fill_struct_package.go.golden +++ /dev/null @@ -1,36 +0,0 @@ --- suggestedfix_fill_struct_package_10_14 -- -package fillstruct - -import ( - h2 "net/http" - - "golang.org/lsptests/fillstruct/data" -) - -func unexported() { - a := data.B{ - ExportedInt: 0, - } //@suggestedfix("}", "refactor.rewrite", "Fill") - _ = h2.Client{} //@suggestedfix("}", "refactor.rewrite", "Fill") -} - --- suggestedfix_fill_struct_package_11_16 -- -package fillstruct - -import ( - h2 "net/http" - - "golang.org/lsptests/fillstruct/data" -) - -func unexported() { - a := data.B{} //@suggestedfix("}", "refactor.rewrite", "Fill") - _ = h2.Client{ - Transport: nil, - CheckRedirect: func(req *h2.Request, via []*h2.Request) error { - }, - Jar: nil, - Timeout: 0, - } //@suggestedfix("}", "refactor.rewrite", "Fill") -} - diff --git a/gopls/internal/lsp/testdata/fillstruct/fill_struct_partial.go b/gopls/internal/lsp/testdata/fillstruct/fill_struct_partial.go deleted file mode 100644 index 5de1722c783..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/fill_struct_partial.go +++ /dev/null @@ -1,24 +0,0 @@ -package fillstruct - -type StructPartialA struct { - PrefilledInt int - UnfilledInt int - StructPartialB -} - -type StructPartialB struct { - PrefilledInt int - UnfilledInt int -} - -func fill() { - a := StructPartialA{ - PrefilledInt: 5, - } //@suggestedfix("}", "refactor.rewrite", "Fill") - b := StructPartialB{ - /* this comment should disappear */ - PrefilledInt: 7, // This comment should be blown away. - /* As should - this one */ - } //@suggestedfix("}", "refactor.rewrite", "Fill") -} diff --git a/gopls/internal/lsp/testdata/fillstruct/fill_struct_partial.go.golden b/gopls/internal/lsp/testdata/fillstruct/fill_struct_partial.go.golden deleted file mode 100644 index 3aa437a0334..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/fill_struct_partial.go.golden +++ /dev/null @@ -1,52 +0,0 @@ --- suggestedfix_fill_struct_partial_17_2 -- -package fillstruct - -type StructPartialA struct { - PrefilledInt int - UnfilledInt int - StructPartialB -} - -type StructPartialB struct { - PrefilledInt int - UnfilledInt int -} - -func fill() { - a := StructPartialA{ - PrefilledInt: 5, - UnfilledInt: 0, - StructPartialB: StructPartialB{}, - } //@suggestedfix("}", "refactor.rewrite", "Fill") - b := StructPartialB{ - /* this comment should disappear */ - PrefilledInt: 7, // This comment should be blown away. - /* As should - this one */ - } //@suggestedfix("}", "refactor.rewrite", "Fill") -} - --- suggestedfix_fill_struct_partial_23_2 -- -package fillstruct - -type StructPartialA struct { - PrefilledInt int - UnfilledInt int - StructPartialB -} - -type StructPartialB struct { - PrefilledInt int - UnfilledInt int -} - -func fill() { - a := StructPartialA{ - PrefilledInt: 5, - } //@suggestedfix("}", "refactor.rewrite", "Fill") - b := StructPartialB{ - PrefilledInt: 7, - UnfilledInt: 0, - } //@suggestedfix("}", "refactor.rewrite", "Fill") -} - diff --git a/gopls/internal/lsp/testdata/fillstruct/fill_struct_spaces.go b/gopls/internal/lsp/testdata/fillstruct/fill_struct_spaces.go deleted file mode 100644 index 6a468cd544c..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/fill_struct_spaces.go +++ /dev/null @@ -1,9 +0,0 @@ -package fillstruct - -type StructD struct { - ExportedIntField int -} - -func spaces() { - d := StructD{} //@suggestedfix("}", "refactor.rewrite", "Fill") -} diff --git a/gopls/internal/lsp/testdata/fillstruct/fill_struct_spaces.go.golden b/gopls/internal/lsp/testdata/fillstruct/fill_struct_spaces.go.golden deleted file mode 100644 index 590c91611d0..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/fill_struct_spaces.go.golden +++ /dev/null @@ -1,13 +0,0 @@ --- suggestedfix_fill_struct_spaces_8_15 -- -package fillstruct - -type StructD struct { - ExportedIntField int -} - -func spaces() { - d := StructD{ - ExportedIntField: 0, - } //@suggestedfix("}", "refactor.rewrite", "Fill") -} - diff --git a/gopls/internal/lsp/testdata/fillstruct/fill_struct_unsafe.go b/gopls/internal/lsp/testdata/fillstruct/fill_struct_unsafe.go deleted file mode 100644 index f5e42a4f2fe..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/fill_struct_unsafe.go +++ /dev/null @@ -1,12 +0,0 @@ -package fillstruct - -import "unsafe" - -type unsafeStruct struct { - x int - p unsafe.Pointer -} - -func fill() { - _ := unsafeStruct{} //@suggestedfix("}", "refactor.rewrite", "Fill") -} diff --git a/gopls/internal/lsp/testdata/fillstruct/fill_struct_unsafe.go.golden b/gopls/internal/lsp/testdata/fillstruct/fill_struct_unsafe.go.golden deleted file mode 100644 index 7e8e1952f86..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/fill_struct_unsafe.go.golden +++ /dev/null @@ -1,17 +0,0 @@ --- suggestedfix_fill_struct_unsafe_11_20 -- -package fillstruct - -import "unsafe" - -type unsafeStruct struct { - x int - p unsafe.Pointer -} - -func fill() { - _ := unsafeStruct{ - x: 0, - p: nil, - } //@suggestedfix("}", "refactor.rewrite", "Fill") -} - diff --git a/gopls/internal/lsp/testdata/fillstruct/typeparams.go b/gopls/internal/lsp/testdata/fillstruct/typeparams.go deleted file mode 100644 index c0b702f57c7..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/typeparams.go +++ /dev/null @@ -1,37 +0,0 @@ -//go:build go1.18 -// +build go1.18 - -package fillstruct - -type emptyStructWithTypeParams[A any] struct{} - -var _ = emptyStructWithTypeParams[int]{} // no suggested fix - -type basicStructWithTypeParams[T any] struct { - foo T -} - -var _ = basicStructWithTypeParams[int]{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type twoArgStructWithTypeParams[F, B any] struct { - foo F - bar B -} - -var _ = twoArgStructWithTypeParams[string, int]{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -var _ = twoArgStructWithTypeParams[int, string]{ - bar: "bar", -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type nestedStructWithTypeParams struct { - bar string - basic basicStructWithTypeParams[int] -} - -var _ = nestedStructWithTypeParams{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -func _[T any]() { - type S struct{ t T } - _ = S{} //@suggestedfix("}", "refactor.rewrite", "Fill") -} diff --git a/gopls/internal/lsp/testdata/fillstruct/typeparams.go.golden b/gopls/internal/lsp/testdata/fillstruct/typeparams.go.golden deleted file mode 100644 index 625df7577b7..00000000000 --- a/gopls/internal/lsp/testdata/fillstruct/typeparams.go.golden +++ /dev/null @@ -1,206 +0,0 @@ --- suggestedfix_typeparams_14_40 -- -//go:build go1.18 -// +build go1.18 - -package fillstruct - -type emptyStructWithTypeParams[A any] struct{} - -var _ = emptyStructWithTypeParams[int]{} // no suggested fix - -type basicStructWithTypeParams[T any] struct { - foo T -} - -var _ = basicStructWithTypeParams[int]{ - foo: 0, -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type twoArgStructWithTypeParams[F, B any] struct { - foo F - bar B -} - -var _ = twoArgStructWithTypeParams[string, int]{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -var _ = twoArgStructWithTypeParams[int, string]{ - bar: "bar", -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type nestedStructWithTypeParams struct { - bar string - basic basicStructWithTypeParams[int] -} - -var _ = nestedStructWithTypeParams{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -func _[T any]() { - type S struct{ t T } - _ = S{} //@suggestedfix("}", "refactor.rewrite", "Fill") -} - --- suggestedfix_typeparams_21_49 -- -//go:build go1.18 -// +build go1.18 - -package fillstruct - -type emptyStructWithTypeParams[A any] struct{} - -var _ = emptyStructWithTypeParams[int]{} // no suggested fix - -type basicStructWithTypeParams[T any] struct { - foo T -} - -var _ = basicStructWithTypeParams[int]{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type twoArgStructWithTypeParams[F, B any] struct { - foo F - bar B -} - -var _ = twoArgStructWithTypeParams[string, int]{ - foo: "", - bar: 0, -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -var _ = twoArgStructWithTypeParams[int, string]{ - bar: "bar", -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type nestedStructWithTypeParams struct { - bar string - basic basicStructWithTypeParams[int] -} - -var _ = nestedStructWithTypeParams{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -func _[T any]() { - type S struct{ t T } - _ = S{} //@suggestedfix("}", "refactor.rewrite", "Fill") -} - --- suggestedfix_typeparams_25_1 -- -//go:build go1.18 -// +build go1.18 - -package fillstruct - -type emptyStructWithTypeParams[A any] struct{} - -var _ = emptyStructWithTypeParams[int]{} // no suggested fix - -type basicStructWithTypeParams[T any] struct { - foo T -} - -var _ = basicStructWithTypeParams[int]{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type twoArgStructWithTypeParams[F, B any] struct { - foo F - bar B -} - -var _ = twoArgStructWithTypeParams[string, int]{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -var _ = twoArgStructWithTypeParams[int, string]{ - foo: 0, - bar: "bar", -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type nestedStructWithTypeParams struct { - bar string - basic basicStructWithTypeParams[int] -} - -var _ = nestedStructWithTypeParams{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -func _[T any]() { - type S struct{ t T } - _ = S{} //@suggestedfix("}", "refactor.rewrite", "Fill") -} - --- suggestedfix_typeparams_32_36 -- -//go:build go1.18 -// +build go1.18 - -package fillstruct - -type emptyStructWithTypeParams[A any] struct{} - -var _ = emptyStructWithTypeParams[int]{} // no suggested fix - -type basicStructWithTypeParams[T any] struct { - foo T -} - -var _ = basicStructWithTypeParams[int]{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type twoArgStructWithTypeParams[F, B any] struct { - foo F - bar B -} - -var _ = twoArgStructWithTypeParams[string, int]{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -var _ = twoArgStructWithTypeParams[int, string]{ - bar: "bar", -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type nestedStructWithTypeParams struct { - bar string - basic basicStructWithTypeParams[int] -} - -var _ = nestedStructWithTypeParams{ - bar: "", - basic: basicStructWithTypeParams{}, -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -func _[T any]() { - type S struct{ t T } - _ = S{} //@suggestedfix("}", "refactor.rewrite", "Fill") -} - --- suggestedfix_typeparams_36_8 -- -//go:build go1.18 -// +build go1.18 - -package fillstruct - -type emptyStructWithTypeParams[A any] struct{} - -var _ = emptyStructWithTypeParams[int]{} // no suggested fix - -type basicStructWithTypeParams[T any] struct { - foo T -} - -var _ = basicStructWithTypeParams[int]{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type twoArgStructWithTypeParams[F, B any] struct { - foo F - bar B -} - -var _ = twoArgStructWithTypeParams[string, int]{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -var _ = twoArgStructWithTypeParams[int, string]{ - bar: "bar", -} //@suggestedfix("}", "refactor.rewrite", "Fill") - -type nestedStructWithTypeParams struct { - bar string - basic basicStructWithTypeParams[int] -} - -var _ = nestedStructWithTypeParams{} //@suggestedfix("}", "refactor.rewrite", "Fill") - -func _[T any]() { - type S struct{ t T } - _ = S{ - t: *new(T), - } //@suggestedfix("}", "refactor.rewrite", "Fill") -} - diff --git a/gopls/internal/lsp/testdata/suggestedfix/has_suggested_fix.go b/gopls/internal/lsp/testdata/suggestedfix/has_suggested_fix.go deleted file mode 100644 index 7ff524479b4..00000000000 --- a/gopls/internal/lsp/testdata/suggestedfix/has_suggested_fix.go +++ /dev/null @@ -1,11 +0,0 @@ -package suggestedfix - -import ( - "log" -) - -func goodbye() { - s := "hiiiiiii" - s = s //@suggestedfix("s = s", "quickfix", "") - log.Print(s) -} diff --git a/gopls/internal/lsp/testdata/suggestedfix/has_suggested_fix.go.golden b/gopls/internal/lsp/testdata/suggestedfix/has_suggested_fix.go.golden deleted file mode 100644 index e7e84fc227d..00000000000 --- a/gopls/internal/lsp/testdata/suggestedfix/has_suggested_fix.go.golden +++ /dev/null @@ -1,13 +0,0 @@ --- suggestedfix_has_suggested_fix_9_2 -- -package suggestedfix - -import ( - "log" -) - -func goodbye() { - s := "hiiiiiii" - //@suggestedfix("s = s", "quickfix", "") - log.Print(s) -} - diff --git a/gopls/internal/lsp/testdata/summary.txt.golden b/gopls/internal/lsp/testdata/summary.txt.golden index 5489921f8d6..2f610b5f2ba 100644 --- a/gopls/internal/lsp/testdata/summary.txt.golden +++ b/gopls/internal/lsp/testdata/summary.txt.golden @@ -1,7 +1,7 @@ -- summary -- CallHierarchyCount = 2 SemanticTokenCount = 3 -SuggestedFixCount = 80 +SuggestedFixCount = 45 MethodExtractionCount = 8 InlayHintsCount = 5 RenamesCount = 45 diff --git a/gopls/internal/regtest/marker/testdata/codeaction/fill_struct.txt b/gopls/internal/regtest/marker/testdata/codeaction/fill_struct.txt new file mode 100644 index 00000000000..c5398ead279 --- /dev/null +++ b/gopls/internal/regtest/marker/testdata/codeaction/fill_struct.txt @@ -0,0 +1,630 @@ +This test checks the behavior of the 'fill struct' code action. + +-- flags -- +-ignore_extra_diags + +-- go.mod -- +module golang.org/lsptests/fillstruct + +go 1.18 + +-- data/data.go -- +package data + +type B struct { + ExportedInt int + unexportedInt int +} + +-- a.go -- +package fillstruct + +import ( + "golang.org/lsptests/fillstruct/data" +) + +type basicStruct struct { + foo int +} + +var _ = basicStruct{} //@codeactionedit("}", "refactor.rewrite", a1) + +type twoArgStruct struct { + foo int + bar string +} + +var _ = twoArgStruct{} //@codeactionedit("}", "refactor.rewrite", a2) + +type nestedStruct struct { + bar string + basic basicStruct +} + +var _ = nestedStruct{} //@codeactionedit("}", "refactor.rewrite", a3) + +var _ = data.B{} //@codeactionedit("}", "refactor.rewrite", a4) +-- @a1/a.go -- +--- before ++++ after +@@ -11 +11,3 @@ +-var _ = basicStruct{} //@codeactionedit("}", "refactor.rewrite", a1) ++var _ = basicStruct{ ++ foo: 0, ++} //@codeactionedit("}", "refactor.rewrite", a1) +-- @a2/a.go -- +--- before ++++ after +@@ -18 +18,4 @@ +-var _ = twoArgStruct{} //@codeactionedit("}", "refactor.rewrite", a2) ++var _ = twoArgStruct{ ++ foo: 0, ++ bar: "", ++} //@codeactionedit("}", "refactor.rewrite", a2) +-- @a3/a.go -- +--- before ++++ after +@@ -25 +25,4 @@ +-var _ = nestedStruct{} //@codeactionedit("}", "refactor.rewrite", a3) ++var _ = nestedStruct{ ++ bar: "", ++ basic: basicStruct{}, ++} //@codeactionedit("}", "refactor.rewrite", a3) +-- @a4/a.go -- +--- before ++++ after +@@ -27 +27,3 @@ +-var _ = data.B{} //@codeactionedit("}", "refactor.rewrite", a4) ++var _ = data.B{ ++ ExportedInt: 0, ++} //@codeactionedit("}", "refactor.rewrite", a4) +-- a2.go -- +package fillstruct + +type typedStruct struct { + m map[string]int + s []int + c chan int + c1 <-chan int + a [2]string +} + +var _ = typedStruct{} //@codeactionedit("}", "refactor.rewrite", a21) + +type funStruct struct { + fn func(i int) int +} + +var _ = funStruct{} //@codeactionedit("}", "refactor.rewrite", a22) + +type funStructCompex struct { + fn func(i int, s string) (string, int) +} + +var _ = funStructCompex{} //@codeactionedit("}", "refactor.rewrite", a23) + +type funStructEmpty struct { + fn func() +} + +var _ = funStructEmpty{} //@codeactionedit("}", "refactor.rewrite", a24) + +-- @a21/a2.go -- +--- before ++++ after +@@ -11 +11,7 @@ +-var _ = typedStruct{} //@codeactionedit("}", "refactor.rewrite", a21) ++var _ = typedStruct{ ++ m: map[string]int{}, ++ s: []int{}, ++ c: make(chan int), ++ c1: make(<-chan int), ++ a: [2]string{}, ++} //@codeactionedit("}", "refactor.rewrite", a21) +-- @a22/a2.go -- +--- before ++++ after +@@ -17 +17,4 @@ +-var _ = funStruct{} //@codeactionedit("}", "refactor.rewrite", a22) ++var _ = funStruct{ ++ fn: func(i int) int { ++ }, ++} //@codeactionedit("}", "refactor.rewrite", a22) +-- @a23/a2.go -- +--- before ++++ after +@@ -23 +23,4 @@ +-var _ = funStructCompex{} //@codeactionedit("}", "refactor.rewrite", a23) ++var _ = funStructCompex{ ++ fn: func(i int, s string) (string, int) { ++ }, ++} //@codeactionedit("}", "refactor.rewrite", a23) +-- @a24/a2.go -- +--- before ++++ after +@@ -29 +29,4 @@ +-var _ = funStructEmpty{} //@codeactionedit("}", "refactor.rewrite", a24) ++var _ = funStructEmpty{ ++ fn: func() { ++ }, ++} //@codeactionedit("}", "refactor.rewrite", a24) +-- a3.go -- +package fillstruct + +import ( + "go/ast" + "go/token" +) + +type Foo struct { + A int +} + +type Bar struct { + X *Foo + Y *Foo +} + +var _ = Bar{} //@codeactionedit("}", "refactor.rewrite", a31) + +type importedStruct struct { + m map[*ast.CompositeLit]ast.Field + s []ast.BadExpr + a [3]token.Token + c chan ast.EmptyStmt + fn func(ast_decl ast.DeclStmt) ast.Ellipsis + st ast.CompositeLit +} + +var _ = importedStruct{} //@codeactionedit("}", "refactor.rewrite", a32) + +type pointerBuiltinStruct struct { + b *bool + s *string + i *int +} + +var _ = pointerBuiltinStruct{} //@codeactionedit("}", "refactor.rewrite", a33) + +var _ = []ast.BasicLit{ + {}, //@codeactionedit("}", "refactor.rewrite", a34) +} + +var _ = []ast.BasicLit{{}} //@codeactionedit("}", "refactor.rewrite", a35) +-- @a31/a3.go -- +--- before ++++ after +@@ -17 +17,4 @@ +-var _ = Bar{} //@codeactionedit("}", "refactor.rewrite", a31) ++var _ = Bar{ ++ X: &Foo{}, ++ Y: &Foo{}, ++} //@codeactionedit("}", "refactor.rewrite", a31) +-- @a32/a3.go -- +--- before ++++ after +@@ -28 +28,9 @@ +-var _ = importedStruct{} //@codeactionedit("}", "refactor.rewrite", a32) ++var _ = importedStruct{ ++ m: map[*ast.CompositeLit]ast.Field{}, ++ s: []ast.BadExpr{}, ++ a: [3]token.Token{}, ++ c: make(chan ast.EmptyStmt), ++ fn: func(ast_decl ast.DeclStmt) ast.Ellipsis { ++ }, ++ st: ast.CompositeLit{}, ++} //@codeactionedit("}", "refactor.rewrite", a32) +-- @a33/a3.go -- +--- before ++++ after +@@ -36 +36,5 @@ +-var _ = pointerBuiltinStruct{} //@codeactionedit("}", "refactor.rewrite", a33) ++var _ = pointerBuiltinStruct{ ++ b: new(bool), ++ s: new(string), ++ i: new(int), ++} //@codeactionedit("}", "refactor.rewrite", a33) +-- @a34/a3.go -- +--- before ++++ after +@@ -39 +39,5 @@ +- {}, //@codeactionedit("}", "refactor.rewrite", a34) ++ { ++ ValuePos: 0, ++ Kind: 0, ++ Value: "", ++ }, //@codeactionedit("}", "refactor.rewrite", a34) +-- @a35/a3.go -- +--- before ++++ after +@@ -42 +42,5 @@ +-var _ = []ast.BasicLit{{}} //@codeactionedit("}", "refactor.rewrite", a35) ++var _ = []ast.BasicLit{{ ++ ValuePos: 0, ++ Kind: 0, ++ Value: "", ++}} //@codeactionedit("}", "refactor.rewrite", a35) +-- a4.go -- +package fillstruct + +import "go/ast" + +type iStruct struct { + X int +} + +type sStruct struct { + str string +} + +type multiFill struct { + num int + strin string + arr []int +} + +type assignStruct struct { + n ast.Node +} + +func fill() { + var x int + var _ = iStruct{} //@codeactionedit("}", "refactor.rewrite", a41) + + var s string + var _ = sStruct{} //@codeactionedit("}", "refactor.rewrite", a42) + + var n int + _ = []int{} + if true { + arr := []int{1, 2} + } + var _ = multiFill{} //@codeactionedit("}", "refactor.rewrite", a43) + + var node *ast.CompositeLit + var _ = assignStruct{} //@codeactionedit("}", "refactor.rewrite", a45) +} + +-- @a41/a4.go -- +--- before ++++ after +@@ -25 +25,3 @@ +- var _ = iStruct{} //@codeactionedit("}", "refactor.rewrite", a41) ++ var _ = iStruct{ ++ X: x, ++ } //@codeactionedit("}", "refactor.rewrite", a41) +-- @a42/a4.go -- +--- before ++++ after +@@ -28 +28,3 @@ +- var _ = sStruct{} //@codeactionedit("}", "refactor.rewrite", a42) ++ var _ = sStruct{ ++ str: s, ++ } //@codeactionedit("}", "refactor.rewrite", a42) +-- @a43/a4.go -- +--- before ++++ after +@@ -35 +35,5 @@ +- var _ = multiFill{} //@codeactionedit("}", "refactor.rewrite", a43) ++ var _ = multiFill{ ++ num: n, ++ strin: s, ++ arr: []int{}, ++ } //@codeactionedit("}", "refactor.rewrite", a43) +-- @a45/a4.go -- +--- before ++++ after +@@ -38 +38,3 @@ +- var _ = assignStruct{} //@codeactionedit("}", "refactor.rewrite", a45) ++ var _ = assignStruct{ ++ n: node, ++ } //@codeactionedit("}", "refactor.rewrite", a45) +-- fill_struct.go -- +package fillstruct + +type StructA struct { + unexportedIntField int + ExportedIntField int + MapA map[int]string + Array []int + StructB +} + +type StructA2 struct { + B *StructB +} + +type StructA3 struct { + B StructB +} + +func fill() { + a := StructA{} //@codeactionedit("}", "refactor.rewrite", fill_struct1) + b := StructA2{} //@codeactionedit("}", "refactor.rewrite", fill_struct2) + c := StructA3{} //@codeactionedit("}", "refactor.rewrite", fill_struct3) + if true { + _ = StructA3{} //@codeactionedit("}", "refactor.rewrite", fill_struct4) + } +} + +-- @fill_struct1/fill_struct.go -- +--- before ++++ after +@@ -20 +20,7 @@ +- a := StructA{} //@codeactionedit("}", "refactor.rewrite", fill_struct1) ++ a := StructA{ ++ unexportedIntField: 0, ++ ExportedIntField: 0, ++ MapA: map[int]string{}, ++ Array: []int{}, ++ StructB: StructB{}, ++ } //@codeactionedit("}", "refactor.rewrite", fill_struct1) +-- @fill_struct2/fill_struct.go -- +--- before ++++ after +@@ -21 +21,3 @@ +- b := StructA2{} //@codeactionedit("}", "refactor.rewrite", fill_struct2) ++ b := StructA2{ ++ B: &StructB{}, ++ } //@codeactionedit("}", "refactor.rewrite", fill_struct2) +-- @fill_struct3/fill_struct.go -- +--- before ++++ after +@@ -22 +22,3 @@ +- c := StructA3{} //@codeactionedit("}", "refactor.rewrite", fill_struct3) ++ c := StructA3{ ++ B: StructB{}, ++ } //@codeactionedit("}", "refactor.rewrite", fill_struct3) +-- @fill_struct4/fill_struct.go -- +--- before ++++ after +@@ -24 +24,3 @@ +- _ = StructA3{} //@codeactionedit("}", "refactor.rewrite", fill_struct4) ++ _ = StructA3{ ++ B: StructB{}, ++ } //@codeactionedit("}", "refactor.rewrite", fill_struct4) +-- fill_struct_anon.go -- +package fillstruct + +type StructAnon struct { + a struct{} + b map[string]interface{} + c map[string]struct { + d int + e bool + } +} + +func fill() { + _ := StructAnon{} //@codeactionedit("}", "refactor.rewrite", fill_struct_anon) +} +-- @fill_struct_anon/fill_struct_anon.go -- +--- before ++++ after +@@ -13 +13,5 @@ +- _ := StructAnon{} //@codeactionedit("}", "refactor.rewrite", fill_struct_anon) ++ _ := StructAnon{ ++ a: struct{}{}, ++ b: map[string]interface{}{}, ++ c: map[string]struct{d int; e bool}{}, ++ } //@codeactionedit("}", "refactor.rewrite", fill_struct_anon) +-- fill_struct_nested.go -- +package fillstruct + +type StructB struct { + StructC +} + +type StructC struct { + unexportedInt int +} + +func nested() { + c := StructB{ + StructC: StructC{}, //@codeactionedit("}", "refactor.rewrite", fill_nested) + } +} + +-- @fill_nested/fill_struct_nested.go -- +--- before ++++ after +@@ -13 +13,3 @@ +- StructC: StructC{}, //@codeactionedit("}", "refactor.rewrite", fill_nested) ++ StructC: StructC{ ++ unexportedInt: 0, ++ }, //@codeactionedit("}", "refactor.rewrite", fill_nested) +-- fill_struct_package.go -- +package fillstruct + +import ( + h2 "net/http" + + "golang.org/lsptests/fillstruct/data" +) + +func unexported() { + a := data.B{} //@codeactionedit("}", "refactor.rewrite", fill_struct_package1) + _ = h2.Client{} //@codeactionedit("}", "refactor.rewrite", fill_struct_package2) +} +-- @fill_struct_package1/fill_struct_package.go -- +--- before ++++ after +@@ -10 +10,3 @@ +- a := data.B{} //@codeactionedit("}", "refactor.rewrite", fill_struct_package1) ++ a := data.B{ ++ ExportedInt: 0, ++ } //@codeactionedit("}", "refactor.rewrite", fill_struct_package1) +-- @fill_struct_package2/fill_struct_package.go -- +--- before ++++ after +@@ -11 +11,7 @@ +- _ = h2.Client{} //@codeactionedit("}", "refactor.rewrite", fill_struct_package2) ++ _ = h2.Client{ ++ Transport: nil, ++ CheckRedirect: func(req *h2.Request, via []*h2.Request) error { ++ }, ++ Jar: nil, ++ Timeout: 0, ++ } //@codeactionedit("}", "refactor.rewrite", fill_struct_package2) +-- fill_struct_partial.go -- +package fillstruct + +type StructPartialA struct { + PrefilledInt int + UnfilledInt int + StructPartialB +} + +type StructPartialB struct { + PrefilledInt int + UnfilledInt int +} + +func fill() { + a := StructPartialA{ + PrefilledInt: 5, + } //@codeactionedit("}", "refactor.rewrite", fill_struct_partial1) + b := StructPartialB{ + /* this comment should disappear */ + PrefilledInt: 7, // This comment should be blown away. + /* As should + this one */ + } //@codeactionedit("}", "refactor.rewrite", fill_struct_partial2) +} + +-- @fill_struct_partial1/fill_struct_partial.go -- +--- before ++++ after +@@ -16 +16,3 @@ +- PrefilledInt: 5, ++ PrefilledInt: 5, ++ UnfilledInt: 0, ++ StructPartialB: StructPartialB{}, +-- @fill_struct_partial2/fill_struct_partial.go -- +--- before ++++ after +@@ -19,4 +19,2 @@ +- /* this comment should disappear */ ++ PrefilledInt: 7, +- PrefilledInt: 7, // This comment should be blown away. +- /* As should +- this one */ ++ UnfilledInt: 0, +-- fill_struct_spaces.go -- +package fillstruct + +type StructD struct { + ExportedIntField int +} + +func spaces() { + d := StructD{} //@codeactionedit("}", "refactor.rewrite", fill_struct_spaces) +} + +-- @fill_struct_spaces/fill_struct_spaces.go -- +--- before ++++ after +@@ -8 +8,3 @@ +- d := StructD{} //@codeactionedit("}", "refactor.rewrite", fill_struct_spaces) ++ d := StructD{ ++ ExportedIntField: 0, ++ } //@codeactionedit("}", "refactor.rewrite", fill_struct_spaces) +-- fill_struct_unsafe.go -- +package fillstruct + +import "unsafe" + +type unsafeStruct struct { + x int + p unsafe.Pointer +} + +func fill() { + _ := unsafeStruct{} //@codeactionedit("}", "refactor.rewrite", fill_struct_unsafe) +} + +-- @fill_struct_unsafe/fill_struct_unsafe.go -- +--- before ++++ after +@@ -11 +11,4 @@ +- _ := unsafeStruct{} //@codeactionedit("}", "refactor.rewrite", fill_struct_unsafe) ++ _ := unsafeStruct{ ++ x: 0, ++ p: nil, ++ } //@codeactionedit("}", "refactor.rewrite", fill_struct_unsafe) +-- typeparams.go -- +package fillstruct + +type emptyStructWithTypeParams[A any] struct{} + +var _ = emptyStructWithTypeParams[int]{} // no suggested fix + +type basicStructWithTypeParams[T any] struct { + foo T +} + +var _ = basicStructWithTypeParams[int]{} //@codeactionedit("}", "refactor.rewrite", typeparams1) + +type twoArgStructWithTypeParams[F, B any] struct { + foo F + bar B +} + +var _ = twoArgStructWithTypeParams[string, int]{} //@codeactionedit("}", "refactor.rewrite", typeparams2) + +var _ = twoArgStructWithTypeParams[int, string]{ + bar: "bar", +} //@codeactionedit("}", "refactor.rewrite", typeparams3) + +type nestedStructWithTypeParams struct { + bar string + basic basicStructWithTypeParams[int] +} + +var _ = nestedStructWithTypeParams{} //@codeactionedit("}", "refactor.rewrite", typeparams4) + +func _[T any]() { + type S struct{ t T } + _ = S{} //@codeactionedit("}", "refactor.rewrite", typeparams5) +} +-- @typeparams1/typeparams.go -- +--- before ++++ after +@@ -11 +11,3 @@ +-var _ = basicStructWithTypeParams[int]{} //@codeactionedit("}", "refactor.rewrite", typeparams1) ++var _ = basicStructWithTypeParams[int]{ ++ foo: 0, ++} //@codeactionedit("}", "refactor.rewrite", typeparams1) +-- @typeparams2/typeparams.go -- +--- before ++++ after +@@ -18 +18,4 @@ +-var _ = twoArgStructWithTypeParams[string, int]{} //@codeactionedit("}", "refactor.rewrite", typeparams2) ++var _ = twoArgStructWithTypeParams[string, int]{ ++ foo: "", ++ bar: 0, ++} //@codeactionedit("}", "refactor.rewrite", typeparams2) +-- @typeparams3/typeparams.go -- +--- before ++++ after +@@ -20 +20,2 @@ +-var _ = twoArgStructWithTypeParams[int, string]{ ++var _ = twoArgStructWithTypeParams[int, string]{ ++ foo: 0, +-- @typeparams4/typeparams.go -- +--- before ++++ after +@@ -29 +29,4 @@ +-var _ = nestedStructWithTypeParams{} //@codeactionedit("}", "refactor.rewrite", typeparams4) ++var _ = nestedStructWithTypeParams{ ++ bar: "", ++ basic: basicStructWithTypeParams{}, ++} //@codeactionedit("}", "refactor.rewrite", typeparams4) +-- @typeparams5/typeparams.go -- +--- before ++++ after +@@ -33 +33,3 @@ +- _ = S{} //@codeactionedit("}", "refactor.rewrite", typeparams5) ++ _ = S{ ++ t: *new(T), ++ } //@codeactionedit("}", "refactor.rewrite", typeparams5) diff --git a/gopls/internal/regtest/marker/testdata/codeaction/functionextraction.txt b/gopls/internal/regtest/marker/testdata/codeaction/functionextraction.txt index d5bd9869d16..b37009c78d9 100644 --- a/gopls/internal/regtest/marker/testdata/codeaction/functionextraction.txt +++ b/gopls/internal/regtest/marker/testdata/codeaction/functionextraction.txt @@ -8,16 +8,16 @@ go 1.18 -- basic.go -- package extract -func _() { //@codeaction("refactor.extract", "{", closeBracket, outer) - a := 1 //@codeaction("refactor.extract", "a", end, inner) +func _() { //@codeaction("{", closeBracket, "refactor.extract", outer) + a := 1 //@codeaction("a", end, "refactor.extract", inner) _ = a + 4 //@loc(end, "4") } //@loc(closeBracket, "}") -- @inner/basic.go -- package extract -func _() { //@codeaction("refactor.extract", "{", closeBracket, outer) - //@codeaction("refactor.extract", "a", end, inner) +func _() { //@codeaction("{", closeBracket, "refactor.extract", outer) + //@codeaction("a", end, "refactor.extract", inner) newFunction() //@loc(end, "4") } @@ -29,8 +29,8 @@ func newFunction() { -- @outer/basic.go -- package extract -func _() { //@codeaction("refactor.extract", "{", closeBracket, outer) - //@codeaction("refactor.extract", "a", end, inner) +func _() { //@codeaction("{", closeBracket, "refactor.extract", outer) + //@codeaction("a", end, "refactor.extract", inner) newFunction() //@loc(end, "4") } @@ -44,7 +44,7 @@ package extract func _() bool { x := 1 - if x == 0 { //@codeaction("refactor.extract", "if", ifend, return) + if x == 0 { //@codeaction("if", ifend, "refactor.extract", return) return true } //@loc(ifend, "}") return false @@ -55,7 +55,7 @@ package extract func _() bool { x := 1 - //@codeaction("refactor.extract", "if", ifend, return) + //@codeaction("if", ifend, "refactor.extract", return) shouldReturn, returnValue := newFunction(x) if shouldReturn { return returnValue @@ -74,7 +74,7 @@ func newFunction(x int) (bool, bool) { package extract func _() bool { - x := 1 //@codeaction("refactor.extract", "x", rnnEnd, rnn) + x := 1 //@codeaction("x", rnnEnd, "refactor.extract", rnn) if x == 0 { return true } @@ -85,7 +85,7 @@ func _() bool { package extract func _() bool { - //@codeaction("refactor.extract", "x", rnnEnd, rnn) + //@codeaction("x", rnnEnd, "refactor.extract", rnn) return newFunction() //@loc(rnnEnd, "false") } @@ -105,7 +105,7 @@ import "fmt" func _() (int, string, error) { x := 1 y := "hello" - z := "bye" //@codeaction("refactor.extract", "z", rcEnd, rc) + z := "bye" //@codeaction("z", rcEnd, "refactor.extract", rc) if y == z { return x, y, fmt.Errorf("same") } else if false { @@ -123,7 +123,7 @@ import "fmt" func _() (int, string, error) { x := 1 y := "hello" - //@codeaction("refactor.extract", "z", rcEnd, rc) + //@codeaction("z", rcEnd, "refactor.extract", rc) z, shouldReturn, returnValue, returnValue1, returnValue2 := newFunction(y, x) if shouldReturn { return returnValue, returnValue1, returnValue2 @@ -150,7 +150,7 @@ import "fmt" func _() (int, string, error) { x := 1 y := "hello" - z := "bye" //@codeaction("refactor.extract", "z", rcnnEnd, rcnn) + z := "bye" //@codeaction("z", rcnnEnd, "refactor.extract", rcnn) if y == z { return x, y, fmt.Errorf("same") } else if false { @@ -168,7 +168,7 @@ import "fmt" func _() (int, string, error) { x := 1 y := "hello" - //@codeaction("refactor.extract", "z", rcnnEnd, rcnn) + //@codeaction("z", rcnnEnd, "refactor.extract", rcnn) return newFunction(y, x) //@loc(rcnnEnd, "nil") } @@ -190,7 +190,7 @@ import "go/ast" func _() { ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool { - if n == nil { //@codeaction("refactor.extract", "if", rflEnd, rfl) + if n == nil { //@codeaction("if", rflEnd, "refactor.extract", rfl) return true } //@loc(rflEnd, "}") return false @@ -204,7 +204,7 @@ import "go/ast" func _() { ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool { - //@codeaction("refactor.extract", "if", rflEnd, rfl) + //@codeaction("if", rflEnd, "refactor.extract", rfl) shouldReturn, returnValue := newFunction(n) if shouldReturn { return returnValue @@ -227,7 +227,7 @@ import "go/ast" func _() { ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool { - if n == nil { //@codeaction("refactor.extract", "if", rflnnEnd, rflnn) + if n == nil { //@codeaction("if", rflnnEnd, "refactor.extract", rflnn) return true } return false //@loc(rflnnEnd, "false") @@ -241,7 +241,7 @@ import "go/ast" func _() { ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool { - //@codeaction("refactor.extract", "if", rflnnEnd, rflnn) + //@codeaction("if", rflnnEnd, "refactor.extract", rflnn) return newFunction(n) //@loc(rflnnEnd, "false") }) } @@ -258,7 +258,7 @@ package extract func _() string { x := 1 - if x == 0 { //@codeaction("refactor.extract", "if", riEnd, ri) + if x == 0 { //@codeaction("if", riEnd, "refactor.extract", ri) x = 3 return "a" } //@loc(riEnd, "}") @@ -271,7 +271,7 @@ package extract func _() string { x := 1 - //@codeaction("refactor.extract", "if", riEnd, ri) + //@codeaction("if", riEnd, "refactor.extract", ri) shouldReturn, returnValue := newFunction(x) if shouldReturn { return returnValue @@ -293,7 +293,7 @@ package extract func _() string { x := 1 - if x == 0 { //@codeaction("refactor.extract", "if", rinnEnd, rinn) + if x == 0 { //@codeaction("if", rinnEnd, "refactor.extract", rinn) x = 3 return "a" } @@ -306,7 +306,7 @@ package extract func _() string { x := 1 - //@codeaction("refactor.extract", "if", rinnEnd, rinn) + //@codeaction("if", rinnEnd, "refactor.extract", rinn) return newFunction(x) //@loc(rinnEnd, "\"b\"") } @@ -324,10 +324,10 @@ package extract func _() { a := 1 - a = 5 //@codeaction("refactor.extract", "a", araend, ara) + a = 5 //@codeaction("a", araend, "refactor.extract", ara) a = a + 2 //@loc(araend, "2") - b := a * 2 //@codeaction("refactor.extract", "b", arbend, arb) + b := a * 2 //@codeaction("b", arbend, "refactor.extract", arb) _ = b + 4 //@loc(arbend, "4") } @@ -336,10 +336,10 @@ package extract func _() { a := 1 - //@codeaction("refactor.extract", "a", araend, ara) + //@codeaction("a", araend, "refactor.extract", ara) a = newFunction(a) //@loc(araend, "2") - b := a * 2 //@codeaction("refactor.extract", "b", arbend, arb) + b := a * 2 //@codeaction("b", arbend, "refactor.extract", arb) _ = b + 4 //@loc(arbend, "4") } @@ -354,10 +354,10 @@ package extract func _() { a := 1 - a = 5 //@codeaction("refactor.extract", "a", araend, ara) + a = 5 //@codeaction("a", araend, "refactor.extract", ara) a = a + 2 //@loc(araend, "2") - //@codeaction("refactor.extract", "b", arbend, arb) + //@codeaction("b", arbend, "refactor.extract", arb) newFunction(a) //@loc(arbend, "4") } @@ -371,7 +371,7 @@ package extract func _() { newFunction := 1 - a := newFunction //@codeaction("refactor.extract", "a", "newFunction", scope) + a := newFunction //@codeaction("a", "newFunction", "refactor.extract", scope) _ = a // avoid diagnostic } @@ -384,7 +384,7 @@ package extract func _() { newFunction := 1 - a := newFunction2(newFunction) //@codeaction("refactor.extract", "a", "newFunction", scope) + a := newFunction2(newFunction) //@codeaction("a", "newFunction", "refactor.extract", scope) _ = a // avoid diagnostic } @@ -402,7 +402,7 @@ package extract func _() { var a []int - a = append(a, 2) //@codeaction("refactor.extract", "a", siEnd, si) + a = append(a, 2) //@codeaction("a", siEnd, "refactor.extract", si) b := 4 //@loc(siEnd, "4") a = append(a, b) } @@ -412,7 +412,7 @@ package extract func _() { var a []int - //@codeaction("refactor.extract", "a", siEnd, si) + //@codeaction("a", siEnd, "refactor.extract", si) a, b := newFunction(a) //@loc(siEnd, "4") a = append(a, b) } @@ -429,7 +429,7 @@ package extract func _() { var b []int var a int - a = 2 //@codeaction("refactor.extract", "a", srEnd, sr) + a = 2 //@codeaction("a", srEnd, "refactor.extract", sr) b = []int{} b = append(b, a) //@loc(srEnd, ")") b[0] = 1 @@ -441,7 +441,7 @@ package extract func _() { var b []int var a int - //@codeaction("refactor.extract", "a", srEnd, sr) + //@codeaction("a", srEnd, "refactor.extract", sr) b = newFunction(a, b) //@loc(srEnd, ")") b[0] = 1 } @@ -458,7 +458,7 @@ package extract func _() { var b []int - a := 2 //@codeaction("refactor.extract", "a", upEnd, up) + a := 2 //@codeaction("a", upEnd, "refactor.extract", up) b = []int{} b = append(b, a) //@loc(upEnd, ")") b[0] = 1 @@ -472,7 +472,7 @@ package extract func _() { var b []int - //@codeaction("refactor.extract", "a", upEnd, up) + //@codeaction("a", upEnd, "refactor.extract", up) a, b := newFunction(b) //@loc(upEnd, ")") b[0] = 1 if a == 2 { @@ -491,9 +491,9 @@ func newFunction(b []int) (int, []int) { package extract func _() { - a := /* comment in the middle of a line */ 1 //@codeaction("refactor.extract", "a", commentEnd, comment1) - // Comment on its own line //@codeaction("refactor.extract", "Comment", commentEnd, comment2) - _ = a + 4 //@loc(commentEnd, "4"),codeaction("refactor.extract", "_", lastComment, comment3) + a := /* comment in the middle of a line */ 1 //@codeaction("a", commentEnd, "refactor.extract", comment1) + // Comment on its own line //@codeaction("Comment", commentEnd, "refactor.extract", comment2) + _ = a + 4 //@loc(commentEnd, "4"),codeaction("_", lastComment, "refactor.extract", comment3) // Comment right after 3 + 4 // Comment after with space //@loc(lastComment, "Comment") @@ -504,9 +504,9 @@ package extract func _() { /* comment in the middle of a line */ - //@codeaction("refactor.extract", "a", commentEnd, comment1) - // Comment on its own line //@codeaction("refactor.extract", "Comment", commentEnd, comment2) - newFunction() //@loc(commentEnd, "4"),codeaction("refactor.extract", "_", lastComment, comment3) + //@codeaction("a", commentEnd, "refactor.extract", comment1) + // Comment on its own line //@codeaction("Comment", commentEnd, "refactor.extract", comment2) + newFunction() //@loc(commentEnd, "4"),codeaction("_", lastComment, "refactor.extract", comment3) // Comment right after 3 + 4 // Comment after with space //@loc(lastComment, "Comment") @@ -522,9 +522,9 @@ func newFunction() { package extract func _() { - a := /* comment in the middle of a line */ 1 //@codeaction("refactor.extract", "a", commentEnd, comment1) - // Comment on its own line //@codeaction("refactor.extract", "Comment", commentEnd, comment2) - newFunction(a) //@loc(commentEnd, "4"),codeaction("refactor.extract", "_", lastComment, comment3) + a := /* comment in the middle of a line */ 1 //@codeaction("a", commentEnd, "refactor.extract", comment1) + // Comment on its own line //@codeaction("Comment", commentEnd, "refactor.extract", comment2) + newFunction(a) //@loc(commentEnd, "4"),codeaction("_", lastComment, "refactor.extract", comment3) // Comment right after 3 + 4 // Comment after with space //@loc(lastComment, "Comment") @@ -538,9 +538,9 @@ func newFunction(a int) { package extract func _() { - a := /* comment in the middle of a line */ 1 //@codeaction("refactor.extract", "a", commentEnd, comment1) - // Comment on its own line //@codeaction("refactor.extract", "Comment", commentEnd, comment2) - newFunction(a) //@loc(commentEnd, "4"),codeaction("refactor.extract", "_", lastComment, comment3) + a := /* comment in the middle of a line */ 1 //@codeaction("a", commentEnd, "refactor.extract", comment1) + // Comment on its own line //@codeaction("Comment", commentEnd, "refactor.extract", comment2) + newFunction(a) //@loc(commentEnd, "4"),codeaction("_", lastComment, "refactor.extract", comment3) // Comment right after 3 + 4 // Comment after with space //@loc(lastComment, "Comment") @@ -557,7 +557,7 @@ import "strconv" func _() { i, err := strconv.Atoi("1") - u, err := strconv.Atoi("2") //@codeaction("refactor.extract", "u", ")", redefine) + u, err := strconv.Atoi("2") //@codeaction("u", ")", "refactor.extract", redefine) if i == u || err == nil { return } @@ -570,7 +570,7 @@ import "strconv" func _() { i, err := strconv.Atoi("1") - u, err := newFunction() //@codeaction("refactor.extract", "u", ")", redefine) + u, err := newFunction() //@codeaction("u", ")", "refactor.extract", redefine) if i == u || err == nil { return } diff --git a/gopls/internal/regtest/marker/testdata/codeaction/functionextraction_issue44813.txt b/gopls/internal/regtest/marker/testdata/codeaction/functionextraction_issue44813.txt index 46369d0a30c..cadc8e94263 100644 --- a/gopls/internal/regtest/marker/testdata/codeaction/functionextraction_issue44813.txt +++ b/gopls/internal/regtest/marker/testdata/codeaction/functionextraction_issue44813.txt @@ -12,7 +12,7 @@ package extract import "fmt" func main() { - x := []rune{} //@codeaction("refactor.extract", "x", end, ext) + x := []rune{} //@codeaction("x", end, "refactor.extract", ext) s := "HELLO" for _, c := range s { x = append(x, c) @@ -26,7 +26,7 @@ package extract import "fmt" func main() { - //@codeaction("refactor.extract", "x", end, ext) + //@codeaction("x", end, "refactor.extract", ext) x := newFunction() //@loc(end, "}") fmt.Printf("%x\n", x) } diff --git a/gopls/internal/regtest/marker/testdata/codeaction/imports.txt b/gopls/internal/regtest/marker/testdata/codeaction/imports.txt index 325733ec86d..3d058fb36a1 100644 --- a/gopls/internal/regtest/marker/testdata/codeaction/imports.txt +++ b/gopls/internal/regtest/marker/testdata/codeaction/imports.txt @@ -6,7 +6,7 @@ module mod.test/imports go 1.18 -- add.go -- -package imports //@codeaction("source.organizeImports", "imports", "", add) +package imports //@codeaction("imports", "", "source.organizeImports", add) import ( "fmt" @@ -18,7 +18,7 @@ func _() { } -- @add/add.go -- -package imports //@codeaction("source.organizeImports", "imports", "", add) +package imports //@codeaction("imports", "", "source.organizeImports", add) import ( "bytes" @@ -31,7 +31,7 @@ func _() { } -- good.go -- -package imports //@codeactionerr("source.organizeImports", "imports", "", re"found 0 CodeActions") +package imports //@codeactionerr("imports", "", "source.organizeImports", re"found 0 CodeActions") import "fmt" @@ -46,7 +46,7 @@ fmt.Println("") // package doc -package imports //@codeaction("source.organizeImports", "imports", "", issue35458) +package imports //@codeaction("imports", "", "source.organizeImports", issue35458) @@ -66,7 +66,7 @@ func _() { -- @issue35458/issue35458.go -- // package doc -package imports //@codeaction("source.organizeImports", "imports", "", issue35458) +package imports //@codeaction("imports", "", "source.organizeImports", issue35458) @@ -85,7 +85,7 @@ func _() { -- multi.go -- -package imports //@codeaction("source.organizeImports", "imports", "", multi) +package imports //@codeaction("imports", "", "source.organizeImports", multi) import "fmt" @@ -96,7 +96,7 @@ func _() { } -- @multi/multi.go -- -package imports //@codeaction("source.organizeImports", "imports", "", multi) +package imports //@codeaction("imports", "", "source.organizeImports", multi) import "fmt" @@ -107,7 +107,7 @@ func _() { } -- needs.go -- -package imports //@codeaction("source.organizeImports", "package", "", needs) +package imports //@codeaction("package", "", "source.organizeImports", needs) func goodbye() { fmt.Printf("HI") //@diag("fmt", re"(undeclared|undefined)") @@ -115,7 +115,7 @@ func goodbye() { } -- @needs/needs.go -- -package imports //@codeaction("source.organizeImports", "package", "", needs) +package imports //@codeaction("package", "", "source.organizeImports", needs) import ( "fmt" @@ -128,7 +128,7 @@ func goodbye() { } -- remove.go -- -package imports //@codeaction("source.organizeImports", "package", "", remove) +package imports //@codeaction("package", "", "source.organizeImports", remove) import ( "bytes" //@diag("\"bytes\"", re"not used") @@ -140,7 +140,7 @@ func _() { } -- @remove/remove.go -- -package imports //@codeaction("source.organizeImports", "package", "", remove) +package imports //@codeaction("package", "", "source.organizeImports", remove) import ( "fmt" @@ -151,7 +151,7 @@ func _() { } -- removeall.go -- -package imports //@codeaction("source.organizeImports", "package", "", removeall) +package imports //@codeaction("package", "", "source.organizeImports", removeall) import ( "bytes" //@diag("\"bytes\"", re"not used") @@ -163,7 +163,7 @@ func _() { } -- @removeall/removeall.go -- -package imports //@codeaction("source.organizeImports", "package", "", removeall) +package imports //@codeaction("package", "", "source.organizeImports", removeall) //@diag("\"fmt\"", re"not used") @@ -172,4 +172,4 @@ func _() { -- twolines.go -- package imports -func main() {} //@codeactionerr("source.organizeImports", "main", "", re"found 0") +func main() {} //@codeactionerr("main", "", "source.organizeImports", re"found 0") diff --git a/gopls/internal/regtest/marker/testdata/codeaction/infertypeargs.txt b/gopls/internal/regtest/marker/testdata/codeaction/infertypeargs.txt index 8ee1b67ff56..6f7b5fbe8c0 100644 --- a/gopls/internal/regtest/marker/testdata/codeaction/infertypeargs.txt +++ b/gopls/internal/regtest/marker/testdata/codeaction/infertypeargs.txt @@ -18,7 +18,7 @@ func app[S interface{ ~[]E }, E interface{}](s S, e E) S { func _() { _ = app[[]int] _ = app[[]int, int] - _ = app[[]int]([]int{}, 0) //@codeaction("refactor.rewrite", "app", ")", infer) + _ = app[[]int]([]int{}, 0) //@codeaction("app", ")", "refactor.rewrite", infer) _ = app([]int{}, 0) } @@ -32,7 +32,7 @@ func app[S interface{ ~[]E }, E interface{}](s S, e E) S { func _() { _ = app[[]int] _ = app[[]int, int] - _ = app([]int{}, 0) //@codeaction("refactor.rewrite", "app", ")", infer) + _ = app([]int{}, 0) //@codeaction("app", ")", "refactor.rewrite", infer) _ = app([]int{}, 0) } diff --git a/gopls/internal/regtest/marker/testdata/codeaction/inline.txt b/gopls/internal/regtest/marker/testdata/codeaction/inline.txt index 15d3cabfcc8..813a69ce09c 100644 --- a/gopls/internal/regtest/marker/testdata/codeaction/inline.txt +++ b/gopls/internal/regtest/marker/testdata/codeaction/inline.txt @@ -8,7 +8,7 @@ go 1.18 package a func _() { - println(add(1, 2)) //@codeaction("refactor.inline", "add", ")", inline) + println(add(1, 2)) //@codeaction("add", ")", "refactor.inline", inline) } func add(x, y int) int { return x + y } @@ -17,7 +17,7 @@ func add(x, y int) int { return x + y } package a func _() { - println(1 + 2) //@codeaction("refactor.inline", "add", ")", inline) + println(1 + 2) //@codeaction("add", ")", "refactor.inline", inline) } func add(x, y int) int { return x + y } diff --git a/gopls/internal/regtest/marker/testdata/codeaction/removeparam.txt b/gopls/internal/regtest/marker/testdata/codeaction/removeparam.txt index 7caa660babe..ad2289284d8 100644 --- a/gopls/internal/regtest/marker/testdata/codeaction/removeparam.txt +++ b/gopls/internal/regtest/marker/testdata/codeaction/removeparam.txt @@ -8,14 +8,14 @@ go 1.18 -- a/a.go -- package a -func A(x, unused int) int { //@codeaction("refactor.rewrite", "unused", "unused", a) +func A(x, unused int) int { //@codeaction("unused", "unused", "refactor.rewrite", a) return x } -- @a/a/a.go -- package a -func A(x int) int { //@codeaction("refactor.rewrite", "unused", "unused", a) +func A(x int) int { //@codeaction("unused", "unused", "refactor.rewrite", a) return x } @@ -98,7 +98,7 @@ func _() { -- field/field.go -- package field -func Field(x int, field int) { //@codeaction("refactor.rewrite", "int", "int", field) +func Field(x int, field int) { //@codeaction("int", "int", "refactor.rewrite", field) } func _() { @@ -107,7 +107,7 @@ func _() { -- @field/field/field.go -- package field -func Field(field int) { //@codeaction("refactor.rewrite", "int", "int", field) +func Field(field int) { //@codeaction("int", "int", "refactor.rewrite", field) } func _() { @@ -116,7 +116,7 @@ func _() { -- ellipsis/ellipsis.go -- package ellipsis -func Ellipsis(...any) { //@codeaction("refactor.rewrite", "any", "any", ellipsis) +func Ellipsis(...any) { //@codeaction("any", "any", "refactor.rewrite", ellipsis) } func _() { @@ -137,7 +137,7 @@ func i() []any -- @ellipsis/ellipsis/ellipsis.go -- package ellipsis -func Ellipsis() { //@codeaction("refactor.rewrite", "any", "any", ellipsis) +func Ellipsis() { //@codeaction("any", "any", "refactor.rewrite", ellipsis) } func _() { @@ -161,7 +161,7 @@ func i() []any -- ellipsis2/ellipsis2.go -- package ellipsis2 -func Ellipsis2(_, _ int, rest ...int) { //@codeaction("refactor.rewrite", "_", "_", ellipsis2) +func Ellipsis2(_, _ int, rest ...int) { //@codeaction("_", "_", "refactor.rewrite", ellipsis2) } func _() { @@ -175,7 +175,7 @@ func h() (int, int) -- @ellipsis2/ellipsis2/ellipsis2.go -- package ellipsis2 -func Ellipsis2(_ int, rest ...int) { //@codeaction("refactor.rewrite", "_", "_", ellipsis2) +func Ellipsis2(_ int, rest ...int) { //@codeaction("_", "_", "refactor.rewrite", ellipsis2) } func _() { @@ -190,7 +190,7 @@ func h() (int, int) -- overlapping/overlapping.go -- package overlapping -func Overlapping(i int) int { //@codeactionerr("refactor.rewrite", re"(i) int", re"(i) int", re"overlapping") +func Overlapping(i int) int { //@codeactionerr(re"(i) int", re"(i) int", "refactor.rewrite", re"overlapping") return 0 } @@ -202,7 +202,7 @@ func _() { -- effects/effects.go -- package effects -func effects(x, y int) int { //@codeaction("refactor.rewrite", "y", "y", effects) +func effects(x, y int) int { //@codeaction("y", "y", "refactor.rewrite", effects) return x } @@ -216,7 +216,7 @@ func _() { -- @effects/effects/effects.go -- package effects -func effects(x int) int { //@codeaction("refactor.rewrite", "y", "y", effects) +func effects(x int) int { //@codeaction("y", "y", "refactor.rewrite", effects) return x } @@ -234,13 +234,13 @@ func _() { -- recursive/recursive.go -- package recursive -func Recursive(x int) int { //@codeaction("refactor.rewrite", "x", "x", recursive) +func Recursive(x int) int { //@codeaction("x", "x", "refactor.rewrite", recursive) return Recursive(1) } -- @recursive/recursive/recursive.go -- package recursive -func Recursive() int { //@codeaction("refactor.rewrite", "x", "x", recursive) +func Recursive() int { //@codeaction("x", "x", "refactor.rewrite", recursive) return Recursive() } diff --git a/gopls/internal/regtest/marker/testdata/codeaction/removeparam_formatting.txt b/gopls/internal/regtest/marker/testdata/codeaction/removeparam_formatting.txt index 39f3ddbf121..17abb98d5c9 100644 --- a/gopls/internal/regtest/marker/testdata/codeaction/removeparam_formatting.txt +++ b/gopls/internal/regtest/marker/testdata/codeaction/removeparam_formatting.txt @@ -14,7 +14,7 @@ go 1.18 package a // A doc comment. -func A(x /* used parameter */, unused int /* unused parameter */ ) int { //@codeaction("refactor.rewrite", "unused", "unused", a) +func A(x /* used parameter */, unused int /* unused parameter */ ) int { //@codeaction("unused", "unused", "refactor.rewrite", a) // about to return return x // returning // just returned @@ -36,7 +36,7 @@ func one() int { package a // A doc comment. -func A(x int) int { //@codeaction("refactor.rewrite", "unused", "unused", a) +func A(x int) int { //@codeaction("unused", "unused", "refactor.rewrite", a) // about to return return x // returning // just returned diff --git a/gopls/internal/regtest/marker/testdata/codeaction/removeparam_funcvalue.txt b/gopls/internal/regtest/marker/testdata/codeaction/removeparam_funcvalue.txt index 417318497d7..e67e378fde3 100644 --- a/gopls/internal/regtest/marker/testdata/codeaction/removeparam_funcvalue.txt +++ b/gopls/internal/regtest/marker/testdata/codeaction/removeparam_funcvalue.txt @@ -10,7 +10,7 @@ go 1.18 -- a/a.go -- package a -func A(x, unused int) int { //@codeactionerr("refactor.rewrite", "unused", "unused", re"non-call function reference") +func A(x, unused int) int { //@codeactionerr("unused", "unused", "refactor.rewrite", re"non-call function reference") return x } diff --git a/gopls/internal/regtest/marker/testdata/codeaction/removeparam_imports.txt b/gopls/internal/regtest/marker/testdata/codeaction/removeparam_imports.txt index 1a483d2525c..d183cc44135 100644 --- a/gopls/internal/regtest/marker/testdata/codeaction/removeparam_imports.txt +++ b/gopls/internal/regtest/marker/testdata/codeaction/removeparam_imports.txt @@ -59,7 +59,7 @@ import "mod.test/c" var Chan chan c.C -func B(x, y c.C) { //@codeaction("refactor.rewrite", "x", "x", b) +func B(x, y c.C) { //@codeaction("x", "x", "refactor.rewrite", b) } -- c/c.go -- @@ -73,7 +73,7 @@ package d // Removing the parameter should remove this import. import "mod.test/c" -func D(x c.C) { //@codeaction("refactor.rewrite", "x", "x", d) +func D(x c.C) { //@codeaction("x", "x", "refactor.rewrite", d) } func _() { @@ -145,14 +145,14 @@ import "mod.test/c" var Chan chan c.C -func B(y c.C) { //@codeaction("refactor.rewrite", "x", "x", b) +func B(y c.C) { //@codeaction("x", "x", "refactor.rewrite", b) } -- @d/d/d.go -- package d // Removing the parameter should remove this import. -func D() { //@codeaction("refactor.rewrite", "x", "x", d) +func D() { //@codeaction("x", "x", "refactor.rewrite", d) } func _() { diff --git a/gopls/internal/regtest/marker/testdata/suggestedfix/self_assignment.txt b/gopls/internal/regtest/marker/testdata/suggestedfix/self_assignment.txt new file mode 100644 index 00000000000..241a80a99c2 --- /dev/null +++ b/gopls/internal/regtest/marker/testdata/suggestedfix/self_assignment.txt @@ -0,0 +1,28 @@ +Test of the suggested fix to remove unnecessary assignments. + +-- a.go -- +package suggestedfix + +import ( + "log" +) + +func goodbye() { + s := "hiiiiiii" + s = s //@suggestedfix("s = s", re"self-assignment", "quickfix", fix) + log.Print(s) +} + +-- @fix/a.go -- +package suggestedfix + +import ( + "log" +) + +func goodbye() { + s := "hiiiiiii" + //@suggestedfix("s = s", re"self-assignment", "quickfix", fix) + log.Print(s) +} + diff --git a/gopls/internal/regtest/marker/testdata/quickfix/undeclared.txt b/gopls/internal/regtest/marker/testdata/suggestedfix/undeclared.txt similarity index 100% rename from gopls/internal/regtest/marker/testdata/quickfix/undeclared.txt rename to gopls/internal/regtest/marker/testdata/suggestedfix/undeclared.txt diff --git a/gopls/internal/regtest/marker/testdata/quickfix/unusedrequire.txt b/gopls/internal/regtest/marker/testdata/suggestedfix/unusedrequire.txt similarity index 100% rename from gopls/internal/regtest/marker/testdata/quickfix/unusedrequire.txt rename to gopls/internal/regtest/marker/testdata/suggestedfix/unusedrequire.txt diff --git a/gopls/internal/regtest/marker/testdata/quickfix/unusedrequire_gowork.txt b/gopls/internal/regtest/marker/testdata/suggestedfix/unusedrequire_gowork.txt similarity index 100% rename from gopls/internal/regtest/marker/testdata/quickfix/unusedrequire_gowork.txt rename to gopls/internal/regtest/marker/testdata/suggestedfix/unusedrequire_gowork.txt diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go index 8948ecce551..2b291680479 100644 --- a/internal/analysisinternal/analysis.go +++ b/internal/analysisinternal/analysis.go @@ -42,7 +42,7 @@ func ZeroValue(f *ast.File, pkg *types.Package, typ types.Type) ast.Expr { case u.Info()&types.IsString != 0: return &ast.BasicLit{Kind: token.STRING, Value: `""`} default: - panic("unknown basic type") + panic(fmt.Sprintf("unknown basic type %v", u)) } case *types.Chan, *types.Interface, *types.Map, *types.Pointer, *types.Signature, *types.Slice, *types.Array: return ast.NewIdent("nil") diff --git a/internal/diff/diff_test.go b/internal/diff/diff_test.go index 7b25c3af5c3..77a20baf272 100644 --- a/internal/diff/diff_test.go +++ b/internal/diff/diff_test.go @@ -123,7 +123,7 @@ func TestToUnified(t *testing.T) { testenv.NeedsTool(t, "patch") for _, tc := range difftest.TestCases { t.Run(tc.Name, func(t *testing.T) { - unified, err := diff.ToUnified(difftest.FileA, difftest.FileB, tc.In, tc.Edits) + unified, err := diff.ToUnified(difftest.FileA, difftest.FileB, tc.In, tc.Edits, diff.DefaultContextLines) if err != nil { t.Fatal(err) } diff --git a/internal/diff/difftest/difftest.go b/internal/diff/difftest/difftest.go index fb691edc386..a5507675f17 100644 --- a/internal/diff/difftest/difftest.go +++ b/internal/diff/difftest/difftest.go @@ -307,7 +307,7 @@ func DiffTest(t *testing.T, compute func(before, after string) []diff.Edit) { if err != nil { t.Fatalf("Apply failed: %v", err) } - unified, err := diff.ToUnified(FileA, FileB, test.In, edits) + unified, err := diff.ToUnified(FileA, FileB, test.In, edits, diff.DefaultContextLines) if err != nil { t.Fatalf("ToUnified: %v", err) } diff --git a/internal/diff/unified.go b/internal/diff/unified.go index 1308503f70c..cfbda61020a 100644 --- a/internal/diff/unified.go +++ b/internal/diff/unified.go @@ -10,12 +10,16 @@ import ( "strings" ) +// DefaultContextLines is the number of unchanged lines of surrounding +// context displayed by Unified. Use ToUnified to specify a different value. +const DefaultContextLines = 3 + // Unified returns a unified diff of the old and new strings. // The old and new labels are the names of the old and new files. // If the strings are equal, it returns the empty string. func Unified(oldLabel, newLabel, old, new string) string { edits := Strings(old, new) - unified, err := ToUnified(oldLabel, newLabel, old, edits) + unified, err := ToUnified(oldLabel, newLabel, old, edits, DefaultContextLines) if err != nil { // Can't happen: edits are consistent. log.Fatalf("internal error in diff.Unified: %v", err) @@ -23,11 +27,12 @@ func Unified(oldLabel, newLabel, old, new string) string { return unified } -// ToUnified applies the edits to content and returns a unified diff. +// ToUnified applies the edits to content and returns a unified diff, +// with contextLines lines of (unchanged) context around each diff hunk. // The old and new labels are the names of the content and result files. // It returns an error if the edits are inconsistent; see ApplyEdits. -func ToUnified(oldLabel, newLabel, content string, edits []Edit) (string, error) { - u, err := toUnified(oldLabel, newLabel, content, edits) +func ToUnified(oldLabel, newLabel, content string, edits []Edit, contextLines int) (string, error) { + u, err := toUnified(oldLabel, newLabel, content, edits, contextLines) if err != nil { return "", err } @@ -93,14 +98,10 @@ func (k opKind) String() string { } } -const ( - edge = 3 - gap = edge * 2 -) - // toUnified takes a file contents and a sequence of edits, and calculates // a unified diff that represents those edits. -func toUnified(fromName, toName string, content string, edits []Edit) (unified, error) { +func toUnified(fromName, toName string, content string, edits []Edit, contextLines int) (unified, error) { + gap := contextLines * 2 u := unified{ from: fromName, to: toName, @@ -136,7 +137,7 @@ func toUnified(fromName, toName string, content string, edits []Edit) (unified, //need to start a new hunk if h != nil { // add the edge to the previous hunk - addEqualLines(h, lines, last, last+edge) + addEqualLines(h, lines, last, last+contextLines) u.hunks = append(u.hunks, h) } toLine += start - last @@ -145,7 +146,7 @@ func toUnified(fromName, toName string, content string, edits []Edit) (unified, toLine: toLine + 1, } // add the edge to the new hunk - delta := addEqualLines(h, lines, start-edge, start) + delta := addEqualLines(h, lines, start-contextLines, start) h.fromLine -= delta h.toLine -= delta } @@ -163,7 +164,7 @@ func toUnified(fromName, toName string, content string, edits []Edit) (unified, } if h != nil { // add the edge to the final hunk - addEqualLines(h, lines, last, last+edge) + addEqualLines(h, lines, last, last+contextLines) u.hunks = append(u.hunks, h) } return u, nil