@@ -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,
19531961func 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
0 commit comments