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