golang.org/x/tools/gopls@v0.15.3/internal/golang/completion/completion.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package completion provides core functionality for code completion in Go 6 // editors and tools. 7 package completion 8 9 import ( 10 "context" 11 "fmt" 12 "go/ast" 13 "go/constant" 14 "go/parser" 15 "go/printer" 16 "go/scanner" 17 "go/token" 18 "go/types" 19 "math" 20 "sort" 21 "strconv" 22 "strings" 23 "sync" 24 "sync/atomic" 25 "time" 26 "unicode" 27 28 "golang.org/x/sync/errgroup" 29 "golang.org/x/tools/go/ast/astutil" 30 "golang.org/x/tools/gopls/internal/cache" 31 "golang.org/x/tools/gopls/internal/cache/metadata" 32 "golang.org/x/tools/gopls/internal/file" 33 "golang.org/x/tools/gopls/internal/golang" 34 "golang.org/x/tools/gopls/internal/golang/completion/snippet" 35 "golang.org/x/tools/gopls/internal/protocol" 36 "golang.org/x/tools/gopls/internal/settings" 37 goplsastutil "golang.org/x/tools/gopls/internal/util/astutil" 38 "golang.org/x/tools/gopls/internal/util/safetoken" 39 "golang.org/x/tools/gopls/internal/util/typesutil" 40 "golang.org/x/tools/internal/event" 41 "golang.org/x/tools/internal/fuzzy" 42 "golang.org/x/tools/internal/imports" 43 "golang.org/x/tools/internal/typeparams" 44 ) 45 46 // A CompletionItem represents a possible completion suggested by the algorithm. 47 type CompletionItem struct { 48 49 // Invariant: CompletionItem does not refer to syntax or types. 50 51 // Label is the primary text the user sees for this completion item. 52 Label string 53 54 // Detail is supplemental information to present to the user. 55 // This often contains the type or return type of the completion item. 56 Detail string 57 58 // InsertText is the text to insert if this item is selected. 59 // Any of the prefix that has already been typed is not trimmed. 60 // The insert text does not contain snippets. 61 InsertText string 62 63 Kind protocol.CompletionItemKind 64 Tags []protocol.CompletionItemTag 65 Deprecated bool // Deprecated, prefer Tags if available 66 67 // An optional array of additional TextEdits that are applied when 68 // selecting this completion. 69 // 70 // Additional text edits should be used to change text unrelated to the current cursor position 71 // (for example adding an import statement at the top of the file if the completion item will 72 // insert an unqualified type). 73 AdditionalTextEdits []protocol.TextEdit 74 75 // Depth is how many levels were searched to find this completion. 76 // For example when completing "foo<>", "fooBar" is depth 0, and 77 // "fooBar.Baz" is depth 1. 78 Depth int 79 80 // Score is the internal relevance score. 81 // A higher score indicates that this completion item is more relevant. 82 Score float64 83 84 // snippet is the LSP snippet for the completion item. The LSP 85 // specification contains details about LSP snippets. For example, a 86 // snippet for a function with the following signature: 87 // 88 // func foo(a, b, c int) 89 // 90 // would be: 91 // 92 // foo(${1:a int}, ${2: b int}, ${3: c int}) 93 // 94 // If Placeholders is false in the CompletionOptions, the above 95 // snippet would instead be: 96 // 97 // foo(${1:}) 98 snippet *snippet.Builder 99 100 // Documentation is the documentation for the completion item. 101 Documentation string 102 103 // isSlice reports whether the underlying type of the object 104 // from which this candidate was derived is a slice. 105 // (Used to complete append() calls.) 106 isSlice bool 107 } 108 109 // completionOptions holds completion specific configuration. 110 type completionOptions struct { 111 unimported bool 112 documentation bool 113 fullDocumentation bool 114 placeholders bool 115 snippets bool 116 postfix bool 117 matcher settings.Matcher 118 budget time.Duration 119 completeFunctionCalls bool 120 } 121 122 // Snippet is a convenience returns the snippet if available, otherwise 123 // the InsertText. 124 // used for an item, depending on if the callee wants placeholders or not. 125 func (i *CompletionItem) Snippet() string { 126 if i.snippet != nil { 127 return i.snippet.String() 128 } 129 return i.InsertText 130 } 131 132 // Scoring constants are used for weighting the relevance of different candidates. 133 const ( 134 // stdScore is the base score for all completion items. 135 stdScore float64 = 1.0 136 137 // highScore indicates a very relevant completion item. 138 highScore float64 = 10.0 139 140 // lowScore indicates an irrelevant or not useful completion item. 141 lowScore float64 = 0.01 142 ) 143 144 // matcher matches a candidate's label against the user input. The 145 // returned score reflects the quality of the match. A score of zero 146 // indicates no match, and a score of one means a perfect match. 147 type matcher interface { 148 Score(candidateLabel string) (score float32) 149 } 150 151 // prefixMatcher implements case sensitive prefix matching. 152 type prefixMatcher string 153 154 func (pm prefixMatcher) Score(candidateLabel string) float32 { 155 if strings.HasPrefix(candidateLabel, string(pm)) { 156 return 1 157 } 158 return -1 159 } 160 161 // insensitivePrefixMatcher implements case insensitive prefix matching. 162 type insensitivePrefixMatcher string 163 164 func (ipm insensitivePrefixMatcher) Score(candidateLabel string) float32 { 165 if strings.HasPrefix(strings.ToLower(candidateLabel), string(ipm)) { 166 return 1 167 } 168 return -1 169 } 170 171 // completer contains the necessary information for a single completion request. 172 type completer struct { 173 snapshot *cache.Snapshot 174 pkg *cache.Package 175 qf types.Qualifier // for qualifying typed expressions 176 mq golang.MetadataQualifier // for syntactic qualifying 177 opts *completionOptions 178 179 // completionContext contains information about the trigger for this 180 // completion request. 181 completionContext completionContext 182 183 // fh is a handle to the file associated with this completion request. 184 fh file.Handle 185 186 // filename is the name of the file associated with this completion request. 187 filename string 188 189 // file is the AST of the file associated with this completion request. 190 file *ast.File 191 192 // (tokFile, pos) is the position at which the request was triggered. 193 tokFile *token.File 194 pos token.Pos 195 196 // path is the path of AST nodes enclosing the position. 197 path []ast.Node 198 199 // seen is the map that ensures we do not return duplicate results. 200 seen map[types.Object]bool 201 202 // items is the list of completion items returned. 203 items []CompletionItem 204 205 // completionCallbacks is a list of callbacks to collect completions that 206 // require expensive operations. This includes operations where we search 207 // through the entire module cache. 208 completionCallbacks []func(context.Context, *imports.Options) error 209 210 // surrounding describes the identifier surrounding the position. 211 surrounding *Selection 212 213 // inference contains information we've inferred about ideal 214 // candidates such as the candidate's type. 215 inference candidateInference 216 217 // enclosingFunc contains information about the function enclosing 218 // the position. 219 enclosingFunc *funcInfo 220 221 // enclosingCompositeLiteral contains information about the composite literal 222 // enclosing the position. 223 enclosingCompositeLiteral *compLitInfo 224 225 // deepState contains the current state of our deep completion search. 226 deepState deepCompletionState 227 228 // matcher matches the candidates against the surrounding prefix. 229 matcher matcher 230 231 // methodSetCache caches the types.NewMethodSet call, which is relatively 232 // expensive and can be called many times for the same type while searching 233 // for deep completions. 234 methodSetCache map[methodSetKey]*types.MethodSet 235 236 // mapper converts the positions in the file from which the completion originated. 237 mapper *protocol.Mapper 238 239 // startTime is when we started processing this completion request. It does 240 // not include any time the request spent in the queue. 241 // 242 // Note: in CL 503016, startTime move to *after* type checking, but it was 243 // subsequently determined that it was better to keep setting it *before* 244 // type checking, so that the completion budget best approximates the user 245 // experience. See golang/go#62665 for more details. 246 startTime time.Time 247 248 // scopes contains all scopes defined by nodes in our path, 249 // including nil values for nodes that don't defined a scope. It 250 // also includes our package scope and the universal scope at the 251 // end. 252 scopes []*types.Scope 253 } 254 255 // funcInfo holds info about a function object. 256 type funcInfo struct { 257 // sig is the function declaration enclosing the position. 258 sig *types.Signature 259 260 // body is the function's body. 261 body *ast.BlockStmt 262 } 263 264 type compLitInfo struct { 265 // cl is the *ast.CompositeLit enclosing the position. 266 cl *ast.CompositeLit 267 268 // clType is the type of cl. 269 clType types.Type 270 271 // kv is the *ast.KeyValueExpr enclosing the position, if any. 272 kv *ast.KeyValueExpr 273 274 // inKey is true if we are certain the position is in the key side 275 // of a key-value pair. 276 inKey bool 277 278 // maybeInFieldName is true if inKey is false and it is possible 279 // we are completing a struct field name. For example, 280 // "SomeStruct{<>}" will be inKey=false, but maybeInFieldName=true 281 // because we _could_ be completing a field name. 282 maybeInFieldName bool 283 } 284 285 type importInfo struct { 286 importPath string 287 name string 288 } 289 290 type methodSetKey struct { 291 typ types.Type 292 addressable bool 293 } 294 295 type completionContext struct { 296 // triggerCharacter is the character used to trigger completion at current 297 // position, if any. 298 triggerCharacter string 299 300 // triggerKind is information about how a completion was triggered. 301 triggerKind protocol.CompletionTriggerKind 302 303 // commentCompletion is true if we are completing a comment. 304 commentCompletion bool 305 306 // packageCompletion is true if we are completing a package name. 307 packageCompletion bool 308 } 309 310 // A Selection represents the cursor position and surrounding identifier. 311 type Selection struct { 312 content string 313 tokFile *token.File 314 start, end, cursor token.Pos // relative to rng.TokFile 315 mapper *protocol.Mapper 316 } 317 318 func (p Selection) Range() (protocol.Range, error) { 319 return p.mapper.PosRange(p.tokFile, p.start, p.end) 320 } 321 322 func (p Selection) Prefix() string { 323 return p.content[:p.cursor-p.start] 324 } 325 326 func (p Selection) Suffix() string { 327 return p.content[p.cursor-p.start:] 328 } 329 330 func (c *completer) setSurrounding(ident *ast.Ident) { 331 if c.surrounding != nil { 332 return 333 } 334 if !(ident.Pos() <= c.pos && c.pos <= ident.End()) { 335 return 336 } 337 338 c.surrounding = &Selection{ 339 content: ident.Name, 340 cursor: c.pos, 341 // Overwrite the prefix only. 342 tokFile: c.tokFile, 343 start: ident.Pos(), 344 end: ident.End(), 345 mapper: c.mapper, 346 } 347 348 c.setMatcherFromPrefix(c.surrounding.Prefix()) 349 } 350 351 func (c *completer) setMatcherFromPrefix(prefix string) { 352 switch c.opts.matcher { 353 case settings.Fuzzy: 354 c.matcher = fuzzy.NewMatcher(prefix) 355 case settings.CaseSensitive: 356 c.matcher = prefixMatcher(prefix) 357 default: 358 c.matcher = insensitivePrefixMatcher(strings.ToLower(prefix)) 359 } 360 } 361 362 func (c *completer) getSurrounding() *Selection { 363 if c.surrounding == nil { 364 c.surrounding = &Selection{ 365 content: "", 366 cursor: c.pos, 367 tokFile: c.tokFile, 368 start: c.pos, 369 end: c.pos, 370 mapper: c.mapper, 371 } 372 } 373 return c.surrounding 374 } 375 376 // candidate represents a completion candidate. 377 type candidate struct { 378 // obj is the types.Object to complete to. 379 // TODO(adonovan): eliminate dependence on go/types throughout this struct. 380 // See comment in (*completer).selector for explanation. 381 obj types.Object 382 383 // score is used to rank candidates. 384 score float64 385 386 // name is the deep object name path, e.g. "foo.bar" 387 name string 388 389 // detail is additional information about this item. If not specified, 390 // defaults to type string for the object. 391 detail string 392 393 // path holds the path from the search root (excluding the candidate 394 // itself) for a deep candidate. 395 path []types.Object 396 397 // pathInvokeMask is a bit mask tracking whether each entry in path 398 // should be formatted with "()" (i.e. whether it is a function 399 // invocation). 400 pathInvokeMask uint16 401 402 // mods contains modifications that should be applied to the 403 // candidate when inserted. For example, "foo" may be inserted as 404 // "*foo" or "foo()". 405 mods []typeModKind 406 407 // addressable is true if a pointer can be taken to the candidate. 408 addressable bool 409 410 // convertTo is a type that this candidate should be cast to. For 411 // example, if convertTo is float64, "foo" should be formatted as 412 // "float64(foo)". 413 convertTo types.Type 414 415 // imp is the import that needs to be added to this package in order 416 // for this candidate to be valid. nil if no import needed. 417 imp *importInfo 418 } 419 420 func (c candidate) hasMod(mod typeModKind) bool { 421 for _, m := range c.mods { 422 if m == mod { 423 return true 424 } 425 } 426 return false 427 } 428 429 // ErrIsDefinition is an error that informs the user they got no 430 // completions because they tried to complete the name of a new object 431 // being defined. 432 type ErrIsDefinition struct { 433 objStr string 434 } 435 436 func (e ErrIsDefinition) Error() string { 437 msg := "this is a definition" 438 if e.objStr != "" { 439 msg += " of " + e.objStr 440 } 441 return msg 442 } 443 444 // Completion returns a list of possible candidates for completion, given a 445 // a file and a position. 446 // 447 // The selection is computed based on the preceding identifier and can be used by 448 // the client to score the quality of the completion. For instance, some clients 449 // may tolerate imperfect matches as valid completion results, since users may make typos. 450 func Completion(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, protoPos protocol.Position, protoContext protocol.CompletionContext) ([]CompletionItem, *Selection, error) { 451 ctx, done := event.Start(ctx, "completion.Completion") 452 defer done() 453 454 startTime := time.Now() 455 456 pkg, pgf, err := golang.NarrowestPackageForFile(ctx, snapshot, fh.URI()) 457 if err != nil || pgf.File.Package == token.NoPos { 458 // If we can't parse this file or find position for the package 459 // keyword, it may be missing a package declaration. Try offering 460 // suggestions for the package declaration. 461 // Note that this would be the case even if the keyword 'package' is 462 // present but no package name exists. 463 items, surrounding, innerErr := packageClauseCompletions(ctx, snapshot, fh, protoPos) 464 if innerErr != nil { 465 // return the error for GetParsedFile since it's more relevant in this situation. 466 return nil, nil, fmt.Errorf("getting file %s for Completion: %v (package completions: %v)", fh.URI(), err, innerErr) 467 } 468 return items, surrounding, nil 469 } 470 471 pos, err := pgf.PositionPos(protoPos) 472 if err != nil { 473 return nil, nil, err 474 } 475 // Completion is based on what precedes the cursor. 476 // Find the path to the position before pos. 477 path, _ := astutil.PathEnclosingInterval(pgf.File, pos-1, pos-1) 478 if path == nil { 479 return nil, nil, fmt.Errorf("cannot find node enclosing position") 480 } 481 482 // Check if completion at this position is valid. If not, return early. 483 switch n := path[0].(type) { 484 case *ast.BasicLit: 485 // Skip completion inside literals except for ImportSpec 486 if len(path) > 1 { 487 if _, ok := path[1].(*ast.ImportSpec); ok { 488 break 489 } 490 } 491 return nil, nil, nil 492 case *ast.CallExpr: 493 if n.Ellipsis.IsValid() && pos > n.Ellipsis && pos <= n.Ellipsis+token.Pos(len("...")) { 494 // Don't offer completions inside or directly after "...". For 495 // example, don't offer completions at "<>" in "foo(bar...<>"). 496 return nil, nil, nil 497 } 498 case *ast.Ident: 499 // reject defining identifiers 500 if obj, ok := pkg.GetTypesInfo().Defs[n]; ok { 501 if v, ok := obj.(*types.Var); ok && v.IsField() && v.Embedded() { 502 // An anonymous field is also a reference to a type. 503 } else if pgf.File.Name == n { 504 // Don't skip completions if Ident is for package name. 505 break 506 } else { 507 objStr := "" 508 if obj != nil { 509 qual := types.RelativeTo(pkg.GetTypes()) 510 objStr = types.ObjectString(obj, qual) 511 } 512 ans, sel := definition(path, obj, pgf) 513 if ans != nil { 514 sort.Slice(ans, func(i, j int) bool { 515 return ans[i].Score > ans[j].Score 516 }) 517 return ans, sel, nil 518 } 519 return nil, nil, ErrIsDefinition{objStr: objStr} 520 } 521 } 522 } 523 524 // Collect all surrounding scopes, innermost first. 525 scopes := golang.CollectScopes(pkg.GetTypesInfo(), path, pos) 526 scopes = append(scopes, pkg.GetTypes().Scope(), types.Universe) 527 528 opts := snapshot.Options() 529 c := &completer{ 530 pkg: pkg, 531 snapshot: snapshot, 532 qf: typesutil.FileQualifier(pgf.File, pkg.GetTypes(), pkg.GetTypesInfo()), 533 mq: golang.MetadataQualifierForFile(snapshot, pgf.File, pkg.Metadata()), 534 completionContext: completionContext{ 535 triggerCharacter: protoContext.TriggerCharacter, 536 triggerKind: protoContext.TriggerKind, 537 }, 538 fh: fh, 539 filename: fh.URI().Path(), 540 tokFile: pgf.Tok, 541 file: pgf.File, 542 path: path, 543 pos: pos, 544 seen: make(map[types.Object]bool), 545 enclosingFunc: enclosingFunction(path, pkg.GetTypesInfo()), 546 enclosingCompositeLiteral: enclosingCompositeLiteral(path, pos, pkg.GetTypesInfo()), 547 deepState: deepCompletionState{ 548 enabled: opts.DeepCompletion, 549 }, 550 opts: &completionOptions{ 551 matcher: opts.Matcher, 552 unimported: opts.CompleteUnimported, 553 documentation: opts.CompletionDocumentation && opts.HoverKind != settings.NoDocumentation, 554 fullDocumentation: opts.HoverKind == settings.FullDocumentation, 555 placeholders: opts.UsePlaceholders, 556 budget: opts.CompletionBudget, 557 snippets: opts.InsertTextFormat == protocol.SnippetTextFormat, 558 postfix: opts.ExperimentalPostfixCompletions, 559 completeFunctionCalls: opts.CompleteFunctionCalls, 560 }, 561 // default to a matcher that always matches 562 matcher: prefixMatcher(""), 563 methodSetCache: make(map[methodSetKey]*types.MethodSet), 564 mapper: pgf.Mapper, 565 startTime: startTime, 566 scopes: scopes, 567 } 568 569 ctx, cancel := context.WithCancel(ctx) 570 defer cancel() 571 572 // Compute the deadline for this operation. Deadline is relative to the 573 // search operation, not the entire completion RPC, as the work up until this 574 // point depends significantly on how long it took to type-check, which in 575 // turn depends on the timing of the request relative to other operations on 576 // the snapshot. Including that work in the budget leads to inconsistent 577 // results (and realistically, if type-checking took 200ms already, the user 578 // is unlikely to be significantly more bothered by e.g. another 100ms of 579 // search). 580 // 581 // Don't overload the context with this deadline, as we don't want to 582 // conflate user cancellation (=fail the operation) with our time limit 583 // (=stop searching and succeed with partial results). 584 var deadline *time.Time 585 if c.opts.budget > 0 { 586 d := startTime.Add(c.opts.budget) 587 deadline = &d 588 } 589 590 if surrounding := c.containingIdent(pgf.Src); surrounding != nil { 591 c.setSurrounding(surrounding) 592 } 593 594 c.inference = expectedCandidate(ctx, c) 595 596 err = c.collectCompletions(ctx) 597 if err != nil { 598 return nil, nil, err 599 } 600 601 // Deep search collected candidates and their members for more candidates. 602 c.deepSearch(ctx, 1, deadline) 603 604 // At this point we have a sufficiently complete set of results, and want to 605 // return as close to the completion budget as possible. Previously, we 606 // avoided cancelling the context because it could result in partial results 607 // for e.g. struct fields. At this point, we have a minimal valid set of 608 // candidates, and so truncating due to context cancellation is acceptable. 609 if c.opts.budget > 0 { 610 timeoutDuration := time.Until(c.startTime.Add(c.opts.budget)) 611 ctx, cancel = context.WithTimeout(ctx, timeoutDuration) 612 defer cancel() 613 } 614 615 for _, callback := range c.completionCallbacks { 616 if deadline == nil || time.Now().Before(*deadline) { 617 if err := c.snapshot.RunProcessEnvFunc(ctx, callback); err != nil { 618 return nil, nil, err 619 } 620 } 621 } 622 623 // Search candidates populated by expensive operations like 624 // unimportedMembers etc. for more completion items. 625 c.deepSearch(ctx, 0, deadline) 626 627 // Statement candidates offer an entire statement in certain contexts, as 628 // opposed to a single object. Add statement candidates last because they 629 // depend on other candidates having already been collected. 630 c.addStatementCandidates() 631 632 c.sortItems() 633 return c.items, c.getSurrounding(), nil 634 } 635 636 // collectCompletions adds possible completion candidates to either the deep 637 // search queue or completion items directly for different completion contexts. 638 func (c *completer) collectCompletions(ctx context.Context) error { 639 // Inside import blocks, return completions for unimported packages. 640 for _, importSpec := range c.file.Imports { 641 if !(importSpec.Path.Pos() <= c.pos && c.pos <= importSpec.Path.End()) { 642 continue 643 } 644 return c.populateImportCompletions(importSpec) 645 } 646 647 // Inside comments, offer completions for the name of the relevant symbol. 648 for _, comment := range c.file.Comments { 649 if comment.Pos() < c.pos && c.pos <= comment.End() { 650 c.populateCommentCompletions(comment) 651 return nil 652 } 653 } 654 655 // Struct literals are handled entirely separately. 656 if c.wantStructFieldCompletions() { 657 // If we are definitely completing a struct field name, deep completions 658 // don't make sense. 659 if c.enclosingCompositeLiteral.inKey { 660 c.deepState.enabled = false 661 } 662 return c.structLiteralFieldName(ctx) 663 } 664 665 if lt := c.wantLabelCompletion(); lt != labelNone { 666 c.labels(lt) 667 return nil 668 } 669 670 if c.emptySwitchStmt() { 671 // Empty switch statements only admit "default" and "case" keywords. 672 c.addKeywordItems(map[string]bool{}, highScore, CASE, DEFAULT) 673 return nil 674 } 675 676 switch n := c.path[0].(type) { 677 case *ast.Ident: 678 if c.file.Name == n { 679 return c.packageNameCompletions(ctx, c.fh.URI(), n) 680 } else if sel, ok := c.path[1].(*ast.SelectorExpr); ok && sel.Sel == n { 681 // Is this the Sel part of a selector? 682 return c.selector(ctx, sel) 683 } 684 return c.lexical(ctx) 685 // The function name hasn't been typed yet, but the parens are there: 686 // recv.‸(arg) 687 case *ast.TypeAssertExpr: 688 // Create a fake selector expression. 689 // 690 // The name "_" is the convention used by go/parser to represent phantom 691 // selectors. 692 sel := &ast.Ident{NamePos: n.X.End() + token.Pos(len(".")), Name: "_"} 693 return c.selector(ctx, &ast.SelectorExpr{X: n.X, Sel: sel}) 694 case *ast.SelectorExpr: 695 return c.selector(ctx, n) 696 // At the file scope, only keywords are allowed. 697 case *ast.BadDecl, *ast.File: 698 c.addKeywordCompletions() 699 default: 700 // fallback to lexical completions 701 return c.lexical(ctx) 702 } 703 704 return nil 705 } 706 707 // containingIdent returns the *ast.Ident containing pos, if any. It 708 // synthesizes an *ast.Ident to allow completion in the face of 709 // certain syntax errors. 710 func (c *completer) containingIdent(src []byte) *ast.Ident { 711 // In the normal case, our leaf AST node is the identifier being completed. 712 if ident, ok := c.path[0].(*ast.Ident); ok { 713 return ident 714 } 715 716 pos, tkn, lit := c.scanToken(src) 717 if !pos.IsValid() { 718 return nil 719 } 720 721 fakeIdent := &ast.Ident{Name: lit, NamePos: pos} 722 723 if _, isBadDecl := c.path[0].(*ast.BadDecl); isBadDecl { 724 // You don't get *ast.Idents at the file level, so look for bad 725 // decls and use the manually extracted token. 726 return fakeIdent 727 } else if c.emptySwitchStmt() { 728 // Only keywords are allowed in empty switch statements. 729 // *ast.Idents are not parsed, so we must use the manually 730 // extracted token. 731 return fakeIdent 732 } else if tkn.IsKeyword() { 733 // Otherwise, manually extract the prefix if our containing token 734 // is a keyword. This improves completion after an "accidental 735 // keyword", e.g. completing to "variance" in "someFunc(var<>)". 736 return fakeIdent 737 } 738 739 return nil 740 } 741 742 // scanToken scans pgh's contents for the token containing pos. 743 func (c *completer) scanToken(contents []byte) (token.Pos, token.Token, string) { 744 tok := c.pkg.FileSet().File(c.pos) 745 746 var s scanner.Scanner 747 s.Init(tok, contents, nil, 0) 748 for { 749 tknPos, tkn, lit := s.Scan() 750 if tkn == token.EOF || tknPos >= c.pos { 751 return token.NoPos, token.ILLEGAL, "" 752 } 753 754 if len(lit) > 0 && tknPos <= c.pos && c.pos <= tknPos+token.Pos(len(lit)) { 755 return tknPos, tkn, lit 756 } 757 } 758 } 759 760 func (c *completer) sortItems() { 761 sort.SliceStable(c.items, func(i, j int) bool { 762 // Sort by score first. 763 if c.items[i].Score != c.items[j].Score { 764 return c.items[i].Score > c.items[j].Score 765 } 766 767 // Then sort by label so order stays consistent. This also has the 768 // effect of preferring shorter candidates. 769 return c.items[i].Label < c.items[j].Label 770 }) 771 } 772 773 // emptySwitchStmt reports whether pos is in an empty switch or select 774 // statement. 775 func (c *completer) emptySwitchStmt() bool { 776 block, ok := c.path[0].(*ast.BlockStmt) 777 if !ok || len(block.List) > 0 || len(c.path) == 1 { 778 return false 779 } 780 781 switch c.path[1].(type) { 782 case *ast.SwitchStmt, *ast.TypeSwitchStmt, *ast.SelectStmt: 783 return true 784 default: 785 return false 786 } 787 } 788 789 // populateImportCompletions yields completions for an import path around the cursor. 790 // 791 // Completions are suggested at the directory depth of the given import path so 792 // that we don't overwhelm the user with a large list of possibilities. As an 793 // example, a completion for the prefix "golang" results in "golang.org/". 794 // Completions for "golang.org/" yield its subdirectories 795 // (i.e. "golang.org/x/"). The user is meant to accept completion suggestions 796 // until they reach a complete import path. 797 func (c *completer) populateImportCompletions(searchImport *ast.ImportSpec) error { 798 if !strings.HasPrefix(searchImport.Path.Value, `"`) { 799 return nil 800 } 801 802 // deepSearch is not valuable for import completions. 803 c.deepState.enabled = false 804 805 importPath := searchImport.Path.Value 806 807 // Extract the text between the quotes (if any) in an import spec. 808 // prefix is the part of import path before the cursor. 809 prefixEnd := c.pos - searchImport.Path.Pos() 810 prefix := strings.Trim(importPath[:prefixEnd], `"`) 811 812 // The number of directories in the import path gives us the depth at 813 // which to search. 814 depth := len(strings.Split(prefix, "/")) - 1 815 816 content := importPath 817 start, end := searchImport.Path.Pos(), searchImport.Path.End() 818 namePrefix, nameSuffix := `"`, `"` 819 // If a starting quote is present, adjust surrounding to either after the 820 // cursor or after the first slash (/), except if cursor is at the starting 821 // quote. Otherwise we provide a completion including the starting quote. 822 if strings.HasPrefix(importPath, `"`) && c.pos > searchImport.Path.Pos() { 823 content = content[1:] 824 start++ 825 if depth > 0 { 826 // Adjust textEdit start to replacement range. For ex: if current 827 // path was "golang.or/x/to<>ols/internal/", where <> is the cursor 828 // position, start of the replacement range would be after 829 // "golang.org/x/". 830 path := strings.SplitAfter(prefix, "/") 831 numChars := len(strings.Join(path[:len(path)-1], "")) 832 content = content[numChars:] 833 start += token.Pos(numChars) 834 } 835 namePrefix = "" 836 } 837 838 // We won't provide an ending quote if one is already present, except if 839 // cursor is after the ending quote but still in import spec. This is 840 // because cursor has to be in our textEdit range. 841 if strings.HasSuffix(importPath, `"`) && c.pos < searchImport.Path.End() { 842 end-- 843 content = content[:len(content)-1] 844 nameSuffix = "" 845 } 846 847 c.surrounding = &Selection{ 848 content: content, 849 cursor: c.pos, 850 tokFile: c.tokFile, 851 start: start, 852 end: end, 853 mapper: c.mapper, 854 } 855 856 seenImports := make(map[string]struct{}) 857 for _, importSpec := range c.file.Imports { 858 if importSpec.Path.Value == importPath { 859 continue 860 } 861 seenImportPath, err := strconv.Unquote(importSpec.Path.Value) 862 if err != nil { 863 return err 864 } 865 seenImports[seenImportPath] = struct{}{} 866 } 867 868 var mu sync.Mutex // guard c.items locally, since searchImports is called in parallel 869 seen := make(map[string]struct{}) 870 searchImports := func(pkg imports.ImportFix) { 871 path := pkg.StmtInfo.ImportPath 872 if _, ok := seenImports[path]; ok { 873 return 874 } 875 876 // Any package path containing fewer directories than the search 877 // prefix is not a match. 878 pkgDirList := strings.Split(path, "/") 879 if len(pkgDirList) < depth+1 { 880 return 881 } 882 pkgToConsider := strings.Join(pkgDirList[:depth+1], "/") 883 884 name := pkgDirList[depth] 885 // if we're adding an opening quote to completion too, set name to full 886 // package path since we'll need to overwrite that range. 887 if namePrefix == `"` { 888 name = pkgToConsider 889 } 890 891 score := pkg.Relevance 892 if len(pkgDirList)-1 == depth { 893 score *= highScore 894 } else { 895 // For incomplete package paths, add a terminal slash to indicate that the 896 // user should keep triggering completions. 897 name += "/" 898 pkgToConsider += "/" 899 } 900 901 if _, ok := seen[pkgToConsider]; ok { 902 return 903 } 904 seen[pkgToConsider] = struct{}{} 905 906 mu.Lock() 907 defer mu.Unlock() 908 909 name = namePrefix + name + nameSuffix 910 obj := types.NewPkgName(0, nil, name, types.NewPackage(pkgToConsider, name)) 911 c.deepState.enqueue(candidate{ 912 obj: obj, 913 detail: fmt.Sprintf("%q", pkgToConsider), 914 score: score, 915 }) 916 } 917 918 c.completionCallbacks = append(c.completionCallbacks, func(ctx context.Context, opts *imports.Options) error { 919 return imports.GetImportPaths(ctx, searchImports, prefix, c.filename, c.pkg.GetTypes().Name(), opts.Env) 920 }) 921 return nil 922 } 923 924 // populateCommentCompletions yields completions for comments preceding or in declarations. 925 func (c *completer) populateCommentCompletions(comment *ast.CommentGroup) { 926 // If the completion was triggered by a period, ignore it. These types of 927 // completions will not be useful in comments. 928 if c.completionContext.triggerCharacter == "." { 929 return 930 } 931 932 // Using the comment position find the line after 933 file := c.pkg.FileSet().File(comment.End()) 934 if file == nil { 935 return 936 } 937 938 // Deep completion doesn't work properly in comments since we don't 939 // have a type object to complete further. 940 c.deepState.enabled = false 941 c.completionContext.commentCompletion = true 942 943 // Documentation isn't useful in comments, since it might end up being the 944 // comment itself. 945 c.opts.documentation = false 946 947 commentLine := safetoken.Line(file, comment.End()) 948 949 // comment is valid, set surrounding as word boundaries around cursor 950 c.setSurroundingForComment(comment) 951 952 // Using the next line pos, grab and parse the exported symbol on that line 953 for _, n := range c.file.Decls { 954 declLine := safetoken.Line(file, n.Pos()) 955 // if the comment is not in, directly above or on the same line as a declaration 956 if declLine != commentLine && declLine != commentLine+1 && 957 !(n.Pos() <= comment.Pos() && comment.End() <= n.End()) { 958 continue 959 } 960 switch node := n.(type) { 961 // handle const, vars, and types 962 case *ast.GenDecl: 963 for _, spec := range node.Specs { 964 switch spec := spec.(type) { 965 case *ast.ValueSpec: 966 for _, name := range spec.Names { 967 if name.String() == "_" { 968 continue 969 } 970 obj := c.pkg.GetTypesInfo().ObjectOf(name) 971 c.deepState.enqueue(candidate{obj: obj, score: stdScore}) 972 } 973 case *ast.TypeSpec: 974 // add TypeSpec fields to completion 975 switch typeNode := spec.Type.(type) { 976 case *ast.StructType: 977 c.addFieldItems(typeNode.Fields) 978 case *ast.FuncType: 979 c.addFieldItems(typeNode.Params) 980 c.addFieldItems(typeNode.Results) 981 case *ast.InterfaceType: 982 c.addFieldItems(typeNode.Methods) 983 } 984 985 if spec.Name.String() == "_" { 986 continue 987 } 988 989 obj := c.pkg.GetTypesInfo().ObjectOf(spec.Name) 990 // Type name should get a higher score than fields but not highScore by default 991 // since field near a comment cursor gets a highScore 992 score := stdScore * 1.1 993 // If type declaration is on the line after comment, give it a highScore. 994 if declLine == commentLine+1 { 995 score = highScore 996 } 997 998 c.deepState.enqueue(candidate{obj: obj, score: score}) 999 } 1000 } 1001 // handle functions 1002 case *ast.FuncDecl: 1003 c.addFieldItems(node.Recv) 1004 c.addFieldItems(node.Type.Params) 1005 c.addFieldItems(node.Type.Results) 1006 1007 // collect receiver struct fields 1008 if node.Recv != nil { 1009 for _, fields := range node.Recv.List { 1010 for _, name := range fields.Names { 1011 obj := c.pkg.GetTypesInfo().ObjectOf(name) 1012 if obj == nil { 1013 continue 1014 } 1015 1016 recvType := obj.Type().Underlying() 1017 if ptr, ok := recvType.(*types.Pointer); ok { 1018 recvType = ptr.Elem() 1019 } 1020 recvStruct, ok := recvType.Underlying().(*types.Struct) 1021 if !ok { 1022 continue 1023 } 1024 for i := 0; i < recvStruct.NumFields(); i++ { 1025 field := recvStruct.Field(i) 1026 c.deepState.enqueue(candidate{obj: field, score: lowScore}) 1027 } 1028 } 1029 } 1030 } 1031 1032 if node.Name.String() == "_" { 1033 continue 1034 } 1035 1036 obj := c.pkg.GetTypesInfo().ObjectOf(node.Name) 1037 if obj == nil || obj.Pkg() != nil && obj.Pkg() != c.pkg.GetTypes() { 1038 continue 1039 } 1040 1041 c.deepState.enqueue(candidate{obj: obj, score: highScore}) 1042 } 1043 } 1044 } 1045 1046 // sets word boundaries surrounding a cursor for a comment 1047 func (c *completer) setSurroundingForComment(comments *ast.CommentGroup) { 1048 var cursorComment *ast.Comment 1049 for _, comment := range comments.List { 1050 if c.pos >= comment.Pos() && c.pos <= comment.End() { 1051 cursorComment = comment 1052 break 1053 } 1054 } 1055 // if cursor isn't in the comment 1056 if cursorComment == nil { 1057 return 1058 } 1059 1060 // index of cursor in comment text 1061 cursorOffset := int(c.pos - cursorComment.Pos()) 1062 start, end := cursorOffset, cursorOffset 1063 for start > 0 && isValidIdentifierChar(cursorComment.Text[start-1]) { 1064 start-- 1065 } 1066 for end < len(cursorComment.Text) && isValidIdentifierChar(cursorComment.Text[end]) { 1067 end++ 1068 } 1069 1070 c.surrounding = &Selection{ 1071 content: cursorComment.Text[start:end], 1072 cursor: c.pos, 1073 tokFile: c.tokFile, 1074 start: token.Pos(int(cursorComment.Slash) + start), 1075 end: token.Pos(int(cursorComment.Slash) + end), 1076 mapper: c.mapper, 1077 } 1078 c.setMatcherFromPrefix(c.surrounding.Prefix()) 1079 } 1080 1081 // isValidIdentifierChar returns true if a byte is a valid go identifier 1082 // character, i.e. unicode letter or digit or underscore. 1083 func isValidIdentifierChar(char byte) bool { 1084 charRune := rune(char) 1085 return unicode.In(charRune, unicode.Letter, unicode.Digit) || char == '_' 1086 } 1087 1088 // adds struct fields, interface methods, function declaration fields to completion 1089 func (c *completer) addFieldItems(fields *ast.FieldList) { 1090 if fields == nil { 1091 return 1092 } 1093 1094 cursor := c.surrounding.cursor 1095 for _, field := range fields.List { 1096 for _, name := range field.Names { 1097 if name.String() == "_" { 1098 continue 1099 } 1100 obj := c.pkg.GetTypesInfo().ObjectOf(name) 1101 if obj == nil { 1102 continue 1103 } 1104 1105 // if we're in a field comment/doc, score that field as more relevant 1106 score := stdScore 1107 if field.Comment != nil && field.Comment.Pos() <= cursor && cursor <= field.Comment.End() { 1108 score = highScore 1109 } else if field.Doc != nil && field.Doc.Pos() <= cursor && cursor <= field.Doc.End() { 1110 score = highScore 1111 } 1112 1113 c.deepState.enqueue(candidate{obj: obj, score: score}) 1114 } 1115 } 1116 } 1117 1118 func (c *completer) wantStructFieldCompletions() bool { 1119 clInfo := c.enclosingCompositeLiteral 1120 if clInfo == nil { 1121 return false 1122 } 1123 1124 return clInfo.isStruct() && (clInfo.inKey || clInfo.maybeInFieldName) 1125 } 1126 1127 func (c *completer) wantTypeName() bool { 1128 return !c.completionContext.commentCompletion && c.inference.typeName.wantTypeName 1129 } 1130 1131 // See https://golang.org/issue/36001. Unimported completions are expensive. 1132 const ( 1133 maxUnimportedPackageNames = 5 1134 unimportedMemberTarget = 100 1135 ) 1136 1137 // selector finds completions for the specified selector expression. 1138 func (c *completer) selector(ctx context.Context, sel *ast.SelectorExpr) error { 1139 c.inference.objChain = objChain(c.pkg.GetTypesInfo(), sel.X) 1140 1141 // True selector? 1142 if tv, ok := c.pkg.GetTypesInfo().Types[sel.X]; ok { 1143 c.methodsAndFields(tv.Type, tv.Addressable(), nil, c.deepState.enqueue) 1144 c.addPostfixSnippetCandidates(ctx, sel) 1145 return nil 1146 } 1147 1148 id, ok := sel.X.(*ast.Ident) 1149 if !ok { 1150 return nil 1151 } 1152 1153 // Treat sel as a qualified identifier. 1154 var filter func(*metadata.Package) bool 1155 needImport := false 1156 if pkgName, ok := c.pkg.GetTypesInfo().Uses[id].(*types.PkgName); ok { 1157 // Qualified identifier with import declaration. 1158 imp := pkgName.Imported() 1159 1160 // Known direct dependency? Expand using type information. 1161 if _, ok := c.pkg.Metadata().DepsByPkgPath[golang.PackagePath(imp.Path())]; ok { 1162 c.packageMembers(imp, stdScore, nil, c.deepState.enqueue) 1163 return nil 1164 } 1165 1166 // Imported declaration with missing type information. 1167 // Fall through to shallow completion of unimported package members. 1168 // Match candidate packages by path. 1169 filter = func(mp *metadata.Package) bool { 1170 return strings.TrimPrefix(string(mp.PkgPath), "vendor/") == imp.Path() 1171 } 1172 } else { 1173 // Qualified identifier without import declaration. 1174 // Match candidate packages by name. 1175 filter = func(mp *metadata.Package) bool { 1176 return string(mp.Name) == id.Name 1177 } 1178 needImport = true 1179 } 1180 1181 // Search unimported packages. 1182 if !c.opts.unimported { 1183 return nil // feature disabled 1184 } 1185 1186 // The deep completion algorithm is exceedingly complex and 1187 // deeply coupled to the now obsolete notions that all 1188 // token.Pos values can be interpreted by as a single FileSet 1189 // belonging to the Snapshot and that all types.Object values 1190 // are canonicalized by a single types.Importer mapping. 1191 // These invariants are no longer true now that gopls uses 1192 // an incremental approach, parsing and type-checking each 1193 // package separately. 1194 // 1195 // Consequently, completion of symbols defined in packages that 1196 // are not currently imported by the query file cannot use the 1197 // deep completion machinery which is based on type information. 1198 // Instead it must use only syntax information from a quick 1199 // parse of top-level declarations (but not function bodies). 1200 // 1201 // TODO(adonovan): rewrite the deep completion machinery to 1202 // not assume global Pos/Object realms and then use export 1203 // data instead of the quick parse approach taken here. 1204 1205 // First, we search among packages in the forward transitive 1206 // closure of the workspace. 1207 // We'll use a fast parse to extract package members 1208 // from those that match the name/path criterion. 1209 all, err := c.snapshot.AllMetadata(ctx) 1210 if err != nil { 1211 return err 1212 } 1213 known := make(map[golang.PackagePath]*metadata.Package) 1214 for _, mp := range all { 1215 if mp.Name == "main" { 1216 continue // not importable 1217 } 1218 if mp.IsIntermediateTestVariant() { 1219 continue 1220 } 1221 // The only test variant we admit is "p [p.test]" 1222 // when we are completing within "p_test [p.test]", 1223 // as in that case we would like to offer completions 1224 // of the test variants' additional symbols. 1225 if mp.ForTest != "" && c.pkg.Metadata().PkgPath != mp.ForTest+"_test" { 1226 continue 1227 } 1228 if !filter(mp) { 1229 continue 1230 } 1231 // Prefer previous entry unless this one is its test variant. 1232 if mp.ForTest != "" || known[mp.PkgPath] == nil { 1233 known[mp.PkgPath] = mp 1234 } 1235 } 1236 1237 paths := make([]string, 0, len(known)) 1238 for path := range known { 1239 paths = append(paths, string(path)) 1240 } 1241 1242 // Rank import paths as goimports would. 1243 var relevances map[string]float64 1244 if len(paths) > 0 { 1245 if err := c.snapshot.RunProcessEnvFunc(ctx, func(ctx context.Context, opts *imports.Options) error { 1246 var err error 1247 relevances, err = imports.ScoreImportPaths(ctx, opts.Env, paths) 1248 return err 1249 }); err != nil { 1250 return err 1251 } 1252 sort.Slice(paths, func(i, j int) bool { 1253 return relevances[paths[i]] > relevances[paths[j]] 1254 }) 1255 } 1256 1257 // quickParse does a quick parse of a single file of package m, 1258 // extracts exported package members and adds candidates to c.items. 1259 // TODO(rfindley): synchronizing access to c here does not feel right. 1260 // Consider adding a concurrency-safe API for completer. 1261 var cMu sync.Mutex // guards c.items and c.matcher 1262 var enough int32 // atomic bool 1263 quickParse := func(uri protocol.DocumentURI, mp *metadata.Package) error { 1264 if atomic.LoadInt32(&enough) != 0 { 1265 return nil 1266 } 1267 1268 fh, err := c.snapshot.ReadFile(ctx, uri) 1269 if err != nil { 1270 return err 1271 } 1272 content, err := fh.Content() 1273 if err != nil { 1274 return err 1275 } 1276 path := string(mp.PkgPath) 1277 forEachPackageMember(content, func(tok token.Token, id *ast.Ident, fn *ast.FuncDecl) { 1278 if atomic.LoadInt32(&enough) != 0 { 1279 return 1280 } 1281 1282 if !id.IsExported() { 1283 return 1284 } 1285 1286 cMu.Lock() 1287 score := c.matcher.Score(id.Name) 1288 cMu.Unlock() 1289 1290 if sel.Sel.Name != "_" && score == 0 { 1291 return // not a match; avoid constructing the completion item below 1292 } 1293 1294 // The only detail is the kind and package: `var (from "example.com/foo")` 1295 // TODO(adonovan): pretty-print FuncDecl.FuncType or TypeSpec.Type? 1296 // TODO(adonovan): should this score consider the actual c.matcher.Score 1297 // of the item? How does this compare with the deepState.enqueue path? 1298 item := CompletionItem{ 1299 Label: id.Name, 1300 Detail: fmt.Sprintf("%s (from %q)", strings.ToLower(tok.String()), mp.PkgPath), 1301 InsertText: id.Name, 1302 Score: float64(score) * unimportedScore(relevances[path]), 1303 } 1304 switch tok { 1305 case token.FUNC: 1306 item.Kind = protocol.FunctionCompletion 1307 case token.VAR: 1308 item.Kind = protocol.VariableCompletion 1309 case token.CONST: 1310 item.Kind = protocol.ConstantCompletion 1311 case token.TYPE: 1312 // Without types, we can't distinguish Class from Interface. 1313 item.Kind = protocol.ClassCompletion 1314 } 1315 1316 if needImport { 1317 imp := &importInfo{importPath: path} 1318 if imports.ImportPathToAssumedName(path) != string(mp.Name) { 1319 imp.name = string(mp.Name) 1320 } 1321 item.AdditionalTextEdits, _ = c.importEdits(imp) 1322 } 1323 1324 // For functions, add a parameter snippet. 1325 if fn != nil { 1326 paramList := func(list *ast.FieldList) []string { 1327 var params []string 1328 if list != nil { 1329 var cfg printer.Config // slight overkill 1330 param := func(name string, typ ast.Expr) { 1331 var buf strings.Builder 1332 buf.WriteString(name) 1333 buf.WriteByte(' ') 1334 cfg.Fprint(&buf, token.NewFileSet(), typ) 1335 params = append(params, buf.String()) 1336 } 1337 1338 for _, field := range list.List { 1339 if field.Names != nil { 1340 for _, name := range field.Names { 1341 param(name.Name, field.Type) 1342 } 1343 } else { 1344 param("_", field.Type) 1345 } 1346 } 1347 } 1348 return params 1349 } 1350 1351 // Ideally we would eliminate the suffix of type 1352 // parameters that are redundant with inference 1353 // from the argument types (#51783), but it's 1354 // quite fiddly to do using syntax alone. 1355 // (See inferableTypeParams in format.go.) 1356 tparams := paramList(fn.Type.TypeParams) 1357 params := paramList(fn.Type.Params) 1358 var sn snippet.Builder 1359 c.functionCallSnippet(id.Name, tparams, params, &sn) 1360 item.snippet = &sn 1361 } 1362 1363 cMu.Lock() 1364 c.items = append(c.items, item) 1365 if len(c.items) >= unimportedMemberTarget { 1366 atomic.StoreInt32(&enough, 1) 1367 } 1368 cMu.Unlock() 1369 }) 1370 return nil 1371 } 1372 1373 // Extract the package-level candidates using a quick parse. 1374 var g errgroup.Group 1375 for _, path := range paths { 1376 mp := known[golang.PackagePath(path)] 1377 for _, uri := range mp.CompiledGoFiles { 1378 uri := uri 1379 g.Go(func() error { 1380 return quickParse(uri, mp) 1381 }) 1382 } 1383 } 1384 if err := g.Wait(); err != nil { 1385 return err 1386 } 1387 1388 // In addition, we search in the module cache using goimports. 1389 ctx, cancel := context.WithCancel(ctx) 1390 var mu sync.Mutex 1391 add := func(pkgExport imports.PackageExport) { 1392 if ignoreUnimportedCompletion(pkgExport.Fix) { 1393 return 1394 } 1395 1396 mu.Lock() 1397 defer mu.Unlock() 1398 // TODO(adonovan): what if the actual package has a vendor/ prefix? 1399 if _, ok := known[golang.PackagePath(pkgExport.Fix.StmtInfo.ImportPath)]; ok { 1400 return // We got this one above. 1401 } 1402 1403 // Continue with untyped proposals. 1404 pkg := types.NewPackage(pkgExport.Fix.StmtInfo.ImportPath, pkgExport.Fix.IdentName) 1405 for _, export := range pkgExport.Exports { 1406 score := unimportedScore(pkgExport.Fix.Relevance) 1407 c.deepState.enqueue(candidate{ 1408 obj: types.NewVar(0, pkg, export, nil), 1409 score: score, 1410 imp: &importInfo{ 1411 importPath: pkgExport.Fix.StmtInfo.ImportPath, 1412 name: pkgExport.Fix.StmtInfo.Name, 1413 }, 1414 }) 1415 } 1416 if len(c.items) >= unimportedMemberTarget { 1417 cancel() 1418 } 1419 } 1420 1421 c.completionCallbacks = append(c.completionCallbacks, func(ctx context.Context, opts *imports.Options) error { 1422 defer cancel() 1423 return imports.GetPackageExports(ctx, add, id.Name, c.filename, c.pkg.GetTypes().Name(), opts.Env) 1424 }) 1425 return nil 1426 } 1427 1428 // unimportedScore returns a score for an unimported package that is generally 1429 // lower than other candidates. 1430 func unimportedScore(relevance float64) float64 { 1431 return (stdScore + .1*relevance) / 2 1432 } 1433 1434 func (c *completer) packageMembers(pkg *types.Package, score float64, imp *importInfo, cb func(candidate)) { 1435 scope := pkg.Scope() 1436 for _, name := range scope.Names() { 1437 obj := scope.Lookup(name) 1438 cb(candidate{ 1439 obj: obj, 1440 score: score, 1441 imp: imp, 1442 addressable: isVar(obj), 1443 }) 1444 } 1445 } 1446 1447 // ignoreUnimportedCompletion reports whether an unimported completion 1448 // resulting in the given import should be ignored. 1449 func ignoreUnimportedCompletion(fix *imports.ImportFix) bool { 1450 // golang/go#60062: don't add unimported completion to golang.org/toolchain. 1451 return fix != nil && strings.HasPrefix(fix.StmtInfo.ImportPath, "golang.org/toolchain") 1452 } 1453 1454 func (c *completer) methodsAndFields(typ types.Type, addressable bool, imp *importInfo, cb func(candidate)) { 1455 mset := c.methodSetCache[methodSetKey{typ, addressable}] 1456 if mset == nil { 1457 if addressable && !types.IsInterface(typ) && !isPointer(typ) { 1458 // Add methods of *T, which includes methods with receiver T. 1459 mset = types.NewMethodSet(types.NewPointer(typ)) 1460 } else { 1461 // Add methods of T. 1462 mset = types.NewMethodSet(typ) 1463 } 1464 c.methodSetCache[methodSetKey{typ, addressable}] = mset 1465 } 1466 1467 if isStarTestingDotF(typ) && addressable { 1468 // is that a sufficient test? (or is more care needed?) 1469 if c.fuzz(mset, imp, cb) { 1470 return 1471 } 1472 } 1473 1474 for i := 0; i < mset.Len(); i++ { 1475 cb(candidate{ 1476 obj: mset.At(i).Obj(), 1477 score: stdScore, 1478 imp: imp, 1479 addressable: addressable || isPointer(typ), 1480 }) 1481 } 1482 1483 // Add fields of T. 1484 eachField(typ, func(v *types.Var) { 1485 cb(candidate{ 1486 obj: v, 1487 score: stdScore - 0.01, 1488 imp: imp, 1489 addressable: addressable || isPointer(typ), 1490 }) 1491 }) 1492 } 1493 1494 // isStarTestingDotF reports whether typ is *testing.F. 1495 func isStarTestingDotF(typ types.Type) bool { 1496 ptr, _ := typ.(*types.Pointer) 1497 if ptr == nil { 1498 return false 1499 } 1500 named, _ := ptr.Elem().(*types.Named) 1501 if named == nil { 1502 return false 1503 } 1504 obj := named.Obj() 1505 // obj.Pkg is nil for the error type. 1506 return obj != nil && obj.Pkg() != nil && obj.Pkg().Path() == "testing" && obj.Name() == "F" 1507 } 1508 1509 // lexical finds completions in the lexical environment. 1510 func (c *completer) lexical(ctx context.Context) error { 1511 var ( 1512 builtinIota = types.Universe.Lookup("iota") 1513 builtinNil = types.Universe.Lookup("nil") 1514 1515 // TODO(rfindley): only allow "comparable" where it is valid (in constraint 1516 // position or embedded in interface declarations). 1517 // builtinComparable = types.Universe.Lookup("comparable") 1518 ) 1519 1520 // Track seen variables to avoid showing completions for shadowed variables. 1521 // This works since we look at scopes from innermost to outermost. 1522 seen := make(map[string]struct{}) 1523 1524 // Process scopes innermost first. 1525 for i, scope := range c.scopes { 1526 if scope == nil { 1527 continue 1528 } 1529 1530 Names: 1531 for _, name := range scope.Names() { 1532 declScope, obj := scope.LookupParent(name, c.pos) 1533 if declScope != scope { 1534 continue // Name was declared in some enclosing scope, or not at all. 1535 } 1536 1537 // If obj's type is invalid, find the AST node that defines the lexical block 1538 // containing the declaration of obj. Don't resolve types for packages. 1539 if !isPkgName(obj) && !typeIsValid(obj.Type()) { 1540 // Match the scope to its ast.Node. If the scope is the package scope, 1541 // use the *ast.File as the starting node. 1542 var node ast.Node 1543 if i < len(c.path) { 1544 node = c.path[i] 1545 } else if i == len(c.path) { // use the *ast.File for package scope 1546 node = c.path[i-1] 1547 } 1548 if node != nil { 1549 if resolved := resolveInvalid(c.pkg.FileSet(), obj, node, c.pkg.GetTypesInfo()); resolved != nil { 1550 obj = resolved 1551 } 1552 } 1553 } 1554 1555 // Don't use LHS of decl in RHS. 1556 for _, ident := range enclosingDeclLHS(c.path) { 1557 if obj.Pos() == ident.Pos() { 1558 continue Names 1559 } 1560 } 1561 1562 // Don't suggest "iota" outside of const decls. 1563 if obj == builtinIota && !c.inConstDecl() { 1564 continue 1565 } 1566 1567 // Rank outer scopes lower than inner. 1568 score := stdScore * math.Pow(.99, float64(i)) 1569 1570 // Dowrank "nil" a bit so it is ranked below more interesting candidates. 1571 if obj == builtinNil { 1572 score /= 2 1573 } 1574 1575 // If we haven't already added a candidate for an object with this name. 1576 if _, ok := seen[obj.Name()]; !ok { 1577 seen[obj.Name()] = struct{}{} 1578 c.deepState.enqueue(candidate{ 1579 obj: obj, 1580 score: score, 1581 addressable: isVar(obj), 1582 }) 1583 } 1584 } 1585 } 1586 1587 if c.inference.objType != nil { 1588 if named, _ := golang.Deref(c.inference.objType).(*types.Named); named != nil { 1589 // If we expected a named type, check the type's package for 1590 // completion items. This is useful when the current file hasn't 1591 // imported the type's package yet. 1592 1593 if named.Obj() != nil && named.Obj().Pkg() != nil { 1594 pkg := named.Obj().Pkg() 1595 1596 // Make sure the package name isn't already in use by another 1597 // object, and that this file doesn't import the package yet. 1598 // TODO(adonovan): what if pkg.Path has vendor/ prefix? 1599 if _, ok := seen[pkg.Name()]; !ok && pkg != c.pkg.GetTypes() && !alreadyImports(c.file, golang.ImportPath(pkg.Path())) { 1600 seen[pkg.Name()] = struct{}{} 1601 obj := types.NewPkgName(0, nil, pkg.Name(), pkg) 1602 imp := &importInfo{ 1603 importPath: pkg.Path(), 1604 } 1605 if imports.ImportPathToAssumedName(pkg.Path()) != pkg.Name() { 1606 imp.name = pkg.Name() 1607 } 1608 c.deepState.enqueue(candidate{ 1609 obj: obj, 1610 score: stdScore, 1611 imp: imp, 1612 }) 1613 } 1614 } 1615 } 1616 } 1617 1618 if c.opts.unimported { 1619 if err := c.unimportedPackages(ctx, seen); err != nil { 1620 return err 1621 } 1622 } 1623 1624 if c.inference.typeName.isTypeParam { 1625 // If we are completing a type param, offer each structural type. 1626 // This ensures we suggest "[]int" and "[]float64" for a constraint 1627 // with type union "[]int | []float64". 1628 if t, _ := c.inference.objType.(*types.Interface); t != nil { 1629 terms, _ := typeparams.InterfaceTermSet(t) 1630 for _, term := range terms { 1631 c.injectType(ctx, term.Type()) 1632 } 1633 } 1634 } else { 1635 c.injectType(ctx, c.inference.objType) 1636 } 1637 1638 // Add keyword completion items appropriate in the current context. 1639 c.addKeywordCompletions() 1640 1641 return nil 1642 } 1643 1644 // injectType manufactures candidates based on the given type. This is 1645 // intended for types not discoverable via lexical search, such as 1646 // composite and/or generic types. For example, if the type is "[]int", 1647 // this method makes sure you get candidates "[]int{}" and "[]int" 1648 // (the latter applies when completing a type name). 1649 func (c *completer) injectType(ctx context.Context, t types.Type) { 1650 if t == nil { 1651 return 1652 } 1653 1654 t = golang.Deref(t) 1655 1656 // If we have an expected type and it is _not_ a named type, handle 1657 // it specially. Non-named types like "[]int" will never be 1658 // considered via a lexical search, so we need to directly inject 1659 // them. Also allow generic types since lexical search does not 1660 // infer instantiated versions of them. 1661 if named, _ := t.(*types.Named); named == nil || named.TypeParams().Len() > 0 { 1662 // If our expected type is "[]int", this will add a literal 1663 // candidate of "[]int{}". 1664 c.literal(ctx, t, nil) 1665 1666 if _, isBasic := t.(*types.Basic); !isBasic { 1667 // If we expect a non-basic type name (e.g. "[]int"), hack up 1668 // a named type whose name is literally "[]int". This allows 1669 // us to reuse our object based completion machinery. 1670 fakeNamedType := candidate{ 1671 obj: types.NewTypeName(token.NoPos, nil, types.TypeString(t, c.qf), t), 1672 score: stdScore, 1673 } 1674 // Make sure the type name matches before considering 1675 // candidate. This cuts down on useless candidates. 1676 if c.matchingTypeName(&fakeNamedType) { 1677 c.deepState.enqueue(fakeNamedType) 1678 } 1679 } 1680 } 1681 } 1682 1683 func (c *completer) unimportedPackages(ctx context.Context, seen map[string]struct{}) error { 1684 var prefix string 1685 if c.surrounding != nil { 1686 prefix = c.surrounding.Prefix() 1687 } 1688 1689 // Don't suggest unimported packages if we have absolutely nothing 1690 // to go on. 1691 if prefix == "" { 1692 return nil 1693 } 1694 1695 count := 0 1696 1697 // Search the forward transitive closure of the workspace. 1698 all, err := c.snapshot.AllMetadata(ctx) 1699 if err != nil { 1700 return err 1701 } 1702 pkgNameByPath := make(map[golang.PackagePath]string) 1703 var paths []string // actually PackagePaths 1704 for _, mp := range all { 1705 if mp.ForTest != "" { 1706 continue // skip all test variants 1707 } 1708 if mp.Name == "main" { 1709 continue // main is non-importable 1710 } 1711 if !strings.HasPrefix(string(mp.Name), prefix) { 1712 continue // not a match 1713 } 1714 paths = append(paths, string(mp.PkgPath)) 1715 pkgNameByPath[mp.PkgPath] = string(mp.Name) 1716 } 1717 1718 // Rank candidates using goimports' algorithm. 1719 var relevances map[string]float64 1720 if len(paths) != 0 { 1721 if err := c.snapshot.RunProcessEnvFunc(ctx, func(ctx context.Context, opts *imports.Options) error { 1722 var err error 1723 relevances, err = imports.ScoreImportPaths(ctx, opts.Env, paths) 1724 return err 1725 }); err != nil { 1726 return err 1727 } 1728 } 1729 sort.Slice(paths, func(i, j int) bool { 1730 if relevances[paths[i]] != relevances[paths[j]] { 1731 return relevances[paths[i]] > relevances[paths[j]] 1732 } 1733 1734 // Fall back to lexical sort to keep truncated set of candidates 1735 // in a consistent order. 1736 return paths[i] < paths[j] 1737 }) 1738 1739 for _, path := range paths { 1740 name := pkgNameByPath[golang.PackagePath(path)] 1741 if _, ok := seen[name]; ok { 1742 continue 1743 } 1744 imp := &importInfo{ 1745 importPath: path, 1746 } 1747 if imports.ImportPathToAssumedName(path) != name { 1748 imp.name = name 1749 } 1750 if count >= maxUnimportedPackageNames { 1751 return nil 1752 } 1753 c.deepState.enqueue(candidate{ 1754 // Pass an empty *types.Package to disable deep completions. 1755 obj: types.NewPkgName(0, nil, name, types.NewPackage(path, name)), 1756 score: unimportedScore(relevances[path]), 1757 imp: imp, 1758 }) 1759 count++ 1760 } 1761 1762 var mu sync.Mutex 1763 add := func(pkg imports.ImportFix) { 1764 if ignoreUnimportedCompletion(&pkg) { 1765 return 1766 } 1767 mu.Lock() 1768 defer mu.Unlock() 1769 if _, ok := seen[pkg.IdentName]; ok { 1770 return 1771 } 1772 if _, ok := relevances[pkg.StmtInfo.ImportPath]; ok { 1773 return 1774 } 1775 1776 if count >= maxUnimportedPackageNames { 1777 return 1778 } 1779 1780 // Do not add the unimported packages to seen, since we can have 1781 // multiple packages of the same name as completion suggestions, since 1782 // only one will be chosen. 1783 obj := types.NewPkgName(0, nil, pkg.IdentName, types.NewPackage(pkg.StmtInfo.ImportPath, pkg.IdentName)) 1784 c.deepState.enqueue(candidate{ 1785 obj: obj, 1786 score: unimportedScore(pkg.Relevance), 1787 imp: &importInfo{ 1788 importPath: pkg.StmtInfo.ImportPath, 1789 name: pkg.StmtInfo.Name, 1790 }, 1791 }) 1792 count++ 1793 } 1794 1795 c.completionCallbacks = append(c.completionCallbacks, func(ctx context.Context, opts *imports.Options) error { 1796 return imports.GetAllCandidates(ctx, add, prefix, c.filename, c.pkg.GetTypes().Name(), opts.Env) 1797 }) 1798 1799 return nil 1800 } 1801 1802 // alreadyImports reports whether f has an import with the specified path. 1803 func alreadyImports(f *ast.File, path golang.ImportPath) bool { 1804 for _, s := range f.Imports { 1805 if metadata.UnquoteImportPath(s) == path { 1806 return true 1807 } 1808 } 1809 return false 1810 } 1811 1812 func (c *completer) inConstDecl() bool { 1813 for _, n := range c.path { 1814 if decl, ok := n.(*ast.GenDecl); ok && decl.Tok == token.CONST { 1815 return true 1816 } 1817 } 1818 return false 1819 } 1820 1821 // structLiteralFieldName finds completions for struct field names inside a struct literal. 1822 func (c *completer) structLiteralFieldName(ctx context.Context) error { 1823 clInfo := c.enclosingCompositeLiteral 1824 1825 // Mark fields of the composite literal that have already been set, 1826 // except for the current field. 1827 addedFields := make(map[*types.Var]bool) 1828 for _, el := range clInfo.cl.Elts { 1829 if kvExpr, ok := el.(*ast.KeyValueExpr); ok { 1830 if clInfo.kv == kvExpr { 1831 continue 1832 } 1833 1834 if key, ok := kvExpr.Key.(*ast.Ident); ok { 1835 if used, ok := c.pkg.GetTypesInfo().Uses[key]; ok { 1836 if usedVar, ok := used.(*types.Var); ok { 1837 addedFields[usedVar] = true 1838 } 1839 } 1840 } 1841 } 1842 } 1843 1844 deltaScore := 0.0001 1845 switch t := clInfo.clType.(type) { 1846 case *types.Struct: 1847 for i := 0; i < t.NumFields(); i++ { 1848 field := t.Field(i) 1849 if !addedFields[field] { 1850 c.deepState.enqueue(candidate{ 1851 obj: field, 1852 score: highScore - float64(i)*deltaScore, 1853 }) 1854 } 1855 } 1856 1857 // Add lexical completions if we aren't certain we are in the key part of a 1858 // key-value pair. 1859 if clInfo.maybeInFieldName { 1860 return c.lexical(ctx) 1861 } 1862 default: 1863 return c.lexical(ctx) 1864 } 1865 1866 return nil 1867 } 1868 1869 func (cl *compLitInfo) isStruct() bool { 1870 _, ok := cl.clType.(*types.Struct) 1871 return ok 1872 } 1873 1874 // enclosingCompositeLiteral returns information about the composite literal enclosing the 1875 // position. 1876 func enclosingCompositeLiteral(path []ast.Node, pos token.Pos, info *types.Info) *compLitInfo { 1877 for _, n := range path { 1878 switch n := n.(type) { 1879 case *ast.CompositeLit: 1880 // The enclosing node will be a composite literal if the user has just 1881 // opened the curly brace (e.g. &x{<>) or the completion request is triggered 1882 // from an already completed composite literal expression (e.g. &x{foo: 1, <>}) 1883 // 1884 // The position is not part of the composite literal unless it falls within the 1885 // curly braces (e.g. "foo.Foo<>Struct{}"). 1886 if !(n.Lbrace < pos && pos <= n.Rbrace) { 1887 // Keep searching since we may yet be inside a composite literal. 1888 // For example "Foo{B: Ba<>{}}". 1889 break 1890 } 1891 1892 tv, ok := info.Types[n] 1893 if !ok { 1894 return nil 1895 } 1896 1897 clInfo := compLitInfo{ 1898 cl: n, 1899 clType: golang.Deref(tv.Type).Underlying(), 1900 } 1901 1902 var ( 1903 expr ast.Expr 1904 hasKeys bool 1905 ) 1906 for _, el := range n.Elts { 1907 // Remember the expression that the position falls in, if any. 1908 if el.Pos() <= pos && pos <= el.End() { 1909 expr = el 1910 } 1911 1912 if kv, ok := el.(*ast.KeyValueExpr); ok { 1913 hasKeys = true 1914 // If expr == el then we know the position falls in this expression, 1915 // so also record kv as the enclosing *ast.KeyValueExpr. 1916 if expr == el { 1917 clInfo.kv = kv 1918 break 1919 } 1920 } 1921 } 1922 1923 if clInfo.kv != nil { 1924 // If in a *ast.KeyValueExpr, we know we are in the key if the position 1925 // is to the left of the colon (e.g. "Foo{F<>: V}". 1926 clInfo.inKey = pos <= clInfo.kv.Colon 1927 } else if hasKeys { 1928 // If we aren't in a *ast.KeyValueExpr but the composite literal has 1929 // other *ast.KeyValueExprs, we must be on the key side of a new 1930 // *ast.KeyValueExpr (e.g. "Foo{F: V, <>}"). 1931 clInfo.inKey = true 1932 } else { 1933 switch clInfo.clType.(type) { 1934 case *types.Struct: 1935 if len(n.Elts) == 0 { 1936 // If the struct literal is empty, next could be a struct field 1937 // name or an expression (e.g. "Foo{<>}" could become "Foo{F:}" 1938 // or "Foo{someVar}"). 1939 clInfo.maybeInFieldName = true 1940 } else if len(n.Elts) == 1 { 1941 // If there is one expression and the position is in that expression 1942 // and the expression is an identifier, we may be writing a field 1943 // name or an expression (e.g. "Foo{F<>}"). 1944 _, clInfo.maybeInFieldName = expr.(*ast.Ident) 1945 } 1946 case *types.Map: 1947 // If we aren't in a *ast.KeyValueExpr we must be adding a new key 1948 // to the map. 1949 clInfo.inKey = true 1950 } 1951 } 1952 1953 return &clInfo 1954 default: 1955 if breaksExpectedTypeInference(n, pos) { 1956 return nil 1957 } 1958 } 1959 } 1960 1961 return nil 1962 } 1963 1964 // enclosingFunction returns the signature and body of the function 1965 // enclosing the given position. 1966 func enclosingFunction(path []ast.Node, info *types.Info) *funcInfo { 1967 for _, node := range path { 1968 switch t := node.(type) { 1969 case *ast.FuncDecl: 1970 if obj, ok := info.Defs[t.Name]; ok { 1971 return &funcInfo{ 1972 sig: obj.Type().(*types.Signature), 1973 body: t.Body, 1974 } 1975 } 1976 case *ast.FuncLit: 1977 if typ, ok := info.Types[t]; ok { 1978 if sig, _ := typ.Type.(*types.Signature); sig == nil { 1979 // golang/go#49397: it should not be possible, but we somehow arrived 1980 // here with a non-signature type, most likely due to AST mangling 1981 // such that node.Type is not a FuncType. 1982 return nil 1983 } 1984 return &funcInfo{ 1985 sig: typ.Type.(*types.Signature), 1986 body: t.Body, 1987 } 1988 } 1989 } 1990 } 1991 return nil 1992 } 1993 1994 func (c *completer) expectedCompositeLiteralType() types.Type { 1995 clInfo := c.enclosingCompositeLiteral 1996 switch t := clInfo.clType.(type) { 1997 case *types.Slice: 1998 if clInfo.inKey { 1999 return types.Typ[types.UntypedInt] 2000 } 2001 return t.Elem() 2002 case *types.Array: 2003 if clInfo.inKey { 2004 return types.Typ[types.UntypedInt] 2005 } 2006 return t.Elem() 2007 case *types.Map: 2008 if clInfo.inKey { 2009 return t.Key() 2010 } 2011 return t.Elem() 2012 case *types.Struct: 2013 // If we are completing a key (i.e. field name), there is no expected type. 2014 if clInfo.inKey { 2015 return nil 2016 } 2017 2018 // If we are in a key-value pair, but not in the key, then we must be on the 2019 // value side. The expected type of the value will be determined from the key. 2020 if clInfo.kv != nil { 2021 if key, ok := clInfo.kv.Key.(*ast.Ident); ok { 2022 for i := 0; i < t.NumFields(); i++ { 2023 if field := t.Field(i); field.Name() == key.Name { 2024 return field.Type() 2025 } 2026 } 2027 } 2028 } else { 2029 // If we aren't in a key-value pair and aren't in the key, we must be using 2030 // implicit field names. 2031 2032 // The order of the literal fields must match the order in the struct definition. 2033 // Find the element that the position belongs to and suggest that field's type. 2034 if i := exprAtPos(c.pos, clInfo.cl.Elts); i < t.NumFields() { 2035 return t.Field(i).Type() 2036 } 2037 } 2038 } 2039 return nil 2040 } 2041 2042 // typeMod represents an operator that changes the expected type. 2043 type typeMod struct { 2044 mod typeModKind 2045 arrayLen int64 2046 } 2047 2048 type typeModKind int 2049 2050 const ( 2051 dereference typeModKind = iota // pointer indirection: "*" 2052 reference // adds level of pointer: "&" for values, "*" for type names 2053 chanRead // channel read operator: "<-" 2054 sliceType // make a slice type: "[]" in "[]int" 2055 arrayType // make an array type: "[2]" in "[2]int" 2056 invoke // make a function call: "()" in "foo()" 2057 takeSlice // take slice of array: "[:]" in "foo[:]" 2058 takeDotDotDot // turn slice into variadic args: "..." in "foo..." 2059 index // index into slice/array: "[0]" in "foo[0]" 2060 ) 2061 2062 type objKind int 2063 2064 const ( 2065 kindAny objKind = 0 2066 kindArray objKind = 1 << iota 2067 kindSlice 2068 kindChan 2069 kindMap 2070 kindStruct 2071 kindString 2072 kindInt 2073 kindBool 2074 kindBytes 2075 kindPtr 2076 kindFloat 2077 kindComplex 2078 kindError 2079 kindStringer 2080 kindFunc 2081 ) 2082 2083 // penalizedObj represents an object that should be disfavored as a 2084 // completion candidate. 2085 type penalizedObj struct { 2086 // objChain is the full "chain", e.g. "foo.bar().baz" becomes 2087 // []types.Object{foo, bar, baz}. 2088 objChain []types.Object 2089 // penalty is score penalty in the range (0, 1). 2090 penalty float64 2091 } 2092 2093 // candidateInference holds information we have inferred about a type that can be 2094 // used at the current position. 2095 type candidateInference struct { 2096 // objType is the desired type of an object used at the query position. 2097 objType types.Type 2098 2099 // objKind is a mask of expected kinds of types such as "map", "slice", etc. 2100 objKind objKind 2101 2102 // variadic is true if we are completing the initial variadic 2103 // parameter. For example: 2104 // append([]T{}, <>) // objType=T variadic=true 2105 // append([]T{}, T{}, <>) // objType=T variadic=false 2106 variadic bool 2107 2108 // modifiers are prefixes such as "*", "&" or "<-" that influence how 2109 // a candidate type relates to the expected type. 2110 modifiers []typeMod 2111 2112 // convertibleTo is a type our candidate type must be convertible to. 2113 convertibleTo types.Type 2114 2115 // typeName holds information about the expected type name at 2116 // position, if any. 2117 typeName typeNameInference 2118 2119 // assignees are the types that would receive a function call's 2120 // results at the position. For example: 2121 // 2122 // foo := 123 2123 // foo, bar := <> 2124 // 2125 // at "<>", the assignees are [int, <invalid>]. 2126 assignees []types.Type 2127 2128 // variadicAssignees is true if we could be completing an inner 2129 // function call that fills out an outer function call's variadic 2130 // params. For example: 2131 // 2132 // func foo(int, ...string) {} 2133 // 2134 // foo(<>) // variadicAssignees=true 2135 // foo(bar<>) // variadicAssignees=true 2136 // foo(bar, baz<>) // variadicAssignees=false 2137 variadicAssignees bool 2138 2139 // penalized holds expressions that should be disfavored as 2140 // candidates. For example, it tracks expressions already used in a 2141 // switch statement's other cases. Each expression is tracked using 2142 // its entire object "chain" allowing differentiation between 2143 // "a.foo" and "b.foo" when "a" and "b" are the same type. 2144 penalized []penalizedObj 2145 2146 // objChain contains the chain of objects representing the 2147 // surrounding *ast.SelectorExpr. For example, if we are completing 2148 // "foo.bar.ba<>", objChain will contain []types.Object{foo, bar}. 2149 objChain []types.Object 2150 } 2151 2152 // typeNameInference holds information about the expected type name at 2153 // position. 2154 type typeNameInference struct { 2155 // wantTypeName is true if we expect the name of a type. 2156 wantTypeName bool 2157 2158 // modifiers are prefixes such as "*", "&" or "<-" that influence how 2159 // a candidate type relates to the expected type. 2160 modifiers []typeMod 2161 2162 // assertableFrom is a type that must be assertable to our candidate type. 2163 assertableFrom types.Type 2164 2165 // wantComparable is true if we want a comparable type. 2166 wantComparable bool 2167 2168 // seenTypeSwitchCases tracks types that have already been used by 2169 // the containing type switch. 2170 seenTypeSwitchCases []types.Type 2171 2172 // compLitType is true if we are completing a composite literal type 2173 // name, e.g "foo<>{}". 2174 compLitType bool 2175 2176 // isTypeParam is true if we are completing a type instantiation parameter 2177 isTypeParam bool 2178 } 2179 2180 // expectedCandidate returns information about the expected candidate 2181 // for an expression at the query position. 2182 func expectedCandidate(ctx context.Context, c *completer) (inf candidateInference) { 2183 inf.typeName = expectTypeName(c) 2184 2185 if c.enclosingCompositeLiteral != nil { 2186 inf.objType = c.expectedCompositeLiteralType() 2187 } 2188 2189 Nodes: 2190 for i, node := range c.path { 2191 switch node := node.(type) { 2192 case *ast.BinaryExpr: 2193 // Determine if query position comes from left or right of op. 2194 e := node.X 2195 if c.pos < node.OpPos { 2196 e = node.Y 2197 } 2198 if tv, ok := c.pkg.GetTypesInfo().Types[e]; ok { 2199 switch node.Op { 2200 case token.LAND, token.LOR: 2201 // Don't infer "bool" type for "&&" or "||". Often you want 2202 // to compose a boolean expression from non-boolean 2203 // candidates. 2204 default: 2205 inf.objType = tv.Type 2206 } 2207 break Nodes 2208 } 2209 case *ast.AssignStmt: 2210 // Only rank completions if you are on the right side of the token. 2211 if c.pos > node.TokPos { 2212 i := exprAtPos(c.pos, node.Rhs) 2213 if i >= len(node.Lhs) { 2214 i = len(node.Lhs) - 1 2215 } 2216 if tv, ok := c.pkg.GetTypesInfo().Types[node.Lhs[i]]; ok { 2217 inf.objType = tv.Type 2218 } 2219 2220 // If we have a single expression on the RHS, record the LHS 2221 // assignees so we can favor multi-return function calls with 2222 // matching result values. 2223 if len(node.Rhs) <= 1 { 2224 for _, lhs := range node.Lhs { 2225 inf.assignees = append(inf.assignees, c.pkg.GetTypesInfo().TypeOf(lhs)) 2226 } 2227 } else { 2228 // Otherwise, record our single assignee, even if its type is 2229 // not available. We use this info to downrank functions 2230 // with the wrong number of result values. 2231 inf.assignees = append(inf.assignees, c.pkg.GetTypesInfo().TypeOf(node.Lhs[i])) 2232 } 2233 } 2234 return inf 2235 case *ast.ValueSpec: 2236 if node.Type != nil && c.pos > node.Type.End() { 2237 inf.objType = c.pkg.GetTypesInfo().TypeOf(node.Type) 2238 } 2239 return inf 2240 case *ast.CallExpr: 2241 // Only consider CallExpr args if position falls between parens. 2242 if node.Lparen < c.pos && c.pos <= node.Rparen { 2243 // For type conversions like "int64(foo)" we can only infer our 2244 // desired type is convertible to int64. 2245 if typ := typeConversion(node, c.pkg.GetTypesInfo()); typ != nil { 2246 inf.convertibleTo = typ 2247 break Nodes 2248 } 2249 2250 sig, _ := c.pkg.GetTypesInfo().Types[node.Fun].Type.(*types.Signature) 2251 2252 if sig != nil && sig.TypeParams().Len() > 0 { 2253 // If we are completing a generic func call, re-check the call expression. 2254 // This allows type param inference to work in cases like: 2255 // 2256 // func foo[T any](T) {} 2257 // foo[int](<>) // <- get "int" completions instead of "T" 2258 // 2259 // TODO: remove this after https://go.dev/issue/52503 2260 info := &types.Info{Types: make(map[ast.Expr]types.TypeAndValue)} 2261 types.CheckExpr(c.pkg.FileSet(), c.pkg.GetTypes(), node.Fun.Pos(), node.Fun, info) 2262 sig, _ = info.Types[node.Fun].Type.(*types.Signature) 2263 } 2264 2265 if sig != nil { 2266 inf = c.expectedCallParamType(inf, node, sig) 2267 } 2268 2269 if funIdent, ok := node.Fun.(*ast.Ident); ok { 2270 obj := c.pkg.GetTypesInfo().ObjectOf(funIdent) 2271 2272 if obj != nil && obj.Parent() == types.Universe { 2273 // Defer call to builtinArgType so we can provide it the 2274 // inferred type from its parent node. 2275 defer func() { 2276 inf = c.builtinArgType(obj, node, inf) 2277 inf.objKind = c.builtinArgKind(ctx, obj, node) 2278 }() 2279 2280 // The expected type of builtin arguments like append() is 2281 // the expected type of the builtin call itself. For 2282 // example: 2283 // 2284 // var foo []int = append(<>) 2285 // 2286 // To find the expected type at <> we "skip" the append() 2287 // node and get the expected type one level up, which is 2288 // []int. 2289 continue Nodes 2290 } 2291 } 2292 2293 return inf 2294 } 2295 case *ast.ReturnStmt: 2296 if c.enclosingFunc != nil { 2297 sig := c.enclosingFunc.sig 2298 // Find signature result that corresponds to our return statement. 2299 if resultIdx := exprAtPos(c.pos, node.Results); resultIdx < len(node.Results) { 2300 if resultIdx < sig.Results().Len() { 2301 inf.objType = sig.Results().At(resultIdx).Type() 2302 } 2303 } 2304 } 2305 return inf 2306 case *ast.CaseClause: 2307 if swtch, ok := findSwitchStmt(c.path[i+1:], c.pos, node).(*ast.SwitchStmt); ok { 2308 if tv, ok := c.pkg.GetTypesInfo().Types[swtch.Tag]; ok { 2309 inf.objType = tv.Type 2310 2311 // Record which objects have already been used in the case 2312 // statements so we don't suggest them again. 2313 for _, cc := range swtch.Body.List { 2314 for _, caseExpr := range cc.(*ast.CaseClause).List { 2315 // Don't record the expression we are currently completing. 2316 if caseExpr.Pos() < c.pos && c.pos <= caseExpr.End() { 2317 continue 2318 } 2319 2320 if objs := objChain(c.pkg.GetTypesInfo(), caseExpr); len(objs) > 0 { 2321 inf.penalized = append(inf.penalized, penalizedObj{objChain: objs, penalty: 0.1}) 2322 } 2323 } 2324 } 2325 } 2326 } 2327 return inf 2328 case *ast.SliceExpr: 2329 // Make sure position falls within the brackets (e.g. "foo[a:<>]"). 2330 if node.Lbrack < c.pos && c.pos <= node.Rbrack { 2331 inf.objType = types.Typ[types.UntypedInt] 2332 } 2333 return inf 2334 case *ast.IndexExpr: 2335 // Make sure position falls within the brackets (e.g. "foo[<>]"). 2336 if node.Lbrack < c.pos && c.pos <= node.Rbrack { 2337 if tv, ok := c.pkg.GetTypesInfo().Types[node.X]; ok { 2338 switch t := tv.Type.Underlying().(type) { 2339 case *types.Map: 2340 inf.objType = t.Key() 2341 case *types.Slice, *types.Array: 2342 inf.objType = types.Typ[types.UntypedInt] 2343 } 2344 2345 if ct := expectedConstraint(tv.Type, 0); ct != nil { 2346 inf.objType = ct 2347 inf.typeName.wantTypeName = true 2348 inf.typeName.isTypeParam = true 2349 } 2350 } 2351 } 2352 return inf 2353 case *ast.IndexListExpr: 2354 if node.Lbrack < c.pos && c.pos <= node.Rbrack { 2355 if tv, ok := c.pkg.GetTypesInfo().Types[node.X]; ok { 2356 if ct := expectedConstraint(tv.Type, exprAtPos(c.pos, node.Indices)); ct != nil { 2357 inf.objType = ct 2358 inf.typeName.wantTypeName = true 2359 inf.typeName.isTypeParam = true 2360 } 2361 } 2362 } 2363 return inf 2364 case *ast.SendStmt: 2365 // Make sure we are on right side of arrow (e.g. "foo <- <>"). 2366 if c.pos > node.Arrow+1 { 2367 if tv, ok := c.pkg.GetTypesInfo().Types[node.Chan]; ok { 2368 if ch, ok := tv.Type.Underlying().(*types.Chan); ok { 2369 inf.objType = ch.Elem() 2370 } 2371 } 2372 } 2373 return inf 2374 case *ast.RangeStmt: 2375 if goplsastutil.NodeContains(node.X, c.pos) { 2376 inf.objKind |= kindSlice | kindArray | kindMap | kindString 2377 if node.Value == nil { 2378 inf.objKind |= kindChan 2379 } 2380 } 2381 return inf 2382 case *ast.StarExpr: 2383 inf.modifiers = append(inf.modifiers, typeMod{mod: dereference}) 2384 case *ast.UnaryExpr: 2385 switch node.Op { 2386 case token.AND: 2387 inf.modifiers = append(inf.modifiers, typeMod{mod: reference}) 2388 case token.ARROW: 2389 inf.modifiers = append(inf.modifiers, typeMod{mod: chanRead}) 2390 } 2391 case *ast.DeferStmt, *ast.GoStmt: 2392 inf.objKind |= kindFunc 2393 return inf 2394 default: 2395 if breaksExpectedTypeInference(node, c.pos) { 2396 return inf 2397 } 2398 } 2399 } 2400 2401 return inf 2402 } 2403 2404 func (c *completer) expectedCallParamType(inf candidateInference, node *ast.CallExpr, sig *types.Signature) candidateInference { 2405 numParams := sig.Params().Len() 2406 if numParams == 0 { 2407 return inf 2408 } 2409 2410 exprIdx := exprAtPos(c.pos, node.Args) 2411 2412 // If we have one or zero arg expressions, we may be 2413 // completing to a function call that returns multiple 2414 // values, in turn getting passed in to the surrounding 2415 // call. Record the assignees so we can favor function 2416 // calls that return matching values. 2417 if len(node.Args) <= 1 && exprIdx == 0 { 2418 for i := 0; i < sig.Params().Len(); i++ { 2419 inf.assignees = append(inf.assignees, sig.Params().At(i).Type()) 2420 } 2421 2422 // Record that we may be completing into variadic parameters. 2423 inf.variadicAssignees = sig.Variadic() 2424 } 2425 2426 // Make sure not to run past the end of expected parameters. 2427 if exprIdx >= numParams { 2428 inf.objType = sig.Params().At(numParams - 1).Type() 2429 } else { 2430 inf.objType = sig.Params().At(exprIdx).Type() 2431 } 2432 2433 if sig.Variadic() && exprIdx >= (numParams-1) { 2434 // If we are completing a variadic param, deslice the variadic type. 2435 inf.objType = deslice(inf.objType) 2436 // Record whether we are completing the initial variadic param. 2437 inf.variadic = exprIdx == numParams-1 && len(node.Args) <= numParams 2438 2439 // Check if we can infer object kind from printf verb. 2440 inf.objKind |= printfArgKind(c.pkg.GetTypesInfo(), node, exprIdx) 2441 } 2442 2443 // If our expected type is an uninstantiated generic type param, 2444 // swap to the constraint which will do a decent job filtering 2445 // candidates. 2446 if tp, _ := inf.objType.(*types.TypeParam); tp != nil { 2447 inf.objType = tp.Constraint() 2448 } 2449 2450 return inf 2451 } 2452 2453 func expectedConstraint(t types.Type, idx int) types.Type { 2454 var tp *types.TypeParamList 2455 if named, _ := t.(*types.Named); named != nil { 2456 tp = named.TypeParams() 2457 } else if sig, _ := t.Underlying().(*types.Signature); sig != nil { 2458 tp = sig.TypeParams() 2459 } 2460 if tp == nil || idx >= tp.Len() { 2461 return nil 2462 } 2463 return tp.At(idx).Constraint() 2464 } 2465 2466 // objChain decomposes e into a chain of objects if possible. For 2467 // example, "foo.bar().baz" will yield []types.Object{foo, bar, baz}. 2468 // If any part can't be turned into an object, return nil. 2469 func objChain(info *types.Info, e ast.Expr) []types.Object { 2470 var objs []types.Object 2471 2472 for e != nil { 2473 switch n := e.(type) { 2474 case *ast.Ident: 2475 obj := info.ObjectOf(n) 2476 if obj == nil { 2477 return nil 2478 } 2479 objs = append(objs, obj) 2480 e = nil 2481 case *ast.SelectorExpr: 2482 obj := info.ObjectOf(n.Sel) 2483 if obj == nil { 2484 return nil 2485 } 2486 objs = append(objs, obj) 2487 e = n.X 2488 case *ast.CallExpr: 2489 if len(n.Args) > 0 { 2490 return nil 2491 } 2492 e = n.Fun 2493 default: 2494 return nil 2495 } 2496 } 2497 2498 // Reverse order so the layout matches the syntactic order. 2499 for i := 0; i < len(objs)/2; i++ { 2500 objs[i], objs[len(objs)-1-i] = objs[len(objs)-1-i], objs[i] 2501 } 2502 2503 return objs 2504 } 2505 2506 // applyTypeModifiers applies the list of type modifiers to a type. 2507 // It returns nil if the modifiers could not be applied. 2508 func (ci candidateInference) applyTypeModifiers(typ types.Type, addressable bool) types.Type { 2509 for _, mod := range ci.modifiers { 2510 switch mod.mod { 2511 case dereference: 2512 // For every "*" indirection operator, remove a pointer layer 2513 // from candidate type. 2514 if ptr, ok := typ.Underlying().(*types.Pointer); ok { 2515 typ = ptr.Elem() 2516 } else { 2517 return nil 2518 } 2519 case reference: 2520 // For every "&" address operator, add another pointer layer to 2521 // candidate type, if the candidate is addressable. 2522 if addressable { 2523 typ = types.NewPointer(typ) 2524 } else { 2525 return nil 2526 } 2527 case chanRead: 2528 // For every "<-" operator, remove a layer of channelness. 2529 if ch, ok := typ.(*types.Chan); ok { 2530 typ = ch.Elem() 2531 } else { 2532 return nil 2533 } 2534 } 2535 } 2536 2537 return typ 2538 } 2539 2540 // applyTypeNameModifiers applies the list of type modifiers to a type name. 2541 func (ci candidateInference) applyTypeNameModifiers(typ types.Type) types.Type { 2542 for _, mod := range ci.typeName.modifiers { 2543 switch mod.mod { 2544 case reference: 2545 typ = types.NewPointer(typ) 2546 case arrayType: 2547 typ = types.NewArray(typ, mod.arrayLen) 2548 case sliceType: 2549 typ = types.NewSlice(typ) 2550 } 2551 } 2552 return typ 2553 } 2554 2555 // matchesVariadic returns true if we are completing a variadic 2556 // parameter and candType is a compatible slice type. 2557 func (ci candidateInference) matchesVariadic(candType types.Type) bool { 2558 return ci.variadic && ci.objType != nil && assignableTo(candType, types.NewSlice(ci.objType)) 2559 } 2560 2561 // findSwitchStmt returns an *ast.CaseClause's corresponding *ast.SwitchStmt or 2562 // *ast.TypeSwitchStmt. path should start from the case clause's first ancestor. 2563 func findSwitchStmt(path []ast.Node, pos token.Pos, c *ast.CaseClause) ast.Stmt { 2564 // Make sure position falls within a "case <>:" clause. 2565 if exprAtPos(pos, c.List) >= len(c.List) { 2566 return nil 2567 } 2568 // A case clause is always nested within a block statement in a switch statement. 2569 if len(path) < 2 { 2570 return nil 2571 } 2572 if _, ok := path[0].(*ast.BlockStmt); !ok { 2573 return nil 2574 } 2575 switch s := path[1].(type) { 2576 case *ast.SwitchStmt: 2577 return s 2578 case *ast.TypeSwitchStmt: 2579 return s 2580 default: 2581 return nil 2582 } 2583 } 2584 2585 // breaksExpectedTypeInference reports if an expression node's type is unrelated 2586 // to its child expression node types. For example, "Foo{Bar: x.Baz(<>)}" should 2587 // expect a function argument, not a composite literal value. 2588 func breaksExpectedTypeInference(n ast.Node, pos token.Pos) bool { 2589 switch n := n.(type) { 2590 case *ast.CompositeLit: 2591 // Doesn't break inference if pos is in type name. 2592 // For example: "Foo<>{Bar: 123}" 2593 return n.Type == nil || !goplsastutil.NodeContains(n.Type, pos) 2594 case *ast.CallExpr: 2595 // Doesn't break inference if pos is in func name. 2596 // For example: "Foo<>(123)" 2597 return !goplsastutil.NodeContains(n.Fun, pos) 2598 case *ast.FuncLit, *ast.IndexExpr, *ast.SliceExpr: 2599 return true 2600 default: 2601 return false 2602 } 2603 } 2604 2605 // expectTypeName returns information about the expected type name at position. 2606 func expectTypeName(c *completer) typeNameInference { 2607 var inf typeNameInference 2608 2609 Nodes: 2610 for i, p := range c.path { 2611 switch n := p.(type) { 2612 case *ast.FieldList: 2613 // Expect a type name if pos is in a FieldList. This applies to 2614 // FuncType params/results, FuncDecl receiver, StructType, and 2615 // InterfaceType. We don't need to worry about the field name 2616 // because completion bails out early if pos is in an *ast.Ident 2617 // that defines an object. 2618 inf.wantTypeName = true 2619 break Nodes 2620 case *ast.CaseClause: 2621 // Expect type names in type switch case clauses. 2622 if swtch, ok := findSwitchStmt(c.path[i+1:], c.pos, n).(*ast.TypeSwitchStmt); ok { 2623 // The case clause types must be assertable from the type switch parameter. 2624 ast.Inspect(swtch.Assign, func(n ast.Node) bool { 2625 if ta, ok := n.(*ast.TypeAssertExpr); ok { 2626 inf.assertableFrom = c.pkg.GetTypesInfo().TypeOf(ta.X) 2627 return false 2628 } 2629 return true 2630 }) 2631 inf.wantTypeName = true 2632 2633 // Track the types that have already been used in this 2634 // switch's case statements so we don't recommend them. 2635 for _, e := range swtch.Body.List { 2636 for _, typeExpr := range e.(*ast.CaseClause).List { 2637 // Skip if type expression contains pos. We don't want to 2638 // count it as already used if the user is completing it. 2639 if typeExpr.Pos() < c.pos && c.pos <= typeExpr.End() { 2640 continue 2641 } 2642 2643 if t := c.pkg.GetTypesInfo().TypeOf(typeExpr); t != nil { 2644 inf.seenTypeSwitchCases = append(inf.seenTypeSwitchCases, t) 2645 } 2646 } 2647 } 2648 2649 break Nodes 2650 } 2651 return typeNameInference{} 2652 case *ast.TypeAssertExpr: 2653 // Expect type names in type assert expressions. 2654 if n.Lparen < c.pos && c.pos <= n.Rparen { 2655 // The type in parens must be assertable from the expression type. 2656 inf.assertableFrom = c.pkg.GetTypesInfo().TypeOf(n.X) 2657 inf.wantTypeName = true 2658 break Nodes 2659 } 2660 return typeNameInference{} 2661 case *ast.StarExpr: 2662 inf.modifiers = append(inf.modifiers, typeMod{mod: reference}) 2663 case *ast.CompositeLit: 2664 // We want a type name if position is in the "Type" part of a 2665 // composite literal (e.g. "Foo<>{}"). 2666 if n.Type != nil && n.Type.Pos() <= c.pos && c.pos <= n.Type.End() { 2667 inf.wantTypeName = true 2668 inf.compLitType = true 2669 2670 if i < len(c.path)-1 { 2671 // Track preceding "&" operator. Technically it applies to 2672 // the composite literal and not the type name, but if 2673 // affects our type completion nonetheless. 2674 if u, ok := c.path[i+1].(*ast.UnaryExpr); ok && u.Op == token.AND { 2675 inf.modifiers = append(inf.modifiers, typeMod{mod: reference}) 2676 } 2677 } 2678 } 2679 break Nodes 2680 case *ast.ArrayType: 2681 // If we are inside the "Elt" part of an array type, we want a type name. 2682 if n.Elt.Pos() <= c.pos && c.pos <= n.Elt.End() { 2683 inf.wantTypeName = true 2684 if n.Len == nil { 2685 // No "Len" expression means a slice type. 2686 inf.modifiers = append(inf.modifiers, typeMod{mod: sliceType}) 2687 } else { 2688 // Try to get the array type using the constant value of "Len". 2689 tv, ok := c.pkg.GetTypesInfo().Types[n.Len] 2690 if ok && tv.Value != nil && tv.Value.Kind() == constant.Int { 2691 if arrayLen, ok := constant.Int64Val(tv.Value); ok { 2692 inf.modifiers = append(inf.modifiers, typeMod{mod: arrayType, arrayLen: arrayLen}) 2693 } 2694 } 2695 } 2696 2697 // ArrayTypes can be nested, so keep going if our parent is an 2698 // ArrayType. 2699 if i < len(c.path)-1 { 2700 if _, ok := c.path[i+1].(*ast.ArrayType); ok { 2701 continue Nodes 2702 } 2703 } 2704 2705 break Nodes 2706 } 2707 case *ast.MapType: 2708 inf.wantTypeName = true 2709 if n.Key != nil { 2710 inf.wantComparable = goplsastutil.NodeContains(n.Key, c.pos) 2711 } else { 2712 // If the key is empty, assume we are completing the key if 2713 // pos is directly after the "map[". 2714 inf.wantComparable = c.pos == n.Pos()+token.Pos(len("map[")) 2715 } 2716 break Nodes 2717 case *ast.ValueSpec: 2718 inf.wantTypeName = n.Type != nil && goplsastutil.NodeContains(n.Type, c.pos) 2719 break Nodes 2720 case *ast.TypeSpec: 2721 inf.wantTypeName = goplsastutil.NodeContains(n.Type, c.pos) 2722 default: 2723 if breaksExpectedTypeInference(p, c.pos) { 2724 return typeNameInference{} 2725 } 2726 } 2727 } 2728 2729 return inf 2730 } 2731 2732 func (c *completer) fakeObj(T types.Type) *types.Var { 2733 return types.NewVar(token.NoPos, c.pkg.GetTypes(), "", T) 2734 } 2735 2736 // derivableTypes iterates types you can derive from t. For example, 2737 // from "foo" we might derive "&foo", and "foo()". 2738 func derivableTypes(t types.Type, addressable bool, f func(t types.Type, addressable bool, mod typeModKind) bool) bool { 2739 switch t := t.Underlying().(type) { 2740 case *types.Signature: 2741 // If t is a func type with a single result, offer the result type. 2742 if t.Results().Len() == 1 && f(t.Results().At(0).Type(), false, invoke) { 2743 return true 2744 } 2745 case *types.Array: 2746 if f(t.Elem(), true, index) { 2747 return true 2748 } 2749 // Try converting array to slice. 2750 if f(types.NewSlice(t.Elem()), false, takeSlice) { 2751 return true 2752 } 2753 case *types.Pointer: 2754 if f(t.Elem(), false, dereference) { 2755 return true 2756 } 2757 case *types.Slice: 2758 if f(t.Elem(), true, index) { 2759 return true 2760 } 2761 case *types.Map: 2762 if f(t.Elem(), false, index) { 2763 return true 2764 } 2765 case *types.Chan: 2766 if f(t.Elem(), false, chanRead) { 2767 return true 2768 } 2769 } 2770 2771 // Check if c is addressable and a pointer to c matches our type inference. 2772 if addressable && f(types.NewPointer(t), false, reference) { 2773 return true 2774 } 2775 2776 return false 2777 } 2778 2779 // anyCandType reports whether f returns true for any candidate type 2780 // derivable from c. It searches up to three levels of type 2781 // modification. For example, given "foo" we could discover "***foo" 2782 // or "*foo()". 2783 func (c *candidate) anyCandType(f func(t types.Type, addressable bool) bool) bool { 2784 if c.obj == nil || c.obj.Type() == nil { 2785 return false 2786 } 2787 2788 const maxDepth = 3 2789 2790 var searchTypes func(t types.Type, addressable bool, mods []typeModKind) bool 2791 searchTypes = func(t types.Type, addressable bool, mods []typeModKind) bool { 2792 if f(t, addressable) { 2793 if len(mods) > 0 { 2794 newMods := make([]typeModKind, len(mods)+len(c.mods)) 2795 copy(newMods, mods) 2796 copy(newMods[len(mods):], c.mods) 2797 c.mods = newMods 2798 } 2799 return true 2800 } 2801 2802 if len(mods) == maxDepth { 2803 return false 2804 } 2805 2806 return derivableTypes(t, addressable, func(t types.Type, addressable bool, mod typeModKind) bool { 2807 return searchTypes(t, addressable, append(mods, mod)) 2808 }) 2809 } 2810 2811 return searchTypes(c.obj.Type(), c.addressable, make([]typeModKind, 0, maxDepth)) 2812 } 2813 2814 // matchingCandidate reports whether cand matches our type inferences. 2815 // It mutates cand's score in certain cases. 2816 func (c *completer) matchingCandidate(cand *candidate) bool { 2817 if c.completionContext.commentCompletion { 2818 return false 2819 } 2820 2821 // Bail out early if we are completing a field name in a composite literal. 2822 if v, ok := cand.obj.(*types.Var); ok && v.IsField() && c.wantStructFieldCompletions() { 2823 return true 2824 } 2825 2826 if isTypeName(cand.obj) { 2827 return c.matchingTypeName(cand) 2828 } else if c.wantTypeName() { 2829 // If we want a type, a non-type object never matches. 2830 return false 2831 } 2832 2833 if c.inference.candTypeMatches(cand) { 2834 return true 2835 } 2836 2837 candType := cand.obj.Type() 2838 if candType == nil { 2839 return false 2840 } 2841 2842 if sig, ok := candType.Underlying().(*types.Signature); ok { 2843 if c.inference.assigneesMatch(cand, sig) { 2844 // Invoke the candidate if its results are multi-assignable. 2845 cand.mods = append(cand.mods, invoke) 2846 return true 2847 } 2848 } 2849 2850 // Default to invoking *types.Func candidates. This is so function 2851 // completions in an empty statement (or other cases with no expected type) 2852 // are invoked by default. 2853 if isFunc(cand.obj) { 2854 cand.mods = append(cand.mods, invoke) 2855 } 2856 2857 return false 2858 } 2859 2860 // candTypeMatches reports whether cand makes a good completion 2861 // candidate given the candidate inference. cand's score may be 2862 // mutated to downrank the candidate in certain situations. 2863 func (ci *candidateInference) candTypeMatches(cand *candidate) bool { 2864 var ( 2865 expTypes = make([]types.Type, 0, 2) 2866 variadicType types.Type 2867 ) 2868 if ci.objType != nil { 2869 expTypes = append(expTypes, ci.objType) 2870 2871 if ci.variadic { 2872 variadicType = types.NewSlice(ci.objType) 2873 expTypes = append(expTypes, variadicType) 2874 } 2875 } 2876 2877 return cand.anyCandType(func(candType types.Type, addressable bool) bool { 2878 // Take into account any type modifiers on the expected type. 2879 candType = ci.applyTypeModifiers(candType, addressable) 2880 if candType == nil { 2881 return false 2882 } 2883 2884 if ci.convertibleTo != nil && convertibleTo(candType, ci.convertibleTo) { 2885 return true 2886 } 2887 2888 for _, expType := range expTypes { 2889 if isEmptyInterface(expType) { 2890 continue 2891 } 2892 2893 matches := ci.typeMatches(expType, candType) 2894 if !matches { 2895 // If candType doesn't otherwise match, consider if we can 2896 // convert candType directly to expType. 2897 if considerTypeConversion(candType, expType, cand.path) { 2898 cand.convertTo = expType 2899 // Give a major score penalty so we always prefer directly 2900 // assignable candidates, all else equal. 2901 cand.score *= 0.5 2902 return true 2903 } 2904 2905 continue 2906 } 2907 2908 if expType == variadicType { 2909 cand.mods = append(cand.mods, takeDotDotDot) 2910 } 2911 2912 // Lower candidate score for untyped conversions. This avoids 2913 // ranking untyped constants above candidates with an exact type 2914 // match. Don't lower score of builtin constants, e.g. "true". 2915 if isUntyped(candType) && !types.Identical(candType, expType) && cand.obj.Parent() != types.Universe { 2916 // Bigger penalty for deep completions into other packages to 2917 // avoid random constants from other packages popping up all 2918 // the time. 2919 if len(cand.path) > 0 && isPkgName(cand.path[0]) { 2920 cand.score *= 0.5 2921 } else { 2922 cand.score *= 0.75 2923 } 2924 } 2925 2926 return true 2927 } 2928 2929 // If we don't have a specific expected type, fall back to coarser 2930 // object kind checks. 2931 if ci.objType == nil || isEmptyInterface(ci.objType) { 2932 // If we were able to apply type modifiers to our candidate type, 2933 // count that as a match. For example: 2934 // 2935 // var foo chan int 2936 // <-fo<> 2937 // 2938 // We were able to apply the "<-" type modifier to "foo", so "foo" 2939 // matches. 2940 if len(ci.modifiers) > 0 { 2941 return true 2942 } 2943 2944 // If we didn't have an exact type match, check if our object kind 2945 // matches. 2946 if ci.kindMatches(candType) { 2947 if ci.objKind == kindFunc { 2948 cand.mods = append(cand.mods, invoke) 2949 } 2950 return true 2951 } 2952 } 2953 2954 return false 2955 }) 2956 } 2957 2958 // considerTypeConversion returns true if we should offer a completion 2959 // automatically converting "from" to "to". 2960 func considerTypeConversion(from, to types.Type, path []types.Object) bool { 2961 // Don't offer to convert deep completions from other packages. 2962 // Otherwise there are many random package level consts/vars that 2963 // pop up as candidates all the time. 2964 if len(path) > 0 && isPkgName(path[0]) { 2965 return false 2966 } 2967 2968 if _, ok := from.(*types.TypeParam); ok { 2969 return false 2970 } 2971 2972 if !convertibleTo(from, to) { 2973 return false 2974 } 2975 2976 // Don't offer to convert ints to strings since that probably 2977 // doesn't do what the user wants. 2978 if isBasicKind(from, types.IsInteger) && isBasicKind(to, types.IsString) { 2979 return false 2980 } 2981 2982 return true 2983 } 2984 2985 // typeMatches reports whether an object of candType makes a good 2986 // completion candidate given the expected type expType. 2987 func (ci *candidateInference) typeMatches(expType, candType types.Type) bool { 2988 // Handle untyped values specially since AssignableTo gives false negatives 2989 // for them (see https://golang.org/issue/32146). 2990 if candBasic, ok := candType.Underlying().(*types.Basic); ok { 2991 if expBasic, ok := expType.Underlying().(*types.Basic); ok { 2992 // Note that the candidate and/or the expected can be untyped. 2993 // In "fo<> == 100" the expected type is untyped, and the 2994 // candidate could also be an untyped constant. 2995 2996 // Sort by is_untyped and then by is_int to simplify below logic. 2997 a, b := candBasic.Info(), expBasic.Info() 2998 if a&types.IsUntyped == 0 || (b&types.IsInteger > 0 && b&types.IsUntyped > 0) { 2999 a, b = b, a 3000 } 3001 3002 // If at least one is untyped... 3003 if a&types.IsUntyped > 0 { 3004 switch { 3005 // Untyped integers are compatible with floats. 3006 case a&types.IsInteger > 0 && b&types.IsFloat > 0: 3007 return true 3008 3009 // Check if their constant kind (bool|int|float|complex|string) matches. 3010 // This doesn't take into account the constant value, so there will be some 3011 // false positives due to integer sign and overflow. 3012 case a&types.IsConstType == b&types.IsConstType: 3013 return true 3014 } 3015 } 3016 } 3017 } 3018 3019 // AssignableTo covers the case where the types are equal, but also handles 3020 // cases like assigning a concrete type to an interface type. 3021 return assignableTo(candType, expType) 3022 } 3023 3024 // kindMatches reports whether candType's kind matches our expected 3025 // kind (e.g. slice, map, etc.). 3026 func (ci *candidateInference) kindMatches(candType types.Type) bool { 3027 return ci.objKind > 0 && ci.objKind&candKind(candType) > 0 3028 } 3029 3030 // assigneesMatch reports whether an invocation of sig matches the 3031 // number and type of any assignees. 3032 func (ci *candidateInference) assigneesMatch(cand *candidate, sig *types.Signature) bool { 3033 if len(ci.assignees) == 0 { 3034 return false 3035 } 3036 3037 // Uniresult functions are always usable and are handled by the 3038 // normal, non-assignees type matching logic. 3039 if sig.Results().Len() == 1 { 3040 return false 3041 } 3042 3043 // Don't prefer completing into func(...interface{}) calls since all 3044 // functions would match. 3045 if ci.variadicAssignees && len(ci.assignees) == 1 && isEmptyInterface(deslice(ci.assignees[0])) { 3046 return false 3047 } 3048 3049 var numberOfResultsCouldMatch bool 3050 if ci.variadicAssignees { 3051 numberOfResultsCouldMatch = sig.Results().Len() >= len(ci.assignees)-1 3052 } else { 3053 numberOfResultsCouldMatch = sig.Results().Len() == len(ci.assignees) 3054 } 3055 3056 // If our signature doesn't return the right number of values, it's 3057 // not a match, so downrank it. For example: 3058 // 3059 // var foo func() (int, int) 3060 // a, b, c := <> // downrank "foo()" since it only returns two values 3061 if !numberOfResultsCouldMatch { 3062 cand.score /= 2 3063 return false 3064 } 3065 3066 // If at least one assignee has a valid type, and all valid 3067 // assignees match the corresponding sig result value, the signature 3068 // is a match. 3069 allMatch := false 3070 for i := 0; i < sig.Results().Len(); i++ { 3071 var assignee types.Type 3072 3073 // If we are completing into variadic parameters, deslice the 3074 // expected variadic type. 3075 if ci.variadicAssignees && i >= len(ci.assignees)-1 { 3076 assignee = ci.assignees[len(ci.assignees)-1] 3077 if elem := deslice(assignee); elem != nil { 3078 assignee = elem 3079 } 3080 } else { 3081 assignee = ci.assignees[i] 3082 } 3083 3084 if assignee == nil || assignee == types.Typ[types.Invalid] { 3085 continue 3086 } 3087 3088 allMatch = ci.typeMatches(assignee, sig.Results().At(i).Type()) 3089 if !allMatch { 3090 break 3091 } 3092 } 3093 return allMatch 3094 } 3095 3096 func (c *completer) matchingTypeName(cand *candidate) bool { 3097 if !c.wantTypeName() { 3098 return false 3099 } 3100 3101 typeMatches := func(candType types.Type) bool { 3102 // Take into account any type name modifier prefixes. 3103 candType = c.inference.applyTypeNameModifiers(candType) 3104 3105 if from := c.inference.typeName.assertableFrom; from != nil { 3106 // Don't suggest the starting type in type assertions. For example, 3107 // if "foo" is an io.Writer, don't suggest "foo.(io.Writer)". 3108 if types.Identical(from, candType) { 3109 return false 3110 } 3111 3112 if intf, ok := from.Underlying().(*types.Interface); ok { 3113 if !types.AssertableTo(intf, candType) { 3114 return false 3115 } 3116 } 3117 } 3118 3119 if c.inference.typeName.wantComparable && !types.Comparable(candType) { 3120 return false 3121 } 3122 3123 // Skip this type if it has already been used in another type 3124 // switch case. 3125 for _, seen := range c.inference.typeName.seenTypeSwitchCases { 3126 if types.Identical(candType, seen) { 3127 return false 3128 } 3129 } 3130 3131 // We can expect a type name and have an expected type in cases like: 3132 // 3133 // var foo []int 3134 // foo = []i<> 3135 // 3136 // Where our expected type is "[]int", and we expect a type name. 3137 if c.inference.objType != nil { 3138 return assignableTo(candType, c.inference.objType) 3139 } 3140 3141 // Default to saying any type name is a match. 3142 return true 3143 } 3144 3145 t := cand.obj.Type() 3146 3147 if typeMatches(t) { 3148 return true 3149 } 3150 3151 if !types.IsInterface(t) && typeMatches(types.NewPointer(t)) { 3152 if c.inference.typeName.compLitType { 3153 // If we are completing a composite literal type as in 3154 // "foo<>{}", to make a pointer we must prepend "&". 3155 cand.mods = append(cand.mods, reference) 3156 } else { 3157 // If we are completing a normal type name such as "foo<>", to 3158 // make a pointer we must prepend "*". 3159 cand.mods = append(cand.mods, dereference) 3160 } 3161 return true 3162 } 3163 3164 return false 3165 } 3166 3167 var ( 3168 // "interface { Error() string }" (i.e. error) 3169 errorIntf = types.Universe.Lookup("error").Type().Underlying().(*types.Interface) 3170 3171 // "interface { String() string }" (i.e. fmt.Stringer) 3172 stringerIntf = types.NewInterfaceType([]*types.Func{ 3173 types.NewFunc(token.NoPos, nil, "String", types.NewSignature( 3174 nil, 3175 nil, 3176 types.NewTuple(types.NewParam(token.NoPos, nil, "", types.Typ[types.String])), 3177 false, 3178 )), 3179 }, nil).Complete() 3180 3181 byteType = types.Universe.Lookup("byte").Type() 3182 ) 3183 3184 // candKind returns the objKind of candType, if any. 3185 func candKind(candType types.Type) objKind { 3186 var kind objKind 3187 3188 switch t := candType.Underlying().(type) { 3189 case *types.Array: 3190 kind |= kindArray 3191 if t.Elem() == byteType { 3192 kind |= kindBytes 3193 } 3194 case *types.Slice: 3195 kind |= kindSlice 3196 if t.Elem() == byteType { 3197 kind |= kindBytes 3198 } 3199 case *types.Chan: 3200 kind |= kindChan 3201 case *types.Map: 3202 kind |= kindMap 3203 case *types.Pointer: 3204 kind |= kindPtr 3205 3206 // Some builtins handle array pointers as arrays, so just report a pointer 3207 // to an array as an array. 3208 if _, isArray := t.Elem().Underlying().(*types.Array); isArray { 3209 kind |= kindArray 3210 } 3211 case *types.Basic: 3212 switch info := t.Info(); { 3213 case info&types.IsString > 0: 3214 kind |= kindString 3215 case info&types.IsInteger > 0: 3216 kind |= kindInt 3217 case info&types.IsFloat > 0: 3218 kind |= kindFloat 3219 case info&types.IsComplex > 0: 3220 kind |= kindComplex 3221 case info&types.IsBoolean > 0: 3222 kind |= kindBool 3223 } 3224 case *types.Signature: 3225 return kindFunc 3226 } 3227 3228 if types.Implements(candType, errorIntf) { 3229 kind |= kindError 3230 } 3231 3232 if types.Implements(candType, stringerIntf) { 3233 kind |= kindStringer 3234 } 3235 3236 return kind 3237 } 3238 3239 // innermostScope returns the innermost scope for c.pos. 3240 func (c *completer) innermostScope() *types.Scope { 3241 for _, s := range c.scopes { 3242 if s != nil { 3243 return s 3244 } 3245 } 3246 return nil 3247 } 3248 3249 // isSlice reports whether the object's underlying type is a slice. 3250 func isSlice(obj types.Object) bool { 3251 if obj != nil && obj.Type() != nil { 3252 if _, ok := obj.Type().Underlying().(*types.Slice); ok { 3253 return true 3254 } 3255 } 3256 return false 3257 } 3258 3259 // forEachPackageMember calls f(tok, id, fn) for each package-level 3260 // TYPE/VAR/CONST/FUNC declaration in the Go source file, based on a 3261 // quick partial parse. fn is non-nil only for function declarations. 3262 // The AST position information is garbage. 3263 func forEachPackageMember(content []byte, f func(tok token.Token, id *ast.Ident, fn *ast.FuncDecl)) { 3264 purged := goplsastutil.PurgeFuncBodies(content) 3265 file, _ := parser.ParseFile(token.NewFileSet(), "", purged, 0) 3266 for _, decl := range file.Decls { 3267 switch decl := decl.(type) { 3268 case *ast.GenDecl: 3269 for _, spec := range decl.Specs { 3270 switch spec := spec.(type) { 3271 case *ast.ValueSpec: // var/const 3272 for _, id := range spec.Names { 3273 f(decl.Tok, id, nil) 3274 } 3275 case *ast.TypeSpec: 3276 f(decl.Tok, spec.Name, nil) 3277 } 3278 } 3279 case *ast.FuncDecl: 3280 if decl.Recv == nil { 3281 f(token.FUNC, decl.Name, decl) 3282 } 3283 } 3284 } 3285 }