| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package server |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"slices" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"golang.org/x/tools/gopls/internal/cache" |
| 14 |
"golang.org/x/tools/gopls/internal/file" |
| 15 |
"golang.org/x/tools/gopls/internal/golang" |
| 16 |
"golang.org/x/tools/gopls/internal/mod" |
| 17 |
"golang.org/x/tools/gopls/internal/protocol" |
| 18 |
"golang.org/x/tools/gopls/internal/protocol/command" |
| 19 |
"golang.org/x/tools/gopls/internal/settings" |
| 20 |
"golang.org/x/tools/internal/event" |
| 21 |
) |
| 22 |
|
| 23 |
func (s *server) CodeAction(ctx context.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) { |
| 24 |
ctx, done := event.Start(ctx, "server.CodeAction") |
| 25 |
defer done() |
| 26 |
|
| 27 |
fh, snapshot, release, err := s.session.FileOf(ctx, params.TextDocument.URI) |
| 28 |
if err != nil { |
| 29 |
return nil, err |
| 30 |
} |
| 31 |
defer release() |
| 32 |
uri := fh.URI() |
| 33 |
kind := snapshot.FileKind(fh) |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
ny |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
codeActionKinds := make(map[protocol.CodeActionKind]bool) |
| 67 |
if len(params.Context.Only) > 0 { |
| 68 |
for _, kind := range params.Context.Only { |
| 69 |
codeActionKinds[kind] = true |
| 70 |
} |
| 71 |
} else { |
| 72 |
|
| 73 |
|
| 74 |
if triggerKind(params) == protocol.CodeActionAutomatic { |
| 75 |
|
| 76 |
codeActionKinds[protocol.QuickFix] = true |
| 77 |
} else { |
| 78 |
|
| 79 |
|
| 80 |
codeActionKinds[protocol.Empty] = true |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
enabled := func(kind protocol.CodeActionKind) bool { |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
for { |
| 93 |
if v, ok := codeActionKinds[kind]; ok { |
| 94 |
return v |
| 95 |
} |
| 96 |
if kind == "" { |
| 97 |
return false |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
if kind == settings.GoTest { |
| 118 |
return false |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
if dot := strings.LastIndexByte(string(kind), '.'); dot >= 0 { |
| 123 |
kind = kind[:dot] |
| 124 |
} else { |
| 125 |
kind = "" |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
switch kind { |
| 131 |
case file.Mod: |
| 132 |
var actions []protocol.CodeAction |
| 133 |
|
| 134 |
fixes, err := s.codeActionsMatchingDiagnostics(ctx, fh.URI(), snapshot, params.Context.Diagnostics, enabled) |
| 135 |
if err != nil { |
| 136 |
return nil, err |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
vulnFixes := make(map[protocol.Range][]protocol.CodeAction) |
| 145 |
searchFixes: |
| 146 |
for _, fix := range fixes { |
| 147 |
for _, diag := range fix.Diagnostics { |
| 148 |
if diag.Source == string(cache.Govulncheck) || diag.Source == string(cache.Vulncheck) { |
| 149 |
vulnFixes[diag.Range] = append(vulnFixes[diag.Range], fix) |
| 150 |
continue searchFixes |
| 151 |
} |
| 152 |
} |
| 153 |
actions = append(actions, fix) |
| 154 |
} |
| 155 |
|
| 156 |
for _, fixes := range vulnFixes { |
| 157 |
fixes = mod.SelectUpgradeCodeActions(fixes) |
| 158 |
actions = append(actions, fixes...) |
| 159 |
} |
| 160 |
|
| 161 |
return actions, nil |
| 162 |
|
| 163 |
case file.Go: |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
actions, err := s.codeActionsMatchingDiagnostics(ctx, uri, snapshot, params.Context.Diagnostics, enabled) |
| 172 |
if err != nil { |
| 173 |
return nil, err |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
moreActions, err := golang.CodeActions(ctx, snapshot, fh, params.Range, params.Context.Diagnostics, enabled, triggerKind(params)) |
| 178 |
if err != nil { |
| 179 |
return nil, err |
| 180 |
} |
| 181 |
actions = append(actions, moreActions...) |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
if golang.IsGenerated(ctx, snapshot, uri) { |
| 194 |
actions = slices.DeleteFunc(actions, func(a protocol.CodeAction) bool { |
| 195 |
switch a.Kind { |
| 196 |
case settings.GoTest, |
| 197 |
settings.GoDoc, |
| 198 |
settings.GoFreeSymbols, |
| 199 |
settings.GoSplitPackage, |
| 200 |
settings.GoAssembly, |
| 201 |
settings.GoplsDocFeatures, |
| 202 |
settings.GoToggleCompilerOptDetails: |
| 203 |
return false |
| 204 |
case settings.OrganizeImports: |
| 205 |
return false |
| 206 |
} |
| 207 |
return true |
| 208 |
}) |
| 209 |
} |
| 210 |
|
| 211 |
return actions, nil |
| 212 |
|
| 213 |
default: |
| 214 |
|
| 215 |
return nil, nil |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
func triggerKind(params *protocol.CodeActionParams) protocol.CodeActionTriggerKind { |
| 220 |
if kind := params.Context.TriggerKind; kind != nil { |
| 221 |
return *kind |
| 222 |
} |
| 223 |
return protocol.CodeActionUnknownTrigger |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
func (s *server) ResolveCodeAction(ctx context.Context, ca *protocol.CodeAction) (*protocol.CodeAction, error) { |
| 236 |
ctx, done := event.Start(ctx, "server.ResolveCodeAction") |
| 237 |
defer done() |
| 238 |
|
| 239 |
|
| 240 |
var cmd protocol.Command |
| 241 |
if ca.Data != nil { |
| 242 |
if err := protocol.UnmarshalJSON(*ca.Data, &cmd); err != nil { |
| 243 |
return nil, err |
| 244 |
} |
| 245 |
} |
| 246 |
ca.Data = nil |
| 247 |
if cmd.Command != "" { |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
params := &protocol.ExecuteCommandParams{ |
| 260 |
Command: cmd.Command, |
| 261 |
Arguments: cmd.Arguments, |
| 262 |
} |
| 263 |
handler := &commandHandler{ |
| 264 |
s: s, |
| 265 |
params: params, |
| 266 |
} |
| 267 |
edit, err := command.Dispatch(ctx, params, handler) |
| 268 |
if err != nil { |
| 269 |
|
| 270 |
ca.Command = &cmd |
| 271 |
return ca, nil |
| 272 |
} |
| 273 |
var ok bool |
| 274 |
if ca.Edit, ok = edit.(*protocol.WorkspaceEdit); !ok { |
| 275 |
return nil, fmt.Errorf("unable to resolve code action %q", ca.Title) |
| 276 |
} |
| 277 |
} |
| 278 |
return ca, nil |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
func (s *server) codeActionsMatchingDiagnostics(ctx context.Context, uri protocol.DocumentURI, snapshot *cache.Snapshot, pds []protocol.Diagnostic, enabled func(protocol.CodeActionKind) bool) ([]protocol.CodeAction, error) { |
| 287 |
var actions []protocol.CodeAction |
| 288 |
var unbundled []protocol.Diagnostic |
| 289 |
for _, pd := range pds { |
| 290 |
bundled, err := cache.BundledLazyFixes(pd) |
| 291 |
if err != nil { |
| 292 |
return nil, err |
| 293 |
} |
| 294 |
if len(bundled) > 0 { |
| 295 |
for _, fix := range bundled { |
| 296 |
if enabled(fix.Kind) { |
| 297 |
actions = append(actions, fix) |
| 298 |
} |
| 299 |
} |
| 300 |
} else { |
| 301 |
|
| 302 |
unbundled = append(unbundled, pd) |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
for _, pd := range unbundled { |
| 307 |
for _, sd := range s.findMatchingDiagnostics(uri, pd) { |
| 308 |
diagActions, err := codeActionsForDiagnostic(ctx, snapshot, sd, &pd, enabled) |
| 309 |
if err != nil { |
| 310 |
return nil, err |
| 311 |
} |
| 312 |
actions = append(actions, diagActions...) |
| 313 |
} |
| 314 |
} |
| 315 |
return actions, nil |
| 316 |
} |
| 317 |
|
| 318 |
func codeActionsForDiagnostic(ctx context.Context, snapshot *cache.Snapshot, sd *cache.Diagnostic, pd *protocol.Diagnostic, enabled func(protocol.CodeActionKind) bool) ([]protocol.CodeAction, error) { |
| 319 |
var actions []protocol.CodeAction |
| 320 |
for _, fix := range sd.SuggestedFixes { |
| 321 |
if !enabled(fix.ActionKind) { |
| 322 |
continue |
| 323 |
} |
| 324 |
var changes []protocol.DocumentChange |
| 325 |
for uri, edits := range fix.Edits { |
| 326 |
fh, err := snapshot.ReadFile(ctx, uri) |
| 327 |
if err != nil { |
| 328 |
return nil, err |
| 329 |
} |
| 330 |
change := protocol.DocumentChangeEdit(fh, edits) |
| 331 |
changes = append(changes, change) |
| 332 |
} |
| 333 |
actions = append(actions, protocol.CodeAction{ |
| 334 |
Title: fix.Title, |
| 335 |
Kind: fix.ActionKind, |
| 336 |
Edit: protocol.NewWorkspaceEdit(changes...), |
| 337 |
Command: fix.Command, |
| 338 |
Diagnostics: []protocol.Diagnostic{*pd}, |
| 339 |
}) |
| 340 |
} |
| 341 |
return actions, nil |
| 342 |
} |
| 343 |
|
| 344 |
func (s *server) findMatchingDiagnostics(uri protocol.DocumentURI, pd protocol.Diagnostic) []*cache.Diagnostic { |
| 345 |
s.diagnosticsMu.Lock() |
| 346 |
defer s.diagnosticsMu.Unlock() |
| 347 |
|
| 348 |
var sds []*cache.Diagnostic |
| 349 |
if fileDiags := s.diagnostics[uri]; fileDiags != nil { |
| 350 |
for _, viewDiags := range fileDiags.byView { |
| 351 |
for _, sd := range viewDiags.diagnostics { |
| 352 |
|
| 353 |
|
| 354 |
sameDiagnostic := pd.Message == strings.TrimSpace(sd.Message) && |
| 355 |
protocol.CompareRange(pd.Range, sd.Range) == 0 && |
| 356 |
pd.Source == string(sd.Source) |
| 357 |
|
| 358 |
if sameDiagnostic { |
| 359 |
sds = append(sds, sd) |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
return sds |
| 365 |
} |
| 366 |
|
| 367 |
func (s *server) getSupportedCodeActions() []protocol.CodeActionKind { |
| 368 |
allCodeActionKinds := make(map[protocol.CodeActionKind]struct{}) |
| 369 |
for _, kinds := range s.Options().SupportedCodeActions { |
| 370 |
for kind := range kinds { |
| 371 |
allCodeActionKinds[kind] = struct{}{} |
| 372 |
} |
| 373 |
} |
| 374 |
var result []protocol.CodeActionKind |
| 375 |
for kind := range allCodeActionKinds { |
| 376 |
result = append(result, kind) |
| 377 |
} |
| 378 |
slices.Sort(result) |
| 379 |
return result |
| 380 |
} |
| 381 |
|
| 382 |
type unit = struct{} |