@@ -10,6 +10,7 @@ import (
10
10
"fmt"
11
11
12
12
"golang.org/x/tools/gopls/internal/lsp/protocol"
13
+ "golang.org/x/tools/gopls/internal/util/slices"
13
14
"golang.org/x/tools/internal/tool"
14
15
)
15
16
@@ -148,42 +149,22 @@ func (s *suggestedFix) Run(ctx context.Context, args ...string) error {
148
149
continue
149
150
}
150
151
151
- // Partially apply CodeAction.Edit, a WorkspaceEdit.
152
- // (See also conn.Client.applyWorkspaceEdit(a.Edit)).
153
- if ! from .HasPosition () {
154
- for _ , c := range a .Edit .DocumentChanges {
155
- if c .TextDocumentEdit != nil {
156
- if c .TextDocumentEdit .TextDocument .URI == uri {
157
- edits = append (edits , protocol .AsTextEdits (c .TextDocumentEdit .Edits )... )
158
- }
159
- }
160
- }
152
+ // If the provided span has a position (not just offsets),
153
+ // and the action has diagnostics, the action must have a
154
+ // diagnostic with the same range as it.
155
+ if from .HasPosition () && len (a .Diagnostics ) > 0 &&
156
+ ! slices .ContainsFunc (a .Diagnostics , func (diag protocol.Diagnostic ) bool {
157
+ return diag .Range .Start == rng .Start
158
+ }) {
161
159
continue
162
160
}
163
161
164
- // The provided span has a position (not just offsets).
165
- // Find the code action that has the same range as it.
166
- for _ , diag := range a .Diagnostics {
167
- if diag .Range .Start == rng .Start {
168
- for _ , c := range a .Edit .DocumentChanges {
169
- if c .TextDocumentEdit != nil {
170
- if c .TextDocumentEdit .TextDocument .URI == uri {
171
- edits = append (edits , protocol .AsTextEdits (c .TextDocumentEdit .Edits )... )
172
- }
173
- }
174
- }
175
- break
176
- }
177
- }
178
-
179
- // If suggested fix is not a diagnostic, still must collect edits.
180
- if len (a .Diagnostics ) == 0 {
181
- for _ , c := range a .Edit .DocumentChanges {
182
- if c .TextDocumentEdit != nil {
183
- if c .TextDocumentEdit .TextDocument .URI == uri {
184
- edits = append (edits , protocol .AsTextEdits (c .TextDocumentEdit .Edits )... )
185
- }
186
- }
162
+ // Partially apply CodeAction.Edit, a WorkspaceEdit.
163
+ // (See also conn.Client.applyWorkspaceEdit(a.Edit)).
164
+ for _ , c := range a .Edit .DocumentChanges {
165
+ tde := c .TextDocumentEdit
166
+ if tde != nil && tde .TextDocument .URI == uri {
167
+ edits = append (edits , protocol .AsTextEdits (tde .Edits )... )
187
168
}
188
169
}
189
170
}
0 commit comments