| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package golang |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"fmt" |
| 19 |
"go/ast" |
| 20 |
"go/types" |
| 21 |
"sort" |
| 22 |
"strings" |
| 23 |
"sync" |
| 24 |
|
| 25 |
"golang.org/x/sync/errgroup" |
| 26 |
"golang.org/x/tools/go/ast/edge" |
| 27 |
"golang.org/x/tools/go/ast/inspector" |
| 28 |
"golang.org/x/tools/go/types/objectpath" |
| 29 |
"golang.org/x/tools/go/types/typeutil" |
| 30 |
"golang.org/x/tools/gopls/internal/cache" |
| 31 |
"golang.org/x/tools/gopls/internal/cache/metadata" |
| 32 |
"golang.org/x/tools/gopls/internal/cache/methodsets" |
| 33 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 34 |
"golang.org/x/tools/gopls/internal/file" |
| 35 |
"golang.org/x/tools/gopls/internal/protocol" |
| 36 |
"golang.org/x/tools/gopls/internal/util/cursorutil" |
| 37 |
"golang.org/x/tools/gopls/internal/util/safetoken" |
| 38 |
|
| 39 |
"golang.org/x/tools/internal/event" |
| 40 |
) |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
func References(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, rng protocol.Range, includeDeclaration bool) ([]protocol.Location, error) { |
| 46 |
references, err := references(ctx, snapshot, fh, rng, includeDeclaration) |
| 47 |
if err != nil { |
| 48 |
return nil, err |
| 49 |
} |
| 50 |
locations := make([]protocol.Location, len(references)) |
| 51 |
for i, ref := range references { |
| 52 |
locations[i] = ref.location |
| 53 |
} |
| 54 |
return locations, nil |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
type reference struct { |
| 60 |
isDeclaration bool |
| 61 |
location protocol.Location |
| 62 |
pkgPath PackagePath |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
func references(ctx context.Context, snapshot *cache.Snapshot, f file.Handle, rng protocol.Range, includeDeclaration bool) ([]reference, error) { |
| 69 |
ctx, done := event.Start(ctx, "golang.references") |
| 70 |
defer done() |
| 71 |
|
| 72 |
|
| 73 |
_, inPackageName, err := parsePackageNameDecl(ctx, snapshot, f, rng) |
| 74 |
if err != nil { |
| 75 |
return nil, err |
| 76 |
} |
| 77 |
|
| 78 |
var refs []reference |
| 79 |
if inPackageName { |
| 80 |
refs, err = packageReferences(ctx, snapshot, f.URI()) |
| 81 |
} else { |
| 82 |
refs, err = ordinaryReferences(ctx, snapshot, f.URI(), rng) |
| 83 |
} |
| 84 |
if err != nil { |
| 85 |
return nil, err |
| 86 |
} |
| 87 |
|
| 88 |
sort.Slice(refs, func(i, j int) bool { |
| 89 |
x, y := refs[i], refs[j] |
| 90 |
if x.isDeclaration != y.isDeclaration { |
| 91 |
return x.isDeclaration |
| 92 |
} |
| 93 |
return protocol.CompareLocation(x.location, y.location) < 0 |
| 94 |
}) |
| 95 |
|
| 96 |
|
| 97 |
out := refs[:0] |
| 98 |
for _, ref := range refs { |
| 99 |
if !includeDeclaration && ref.isDeclaration { |
| 100 |
continue |
| 101 |
} |
| 102 |
if len(out) == 0 || out[len(out)-1].location != ref.location { |
| 103 |
out = append(out, ref) |
| 104 |
} |
| 105 |
} |
| 106 |
refs = out |
| 107 |
|
| 108 |
return refs, nil |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
func packageReferences(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI) ([]reference, error) { |
| 116 |
metas, err := snapshot.MetadataForFile(ctx, uri, false) |
| 117 |
if err != nil { |
| 118 |
return nil, err |
| 119 |
} |
| 120 |
if len(metas) == 0 { |
| 121 |
return nil, fmt.Errorf("found no package containing %s", uri) |
| 122 |
} |
| 123 |
|
| 124 |
var refs []reference |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
narrowest := metas[0] |
| 137 |
if narrowest.ForTest != "" && strings.HasSuffix(string(uri), "_test.go") { |
| 138 |
for _, f := range narrowest.CompiledGoFiles { |
| 139 |
if !strings.HasSuffix(string(f), "_test.go") { |
| 140 |
return packageReferences(ctx, snapshot, f) |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
} else { |
| 147 |
rdeps, err := snapshot.ReverseDependencies(ctx, narrowest.ID, false) |
| 148 |
if err != nil { |
| 149 |
return nil, err |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
workspace, err := snapshot.WorkspaceMetadata(ctx) |
| 154 |
if err != nil { |
| 155 |
return nil, err |
| 156 |
} |
| 157 |
workspaceMap := make(map[PackageID]*metadata.Package, len(workspace)) |
| 158 |
for _, mp := range workspace { |
| 159 |
workspaceMap[mp.ID] = mp |
| 160 |
} |
| 161 |
|
| 162 |
for _, rdep := range rdeps { |
| 163 |
if _, ok := workspaceMap[rdep.ID]; !ok { |
| 164 |
continue |
| 165 |
} |
| 166 |
for _, uri := range rdep.CompiledGoFiles { |
| 167 |
fh, err := snapshot.ReadFile(ctx, uri) |
| 168 |
if err != nil { |
| 169 |
return nil, err |
| 170 |
} |
| 171 |
f, err := snapshot.ParseGo(ctx, fh, parsego.Header) |
| 172 |
if err != nil { |
| 173 |
return nil, err |
| 174 |
} |
| 175 |
for _, imp := range f.File.Imports { |
| 176 |
if rdep.DepsByImpPath[metadata.UnquoteImportPath(imp)] == narrowest.ID { |
| 177 |
refs = append(refs, reference{ |
| 178 |
isDeclaration: false, |
| 179 |
location: mustLocation(f, imp), |
| 180 |
pkgPath: narrowest.PkgPath, |
| 181 |
}) |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
widest := metas[len(metas)-1] |
| 195 |
for _, uri := range widest.CompiledGoFiles { |
| 196 |
fh, err := snapshot.ReadFile(ctx, uri) |
| 197 |
if err != nil { |
| 198 |
return nil, err |
| 199 |
} |
| 200 |
f, err := snapshot.ParseGo(ctx, fh, parsego.Header) |
| 201 |
if err != nil { |
| 202 |
return nil, err |
| 203 |
} |
| 204 |
|
| 205 |
if f.File.Name.Pos().IsValid() { |
| 206 |
refs = append(refs, reference{ |
| 207 |
isDeclaration: true, |
| 208 |
location: mustLocation(f, f.File.Name), |
| 209 |
pkgPath: widest.PkgPath, |
| 210 |
}) |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
return refs, nil |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
func ordinaryReferences(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI, rng protocol.Range) ([]reference, error) { |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
pkg, pgf, err := NarrowestPackageForFile(ctx, snapshot, uri) |
| 231 |
if err != nil { |
| 232 |
return nil, err |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
start, end, err := pgf.RangePos(rng) |
| 238 |
if err != nil { |
| 239 |
return nil, err |
| 240 |
} |
| 241 |
cur, _ := pgf.Cursor().FindByPos(start, end) |
| 242 |
|
| 243 |
candidates, err := objectsAt(pkg.TypesInfo(), cur) |
| 244 |
if err != nil { |
| 245 |
return nil, err |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
obj := candidates[0].obj |
| 251 |
|
| 252 |
|
| 253 |
if isBuiltin(obj) { |
| 254 |
return nil, fmt.Errorf("references to builtin %q are not supported", obj.Name()) |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
declPosn := safetoken.StartPosition(pkg.FileSet(), obj.Pos()) |
| 260 |
declURI := protocol.URIFromPath(declPosn.Filename) |
| 261 |
variants, err := snapshot.MetadataForFile(ctx, declURI, false) |
| 262 |
if err != nil { |
| 263 |
return nil, err |
| 264 |
} |
| 265 |
if len(variants) == 0 { |
| 266 |
return nil, fmt.Errorf("no packages for file %q", declURI) |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
var ( |
| 273 |
globalScope = make(map[PackageID]*metadata.Package) |
| 274 |
globalTargets map[PackagePath]map[objectpath.Path]unit |
| 275 |
expansions = make(map[PackageID]unit) |
| 276 |
) |
| 277 |
|
| 278 |
|
| 279 |
if path, err := objectpath.For(obj); err == nil && obj.Exported() { |
| 280 |
pkgPath := variants[0].PkgPath |
| 281 |
globalTargets = map[PackagePath]map[objectpath.Path]unit{ |
| 282 |
pkgPath: {path: {}}, |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
workspace, err := snapshot.WorkspaceMetadata(ctx) |
| 288 |
if err != nil { |
| 289 |
return nil, err |
| 290 |
} |
| 291 |
workspaceMap := make(map[PackageID]*metadata.Package, len(workspace)) |
| 292 |
workspaceIDs := make([]PackageID, 0, len(workspace)) |
| 293 |
for _, mp := range workspace { |
| 294 |
workspaceMap[mp.ID] = mp |
| 295 |
workspaceIDs = append(workspaceIDs, mp.ID) |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
addRdeps := func(id PackageID, transitive bool) error { |
| 301 |
rdeps, err := snapshot.ReverseDependencies(ctx, id, transitive) |
| 302 |
if err != nil { |
| 303 |
return err |
| 304 |
} |
| 305 |
for rdepID, rdep := range rdeps { |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
if _, ok := workspaceMap[rdepID]; !ok { |
| 318 |
continue |
| 319 |
} |
| 320 |
|
| 321 |
globalScope[rdepID] = rdep |
| 322 |
} |
| 323 |
return nil |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
transitive := obj.Pkg().Scope().Lookup(obj.Name()) != obj |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
for _, mp := range variants { |
| 335 |
if err := addRdeps(mp.ID, transitive); err != nil { |
| 336 |
return nil, err |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
if recv := effectiveReceiver(obj); recv != nil { |
| 350 |
if err := expandMethodSearch(ctx, snapshot, workspaceIDs, obj.(*types.Func), recv, addRdeps, globalTargets, expansions); err != nil { |
| 351 |
return nil, err |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
var ( |
| 358 |
refsMu sync.Mutex |
| 359 |
refs []reference |
| 360 |
) |
| 361 |
report := func(loc protocol.Location, isDecl bool) { |
| 362 |
ref := reference{ |
| 363 |
isDeclaration: isDecl, |
| 364 |
location: loc, |
| 365 |
pkgPath: pkg.Metadata().PkgPath, |
| 366 |
} |
| 367 |
refsMu.Lock() |
| 368 |
refs = append(refs, ref) |
| 369 |
refsMu.Unlock() |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
var group errgroup.Group |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
for _, mp := range variants { |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
if mp.IsIntermediateTestVariant() { |
| 396 |
continue |
| 397 |
} |
| 398 |
mp := mp |
| 399 |
group.Go(func() error { |
| 400 |
|
| 401 |
pkgs, err := snapshot.TypeCheck(ctx, mp.ID) |
| 402 |
if err != nil { |
| 403 |
return err |
| 404 |
} |
| 405 |
pkg := pkgs[0] |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
pgf, err := pkg.File(declURI) |
| 410 |
if err != nil { |
| 411 |
return err |
| 412 |
} |
| 413 |
pos, err := safetoken.Pos(pgf.Tok, declPosn.Offset) |
| 414 |
if err != nil { |
| 415 |
return err |
| 416 |
} |
| 417 |
cur, _ := pgf.Cursor().FindByPos(pos, pos) |
| 418 |
|
| 419 |
objects, err := objectsAt(pkg.TypesInfo(), cur) |
| 420 |
if err != nil { |
| 421 |
return err |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
for _, o := range objects { |
| 427 |
report(mustLocation(pgf, o.cur.Node()), true) |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
targets := make(map[types.Object]bool) |
| 432 |
for _, o := range objects { |
| 433 |
targets[o.obj] = true |
| 434 |
} |
| 435 |
|
| 436 |
return localReferences(pkg, targets, true, report) |
| 437 |
}) |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
for id := range expansions { |
| 444 |
group.Go(func() error { |
| 445 |
|
| 446 |
pkgs, err := snapshot.TypeCheck(ctx, id) |
| 447 |
if err != nil { |
| 448 |
return err |
| 449 |
} |
| 450 |
pkg := pkgs[0] |
| 451 |
|
| 452 |
targets := make(map[types.Object]bool) |
| 453 |
for objpath := range globalTargets[pkg.Metadata().PkgPath] { |
| 454 |
obj, err := objectpath.Object(pkg.Types(), objpath) |
| 455 |
if err != nil { |
| 456 |
|
| 457 |
|
| 458 |
continue |
| 459 |
} |
| 460 |
targets[obj] = true |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
const correspond = false |
| 467 |
return localReferences(pkg, targets, correspond, report) |
| 468 |
}) |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
group.Go(func() error { |
| 473 |
var globalIDs []PackageID |
| 474 |
for id := range globalScope { |
| 475 |
globalIDs = append(globalIDs, id) |
| 476 |
} |
| 477 |
indexes, err := snapshot.References(ctx, globalIDs...) |
| 478 |
if err != nil { |
| 479 |
return err |
| 480 |
} |
| 481 |
for _, index := range indexes { |
| 482 |
for _, loc := range index.Lookup(globalTargets) { |
| 483 |
report(loc, false) |
| 484 |
} |
| 485 |
} |
| 486 |
return nil |
| 487 |
}) |
| 488 |
|
| 489 |
if err := group.Wait(); err != nil { |
| 490 |
return nil, err |
| 491 |
} |
| 492 |
return refs, nil |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
func expandMethodSearch(ctx context.Context, snapshot *cache.Snapshot, workspaceIDs []PackageID, method *types.Func, recv types.Type, addRdeps func(id PackageID, transitive bool) error, targets map[PackagePath]map[objectpath.Path]unit, expansions map[PackageID]unit) error { |
| 507 |
|
| 508 |
key, hasMethods := methodsets.KeyOf(recv) |
| 509 |
if !hasMethods { |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
return nil |
| 514 |
} |
| 515 |
|
| 516 |
indexes, err := snapshot.MethodSets(ctx, workspaceIDs...) |
| 517 |
if err != nil { |
| 518 |
return err |
| 519 |
} |
| 520 |
var mu sync.Mutex |
| 521 |
var group errgroup.Group |
| 522 |
for i, index := range indexes { |
| 523 |
group.Go(func() error { |
| 524 |
|
| 525 |
const want = methodsets.Supertype | methodsets.Subtype |
| 526 |
results := index.Search(key, want, method) |
| 527 |
if len(results) == 0 { |
| 528 |
return nil |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
id := workspaceIDs[i] |
| 533 |
|
| 534 |
mu.Lock() |
| 535 |
defer mu.Unlock() |
| 536 |
|
| 537 |
|
| 538 |
if err := addRdeps(id, true); err != nil { |
| 539 |
return err |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
expansions[id] = unit{} |
| 545 |
|
| 546 |
|
| 547 |
for _, res := range results { |
| 548 |
methodPkg := PackagePath(res.PkgPath) |
| 549 |
opaths, ok := targets[methodPkg] |
| 550 |
if !ok { |
| 551 |
opaths = make(map[objectpath.Path]unit) |
| 552 |
targets[methodPkg] = opaths |
| 553 |
} |
| 554 |
opaths[res.ObjectPath] = unit{} |
| 555 |
} |
| 556 |
return nil |
| 557 |
}) |
| 558 |
} |
| 559 |
return group.Wait() |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
func localReferences(pkg *cache.Package, targets map[types.Object]bool, correspond bool, report func(loc protocol.Location, isDecl bool)) error { |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
var methodRecvs []types.Type |
| 571 |
var methodName string |
| 572 |
if correspond { |
| 573 |
for obj := range targets { |
| 574 |
if t := effectiveReceiver(obj); t != nil { |
| 575 |
methodRecvs = append(methodRecvs, t) |
| 576 |
methodName = obj.Name() |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
var msets typeutil.MethodSetCache |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
matches := func(obj types.Object) bool { |
| 586 |
if containsOrigin(targets, obj) { |
| 587 |
return true |
| 588 |
} |
| 589 |
if methodRecvs != nil && obj.Name() == methodName { |
| 590 |
if orecv := effectiveReceiver(obj); orecv != nil { |
| 591 |
for _, mrecv := range methodRecvs { |
| 592 |
if implements(&msets, orecv, mrecv) || |
| 593 |
implements(&msets, mrecv, orecv) { |
| 594 |
return true |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
return false |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
targetNames := make(map[string]struct{}, len(targets)) |
| 605 |
for obj := range targets { |
| 606 |
targetNames[obj.Name()] = struct{}{} |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
for _, pgf := range pkg.CompiledGoFiles() { |
| 611 |
for curId := range pgf.Cursor().Preorder((*ast.Ident)(nil)) { |
| 612 |
id := curId.Node().(*ast.Ident) |
| 613 |
if _, ok := targetNames[id.Name]; !ok { |
| 614 |
continue |
| 615 |
} |
| 616 |
if obj, ok := pkg.TypesInfo().Uses[id]; ok && matches(obj) { |
| 617 |
report(mustLocation(pgf, id), false) |
| 618 |
} |
| 619 |
} |
| 620 |
} |
| 621 |
return nil |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
func effectiveReceiver(obj types.Object) types.Type { |
| 627 |
if fn, ok := obj.(*types.Func); ok { |
| 628 |
if recv := fn.Signature().Recv(); recv != nil { |
| 629 |
return methodsets.EnsurePointer(recv.Type()) |
| 630 |
} |
| 631 |
} |
| 632 |
return nil |
| 633 |
} |
| 634 |
|
| 635 |
type objectAt struct { |
| 636 |
obj types.Object |
| 637 |
cur inspector.Cursor |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
func objectsAt(info *types.Info, cur inspector.Cursor) ([]objectAt, error) { |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
if cur.ParentEdgeKind() == edge.ImportSpec_Path { |
| 658 |
cur = cur.Parent() |
| 659 |
spec := cur.Node().(*ast.ImportSpec) |
| 660 |
pkgname := info.PkgNameOf(spec) |
| 661 |
if pkgname == nil { |
| 662 |
return nil, fmt.Errorf("%w for import %s", errNoObjectFound, metadata.UnquoteImportPath(spec)) |
| 663 |
} |
| 664 |
if spec.Name != nil { |
| 665 |
cur = cur.ChildAt(edge.ImportSpec_Name, -1) |
| 666 |
} |
| 667 |
return []objectAt{{pkgname, cur}}, nil |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
if is[*ast.StarExpr](cur.Node()) { |
| 675 |
cur, _ = cur.FirstChild() |
| 676 |
} |
| 677 |
|
| 678 |
id, ok := cur.Node().(*ast.Ident) |
| 679 |
if !ok { |
| 680 |
return nil, ErrNoIdentFound |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
objects, _ := typeSwitchVars(info, cur) |
| 687 |
if len(objects) > 0 { |
| 688 |
return objects, nil |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
obj := info.ObjectOf(id) |
| 694 |
if obj == nil { |
| 695 |
return nil, fmt.Errorf("%w for %q", errNoObjectFound, id.Name) |
| 696 |
} |
| 697 |
return []objectAt{{obj, cur}}, nil |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
func typeSwitchVars(info *types.Info, curIdent inspector.Cursor) ([]objectAt, types.Type) { |
| 722 |
sw, curSwitch := cursorutil.FirstEnclosing[*ast.TypeSwitchStmt](curIdent) |
| 723 |
if sw == nil { |
| 724 |
return nil, nil |
| 725 |
} |
| 726 |
assign, ok := sw.Assign.(*ast.AssignStmt) |
| 727 |
if !(ok && |
| 728 |
len(assign.Lhs) == 1 && |
| 729 |
is[*ast.Ident](assign.Lhs[0]) && |
| 730 |
len(assign.Rhs) == 1 && |
| 731 |
is[*ast.TypeAssertExpr](assign.Rhs[0])) { |
| 732 |
return nil, nil |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
id := curIdent.Node().(*ast.Ident) |
| 737 |
|
| 738 |
match := false |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
var t types.Type |
| 743 |
if id == assign.Lhs[0] { |
| 744 |
match = true |
| 745 |
t = info.TypeOf(assign.Rhs[0].(*ast.TypeAssertExpr).X) |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
var objects []objectAt |
| 750 |
for curCase := range curSwitch.ChildAt(edge.TypeSwitchStmt_Body, -1).Children() { |
| 751 |
clause := curCase.Node().(*ast.CaseClause) |
| 752 |
v, ok := info.Implicits[clause] |
| 753 |
if ok { |
| 754 |
if v == info.Uses[id] { |
| 755 |
|
| 756 |
t = v.Type() |
| 757 |
match = true |
| 758 |
} |
| 759 |
objects = append(objects, objectAt{v, curIdent}) |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
if !match { |
| 764 |
|
| 765 |
return nil, nil |
| 766 |
} |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
return objects, t |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
func mustLocation(pgf *parsego.File, n ast.Node) protocol.Location { |
| 779 |
loc, err := pgf.NodeLocation(n) |
| 780 |
if err != nil { |
| 781 |
panic(err) |
| 782 |
} |
| 783 |
return loc |
| 784 |
} |