@@ -153,15 +153,20 @@ var update = flag.Bool("update", false, "if set, update test data during marker
153
153
// completion candidate produced at the given location with provided label
154
154
// results in the given golden state.
155
155
//
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.
160
162
//
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.
165
170
//
166
171
// - codeactionerr(start, end, kind, wantError): specifies a codeaction that
167
172
// 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
381
386
// - Provide some means by which locations in the standard library
382
387
// (or builtin.go) can be named, so that, for example, we can we
383
388
// 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?
384
393
//
385
394
// Existing marker tests (in ../testdata) to port:
386
395
// - CallHierarchy
387
396
// - SemanticTokens
388
397
// - SuggestedFixes
389
- // - MethodExtractions
390
398
// - InlayHints
391
399
// - Renames
392
400
// - SelectionRanges
@@ -1924,13 +1932,13 @@ func applyDocumentChanges(env *Env, changes []protocol.DocumentChanges, fileChan
1924
1932
return nil
1925
1933
}
1926
1934
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 ) {
1928
1936
// Request the range from start.Start to end.End.
1929
1937
loc := start
1930
1938
loc .Range .End = end .Range .End
1931
1939
1932
1940
// 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 )
1934
1942
if err != nil {
1935
1943
mark .errorf ("codeAction failed: %v" , err )
1936
1944
return
@@ -1940,8 +1948,8 @@ func codeActionMarker(mark marker, start, end protocol.Location, actionKind stri
1940
1948
checkChangedFiles (mark , changed , g )
1941
1949
}
1942
1950
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 )
1945
1953
if err != nil {
1946
1954
mark .errorf ("codeAction failed: %v" , err )
1947
1955
return
@@ -1953,7 +1961,7 @@ func codeActionEditMarker(mark marker, loc protocol.Location, actionKind string,
1953
1961
func codeActionErrMarker (mark marker , start , end protocol.Location , actionKind string , wantErr wantError ) {
1954
1962
loc := start
1955
1963
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 )
1957
1965
wantErr .check (mark , err )
1958
1966
}
1959
1967
@@ -2037,7 +2045,7 @@ func suggestedfixMarker(mark marker, loc protocol.Location, re *regexp.Regexp, g
2037
2045
}
2038
2046
2039
2047
// 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 )
2041
2049
if err != nil {
2042
2050
mark .errorf ("suggestedfix failed: %v. (Use @suggestedfixerr for expected errors.)" , err )
2043
2051
return
@@ -2054,8 +2062,8 @@ func suggestedfixMarker(mark marker, loc protocol.Location, re *regexp.Regexp, g
2054
2062
// The resulting map contains resulting file contents after the code action is
2055
2063
// applied. Currently, this function does not support code actions that return
2056
2064
// 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 )
2059
2067
if err != nil {
2060
2068
return nil , err
2061
2069
}
@@ -2069,7 +2077,8 @@ func codeAction(env *Env, uri protocol.DocumentURI, rng protocol.Range, actionKi
2069
2077
// codeActionChanges executes a textDocument/codeAction request for the
2070
2078
// specified location and kind, and captures the resulting document changes.
2071
2079
// 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 ) {
2073
2082
// Request all code actions that apply to the diagnostic.
2074
2083
// (The protocol supports filtering using Context.Only={actionKind}
2075
2084
// 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
2093
2102
var candidates []protocol.CodeAction
2094
2103
for _ , act := range actions {
2095
2104
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
+ }
2097
2115
}
2098
2116
}
2099
2117
if len (candidates ) != 1 {
2100
2118
for _ , act := range actions {
2101
2119
env .T .Logf ("found CodeAction Kind=%s Title=%q" , act .Kind , act .Title )
2102
2120
}
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 )
2104
2122
}
2105
2123
action := candidates [0 ]
2106
2124
0 commit comments