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.
5 // Package completion provides core functionality for code completion in Go
25 "golang.org/x/tools/go/ast/astutil"
26 "golang.org/x/tools/internal/event"
27 "golang.org/x/tools/internal/imports"
28 "golang.org/x/tools/internal/lsp/fuzzy"
29 "golang.org/x/tools/internal/lsp/protocol"
30 "golang.org/x/tools/internal/lsp/snippet"
31 "golang.org/x/tools/internal/lsp/source"
32 errors "golang.org/x/xerrors"
35 type CompletionItem struct {
36 // Label is the primary text the user sees for this completion item.
39 // Detail is supplemental information to present to the user.
40 // This often contains the type or return type of the completion item.
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.
48 Kind protocol.CompletionItemKind
50 // An optional array of additional TextEdits that are applied when
51 // selecting this completion.
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
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.
63 // Score is the internal relevance score.
64 // A higher score indicates that this completion item is more relevant.
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:
71 // func foo(a, b, c int)
75 // foo(${1:a int}, ${2: b int}, ${3: c int})
77 // If Placeholders is false in the CompletionOptions, the above
78 // snippet would instead be:
81 snippet *snippet.Builder
83 // Documentation is the documentation for the completion item.
86 // obj is the object from which this candidate was derived, if any.
87 // obj is for internal use only.
91 // completionOptions holds completion specific configuration.
92 type completionOptions struct {
95 fullDocumentation bool
98 matcher source.Matcher
102 // Snippet is a convenience returns the snippet if available, otherwise
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()
112 // Scoring constants are used for weighting the relevance of different candidates.
114 // stdScore is the base score for all completion items.
115 stdScore float64 = 1.0
117 // highScore indicates a very relevant completion item.
118 highScore float64 = 10.0
120 // lowScore indicates an irrelevant or not useful completion item.
121 lowScore float64 = 0.01
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)
131 // prefixMatcher implements case sensitive prefix matching.
132 type prefixMatcher string
134 func (pm prefixMatcher) Score(candidateLabel string) float32 {
135 if strings.HasPrefix(candidateLabel, string(pm)) {
141 // insensitivePrefixMatcher implements case insensitive prefix matching.
142 type insensitivePrefixMatcher string
144 func (ipm insensitivePrefixMatcher) Score(candidateLabel string) float32 {
145 if strings.HasPrefix(strings.ToLower(candidateLabel), string(ipm)) {
151 // completer contains the necessary information for a single completion request.
152 type completer struct {
153 snapshot source.Snapshot
156 opts *completionOptions
158 // completionContext contains information about the trigger for this
159 // completion request.
160 completionContext completionContext
162 // fh is a handle to the file associated with this completion request.
165 // filename is the name of the file associated with this completion request.
168 // file is the AST of the file associated with this completion request.
171 // pos is the position at which the request was triggered.
174 // path is the path of AST nodes enclosing the position.
177 // seen is the map that ensures we do not return duplicate results.
178 seen map[types.Object]bool
180 // items is the list of completion items returned.
181 items []CompletionItem
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
188 // surrounding describes the identifier surrounding the position.
189 surrounding *Selection
191 // inference contains information we've inferred about ideal
192 // candidates such as the candidate's type.
193 inference candidateInference
195 // enclosingFunc contains information about the function enclosing
197 enclosingFunc *funcInfo
199 // enclosingCompositeLiteral contains information about the composite literal
200 // enclosing the position.
201 enclosingCompositeLiteral *compLitInfo
203 // deepState contains the current state of our deep completion search.
204 deepState deepCompletionState
206 // matcher matches the candidates against the surrounding prefix.
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
214 // mapper converts the positions in the file from which the completion originated.
215 mapper *protocol.ColumnMapper
217 // startTime is when we started processing this completion request. It does
218 // not include any time the request spent in the queue.
222 // funcInfo holds info about a function object.
223 type funcInfo struct {
224 // sig is the function declaration enclosing the position.
227 // body is the function's body.
231 type compLitInfo struct {
232 // cl is the *ast.CompositeLit enclosing the position.
235 // clType is the type of cl.
238 // kv is the *ast.KeyValueExpr enclosing the position, if any.
241 // inKey is true if we are certain the position is in the key side
242 // of a key-value pair.
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
252 type importInfo struct {
258 type methodSetKey struct {
263 type completionContext struct {
264 // triggerCharacter is the character used to trigger completion at current
266 triggerCharacter string
268 // triggerKind is information about how a completion was triggered.
269 triggerKind protocol.CompletionTriggerKind
271 // commentCompletion is true if we are completing a comment.
272 commentCompletion bool
274 // packageCompletion is true if we are completing a package name.
275 packageCompletion bool
278 // A Selection represents the cursor position and surrounding identifier.
279 type Selection struct {
285 func (p Selection) Content() string {
289 func (p Selection) Start() token.Pos {
290 return p.MappedRange.SpanRange().Start
293 func (p Selection) End() token.Pos {
294 return p.MappedRange.SpanRange().End
297 func (p Selection) Prefix() string {
298 return p.content[:p.cursor-p.SpanRange().Start]
301 func (p Selection) Suffix() string {
302 return p.content[p.cursor-p.SpanRange().Start:]
305 func (c *completer) setSurrounding(ident *ast.Ident) {
306 if c.surrounding != nil {
309 if !(ident.Pos() <= c.pos && c.pos <= ident.End()) {
313 c.surrounding = &Selection{
316 // Overwrite the prefix only.
317 MappedRange: source.NewMappedRange(c.snapshot.FileSet(), c.mapper, ident.Pos(), ident.End()),
320 c.setMatcherFromPrefix(c.surrounding.Prefix())
323 func (c *completer) setMatcherFromPrefix(prefix string) {
324 switch c.opts.matcher {
326 c.matcher = fuzzy.NewMatcher(prefix)
327 case source.CaseSensitive:
328 c.matcher = prefixMatcher(prefix)
330 c.matcher = insensitivePrefixMatcher(strings.ToLower(prefix))
334 func (c *completer) getSurrounding() *Selection {
335 if c.surrounding == nil {
336 c.surrounding = &Selection{
339 MappedRange: source.NewMappedRange(c.snapshot.FileSet(), c.mapper, c.pos, c.pos),
345 // candidate represents a completion candidate.
346 type candidate struct {
347 // obj is the types.Object to complete to.
350 // score is used to rank candidates.
353 // name is the deep object name path, e.g. "foo.bar"
356 // detail is additional information about this item. If not specified,
357 // defaults to type string for the object.
360 // path holds the path from the search root (excluding the candidate
361 // itself) for a deep candidate.
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.
369 // expandFuncCall is true if obj should be invoked in the completion.
370 // For example, expandFuncCall=true yields "foo()", expandFuncCall=false yields "foo".
373 // takeAddress is true if the completion should take a pointer to obj.
374 // For example, takeAddress=true yields "&foo", takeAddress=false yields "foo".
377 // addressable is true if a pointer can be taken to the candidate.
380 // makePointer is true if the candidate type name T should be made into *T.
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.
387 // variadic is true if this candidate fills a variadic param and
388 // needs "..." appended.
391 // convertTo is a type that this candidate should be cast to. For
392 // example, if convertTo is float64, "foo" should be formatted as
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.
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
404 type ErrIsDefinition struct {
408 func (e ErrIsDefinition) Error() string {
409 msg := "this is a definition"
411 msg += " of " + e.objStr
416 // Completion returns a list of possible candidates for completion, given a
417 // a file and a position.
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")
426 startTime := time.Now()
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)
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)
440 return items, surrounding, nil
442 spn, err := pgf.Mapper.PointSpan(protoPos)
446 rng, err := spn.Range(pgf.Mapper.Converter)
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)
454 return nil, nil, errors.Errorf("cannot find node enclosing position")
459 // Check if completion at this position is valid. If not, return early.
460 switch n := path[0].(type) {
462 // Skip completion inside literals except for ImportSpec
464 if _, ok := path[1].(*ast.ImportSpec); ok {
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...<>").
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.
486 qual := types.RelativeTo(pkg.GetTypes())
487 objStr = types.ObjectString(obj, qual)
489 return nil, nil, ErrIsDefinition{objStr: objStr}
494 opts := snapshot.View().Options()
498 qf: source.Qualifier(pgf.File, pkg.GetTypes(), pkg.GetTypesInfo()),
499 completionContext: completionContext{
500 triggerCharacter: protoContext.TriggerCharacter,
501 triggerKind: protoContext.TriggerKind,
504 filename: fh.URI().Filename(),
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,
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,
523 // default to a matcher that always matches
524 matcher: prefixMatcher(""),
525 methodSetCache: make(map[methodSetKey]*types.MethodSet),
527 startTime: startTime,
530 var cancel context.CancelFunc
531 if c.opts.budget == 0 {
532 ctx, cancel = context.WithCancel(ctx)
534 // timeoutDuration is the completion budget remaining. If less than
536 timeoutDuration := time.Until(c.startTime.Add(c.opts.budget))
537 if timeoutDuration < 10*time.Millisecond {
538 timeoutDuration = 10 * time.Millisecond
540 ctx, cancel = context.WithTimeout(ctx, timeoutDuration)
544 if surrounding := c.containingIdent(pgf.Src); surrounding != nil {
545 c.setSurrounding(surrounding)
548 c.inference = expectedCandidate(ctx, c)
550 err = c.collectCompletions(ctx)
555 // Deep search collected candidates and their members for more candidates.
557 c.deepState.searchQueue = nil
559 for _, callback := range c.completionCallbacks {
560 if err := c.snapshot.RunProcessEnvFunc(ctx, callback); err != nil {
565 // Search candidates populated by expensive operations like
566 // unimportedMembers etc. for more completion items.
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()
575 return c.items, c.getSurrounding(), nil
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()) {
586 return c.populateImportCompletions(ctx, importSpec)
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)
597 // Struct literals are handled entirely separately.
598 if c.wantStructFieldCompletions() {
599 // If we are definitely completing a struct field name, deep completions
601 if c.enclosingCompositeLiteral.inKey {
602 c.deepState.enabled = false
604 return c.structLiteralFieldName(ctx)
607 if lt := c.wantLabelCompletion(); lt != labelNone {
612 if c.emptySwitchStmt() {
613 // Empty switch statements only admit "default" and "case" keywords.
614 c.addKeywordItems(map[string]bool{}, highScore, CASE, DEFAULT)
618 switch n := c.path[0].(type) {
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)
626 return c.lexical(ctx)
627 // The function name hasn't been typed yet, but the parens are there:
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()
638 // fallback to lexical completions
639 return c.lexical(ctx)
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 {
654 pos, tkn, lit := c.scanToken(src)
659 fakeIdent := &ast.Ident{Name: lit, NamePos: pos}
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.
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
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<>)".
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)
684 var s scanner.Scanner
685 s.Init(tok, contents, nil, 0)
687 tknPos, tkn, lit := s.Scan()
688 if tkn == token.EOF || tknPos >= c.pos {
689 return token.NoPos, token.ILLEGAL, ""
692 if len(lit) > 0 && tknPos <= c.pos && c.pos <= tknPos+token.Pos(len(lit)) {
693 return tknPos, tkn, lit
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
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
711 // emptySwitchStmt reports whether pos is in an empty switch or select
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 {
719 switch c.path[1].(type) {
720 case *ast.SwitchStmt, *ast.TypeSwitchStmt, *ast.SelectStmt:
727 // populateImportCompletions yields completions for an import path around the cursor.
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 if !strings.HasPrefix(searchImport.Path.Value, `"`) {
740 // deepSearch is not valuable for import completions.
741 c.deepState.enabled = false
743 importPath := searchImport.Path.Value
745 // Extract the text between the quotes (if any) in an import spec.
746 // prefix is the part of import path before the cursor.
747 prefixEnd := c.pos - searchImport.Path.Pos()
748 prefix := strings.Trim(importPath[:prefixEnd], `"`)
750 // The number of directories in the import path gives us the depth at
752 depth := len(strings.Split(prefix, "/")) - 1
754 content := importPath
755 start, end := searchImport.Path.Pos(), searchImport.Path.End()
756 namePrefix, nameSuffix := `"`, `"`
757 // If a starting quote is present, adjust surrounding to either after the
758 // cursor or after the first slash (/), except if cursor is at the starting
759 // quote. Otherwise we provide a completion including the starting quote.
760 if strings.HasPrefix(importPath, `"`) && c.pos > searchImport.Path.Pos() {
761 content = content[1:]
764 // Adjust textEdit start to replacement range. For ex: if current
765 // path was "golang.or/x/to<>ols/internal/", where <> is the cursor
766 // position, start of the replacement range would be after
768 path := strings.SplitAfter(prefix, "/")
769 numChars := len(strings.Join(path[:len(path)-1], ""))
770 content = content[numChars:]
771 start += token.Pos(numChars)
776 // We won't provide an ending quote if one is already present, except if
777 // cursor is after the ending quote but still in import spec. This is
778 // because cursor has to be in our textEdit range.
779 if strings.HasSuffix(importPath, `"`) && c.pos < searchImport.Path.End() {
781 content = content[:len(content)-1]
785 c.surrounding = &Selection{
788 MappedRange: source.NewMappedRange(c.snapshot.FileSet(), c.mapper, start, end),
791 seenImports := make(map[string]struct{})
792 for _, importSpec := range c.file.Imports {
793 if importSpec.Path.Value == importPath {
796 seenImportPath, err := strconv.Unquote(importSpec.Path.Value)
800 seenImports[seenImportPath] = struct{}{}
803 var mu sync.Mutex // guard c.items locally, since searchImports is called in parallel
804 seen := make(map[string]struct{})
805 searchImports := func(pkg imports.ImportFix) {
806 path := pkg.StmtInfo.ImportPath
807 if _, ok := seenImports[path]; ok {
811 // Any package path containing fewer directories than the search
812 // prefix is not a match.
813 pkgDirList := strings.Split(path, "/")
814 if len(pkgDirList) < depth+1 {
817 pkgToConsider := strings.Join(pkgDirList[:depth+1], "/")
819 name := pkgDirList[depth]
820 // if we're adding an opening quote to completion too, set name to full
821 // package path since we'll need to overwrite that range.
822 if namePrefix == `"` {
826 score := pkg.Relevance
827 if len(pkgDirList)-1 == depth {
830 // For incomplete package paths, add a terminal slash to indicate that the
831 // user should keep triggering completions.
836 if _, ok := seen[pkgToConsider]; ok {
839 seen[pkgToConsider] = struct{}{}
844 name = namePrefix + name + nameSuffix
845 obj := types.NewPkgName(0, nil, name, types.NewPackage(pkgToConsider, name))
846 c.deepState.enqueue(candidate{
848 detail: fmt.Sprintf("%q", pkgToConsider),
853 c.completionCallbacks = append(c.completionCallbacks, func(opts *imports.Options) error {
854 return imports.GetImportPaths(ctx, searchImports, prefix, c.filename, c.pkg.GetTypes().Name(), opts.Env)
859 // populateCommentCompletions yields completions for comments preceding or in declarations.
860 func (c *completer) populateCommentCompletions(ctx context.Context, comment *ast.CommentGroup) {
861 // If the completion was triggered by a period, ignore it. These types of
862 // completions will not be useful in comments.
863 if c.completionContext.triggerCharacter == "." {
867 // Using the comment position find the line after
868 file := c.snapshot.FileSet().File(comment.End())
873 // Deep completion doesn't work properly in comments since we don't
874 // have a type object to complete further.
875 c.deepState.enabled = false
876 c.completionContext.commentCompletion = true
878 // Documentation isn't useful in comments, since it might end up being the
880 c.opts.documentation = false
882 commentLine := file.Line(comment.End())
884 // comment is valid, set surrounding as word boundaries around cursor
885 c.setSurroundingForComment(comment)
887 // Using the next line pos, grab and parse the exported symbol on that line
888 for _, n := range c.file.Decls {
889 declLine := file.Line(n.Pos())
890 // if the comment is not in, directly above or on the same line as a declaration
891 if declLine != commentLine && declLine != commentLine+1 &&
892 !(n.Pos() <= comment.Pos() && comment.End() <= n.End()) {
895 switch node := n.(type) {
896 // handle const, vars, and types
898 for _, spec := range node.Specs {
899 switch spec := spec.(type) {
901 for _, name := range spec.Names {
902 if name.String() == "_" {
905 obj := c.pkg.GetTypesInfo().ObjectOf(name)
906 c.deepState.enqueue(candidate{obj: obj, score: stdScore})
909 // add TypeSpec fields to completion
910 switch typeNode := spec.Type.(type) {
911 case *ast.StructType:
912 c.addFieldItems(ctx, typeNode.Fields)
914 c.addFieldItems(ctx, typeNode.Params)
915 c.addFieldItems(ctx, typeNode.Results)
916 case *ast.InterfaceType:
917 c.addFieldItems(ctx, typeNode.Methods)
920 if spec.Name.String() == "_" {
924 obj := c.pkg.GetTypesInfo().ObjectOf(spec.Name)
925 // Type name should get a higher score than fields but not highScore by default
926 // since field near a comment cursor gets a highScore
927 score := stdScore * 1.1
928 // If type declaration is on the line after comment, give it a highScore.
929 if declLine == commentLine+1 {
933 c.deepState.enqueue(candidate{obj: obj, score: score})
938 c.addFieldItems(ctx, node.Recv)
939 c.addFieldItems(ctx, node.Type.Params)
940 c.addFieldItems(ctx, node.Type.Results)
942 // collect receiver struct fields
943 if node.Recv != nil {
944 for _, fields := range node.Recv.List {
945 for _, name := range fields.Names {
946 obj := c.pkg.GetTypesInfo().ObjectOf(name)
951 recvType := obj.Type().Underlying()
952 if ptr, ok := recvType.(*types.Pointer); ok {
953 recvType = ptr.Elem()
955 recvStruct, ok := recvType.Underlying().(*types.Struct)
959 for i := 0; i < recvStruct.NumFields(); i++ {
960 field := recvStruct.Field(i)
961 c.deepState.enqueue(candidate{obj: field, score: lowScore})
967 if node.Name.String() == "_" {
971 obj := c.pkg.GetTypesInfo().ObjectOf(node.Name)
972 if obj == nil || obj.Pkg() != nil && obj.Pkg() != c.pkg.GetTypes() {
976 c.deepState.enqueue(candidate{obj: obj, score: highScore})
981 // sets word boundaries surrounding a cursor for a comment
982 func (c *completer) setSurroundingForComment(comments *ast.CommentGroup) {
983 var cursorComment *ast.Comment
984 for _, comment := range comments.List {
985 if c.pos >= comment.Pos() && c.pos <= comment.End() {
986 cursorComment = comment
990 // if cursor isn't in the comment
991 if cursorComment == nil {
995 // index of cursor in comment text
996 cursorOffset := int(c.pos - cursorComment.Pos())
997 start, end := cursorOffset, cursorOffset
998 for start > 0 && isValidIdentifierChar(cursorComment.Text[start-1]) {
1001 for end < len(cursorComment.Text) && isValidIdentifierChar(cursorComment.Text[end]) {
1005 c.surrounding = &Selection{
1006 content: cursorComment.Text[start:end],
1008 MappedRange: source.NewMappedRange(c.snapshot.FileSet(), c.mapper,
1009 token.Pos(int(cursorComment.Slash)+start), token.Pos(int(cursorComment.Slash)+end)),
1011 c.setMatcherFromPrefix(c.surrounding.Prefix())
1014 // isValidIdentifierChar returns true if a byte is a valid go identifier
1015 // character, i.e. unicode letter or digit or underscore.
1016 func isValidIdentifierChar(char byte) bool {
1017 charRune := rune(char)
1018 return unicode.In(charRune, unicode.Letter, unicode.Digit) || char == '_'
1021 // adds struct fields, interface methods, function declaration fields to completion
1022 func (c *completer) addFieldItems(ctx context.Context, fields *ast.FieldList) {
1027 cursor := c.surrounding.cursor
1028 for _, field := range fields.List {
1029 for _, name := range field.Names {
1030 if name.String() == "_" {
1033 obj := c.pkg.GetTypesInfo().ObjectOf(name)
1038 // if we're in a field comment/doc, score that field as more relevant
1040 if field.Comment != nil && field.Comment.Pos() <= cursor && cursor <= field.Comment.End() {
1042 } else if field.Doc != nil && field.Doc.Pos() <= cursor && cursor <= field.Doc.End() {
1046 c.deepState.enqueue(candidate{obj: obj, score: score})
1051 func (c *completer) wantStructFieldCompletions() bool {
1052 clInfo := c.enclosingCompositeLiteral
1057 return clInfo.isStruct() && (clInfo.inKey || clInfo.maybeInFieldName)
1060 func (c *completer) wantTypeName() bool {
1061 return !c.completionContext.commentCompletion && c.inference.typeName.wantTypeName
1064 // See https://golang.org/issue/36001. Unimported completions are expensive.
1066 maxUnimportedPackageNames = 5
1067 unimportedMemberTarget = 100
1070 // selector finds completions for the specified selector expression.
1071 func (c *completer) selector(ctx context.Context, sel *ast.SelectorExpr) error {
1072 c.inference.objChain = objChain(c.pkg.GetTypesInfo(), sel.X)
1074 // Is sel a qualified identifier?
1075 if id, ok := sel.X.(*ast.Ident); ok {
1076 if pkgName, ok := c.pkg.GetTypesInfo().Uses[id].(*types.PkgName); ok {
1077 var pkg source.Package
1078 for _, imp := range c.pkg.Imports() {
1079 if imp.PkgPath() == pkgName.Imported().Path() {
1083 // If the package is not imported, try searching for unimported
1085 if pkg == nil && c.opts.unimported {
1086 if err := c.unimportedMembers(ctx, id); err != nil {
1090 candidates := c.packageMembers(pkgName.Imported(), stdScore, nil)
1091 for _, cand := range candidates {
1092 c.deepState.enqueue(cand)
1098 // Invariant: sel is a true selector.
1099 tv, ok := c.pkg.GetTypesInfo().Types[sel.X]
1101 candidates := c.methodsAndFields(tv.Type, tv.Addressable(), nil)
1102 for _, cand := range candidates {
1103 c.deepState.enqueue(cand)
1108 // Try unimported packages.
1109 if id, ok := sel.X.(*ast.Ident); ok && c.opts.unimported {
1110 if err := c.unimportedMembers(ctx, id); err != nil {
1117 func (c *completer) unimportedMembers(ctx context.Context, id *ast.Ident) error {
1118 // Try loaded packages first. They're relevant, fast, and fully typed.
1119 known, err := c.snapshot.CachedImportPaths(ctx)
1125 for path, pkg := range known {
1126 if pkg.GetTypes().Name() != id.Name {
1129 paths = append(paths, path)
1132 var relevances map[string]float64
1133 if len(paths) != 0 {
1134 if err := c.snapshot.RunProcessEnvFunc(ctx, func(opts *imports.Options) error {
1136 relevances, err = imports.ScoreImportPaths(ctx, opts.Env, paths)
1142 sort.Slice(paths, func(i, j int) bool {
1143 return relevances[paths[i]] > relevances[paths[j]]
1146 for _, path := range paths {
1148 if pkg.GetTypes().Name() != id.Name {
1155 if imports.ImportPathToAssumedName(path) != pkg.GetTypes().Name() {
1156 imp.name = pkg.GetTypes().Name()
1158 candidates := c.packageMembers(pkg.GetTypes(), unimportedScore(relevances[path]), imp)
1159 for _, cand := range candidates {
1160 c.deepState.enqueue(cand)
1162 if len(c.items) >= unimportedMemberTarget {
1167 ctx, cancel := context.WithCancel(ctx)
1170 add := func(pkgExport imports.PackageExport) {
1173 if _, ok := known[pkgExport.Fix.StmtInfo.ImportPath]; ok {
1174 return // We got this one above.
1177 // Continue with untyped proposals.
1178 pkg := types.NewPackage(pkgExport.Fix.StmtInfo.ImportPath, pkgExport.Fix.IdentName)
1179 for _, export := range pkgExport.Exports {
1180 score := unimportedScore(pkgExport.Fix.Relevance)
1181 c.deepState.enqueue(candidate{
1182 obj: types.NewVar(0, pkg, export, nil),
1185 importPath: pkgExport.Fix.StmtInfo.ImportPath,
1186 name: pkgExport.Fix.StmtInfo.Name,
1190 if len(c.items) >= unimportedMemberTarget {
1195 c.completionCallbacks = append(c.completionCallbacks, func(opts *imports.Options) error {
1197 return imports.GetPackageExports(ctx, add, id.Name, c.filename, c.pkg.GetTypes().Name(), opts.Env)
1202 // unimportedScore returns a score for an unimported package that is generally
1203 // lower than other candidates.
1204 func unimportedScore(relevance float64) float64 {
1205 return (stdScore + .1*relevance) / 2
1208 func (c *completer) packageMembers(pkg *types.Package, score float64, imp *importInfo) []candidate {
1209 var candidates []candidate
1210 scope := pkg.Scope()
1211 for _, name := range scope.Names() {
1212 obj := scope.Lookup(name)
1213 candidates = append(candidates, candidate{
1217 addressable: isVar(obj),
1223 func (c *completer) methodsAndFields(typ types.Type, addressable bool, imp *importInfo) []candidate {
1224 mset := c.methodSetCache[methodSetKey{typ, addressable}]
1226 if addressable && !types.IsInterface(typ) && !isPointer(typ) {
1227 // Add methods of *T, which includes methods with receiver T.
1228 mset = types.NewMethodSet(types.NewPointer(typ))
1230 // Add methods of T.
1231 mset = types.NewMethodSet(typ)
1233 c.methodSetCache[methodSetKey{typ, addressable}] = mset
1236 var candidates []candidate
1237 for i := 0; i < mset.Len(); i++ {
1238 candidates = append(candidates, candidate{
1239 obj: mset.At(i).Obj(),
1242 addressable: addressable || isPointer(typ),
1247 eachField(typ, func(v *types.Var) {
1248 candidates = append(candidates, candidate{
1250 score: stdScore - 0.01,
1252 addressable: addressable || isPointer(typ),
1259 // lexical finds completions in the lexical environment.
1260 func (c *completer) lexical(ctx context.Context) error {
1261 scopes := source.CollectScopes(c.pkg.GetTypesInfo(), c.path, c.pos)
1262 scopes = append(scopes, c.pkg.GetTypes().Scope(), types.Universe)
1265 builtinIota = types.Universe.Lookup("iota")
1266 builtinNil = types.Universe.Lookup("nil")
1269 // Track seen variables to avoid showing completions for shadowed variables.
1270 // This works since we look at scopes from innermost to outermost.
1271 seen := make(map[string]struct{})
1273 // Process scopes innermost first.
1274 for i, scope := range scopes {
1280 for _, name := range scope.Names() {
1281 declScope, obj := scope.LookupParent(name, c.pos)
1282 if declScope != scope {
1283 continue // Name was declared in some enclosing scope, or not at all.
1286 // If obj's type is invalid, find the AST node that defines the lexical block
1287 // containing the declaration of obj. Don't resolve types for packages.
1288 if !isPkgName(obj) && !typeIsValid(obj.Type()) {
1289 // Match the scope to its ast.Node. If the scope is the package scope,
1290 // use the *ast.File as the starting node.
1292 if i < len(c.path) {
1294 } else if i == len(c.path) { // use the *ast.File for package scope
1298 if resolved := resolveInvalid(c.snapshot.FileSet(), obj, node, c.pkg.GetTypesInfo()); resolved != nil {
1304 // Don't use LHS of decl in RHS.
1305 for _, ident := range enclosingDeclLHS(c.path) {
1306 if obj.Pos() == ident.Pos() {
1311 // Don't suggest "iota" outside of const decls.
1312 if obj == builtinIota && !c.inConstDecl() {
1316 // Rank outer scopes lower than inner.
1317 score := stdScore * math.Pow(.99, float64(i))
1319 // Dowrank "nil" a bit so it is ranked below more interesting candidates.
1320 if obj == builtinNil {
1324 // If we haven't already added a candidate for an object with this name.
1325 if _, ok := seen[obj.Name()]; !ok {
1326 seen[obj.Name()] = struct{}{}
1327 c.deepState.enqueue(candidate{
1330 addressable: isVar(obj),
1336 if c.inference.objType != nil {
1337 if named, _ := source.Deref(c.inference.objType).(*types.Named); named != nil {
1338 // If we expected a named type, check the type's package for
1339 // completion items. This is useful when the current file hasn't
1340 // imported the type's package yet.
1342 if named.Obj() != nil && named.Obj().Pkg() != nil {
1343 pkg := named.Obj().Pkg()
1345 // Make sure the package name isn't already in use by another
1346 // object, and that this file doesn't import the package yet.
1347 if _, ok := seen[pkg.Name()]; !ok && pkg != c.pkg.GetTypes() && !alreadyImports(c.file, pkg.Path()) {
1348 seen[pkg.Name()] = struct{}{}
1349 obj := types.NewPkgName(0, nil, pkg.Name(), pkg)
1351 importPath: pkg.Path(),
1353 if imports.ImportPathToAssumedName(pkg.Path()) != pkg.Name() {
1354 imp.name = pkg.Name()
1356 c.deepState.enqueue(candidate{
1366 if c.opts.unimported {
1367 if err := c.unimportedPackages(ctx, seen); err != nil {
1372 if t := c.inference.objType; t != nil {
1375 // If we have an expected type and it is _not_ a named type,
1376 // handle it specially. Non-named types like "[]int" will never be
1377 // considered via a lexical search, so we need to directly inject
1379 if _, named := t.(*types.Named); !named {
1380 // If our expected type is "[]int", this will add a literal
1381 // candidate of "[]int{}".
1382 c.literal(ctx, t, nil)
1384 if _, isBasic := t.(*types.Basic); !isBasic {
1385 // If we expect a non-basic type name (e.g. "[]int"), hack up
1386 // a named type whose name is literally "[]int". This allows
1387 // us to reuse our object based completion machinery.
1388 fakeNamedType := candidate{
1389 obj: types.NewTypeName(token.NoPos, nil, types.TypeString(t, c.qf), t),
1392 // Make sure the type name matches before considering
1393 // candidate. This cuts down on useless candidates.
1394 if c.matchingTypeName(&fakeNamedType) {
1395 c.deepState.enqueue(fakeNamedType)
1401 // Add keyword completion items appropriate in the current context.
1402 c.addKeywordCompletions()
1407 func (c *completer) unimportedPackages(ctx context.Context, seen map[string]struct{}) error {
1409 if c.surrounding != nil {
1410 prefix = c.surrounding.Prefix()
1414 known, err := c.snapshot.CachedImportPaths(ctx)
1419 for path, pkg := range known {
1420 if !strings.HasPrefix(pkg.GetTypes().Name(), prefix) {
1423 paths = append(paths, path)
1426 var relevances map[string]float64
1427 if len(paths) != 0 {
1428 if err := c.snapshot.RunProcessEnvFunc(ctx, func(opts *imports.Options) error {
1430 relevances, err = imports.ScoreImportPaths(ctx, opts.Env, paths)
1436 sort.Slice(paths, func(i, j int) bool {
1437 return relevances[paths[i]] > relevances[paths[j]]
1440 for _, path := range paths {
1442 if _, ok := seen[pkg.GetTypes().Name()]; ok {
1449 if imports.ImportPathToAssumedName(path) != pkg.GetTypes().Name() {
1450 imp.name = pkg.GetTypes().Name()
1452 if count >= maxUnimportedPackageNames {
1455 c.deepState.enqueue(candidate{
1456 obj: types.NewPkgName(0, nil, pkg.GetTypes().Name(), pkg.GetTypes()),
1457 score: unimportedScore(relevances[path]),
1463 ctx, cancel := context.WithCancel(ctx)
1466 add := func(pkg imports.ImportFix) {
1469 if _, ok := seen[pkg.IdentName]; ok {
1472 if _, ok := relevances[pkg.StmtInfo.ImportPath]; ok {
1476 if count >= maxUnimportedPackageNames {
1481 // Do not add the unimported packages to seen, since we can have
1482 // multiple packages of the same name as completion suggestions, since
1483 // only one will be chosen.
1484 obj := types.NewPkgName(0, nil, pkg.IdentName, types.NewPackage(pkg.StmtInfo.ImportPath, pkg.IdentName))
1485 c.deepState.enqueue(candidate{
1487 score: unimportedScore(pkg.Relevance),
1489 importPath: pkg.StmtInfo.ImportPath,
1490 name: pkg.StmtInfo.Name,
1495 c.completionCallbacks = append(c.completionCallbacks, func(opts *imports.Options) error {
1497 return imports.GetAllCandidates(ctx, add, prefix, c.filename, c.pkg.GetTypes().Name(), opts.Env)
1502 // alreadyImports reports whether f has an import with the specified path.
1503 func alreadyImports(f *ast.File, path string) bool {
1504 for _, s := range f.Imports {
1505 if source.ImportPath(s) == path {
1512 func (c *completer) inConstDecl() bool {
1513 for _, n := range c.path {
1514 if decl, ok := n.(*ast.GenDecl); ok && decl.Tok == token.CONST {
1521 // structLiteralFieldName finds completions for struct field names inside a struct literal.
1522 func (c *completer) structLiteralFieldName(ctx context.Context) error {
1523 clInfo := c.enclosingCompositeLiteral
1525 // Mark fields of the composite literal that have already been set,
1526 // except for the current field.
1527 addedFields := make(map[*types.Var]bool)
1528 for _, el := range clInfo.cl.Elts {
1529 if kvExpr, ok := el.(*ast.KeyValueExpr); ok {
1530 if clInfo.kv == kvExpr {
1534 if key, ok := kvExpr.Key.(*ast.Ident); ok {
1535 if used, ok := c.pkg.GetTypesInfo().Uses[key]; ok {
1536 if usedVar, ok := used.(*types.Var); ok {
1537 addedFields[usedVar] = true
1544 deltaScore := 0.0001
1545 switch t := clInfo.clType.(type) {
1547 for i := 0; i < t.NumFields(); i++ {
1549 if !addedFields[field] {
1550 c.deepState.enqueue(candidate{
1552 score: highScore - float64(i)*deltaScore,
1557 // Add lexical completions if we aren't certain we are in the key part of a
1559 if clInfo.maybeInFieldName {
1560 return c.lexical(ctx)
1563 return c.lexical(ctx)
1569 func (cl *compLitInfo) isStruct() bool {
1570 _, ok := cl.clType.(*types.Struct)
1574 // enclosingCompositeLiteral returns information about the composite literal enclosing the
1576 func enclosingCompositeLiteral(path []ast.Node, pos token.Pos, info *types.Info) *compLitInfo {
1577 for _, n := range path {
1578 switch n := n.(type) {
1579 case *ast.CompositeLit:
1580 // The enclosing node will be a composite literal if the user has just
1581 // opened the curly brace (e.g. &x{<>) or the completion request is triggered
1582 // from an already completed composite literal expression (e.g. &x{foo: 1, <>})
1584 // The position is not part of the composite literal unless it falls within the
1585 // curly braces (e.g. "foo.Foo<>Struct{}").
1586 if !(n.Lbrace < pos && pos <= n.Rbrace) {
1587 // Keep searching since we may yet be inside a composite literal.
1588 // For example "Foo{B: Ba<>{}}".
1592 tv, ok := info.Types[n]
1597 clInfo := compLitInfo{
1599 clType: source.Deref(tv.Type).Underlying(),
1606 for _, el := range n.Elts {
1607 // Remember the expression that the position falls in, if any.
1608 if el.Pos() <= pos && pos <= el.End() {
1612 if kv, ok := el.(*ast.KeyValueExpr); ok {
1614 // If expr == el then we know the position falls in this expression,
1615 // so also record kv as the enclosing *ast.KeyValueExpr.
1623 if clInfo.kv != nil {
1624 // If in a *ast.KeyValueExpr, we know we are in the key if the position
1625 // is to the left of the colon (e.g. "Foo{F<>: V}".
1626 clInfo.inKey = pos <= clInfo.kv.Colon
1628 // If we aren't in a *ast.KeyValueExpr but the composite literal has
1629 // other *ast.KeyValueExprs, we must be on the key side of a new
1630 // *ast.KeyValueExpr (e.g. "Foo{F: V, <>}").
1633 switch clInfo.clType.(type) {
1635 if len(n.Elts) == 0 {
1636 // If the struct literal is empty, next could be a struct field
1637 // name or an expression (e.g. "Foo{<>}" could become "Foo{F:}"
1638 // or "Foo{someVar}").
1639 clInfo.maybeInFieldName = true
1640 } else if len(n.Elts) == 1 {
1641 // If there is one expression and the position is in that expression
1642 // and the expression is an identifier, we may be writing a field
1643 // name or an expression (e.g. "Foo{F<>}").
1644 _, clInfo.maybeInFieldName = expr.(*ast.Ident)
1647 // If we aren't in a *ast.KeyValueExpr we must be adding a new key
1655 if breaksExpectedTypeInference(n, pos) {
1664 // enclosingFunction returns the signature and body of the function
1665 // enclosing the given position.
1666 func enclosingFunction(path []ast.Node, info *types.Info) *funcInfo {
1667 for _, node := range path {
1668 switch t := node.(type) {
1670 if obj, ok := info.Defs[t.Name]; ok {
1672 sig: obj.Type().(*types.Signature),
1677 if typ, ok := info.Types[t]; ok {
1679 sig: typ.Type.(*types.Signature),
1688 func (c *completer) expectedCompositeLiteralType() types.Type {
1689 clInfo := c.enclosingCompositeLiteral
1690 switch t := clInfo.clType.(type) {
1693 return types.Typ[types.Int]
1698 return types.Typ[types.Int]
1707 // If we are completing a key (i.e. field name), there is no expected type.
1712 // If we are in a key-value pair, but not in the key, then we must be on the
1713 // value side. The expected type of the value will be determined from the key.
1714 if clInfo.kv != nil {
1715 if key, ok := clInfo.kv.Key.(*ast.Ident); ok {
1716 for i := 0; i < t.NumFields(); i++ {
1717 if field := t.Field(i); field.Name() == key.Name {
1723 // If we aren't in a key-value pair and aren't in the key, we must be using
1724 // implicit field names.
1726 // The order of the literal fields must match the order in the struct definition.
1727 // Find the element that the position belongs to and suggest that field's type.
1728 if i := exprAtPos(c.pos, clInfo.cl.Elts); i < t.NumFields() {
1729 return t.Field(i).Type()
1736 // typeModifier represents an operator that changes the expected type.
1737 type typeModifier struct {
1745 dereference typeMod = iota // pointer indirection: "*"
1746 reference // adds level of pointer: "&" for values, "*" for type names
1747 chanRead // channel read operator ("<-")
1748 slice // make a slice type ("[]" in "[]int")
1749 array // make an array type ("[2]" in "[2]int")
1756 kindArray objKind = 1 << iota
1773 // penalizedObj represents an object that should be disfavored as a
1774 // completion candidate.
1775 type penalizedObj struct {
1776 // objChain is the full "chain", e.g. "foo.bar().baz" becomes
1777 // []types.Object{foo, bar, baz}.
1778 objChain []types.Object
1779 // penalty is score penalty in the range (0, 1).
1783 // candidateInference holds information we have inferred about a type that can be
1784 // used at the current position.
1785 type candidateInference struct {
1786 // objType is the desired type of an object used at the query position.
1789 // objKind is a mask of expected kinds of types such as "map", "slice", etc.
1792 // variadic is true if we are completing the initial variadic
1793 // parameter. For example:
1794 // append([]T{}, <>) // objType=T variadic=true
1795 // append([]T{}, T{}, <>) // objType=T variadic=false
1798 // modifiers are prefixes such as "*", "&" or "<-" that influence how
1799 // a candidate type relates to the expected type.
1800 modifiers []typeModifier
1802 // convertibleTo is a type our candidate type must be convertible to.
1803 convertibleTo types.Type
1805 // typeName holds information about the expected type name at
1806 // position, if any.
1807 typeName typeNameInference
1809 // assignees are the types that would receive a function call's
1810 // results at the position. For example:
1815 // at "<>", the assignees are [int, <invalid>].
1816 assignees []types.Type
1818 // variadicAssignees is true if we could be completing an inner
1819 // function call that fills out an outer function call's variadic
1820 // params. For example:
1822 // func foo(int, ...string) {}
1824 // foo(<>) // variadicAssignees=true
1825 // foo(bar<>) // variadicAssignees=true
1826 // foo(bar, baz<>) // variadicAssignees=false
1827 variadicAssignees bool
1829 // penalized holds expressions that should be disfavored as
1830 // candidates. For example, it tracks expressions already used in a
1831 // switch statement's other cases. Each expression is tracked using
1832 // its entire object "chain" allowing differentiation between
1833 // "a.foo" and "b.foo" when "a" and "b" are the same type.
1834 penalized []penalizedObj
1836 // objChain contains the chain of objects representing the
1837 // surrounding *ast.SelectorExpr. For example, if we are completing
1838 // "foo.bar.ba<>", objChain will contain []types.Object{foo, bar}.
1839 objChain []types.Object
1842 // typeNameInference holds information about the expected type name at
1844 type typeNameInference struct {
1845 // wantTypeName is true if we expect the name of a type.
1848 // modifiers are prefixes such as "*", "&" or "<-" that influence how
1849 // a candidate type relates to the expected type.
1850 modifiers []typeModifier
1852 // assertableFrom is a type that must be assertable to our candidate type.
1853 assertableFrom types.Type
1855 // wantComparable is true if we want a comparable type.
1858 // seenTypeSwitchCases tracks types that have already been used by
1859 // the containing type switch.
1860 seenTypeSwitchCases []types.Type
1862 // compLitType is true if we are completing a composite literal type
1863 // name, e.g "foo<>{}".
1867 // expectedCandidate returns information about the expected candidate
1868 // for an expression at the query position.
1869 func expectedCandidate(ctx context.Context, c *completer) (inf candidateInference) {
1870 inf.typeName = expectTypeName(c)
1872 if c.enclosingCompositeLiteral != nil {
1873 inf.objType = c.expectedCompositeLiteralType()
1877 for i, node := range c.path {
1878 switch node := node.(type) {
1879 case *ast.BinaryExpr:
1880 // Determine if query position comes from left or right of op.
1882 if c.pos < node.OpPos {
1885 if tv, ok := c.pkg.GetTypesInfo().Types[e]; ok {
1887 case token.LAND, token.LOR:
1888 // Don't infer "bool" type for "&&" or "||". Often you want
1889 // to compose a boolean expression from non-boolean
1892 inf.objType = tv.Type
1896 case *ast.AssignStmt:
1897 // Only rank completions if you are on the right side of the token.
1898 if c.pos > node.TokPos {
1899 i := exprAtPos(c.pos, node.Rhs)
1900 if i >= len(node.Lhs) {
1901 i = len(node.Lhs) - 1
1903 if tv, ok := c.pkg.GetTypesInfo().Types[node.Lhs[i]]; ok {
1904 inf.objType = tv.Type
1907 // If we have a single expression on the RHS, record the LHS
1908 // assignees so we can favor multi-return function calls with
1909 // matching result values.
1910 if len(node.Rhs) <= 1 {
1911 for _, lhs := range node.Lhs {
1912 inf.assignees = append(inf.assignees, c.pkg.GetTypesInfo().TypeOf(lhs))
1915 // Otherwse, record our single assignee, even if its type is
1916 // not available. We use this info to downrank functions
1917 // with the wrong number of result values.
1918 inf.assignees = append(inf.assignees, c.pkg.GetTypesInfo().TypeOf(node.Lhs[i]))
1922 case *ast.ValueSpec:
1923 if node.Type != nil && c.pos > node.Type.End() {
1924 inf.objType = c.pkg.GetTypesInfo().TypeOf(node.Type)
1928 // Only consider CallExpr args if position falls between parens.
1929 if node.Lparen < c.pos && c.pos <= node.Rparen {
1930 // For type conversions like "int64(foo)" we can only infer our
1931 // desired type is convertible to int64.
1932 if typ := typeConversion(node, c.pkg.GetTypesInfo()); typ != nil {
1933 inf.convertibleTo = typ
1937 if tv, ok := c.pkg.GetTypesInfo().Types[node.Fun]; ok {
1938 if sig, ok := tv.Type.(*types.Signature); ok {
1939 numParams := sig.Params().Len()
1944 exprIdx := exprAtPos(c.pos, node.Args)
1946 // If we have one or zero arg expressions, we may be
1947 // completing to a function call that returns multiple
1948 // values, in turn getting passed in to the surrounding
1949 // call. Record the assignees so we can favor function
1950 // calls that return matching values.
1951 if len(node.Args) <= 1 && exprIdx == 0 {
1952 for i := 0; i < sig.Params().Len(); i++ {
1953 inf.assignees = append(inf.assignees, sig.Params().At(i).Type())
1956 // Record that we may be completing into variadic parameters.
1957 inf.variadicAssignees = sig.Variadic()
1960 // Make sure not to run past the end of expected parameters.
1961 if exprIdx >= numParams {
1962 inf.objType = sig.Params().At(numParams - 1).Type()
1964 inf.objType = sig.Params().At(exprIdx).Type()
1967 if sig.Variadic() && exprIdx >= (numParams-1) {
1968 // If we are completing a variadic param, deslice the variadic type.
1969 inf.objType = deslice(inf.objType)
1970 // Record whether we are completing the initial variadic param.
1971 inf.variadic = exprIdx == numParams-1 && len(node.Args) <= numParams
1973 // Check if we can infer object kind from printf verb.
1974 inf.objKind |= printfArgKind(c.pkg.GetTypesInfo(), node, exprIdx)
1979 if funIdent, ok := node.Fun.(*ast.Ident); ok {
1980 obj := c.pkg.GetTypesInfo().ObjectOf(funIdent)
1982 if obj != nil && obj.Parent() == types.Universe {
1983 // Defer call to builtinArgType so we can provide it the
1984 // inferred type from its parent node.
1986 inf = c.builtinArgType(obj, node, inf)
1987 inf.objKind = c.builtinArgKind(ctx, obj, node)
1990 // The expected type of builtin arguments like append() is
1991 // the expected type of the builtin call itself. For
1994 // var foo []int = append(<>)
1996 // To find the expected type at <> we "skip" the append()
1997 // node and get the expected type one level up, which is
2005 case *ast.ReturnStmt:
2006 if c.enclosingFunc != nil {
2007 sig := c.enclosingFunc.sig
2008 // Find signature result that corresponds to our return statement.
2009 if resultIdx := exprAtPos(c.pos, node.Results); resultIdx < len(node.Results) {
2010 if resultIdx < sig.Results().Len() {
2011 inf.objType = sig.Results().At(resultIdx).Type()
2016 case *ast.CaseClause:
2017 if swtch, ok := findSwitchStmt(c.path[i+1:], c.pos, node).(*ast.SwitchStmt); ok {
2018 if tv, ok := c.pkg.GetTypesInfo().Types[swtch.Tag]; ok {
2019 inf.objType = tv.Type
2021 // Record which objects have already been used in the case
2022 // statements so we don't suggest them again.
2023 for _, cc := range swtch.Body.List {
2024 for _, caseExpr := range cc.(*ast.CaseClause).List {
2025 // Don't record the expression we are currently completing.
2026 if caseExpr.Pos() < c.pos && c.pos <= caseExpr.End() {
2030 if objs := objChain(c.pkg.GetTypesInfo(), caseExpr); len(objs) > 0 {
2031 inf.penalized = append(inf.penalized, penalizedObj{objChain: objs, penalty: 0.1})
2038 case *ast.SliceExpr:
2039 // Make sure position falls within the brackets (e.g. "foo[a:<>]").
2040 if node.Lbrack < c.pos && c.pos <= node.Rbrack {
2041 inf.objType = types.Typ[types.Int]
2044 case *ast.IndexExpr:
2045 // Make sure position falls within the brackets (e.g. "foo[<>]").
2046 if node.Lbrack < c.pos && c.pos <= node.Rbrack {
2047 if tv, ok := c.pkg.GetTypesInfo().Types[node.X]; ok {
2048 switch t := tv.Type.Underlying().(type) {
2050 inf.objType = t.Key()
2051 case *types.Slice, *types.Array:
2052 inf.objType = types.Typ[types.Int]
2058 // Make sure we are on right side of arrow (e.g. "foo <- <>").
2059 if c.pos > node.Arrow+1 {
2060 if tv, ok := c.pkg.GetTypesInfo().Types[node.Chan]; ok {
2061 if ch, ok := tv.Type.Underlying().(*types.Chan); ok {
2062 inf.objType = ch.Elem()
2067 case *ast.RangeStmt:
2068 if source.NodeContains(node.X, c.pos) {
2069 inf.objKind |= kindSlice | kindArray | kindMap | kindString
2070 if node.Value == nil {
2071 inf.objKind |= kindChan
2076 inf.modifiers = append(inf.modifiers, typeModifier{mod: dereference})
2077 case *ast.UnaryExpr:
2080 inf.modifiers = append(inf.modifiers, typeModifier{mod: reference})
2082 inf.modifiers = append(inf.modifiers, typeModifier{mod: chanRead})
2084 case *ast.DeferStmt, *ast.GoStmt:
2085 inf.objKind |= kindFunc
2088 if breaksExpectedTypeInference(node, c.pos) {
2097 // objChain decomposes e into a chain of objects if possible. For
2098 // example, "foo.bar().baz" will yield []types.Object{foo, bar, baz}.
2099 // If any part can't be turned into an object, return nil.
2100 func objChain(info *types.Info, e ast.Expr) []types.Object {
2101 var objs []types.Object
2104 switch n := e.(type) {
2106 obj := info.ObjectOf(n)
2110 objs = append(objs, obj)
2112 case *ast.SelectorExpr:
2113 obj := info.ObjectOf(n.Sel)
2117 objs = append(objs, obj)
2120 if len(n.Args) > 0 {
2129 // Reverse order so the layout matches the syntactic order.
2130 for i := 0; i < len(objs)/2; i++ {
2131 objs[i], objs[len(objs)-1-i] = objs[len(objs)-1-i], objs[i]
2137 // applyTypeModifiers applies the list of type modifiers to a type.
2138 // It returns nil if the modifiers could not be applied.
2139 func (ci candidateInference) applyTypeModifiers(typ types.Type, addressable bool) types.Type {
2140 for _, mod := range ci.modifiers {
2143 // For every "*" indirection operator, remove a pointer layer
2144 // from candidate type.
2145 if ptr, ok := typ.Underlying().(*types.Pointer); ok {
2151 // For every "&" address operator, add another pointer layer to
2152 // candidate type, if the candidate is addressable.
2154 typ = types.NewPointer(typ)
2159 // For every "<-" operator, remove a layer of channelness.
2160 if ch, ok := typ.(*types.Chan); ok {
2171 // applyTypeNameModifiers applies the list of type modifiers to a type name.
2172 func (ci candidateInference) applyTypeNameModifiers(typ types.Type) types.Type {
2173 for _, mod := range ci.typeName.modifiers {
2176 typ = types.NewPointer(typ)
2178 typ = types.NewArray(typ, mod.arrayLen)
2180 typ = types.NewSlice(typ)
2186 // matchesVariadic returns true if we are completing a variadic
2187 // parameter and candType is a compatible slice type.
2188 func (ci candidateInference) matchesVariadic(candType types.Type) bool {
2189 return ci.variadic && ci.objType != nil && types.AssignableTo(candType, types.NewSlice(ci.objType))
2192 // findSwitchStmt returns an *ast.CaseClause's corresponding *ast.SwitchStmt or
2193 // *ast.TypeSwitchStmt. path should start from the case clause's first ancestor.
2194 func findSwitchStmt(path []ast.Node, pos token.Pos, c *ast.CaseClause) ast.Stmt {
2195 // Make sure position falls within a "case <>:" clause.
2196 if exprAtPos(pos, c.List) >= len(c.List) {
2199 // A case clause is always nested within a block statement in a switch statement.
2203 if _, ok := path[0].(*ast.BlockStmt); !ok {
2206 switch s := path[1].(type) {
2207 case *ast.SwitchStmt:
2209 case *ast.TypeSwitchStmt:
2216 // breaksExpectedTypeInference reports if an expression node's type is unrelated
2217 // to its child expression node types. For example, "Foo{Bar: x.Baz(<>)}" should
2218 // expect a function argument, not a composite literal value.
2219 func breaksExpectedTypeInference(n ast.Node, pos token.Pos) bool {
2220 switch n := n.(type) {
2221 case *ast.CompositeLit:
2222 // Doesn't break inference if pos is in type name.
2223 // For example: "Foo<>{Bar: 123}"
2224 return !source.NodeContains(n.Type, pos)
2226 // Doesn't break inference if pos is in func name.
2227 // For example: "Foo<>(123)"
2228 return !source.NodeContains(n.Fun, pos)
2229 case *ast.FuncLit, *ast.IndexExpr, *ast.SliceExpr:
2236 // expectTypeName returns information about the expected type name at position.
2237 func expectTypeName(c *completer) typeNameInference {
2238 var inf typeNameInference
2241 for i, p := range c.path {
2242 switch n := p.(type) {
2243 case *ast.FieldList:
2244 // Expect a type name if pos is in a FieldList. This applies to
2245 // FuncType params/results, FuncDecl receiver, StructType, and
2246 // InterfaceType. We don't need to worry about the field name
2247 // because completion bails out early if pos is in an *ast.Ident
2248 // that defines an object.
2249 inf.wantTypeName = true
2251 case *ast.CaseClause:
2252 // Expect type names in type switch case clauses.
2253 if swtch, ok := findSwitchStmt(c.path[i+1:], c.pos, n).(*ast.TypeSwitchStmt); ok {
2254 // The case clause types must be assertable from the type switch parameter.
2255 ast.Inspect(swtch.Assign, func(n ast.Node) bool {
2256 if ta, ok := n.(*ast.TypeAssertExpr); ok {
2257 inf.assertableFrom = c.pkg.GetTypesInfo().TypeOf(ta.X)
2262 inf.wantTypeName = true
2264 // Track the types that have already been used in this
2265 // switch's case statements so we don't recommend them.
2266 for _, e := range swtch.Body.List {
2267 for _, typeExpr := range e.(*ast.CaseClause).List {
2268 // Skip if type expression contains pos. We don't want to
2269 // count it as already used if the user is completing it.
2270 if typeExpr.Pos() < c.pos && c.pos <= typeExpr.End() {
2274 if t := c.pkg.GetTypesInfo().TypeOf(typeExpr); t != nil {
2275 inf.seenTypeSwitchCases = append(inf.seenTypeSwitchCases, t)
2282 return typeNameInference{}
2283 case *ast.TypeAssertExpr:
2284 // Expect type names in type assert expressions.
2285 if n.Lparen < c.pos && c.pos <= n.Rparen {
2286 // The type in parens must be assertable from the expression type.
2287 inf.assertableFrom = c.pkg.GetTypesInfo().TypeOf(n.X)
2288 inf.wantTypeName = true
2291 return typeNameInference{}
2293 inf.modifiers = append(inf.modifiers, typeModifier{mod: reference})
2294 case *ast.CompositeLit:
2295 // We want a type name if position is in the "Type" part of a
2296 // composite literal (e.g. "Foo<>{}").
2297 if n.Type != nil && n.Type.Pos() <= c.pos && c.pos <= n.Type.End() {
2298 inf.wantTypeName = true
2299 inf.compLitType = true
2301 if i < len(c.path)-1 {
2302 // Track preceding "&" operator. Technically it applies to
2303 // the composite literal and not the type name, but if
2304 // affects our type completion nonetheless.
2305 if u, ok := c.path[i+1].(*ast.UnaryExpr); ok && u.Op == token.AND {
2306 inf.modifiers = append(inf.modifiers, typeModifier{mod: reference})
2311 case *ast.ArrayType:
2312 // If we are inside the "Elt" part of an array type, we want a type name.
2313 if n.Elt.Pos() <= c.pos && c.pos <= n.Elt.End() {
2314 inf.wantTypeName = true
2316 // No "Len" expression means a slice type.
2317 inf.modifiers = append(inf.modifiers, typeModifier{mod: slice})
2319 // Try to get the array type using the constant value of "Len".
2320 tv, ok := c.pkg.GetTypesInfo().Types[n.Len]
2321 if ok && tv.Value != nil && tv.Value.Kind() == constant.Int {
2322 if arrayLen, ok := constant.Int64Val(tv.Value); ok {
2323 inf.modifiers = append(inf.modifiers, typeModifier{mod: array, arrayLen: arrayLen})
2328 // ArrayTypes can be nested, so keep going if our parent is an
2330 if i < len(c.path)-1 {
2331 if _, ok := c.path[i+1].(*ast.ArrayType); ok {
2339 inf.wantTypeName = true
2341 inf.wantComparable = source.NodeContains(n.Key, c.pos)
2343 // If the key is empty, assume we are completing the key if
2344 // pos is directly after the "map[".
2345 inf.wantComparable = c.pos == n.Pos()+token.Pos(len("map["))
2348 case *ast.ValueSpec:
2349 inf.wantTypeName = source.NodeContains(n.Type, c.pos)
2352 inf.wantTypeName = source.NodeContains(n.Type, c.pos)
2354 if breaksExpectedTypeInference(p, c.pos) {
2355 return typeNameInference{}
2363 func (c *completer) fakeObj(T types.Type) *types.Var {
2364 return types.NewVar(token.NoPos, c.pkg.GetTypes(), "", T)
2367 // anyCandType reports whether f returns true for any candidate type
2368 // derivable from c. For example, from "foo" we might derive "&foo",
2370 func (c *candidate) anyCandType(f func(t types.Type, addressable bool) bool) bool {
2371 if c.obj == nil || c.obj.Type() == nil {
2375 objType := c.obj.Type()
2377 if f(objType, c.addressable) {
2381 // If c is a func type with a single result, offer the result type.
2382 if sig, ok := objType.Underlying().(*types.Signature); ok {
2383 if sig.Results().Len() == 1 && f(sig.Results().At(0).Type(), false) {
2384 // Mark the candidate so we know to append "()" when formatting.
2385 c.expandFuncCall = true
2391 seenPtrTypes map[types.Type]bool
2396 // Check if dereferencing c would match our type inference. We loop
2397 // since c could have arbitrary levels of pointerness.
2399 ptr, ok := ptrType.Underlying().(*types.Pointer)
2406 // Avoid pointer type cycles.
2407 if seenPtrTypes[ptrType] {
2411 if _, named := ptrType.(*types.Named); named {
2412 // Lazily allocate "seen" since it isn't used normally.
2413 if seenPtrTypes == nil {
2414 seenPtrTypes = make(map[types.Type]bool)
2417 // Track named pointer types we have seen to detect cycles.
2418 seenPtrTypes[ptrType] = true
2421 if f(ptr.Elem(), false) {
2422 // Mark the candidate so we know to prepend "*" when formatting.
2423 c.dereference = ptrDepth
2427 ptrType = ptr.Elem()
2430 // Check if c is addressable and a pointer to c matches our type inference.
2431 if c.addressable && f(types.NewPointer(objType), false) {
2432 // Mark the candidate so we know to prepend "&" when formatting.
2433 c.takeAddress = true
2440 // matchingCandidate reports whether cand matches our type inferences.
2441 // It mutates cand's score in certain cases.
2442 func (c *completer) matchingCandidate(cand *candidate) bool {
2443 if c.completionContext.commentCompletion {
2447 if isTypeName(cand.obj) {
2448 return c.matchingTypeName(cand)
2449 } else if c.wantTypeName() {
2450 // If we want a type, a non-type object never matches.
2454 if c.inference.candTypeMatches(cand) {
2458 candType := cand.obj.Type()
2459 if candType == nil {
2463 if sig, ok := candType.Underlying().(*types.Signature); ok {
2464 if c.inference.assigneesMatch(cand, sig) {
2465 // Invoke the candidate if its results are multi-assignable.
2466 cand.expandFuncCall = true
2471 // Default to invoking *types.Func candidates. This is so function
2472 // completions in an empty statement (or other cases with no expected type)
2473 // are invoked by default.
2474 cand.expandFuncCall = isFunc(cand.obj)
2479 // candTypeMatches reports whether cand makes a good completion
2480 // candidate given the candidate inference. cand's score may be
2481 // mutated to downrank the candidate in certain situations.
2482 func (ci *candidateInference) candTypeMatches(cand *candidate) bool {
2484 expTypes = make([]types.Type, 0, 2)
2485 variadicType types.Type
2487 if ci.objType != nil {
2488 expTypes = append(expTypes, ci.objType)
2491 variadicType = types.NewSlice(ci.objType)
2492 expTypes = append(expTypes, variadicType)
2496 return cand.anyCandType(func(candType types.Type, addressable bool) bool {
2497 // Take into account any type modifiers on the expected type.
2498 candType = ci.applyTypeModifiers(candType, addressable)
2499 if candType == nil {
2503 if ci.convertibleTo != nil && types.ConvertibleTo(candType, ci.convertibleTo) {
2507 for _, expType := range expTypes {
2508 if isEmptyInterface(expType) {
2512 matches, untyped := ci.typeMatches(expType, candType)
2514 // If candType doesn't otherwise match, consider if we can
2515 // convert candType directly to expType.
2516 if considerTypeConversion(candType, expType, cand.path) {
2517 cand.convertTo = expType
2518 // Give a major score penalty so we always prefer directly
2519 // assignable candidates, all else equal.
2527 if expType == variadicType {
2528 cand.variadic = true
2531 // Lower candidate score for untyped conversions. This avoids
2532 // ranking untyped constants above candidates with an exact type
2533 // match. Don't lower score of builtin constants, e.g. "true".
2534 if untyped && !types.Identical(candType, expType) && cand.obj.Parent() != types.Universe {
2535 // Bigger penalty for deep completions into other packages to
2536 // avoid random constants from other packages popping up all
2538 if len(cand.path) > 0 && isPkgName(cand.path[0]) {
2548 // If we don't have a specific expected type, fall back to coarser
2549 // object kind checks.
2550 if ci.objType == nil || isEmptyInterface(ci.objType) {
2551 // If we were able to apply type modifiers to our candidate type,
2552 // count that as a match. For example:
2557 // We were able to apply the "<-" type modifier to "foo", so "foo"
2559 if len(ci.modifiers) > 0 {
2563 // If we didn't have an exact type match, check if our object kind
2565 if ci.kindMatches(candType) {
2566 if ci.objKind == kindFunc {
2567 cand.expandFuncCall = true
2577 // considerTypeConversion returns true if we should offer a completion
2578 // automatically converting "from" to "to".
2579 func considerTypeConversion(from, to types.Type, path []types.Object) bool {
2580 // Don't offer to convert deep completions from other packages.
2581 // Otherwise there are many random package level consts/vars that
2582 // pop up as candidates all the time.
2583 if len(path) > 0 && isPkgName(path[0]) {
2587 if !types.ConvertibleTo(from, to) {
2591 // Don't offer to convert ints to strings since that probably
2592 // doesn't do what the user wants.
2593 if isBasicKind(from, types.IsInteger) && isBasicKind(to, types.IsString) {
2600 // typeMatches reports whether an object of candType makes a good
2601 // completion candidate given the expected type expType. It also
2602 // returns a second bool which is true if both types are basic types
2603 // of the same kind, and at least one is untyped.
2604 func (ci *candidateInference) typeMatches(expType, candType types.Type) (bool, bool) {
2605 // Handle untyped values specially since AssignableTo gives false negatives
2606 // for them (see https://golang.org/issue/32146).
2607 if candBasic, ok := candType.Underlying().(*types.Basic); ok {
2608 if wantBasic, ok := expType.Underlying().(*types.Basic); ok {
2609 // Make sure at least one of them is untyped.
2610 if isUntyped(candType) || isUntyped(expType) {
2611 // Check that their constant kind (bool|int|float|complex|string) matches.
2612 // This doesn't take into account the constant value, so there will be some
2613 // false positives due to integer sign and overflow.
2614 if candBasic.Info()&types.IsConstType == wantBasic.Info()&types.IsConstType {
2621 // AssignableTo covers the case where the types are equal, but also handles
2622 // cases like assigning a concrete type to an interface type.
2623 return types.AssignableTo(candType, expType), false
2626 // kindMatches reports whether candType's kind matches our expected
2627 // kind (e.g. slice, map, etc.).
2628 func (ci *candidateInference) kindMatches(candType types.Type) bool {
2629 return ci.objKind > 0 && ci.objKind&candKind(candType) > 0
2632 // assigneesMatch reports whether an invocation of sig matches the
2633 // number and type of any assignees.
2634 func (ci *candidateInference) assigneesMatch(cand *candidate, sig *types.Signature) bool {
2635 if len(ci.assignees) == 0 {
2639 // Uniresult functions are always usable and are handled by the
2640 // normal, non-assignees type matching logic.
2641 if sig.Results().Len() == 1 {
2645 var numberOfResultsCouldMatch bool
2646 if ci.variadicAssignees {
2647 numberOfResultsCouldMatch = sig.Results().Len() >= len(ci.assignees)-1
2649 numberOfResultsCouldMatch = sig.Results().Len() == len(ci.assignees)
2652 // If our signature doesn't return the right number of values, it's
2653 // not a match, so downrank it. For example:
2655 // var foo func() (int, int)
2656 // a, b, c := <> // downrank "foo()" since it only returns two values
2657 if !numberOfResultsCouldMatch {
2662 // If at least one assignee has a valid type, and all valid
2663 // assignees match the corresponding sig result value, the signature
2666 for i := 0; i < sig.Results().Len(); i++ {
2667 var assignee types.Type
2669 // If we are completing into variadic parameters, deslice the
2670 // expected variadic type.
2671 if ci.variadicAssignees && i >= len(ci.assignees)-1 {
2672 assignee = ci.assignees[len(ci.assignees)-1]
2673 if elem := deslice(assignee); elem != nil {
2677 assignee = ci.assignees[i]
2680 if assignee == nil {
2684 allMatch, _ = ci.typeMatches(assignee, sig.Results().At(i).Type())
2692 func (c *completer) matchingTypeName(cand *candidate) bool {
2693 if !c.wantTypeName() {
2697 typeMatches := func(candType types.Type) bool {
2698 // Take into account any type name modifier prefixes.
2699 candType = c.inference.applyTypeNameModifiers(candType)
2701 if from := c.inference.typeName.assertableFrom; from != nil {
2702 // Don't suggest the starting type in type assertions. For example,
2703 // if "foo" is an io.Writer, don't suggest "foo.(io.Writer)".
2704 if types.Identical(from, candType) {
2708 if intf, ok := from.Underlying().(*types.Interface); ok {
2709 if !types.AssertableTo(intf, candType) {
2715 if c.inference.typeName.wantComparable && !types.Comparable(candType) {
2719 // Skip this type if it has already been used in another type
2721 for _, seen := range c.inference.typeName.seenTypeSwitchCases {
2722 if types.Identical(candType, seen) {
2727 // We can expect a type name and have an expected type in cases like:
2732 // Where our expected type is "[]int", and we expect a type name.
2733 if c.inference.objType != nil {
2734 return types.AssignableTo(candType, c.inference.objType)
2737 // Default to saying any type name is a match.
2741 t := cand.obj.Type()
2747 if !source.IsInterface(t) && typeMatches(types.NewPointer(t)) {
2748 if c.inference.typeName.compLitType {
2749 // If we are completing a composite literal type as in
2750 // "foo<>{}", to make a pointer we must prepend "&".
2751 cand.takeAddress = true
2753 // If we are completing a normal type name such as "foo<>", to
2754 // make a pointer we must prepend "*".
2755 cand.makePointer = true
2764 // "interface { Error() string }" (i.e. error)
2765 errorIntf = types.Universe.Lookup("error").Type().Underlying().(*types.Interface)
2767 // "interface { String() string }" (i.e. fmt.Stringer)
2768 stringerIntf = types.NewInterfaceType([]*types.Func{
2769 types.NewFunc(token.NoPos, nil, "String", types.NewSignature(
2772 types.NewTuple(types.NewParam(token.NoPos, nil, "", types.Typ[types.String])),
2777 byteType = types.Universe.Lookup("byte").Type()
2780 // candKind returns the objKind of candType, if any.
2781 func candKind(candType types.Type) objKind {
2784 switch t := candType.Underlying().(type) {
2787 if t.Elem() == byteType {
2792 if t.Elem() == byteType {
2799 case *types.Pointer:
2802 // Some builtins handle array pointers as arrays, so just report a pointer
2803 // to an array as an array.
2804 if _, isArray := t.Elem().Underlying().(*types.Array); isArray {
2808 switch info := t.Info(); {
2809 case info&types.IsString > 0:
2811 case info&types.IsInteger > 0:
2813 case info&types.IsFloat > 0:
2815 case info&types.IsComplex > 0:
2817 case info&types.IsBoolean > 0:
2820 case *types.Signature:
2824 if types.Implements(candType, errorIntf) {
2828 if types.Implements(candType, stringerIntf) {
2829 kind |= kindStringer