Skip to content

Commit 38ed81a

Browse files
committed
gopls/internal/regtest/marker: porting extract tests
Port the extraction tests, which involved a mixture of suggestedfix, extractmethod, and extractfunc markers (notably, nothing was running the extractfunc markers!). To do this, extend the new codeaction markers to accept an option slice of titles to use for filtering. Modify the method/func extraction tests to reduce verbosity and long lines. For golang/go#54845 Change-Id: I319d1c731a4cdb4ad952944667bc57d781ad176d Reviewed-on: https://go-review.googlesource.com/c/tools/+/539481 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent bbf8380 commit 38ed81a

16 files changed

+379
-675
lines changed

gopls/internal/lsp/lsp_test.go

-52
Original file line numberDiff line numberDiff line change
@@ -345,58 +345,6 @@ func (r *runner) SuggestedFix(t *testing.T, spn span.Span, actionKinds []tests.S
345345
}
346346
}
347347

348-
func (r *runner) MethodExtraction(t *testing.T, start span.Span, end span.Span) {
349-
uri := start.URI()
350-
m, err := r.data.Mapper(uri)
351-
if err != nil {
352-
t.Fatal(err)
353-
}
354-
spn := span.New(start.URI(), start.Start(), end.End())
355-
rng, err := m.SpanRange(spn)
356-
if err != nil {
357-
t.Fatal(err)
358-
}
359-
actionsRaw, err := r.server.CodeAction(r.ctx, &protocol.CodeActionParams{
360-
TextDocument: protocol.TextDocumentIdentifier{
361-
URI: protocol.URIFromSpanURI(uri),
362-
},
363-
Range: rng,
364-
Context: protocol.CodeActionContext{
365-
Only: []protocol.CodeActionKind{"refactor.extract"},
366-
},
367-
})
368-
if err != nil {
369-
t.Fatal(err)
370-
}
371-
var actions []protocol.CodeAction
372-
for _, action := range actionsRaw {
373-
if action.Command.Title == "Extract method" {
374-
actions = append(actions, action)
375-
}
376-
}
377-
// Hack: We assume that we only get one matching code action per range.
378-
// TODO(rstambler): Support multiple code actions per test.
379-
if len(actions) == 0 || len(actions) > 1 {
380-
t.Fatalf("unexpected number of code actions, want 1, got %v", len(actions))
381-
}
382-
_, err = r.server.ExecuteCommand(r.ctx, &protocol.ExecuteCommandParams{
383-
Command: actions[0].Command.Command,
384-
Arguments: actions[0].Command.Arguments,
385-
})
386-
if err != nil {
387-
t.Fatal(err)
388-
}
389-
res := <-r.editRecv
390-
for u, got := range res {
391-
want := r.data.Golden(t, "methodextraction_"+tests.SpanName(spn), u.Filename(), func() ([]byte, error) {
392-
return got, nil
393-
})
394-
if diff := compare.Bytes(want, got); diff != "" {
395-
t.Errorf("method extraction failed for %s:\n%s", u.Filename(), diff)
396-
}
397-
}
398-
}
399-
400348
func (r *runner) InlayHints(t *testing.T, spn span.Span) {
401349
uri := spn.URI()
402350
filename := uri.Filename()

gopls/internal/lsp/regtest/marker.go

+38-20
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,20 @@ var update = flag.Bool("update", false, "if set, update test data during marker
153153
// completion candidate produced at the given location with provided label
154154
// results in the given golden state.
155155
//
156-
// - codeaction(start, end, kind, golden): specifies a code action to request
157-
// for the given range. To support multi-line ranges, the range is defined
158-
// to be between start.Start and end.End. The golden directory contains
159-
// changed file content after the code action is applied.
156+
// - codeaction(start, end, kind, golden, ...titles): specifies a code action
157+
// to request for the given range. To support multi-line ranges, the range
158+
// is defined to be between start.Start and end.End. The golden directory
159+
// contains changed file content after the code action is applied.
160+
// If titles are provided, they are used to filter the matching code
161+
// action.
160162
//
161-
// - codeactionedit(range, kind, golden): a shorter form of codeaction.
162-
// Invokes a code action of the given kind for the given in-line range, and
163-
// compares the resulting formatted unified *edits* (notably, not the full
164-
// file content) with the golden directory.
163+
// TODO(rfindley): consolidate with codeactionedit, via a @loc2 marker that
164+
// allows binding multi-line locations.
165+
//
166+
// - codeactionedit(range, kind, golden, ...titles): a shorter form of
167+
// codeaction. Invokes a code action of the given kind for the given
168+
// in-line range, and compares the resulting formatted unified *edits*
169+
// (notably, not the full file content) with the golden directory.
165170
//
166171
// - codeactionerr(start, end, kind, wantError): specifies a codeaction that
167172
// fails with an error that matches the expectation.
@@ -381,12 +386,15 @@ var update = flag.Bool("update", false, "if set, update test data during marker
381386
// - Provide some means by which locations in the standard library
382387
// (or builtin.go) can be named, so that, for example, we can we
383388
// can assert that MyError implements the built-in error type.
389+
// - If possible, improve handling for optional arguments. Rather than have
390+
// multiple variations of a marker, it would be nice to support a more
391+
// flexible signature: can codeaction, codeactionedit, codeactionerr, and
392+
// suggestedfix be consolidated?
384393
//
385394
// Existing marker tests (in ../testdata) to port:
386395
// - CallHierarchy
387396
// - SemanticTokens
388397
// - SuggestedFixes
389-
// - MethodExtractions
390398
// - InlayHints
391399
// - Renames
392400
// - SelectionRanges
@@ -1924,13 +1932,13 @@ func applyDocumentChanges(env *Env, changes []protocol.DocumentChanges, fileChan
19241932
return nil
19251933
}
19261934

1927-
func codeActionMarker(mark marker, start, end protocol.Location, actionKind string, g *Golden) {
1935+
func codeActionMarker(mark marker, start, end protocol.Location, actionKind string, g *Golden, titles ...string) {
19281936
// Request the range from start.Start to end.End.
19291937
loc := start
19301938
loc.Range.End = end.Range.End
19311939

19321940
// Apply the fix it suggests.
1933-
changed, err := codeAction(mark.run.env, loc.URI, loc.Range, actionKind, nil)
1941+
changed, err := codeAction(mark.run.env, loc.URI, loc.Range, actionKind, nil, titles)
19341942
if err != nil {
19351943
mark.errorf("codeAction failed: %v", err)
19361944
return
@@ -1940,8 +1948,8 @@ func codeActionMarker(mark marker, start, end protocol.Location, actionKind stri
19401948
checkChangedFiles(mark, changed, g)
19411949
}
19421950

1943-
func codeActionEditMarker(mark marker, loc protocol.Location, actionKind string, g *Golden) {
1944-
changed, err := codeAction(mark.run.env, loc.URI, loc.Range, actionKind, nil)
1951+
func codeActionEditMarker(mark marker, loc protocol.Location, actionKind string, g *Golden, titles ...string) {
1952+
changed, err := codeAction(mark.run.env, loc.URI, loc.Range, actionKind, nil, titles)
19451953
if err != nil {
19461954
mark.errorf("codeAction failed: %v", err)
19471955
return
@@ -1953,7 +1961,7 @@ func codeActionEditMarker(mark marker, loc protocol.Location, actionKind string,
19531961
func codeActionErrMarker(mark marker, start, end protocol.Location, actionKind string, wantErr wantError) {
19541962
loc := start
19551963
loc.Range.End = end.Range.End
1956-
_, err := codeAction(mark.run.env, loc.URI, loc.Range, actionKind, nil)
1964+
_, err := codeAction(mark.run.env, loc.URI, loc.Range, actionKind, nil, nil)
19571965
wantErr.check(mark, err)
19581966
}
19591967

@@ -2037,7 +2045,7 @@ func suggestedfixMarker(mark marker, loc protocol.Location, re *regexp.Regexp, g
20372045
}
20382046

20392047
// Apply the fix it suggests.
2040-
changed, err := codeAction(mark.run.env, loc.URI, diag.Range, "quickfix", &diag)
2048+
changed, err := codeAction(mark.run.env, loc.URI, diag.Range, "quickfix", &diag, nil)
20412049
if err != nil {
20422050
mark.errorf("suggestedfix failed: %v. (Use @suggestedfixerr for expected errors.)", err)
20432051
return
@@ -2054,8 +2062,8 @@ func suggestedfixMarker(mark marker, loc protocol.Location, re *regexp.Regexp, g
20542062
// The resulting map contains resulting file contents after the code action is
20552063
// applied. Currently, this function does not support code actions that return
20562064
// edits directly; it only supports code action commands.
2057-
func codeAction(env *Env, uri protocol.DocumentURI, rng protocol.Range, actionKind string, diag *protocol.Diagnostic) (map[string][]byte, error) {
2058-
changes, err := codeActionChanges(env, uri, rng, actionKind, diag)
2065+
func codeAction(env *Env, uri protocol.DocumentURI, rng protocol.Range, actionKind string, diag *protocol.Diagnostic, titles []string) (map[string][]byte, error) {
2066+
changes, err := codeActionChanges(env, uri, rng, actionKind, diag, titles)
20592067
if err != nil {
20602068
return nil, err
20612069
}
@@ -2069,7 +2077,8 @@ func codeAction(env *Env, uri protocol.DocumentURI, rng protocol.Range, actionKi
20692077
// codeActionChanges executes a textDocument/codeAction request for the
20702078
// specified location and kind, and captures the resulting document changes.
20712079
// If diag is non-nil, it is used as the code action context.
2072-
func codeActionChanges(env *Env, uri protocol.DocumentURI, rng protocol.Range, actionKind string, diag *protocol.Diagnostic) ([]protocol.DocumentChanges, error) {
2080+
// If titles is non-empty, the code action title must be present among the provided titles.
2081+
func codeActionChanges(env *Env, uri protocol.DocumentURI, rng protocol.Range, actionKind string, diag *protocol.Diagnostic, titles []string) ([]protocol.DocumentChanges, error) {
20732082
// Request all code actions that apply to the diagnostic.
20742083
// (The protocol supports filtering using Context.Only={actionKind}
20752084
// but we can give a better error if we don't filter.)
@@ -2093,14 +2102,23 @@ func codeActionChanges(env *Env, uri protocol.DocumentURI, rng protocol.Range, a
20932102
var candidates []protocol.CodeAction
20942103
for _, act := range actions {
20952104
if act.Kind == protocol.CodeActionKind(actionKind) {
2096-
candidates = append(candidates, act)
2105+
if len(titles) > 0 {
2106+
for _, f := range titles {
2107+
if act.Title == f {
2108+
candidates = append(candidates, act)
2109+
break
2110+
}
2111+
}
2112+
} else {
2113+
candidates = append(candidates, act)
2114+
}
20972115
}
20982116
}
20992117
if len(candidates) != 1 {
21002118
for _, act := range actions {
21012119
env.T.Logf("found CodeAction Kind=%s Title=%q", act.Kind, act.Title)
21022120
}
2103-
return nil, fmt.Errorf("found %d CodeActions of kind %s for this diagnostic, want 1", len(candidates), actionKind)
2121+
return nil, fmt.Errorf("found %d CodeActions of kind %s matching filters %v for this diagnostic, want 1", len(candidates), actionKind, titles)
21042122
}
21052123
action := candidates[0]
21062124

gopls/internal/lsp/testdata/extract/extract_method/extract_basic.go

-24
This file was deleted.

0 commit comments

Comments
 (0)