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 // imp is the import that needs to be added to this package in order
392 // for this candidate to be valid. nil if no import needed.
396 // ErrIsDefinition is an error that informs the user they got no
397 // completions because they tried to complete the name of a new object
399 type ErrIsDefinition struct {
403 func (e ErrIsDefinition) Error() string {
404 msg := "this is a definition"
406 msg += " of " + e.objStr
411 // Completion returns a list of possible candidates for completion, given a
412 // a file and a position.
414 // The selection is computed based on the preceding identifier and can be used by
415 // the client to score the quality of the completion. For instance, some clients
416 // may tolerate imperfect matches as valid completion results, since users may make typos.
417 func Completion(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle, protoPos protocol.Position, protoContext protocol.CompletionContext) ([]CompletionItem, *Selection, error) {
418 ctx, done := event.Start(ctx, "completion.Completion")
421 startTime := time.Now()
423 pkg, pgf, err := source.GetParsedFile(ctx, snapshot, fh, source.NarrowestPackage)
424 if err != nil || pgf.File.Package == token.NoPos {
425 // If we can't parse this file or find position for the package
426 // keyword, it may be missing a package declaration. Try offering
427 // suggestions for the package declaration.
428 // Note that this would be the case even if the keyword 'package' is
429 // present but no package name exists.
430 items, surrounding, innerErr := packageClauseCompletions(ctx, snapshot, fh, protoPos)
432 // return the error for GetParsedFile since it's more relevant in this situation.
433 return nil, nil, errors.Errorf("getting file for Completion: %w (package completions: %v)", err, innerErr)
436 return items, surrounding, nil
438 spn, err := pgf.Mapper.PointSpan(protoPos)
442 rng, err := spn.Range(pgf.Mapper.Converter)
446 // Completion is based on what precedes the cursor.
447 // Find the path to the position before pos.
448 path, _ := astutil.PathEnclosingInterval(pgf.File, rng.Start-1, rng.Start-1)
450 return nil, nil, errors.Errorf("cannot find node enclosing position")
455 // Check if completion at this position is valid. If not, return early.
456 switch n := path[0].(type) {
458 // Skip completion inside literals except for ImportSpec
460 if _, ok := path[1].(*ast.ImportSpec); ok {
466 if n.Ellipsis.IsValid() && pos > n.Ellipsis && pos <= n.Ellipsis+token.Pos(len("...")) {
467 // Don't offer completions inside or directly after "...". For
468 // example, don't offer completions at "<>" in "foo(bar...<>").
472 // reject defining identifiers
473 if obj, ok := pkg.GetTypesInfo().Defs[n]; ok {
474 if v, ok := obj.(*types.Var); ok && v.IsField() && v.Embedded() {
475 // An anonymous field is also a reference to a type.
476 } else if pgf.File.Name == n {
477 // Don't skip completions if Ident is for package name.
482 qual := types.RelativeTo(pkg.GetTypes())
483 objStr = types.ObjectString(obj, qual)
485 return nil, nil, ErrIsDefinition{objStr: objStr}
490 opts := snapshot.View().Options()
494 qf: source.Qualifier(pgf.File, pkg.GetTypes(), pkg.GetTypesInfo()),
495 completionContext: completionContext{
496 triggerCharacter: protoContext.TriggerCharacter,
497 triggerKind: protoContext.TriggerKind,
500 filename: fh.URI().Filename(),
504 seen: make(map[types.Object]bool),
505 enclosingFunc: enclosingFunction(path, pkg.GetTypesInfo()),
506 enclosingCompositeLiteral: enclosingCompositeLiteral(path, rng.Start, pkg.GetTypesInfo()),
507 deepState: deepCompletionState{
508 enabled: opts.DeepCompletion,
510 opts: &completionOptions{
511 matcher: opts.Matcher,
512 unimported: opts.CompleteUnimported,
513 documentation: opts.CompletionDocumentation && opts.HoverKind != source.NoDocumentation,
514 fullDocumentation: opts.HoverKind == source.FullDocumentation,
515 placeholders: opts.UsePlaceholders,
516 literal: opts.LiteralCompletions && opts.InsertTextFormat == protocol.SnippetTextFormat,
517 budget: opts.CompletionBudget,
519 // default to a matcher that always matches
520 matcher: prefixMatcher(""),
521 methodSetCache: make(map[methodSetKey]*types.MethodSet),
523 startTime: startTime,
526 var cancel context.CancelFunc
527 if c.opts.budget == 0 {
528 ctx, cancel = context.WithCancel(ctx)
530 // timeoutDuration is the completion budget remaining. If less than
532 timeoutDuration := time.Until(c.startTime.Add(c.opts.budget))
533 if timeoutDuration < 10*time.Millisecond {
534 timeoutDuration = 10 * time.Millisecond
536 ctx, cancel = context.WithTimeout(ctx, timeoutDuration)
540 if surrounding := c.containingIdent(pgf.Src); surrounding != nil {
541 c.setSurrounding(surrounding)
544 c.inference = expectedCandidate(ctx, c)
546 err = c.collectCompletions(ctx)
551 // Deep search collected candidates and their members for more candidates.
553 c.deepState.searchQueue = nil
555 for _, callback := range c.completionCallbacks {
556 if err := c.snapshot.RunProcessEnvFunc(ctx, callback); err != nil {
561 // Search candidates populated by expensive operations like
562 // unimportedMembers etc. for more completion items.
565 // Statement candidates offer an entire statement in certain contexts, as
566 // opposed to a single object. Add statement candidates last because they
567 // depend on other candidates having already been collected.
568 c.addStatementCandidates()
571 return c.items, c.getSurrounding(), nil
574 // collectCompletions adds possible completion candidates to either the deep
575 // search queue or completion items directly for different completion contexts.
576 func (c *completer) collectCompletions(ctx context.Context) error {
577 // Inside import blocks, return completions for unimported packages.
578 for _, importSpec := range c.file.Imports {
579 if !(importSpec.Path.Pos() <= c.pos && c.pos <= importSpec.Path.End()) {
582 return c.populateImportCompletions(ctx, importSpec)
585 // Inside comments, offer completions for the name of the relevant symbol.
586 for _, comment := range c.file.Comments {
587 if comment.Pos() < c.pos && c.pos <= comment.End() {
588 c.populateCommentCompletions(ctx, comment)
593 // Struct literals are handled entirely separately.
594 if c.wantStructFieldCompletions() {
595 // If we are definitely completing a struct field name, deep completions
597 if c.enclosingCompositeLiteral.inKey {
598 c.deepState.enabled = false
600 return c.structLiteralFieldName(ctx)
603 if lt := c.wantLabelCompletion(); lt != labelNone {
608 if c.emptySwitchStmt() {
609 // Empty switch statements only admit "default" and "case" keywords.
610 c.addKeywordItems(map[string]bool{}, highScore, CASE, DEFAULT)
614 switch n := c.path[0].(type) {
616 if c.file.Name == n {
617 return c.packageNameCompletions(ctx, c.fh.URI(), n)
618 } else if sel, ok := c.path[1].(*ast.SelectorExpr); ok && sel.Sel == n {
619 // Is this the Sel part of a selector?
620 return c.selector(ctx, sel)
622 return c.lexical(ctx)
623 // The function name hasn't been typed yet, but the parens are there:
625 case *ast.TypeAssertExpr:
626 // Create a fake selector expression.
627 return c.selector(ctx, &ast.SelectorExpr{X: n.X})
628 case *ast.SelectorExpr:
629 return c.selector(ctx, n)
630 // At the file scope, only keywords are allowed.
631 case *ast.BadDecl, *ast.File:
632 c.addKeywordCompletions()
634 // fallback to lexical completions
635 return c.lexical(ctx)
641 // containingIdent returns the *ast.Ident containing pos, if any. It
642 // synthesizes an *ast.Ident to allow completion in the face of
643 // certain syntax errors.
644 func (c *completer) containingIdent(src []byte) *ast.Ident {
645 // In the normal case, our leaf AST node is the identifer being completed.
646 if ident, ok := c.path[0].(*ast.Ident); ok {
650 pos, tkn, lit := c.scanToken(src)
655 fakeIdent := &ast.Ident{Name: lit, NamePos: pos}
657 if _, isBadDecl := c.path[0].(*ast.BadDecl); isBadDecl {
658 // You don't get *ast.Idents at the file level, so look for bad
659 // decls and use the manually extracted token.
661 } else if c.emptySwitchStmt() {
662 // Only keywords are allowed in empty switch statements.
663 // *ast.Idents are not parsed, so we must use the manually
666 } else if tkn.IsKeyword() {
667 // Otherwise, manually extract the prefix if our containing token
668 // is a keyword. This improves completion after an "accidental
669 // keyword", e.g. completing to "variance" in "someFunc(var<>)".
676 // scanToken scans pgh's contents for the token containing pos.
677 func (c *completer) scanToken(contents []byte) (token.Pos, token.Token, string) {
678 tok := c.snapshot.FileSet().File(c.pos)
680 var s scanner.Scanner
681 s.Init(tok, contents, nil, 0)
683 tknPos, tkn, lit := s.Scan()
684 if tkn == token.EOF || tknPos >= c.pos {
685 return token.NoPos, token.ILLEGAL, ""
688 if len(lit) > 0 && tknPos <= c.pos && c.pos <= tknPos+token.Pos(len(lit)) {
689 return tknPos, tkn, lit
694 func (c *completer) sortItems() {
695 sort.SliceStable(c.items, func(i, j int) bool {
696 // Sort by score first.
697 if c.items[i].Score != c.items[j].Score {
698 return c.items[i].Score > c.items[j].Score
701 // Then sort by label so order stays consistent. This also has the
702 // effect of prefering shorter candidates.
703 return c.items[i].Label < c.items[j].Label
707 // emptySwitchStmt reports whether pos is in an empty switch or select
709 func (c *completer) emptySwitchStmt() bool {
710 block, ok := c.path[0].(*ast.BlockStmt)
711 if !ok || len(block.List) > 0 || len(c.path) == 1 {
715 switch c.path[1].(type) {
716 case *ast.SwitchStmt, *ast.TypeSwitchStmt, *ast.SelectStmt:
723 // populateImportCompletions yields completions for an import path around the cursor.
725 // Completions are suggested at the directory depth of the given import path so
726 // that we don't overwhelm the user with a large list of possibilities. As an
727 // example, a completion for the prefix "golang" results in "golang.org/".
728 // Completions for "golang.org/" yield its subdirectories
729 // (i.e. "golang.org/x/"). The user is meant to accept completion suggestions
730 // until they reach a complete import path.
731 func (c *completer) populateImportCompletions(ctx context.Context, searchImport *ast.ImportSpec) error {
732 // deepSearch is not valuable for import completions.
733 c.deepState.enabled = false
735 importPath := searchImport.Path.Value
737 // Extract the text between the quotes (if any) in an import spec.
738 // prefix is the part of import path before the cursor.
739 prefixEnd := c.pos - searchImport.Path.Pos()
740 prefix := strings.Trim(importPath[:prefixEnd], `"`)
742 // The number of directories in the import path gives us the depth at
744 depth := len(strings.Split(prefix, "/")) - 1
746 content := importPath
747 start, end := searchImport.Path.Pos(), searchImport.Path.End()
748 namePrefix, nameSuffix := `"`, `"`
749 // If a starting quote is present, adjust surrounding to either after the
750 // cursor or after the first slash (/), except if cursor is at the starting
751 // quote. Otherwise we provide a completion including the starting quote.
752 if strings.HasPrefix(importPath, `"`) && c.pos > searchImport.Path.Pos() {
753 content = content[1:]
756 // Adjust textEdit start to replacement range. For ex: if current
757 // path was "golang.or/x/to<>ols/internal/", where <> is the cursor
758 // position, start of the replacement range would be after
760 path := strings.SplitAfter(prefix, "/")
761 numChars := len(strings.Join(path[:len(path)-1], ""))
762 content = content[numChars:]
763 start += token.Pos(numChars)
768 // We won't provide an ending quote if one is already present, except if
769 // cursor is after the ending quote but still in import spec. This is
770 // because cursor has to be in our textEdit range.
771 if strings.HasSuffix(importPath, `"`) && c.pos < searchImport.Path.End() {
773 content = content[:len(content)-1]
777 c.surrounding = &Selection{
780 MappedRange: source.NewMappedRange(c.snapshot.FileSet(), c.mapper, start, end),
783 seenImports := make(map[string]struct{})
784 for _, importSpec := range c.file.Imports {
785 if importSpec.Path.Value == importPath {
788 seenImportPath, err := strconv.Unquote(importSpec.Path.Value)
792 seenImports[seenImportPath] = struct{}{}
795 var mu sync.Mutex // guard c.items locally, since searchImports is called in parallel
796 seen := make(map[string]struct{})
797 searchImports := func(pkg imports.ImportFix) {
798 path := pkg.StmtInfo.ImportPath
799 if _, ok := seenImports[path]; ok {
803 // Any package path containing fewer directories than the search
804 // prefix is not a match.
805 pkgDirList := strings.Split(path, "/")
806 if len(pkgDirList) < depth+1 {
809 pkgToConsider := strings.Join(pkgDirList[:depth+1], "/")
811 name := pkgDirList[depth]
812 // if we're adding an opening quote to completion too, set name to full
813 // package path since we'll need to overwrite that range.
814 if namePrefix == `"` {
818 score := pkg.Relevance
819 if len(pkgDirList)-1 == depth {
822 // For incomplete package paths, add a terminal slash to indicate that the
823 // user should keep triggering completions.
828 if _, ok := seen[pkgToConsider]; ok {
831 seen[pkgToConsider] = struct{}{}
836 name = namePrefix + name + nameSuffix
837 obj := types.NewPkgName(0, nil, name, types.NewPackage(pkgToConsider, name))
838 c.deepState.enqueue(candidate{
840 detail: fmt.Sprintf("%q", pkgToConsider),
845 c.completionCallbacks = append(c.completionCallbacks, func(opts *imports.Options) error {
846 return imports.GetImportPaths(ctx, searchImports, prefix, c.filename, c.pkg.GetTypes().Name(), opts.Env)
851 // populateCommentCompletions yields completions for comments preceding or in declarations.
852 func (c *completer) populateCommentCompletions(ctx context.Context, comment *ast.CommentGroup) {
853 // If the completion was triggered by a period, ignore it. These types of
854 // completions will not be useful in comments.
855 if c.completionContext.triggerCharacter == "." {
859 // Using the comment position find the line after
860 file := c.snapshot.FileSet().File(comment.End())
865 // Deep completion doesn't work properly in comments since we don't
866 // have a type object to complete further.
867 c.deepState.enabled = false
868 c.completionContext.commentCompletion = true
870 // Documentation isn't useful in comments, since it might end up being the
872 c.opts.documentation = false
874 commentLine := file.Line(comment.End())
876 // comment is valid, set surrounding as word boundaries around cursor
877 c.setSurroundingForComment(comment)
879 // Using the next line pos, grab and parse the exported symbol on that line
880 for _, n := range c.file.Decls {
881 declLine := file.Line(n.Pos())
882 // if the comment is not in, directly above or on the same line as a declaration
883 if declLine != commentLine && declLine != commentLine+1 &&
884 !(n.Pos() <= comment.Pos() && comment.End() <= n.End()) {
887 switch node := n.(type) {
888 // handle const, vars, and types
890 for _, spec := range node.Specs {
891 switch spec := spec.(type) {
893 for _, name := range spec.Names {
894 if name.String() == "_" {
897 obj := c.pkg.GetTypesInfo().ObjectOf(name)
898 c.deepState.enqueue(candidate{obj: obj, score: stdScore})
901 // add TypeSpec fields to completion
902 switch typeNode := spec.Type.(type) {
903 case *ast.StructType:
904 c.addFieldItems(ctx, typeNode.Fields)
906 c.addFieldItems(ctx, typeNode.Params)
907 c.addFieldItems(ctx, typeNode.Results)
908 case *ast.InterfaceType:
909 c.addFieldItems(ctx, typeNode.Methods)
912 if spec.Name.String() == "_" {
916 obj := c.pkg.GetTypesInfo().ObjectOf(spec.Name)
917 // Type name should get a higher score than fields but not highScore by default
918 // since field near a comment cursor gets a highScore
919 score := stdScore * 1.1
920 // If type declaration is on the line after comment, give it a highScore.
921 if declLine == commentLine+1 {
925 c.deepState.enqueue(candidate{obj: obj, score: score})
930 c.addFieldItems(ctx, node.Recv)
931 c.addFieldItems(ctx, node.Type.Params)
932 c.addFieldItems(ctx, node.Type.Results)
934 // collect receiver struct fields
935 if node.Recv != nil {
936 for _, fields := range node.Recv.List {
937 for _, name := range fields.Names {
938 obj := c.pkg.GetTypesInfo().ObjectOf(name)
943 recvType := obj.Type().Underlying()
944 if ptr, ok := recvType.(*types.Pointer); ok {
945 recvType = ptr.Elem()
947 recvStruct, ok := recvType.Underlying().(*types.Struct)
951 for i := 0; i < recvStruct.NumFields(); i++ {
952 field := recvStruct.Field(i)
953 c.deepState.enqueue(candidate{obj: field, score: lowScore})
959 if node.Name.String() == "_" {
963 obj := c.pkg.GetTypesInfo().ObjectOf(node.Name)
964 if obj == nil || obj.Pkg() != nil && obj.Pkg() != c.pkg.GetTypes() {
968 c.deepState.enqueue(candidate{obj: obj, score: highScore})
973 // sets word boundaries surrounding a cursor for a comment
974 func (c *completer) setSurroundingForComment(comments *ast.CommentGroup) {
975 var cursorComment *ast.Comment
976 for _, comment := range comments.List {
977 if c.pos >= comment.Pos() && c.pos <= comment.End() {
978 cursorComment = comment
982 // if cursor isn't in the comment
983 if cursorComment == nil {
987 // index of cursor in comment text
988 cursorOffset := int(c.pos - cursorComment.Pos())
989 start, end := cursorOffset, cursorOffset
990 for start > 0 && isValidIdentifierChar(cursorComment.Text[start-1]) {
993 for end < len(cursorComment.Text) && isValidIdentifierChar(cursorComment.Text[end]) {
997 c.surrounding = &Selection{
998 content: cursorComment.Text[start:end],
1000 MappedRange: source.NewMappedRange(c.snapshot.FileSet(), c.mapper,
1001 token.Pos(int(cursorComment.Slash)+start), token.Pos(int(cursorComment.Slash)+end)),
1003 c.setMatcherFromPrefix(c.surrounding.Prefix())
1006 // isValidIdentifierChar returns true if a byte is a valid go identifier character
1007 // i.e unicode letter or digit or undescore
1008 func isValidIdentifierChar(char byte) bool {
1009 charRune := rune(char)
1010 return unicode.In(charRune, unicode.Letter, unicode.Digit) || char == '_'
1013 // adds struct fields, interface methods, function declaration fields to completion
1014 func (c *completer) addFieldItems(ctx context.Context, fields *ast.FieldList) {
1019 cursor := c.surrounding.cursor
1020 for _, field := range fields.List {
1021 for _, name := range field.Names {
1022 if name.String() == "_" {
1025 obj := c.pkg.GetTypesInfo().ObjectOf(name)
1030 // if we're in a field comment/doc, score that field as more relevant
1032 if field.Comment != nil && field.Comment.Pos() <= cursor && cursor <= field.Comment.End() {
1034 } else if field.Doc != nil && field.Doc.Pos() <= cursor && cursor <= field.Doc.End() {
1038 c.deepState.enqueue(candidate{obj: obj, score: score})
1043 func (c *completer) wantStructFieldCompletions() bool {
1044 clInfo := c.enclosingCompositeLiteral
1049 return clInfo.isStruct() && (clInfo.inKey || clInfo.maybeInFieldName)
1052 func (c *completer) wantTypeName() bool {
1053 return !c.completionContext.commentCompletion && c.inference.typeName.wantTypeName
1056 // See https://golang.org/issue/36001. Unimported completions are expensive.
1058 maxUnimportedPackageNames = 5
1059 unimportedMemberTarget = 100
1062 // selector finds completions for the specified selector expression.
1063 func (c *completer) selector(ctx context.Context, sel *ast.SelectorExpr) error {
1064 c.inference.objChain = objChain(c.pkg.GetTypesInfo(), sel.X)
1066 // Is sel a qualified identifier?
1067 if id, ok := sel.X.(*ast.Ident); ok {
1068 if pkgName, ok := c.pkg.GetTypesInfo().Uses[id].(*types.PkgName); ok {
1069 candidates := c.packageMembers(pkgName.Imported(), stdScore, nil)
1070 for _, cand := range candidates {
1071 c.deepState.enqueue(cand)
1077 // Invariant: sel is a true selector.
1078 tv, ok := c.pkg.GetTypesInfo().Types[sel.X]
1080 candidates := c.methodsAndFields(tv.Type, tv.Addressable(), nil)
1081 for _, cand := range candidates {
1082 c.deepState.enqueue(cand)
1087 // Try unimported packages.
1088 if id, ok := sel.X.(*ast.Ident); ok && c.opts.unimported {
1089 if err := c.unimportedMembers(ctx, id); err != nil {
1096 func (c *completer) unimportedMembers(ctx context.Context, id *ast.Ident) error {
1097 // Try loaded packages first. They're relevant, fast, and fully typed.
1098 known, err := c.snapshot.CachedImportPaths(ctx)
1104 for path, pkg := range known {
1105 if pkg.GetTypes().Name() != id.Name {
1108 paths = append(paths, path)
1111 var relevances map[string]float64
1112 if len(paths) != 0 {
1113 if err := c.snapshot.RunProcessEnvFunc(ctx, func(opts *imports.Options) error {
1115 relevances, err = imports.ScoreImportPaths(ctx, opts.Env, paths)
1121 sort.Slice(paths, func(i, j int) bool {
1122 return relevances[paths[i]] > relevances[paths[j]]
1125 for _, path := range paths {
1127 if pkg.GetTypes().Name() != id.Name {
1134 if imports.ImportPathToAssumedName(path) != pkg.GetTypes().Name() {
1135 imp.name = pkg.GetTypes().Name()
1137 candidates := c.packageMembers(pkg.GetTypes(), unimportedScore(relevances[path]), imp)
1138 for _, cand := range candidates {
1139 c.deepState.enqueue(cand)
1141 if len(c.items) >= unimportedMemberTarget {
1146 ctx, cancel := context.WithCancel(ctx)
1149 add := func(pkgExport imports.PackageExport) {
1152 if _, ok := known[pkgExport.Fix.StmtInfo.ImportPath]; ok {
1153 return // We got this one above.
1156 // Continue with untyped proposals.
1157 pkg := types.NewPackage(pkgExport.Fix.StmtInfo.ImportPath, pkgExport.Fix.IdentName)
1158 for _, export := range pkgExport.Exports {
1159 score := unimportedScore(pkgExport.Fix.Relevance)
1160 c.deepState.enqueue(candidate{
1161 obj: types.NewVar(0, pkg, export, nil),
1164 importPath: pkgExport.Fix.StmtInfo.ImportPath,
1165 name: pkgExport.Fix.StmtInfo.Name,
1169 if len(c.items) >= unimportedMemberTarget {
1174 c.completionCallbacks = append(c.completionCallbacks, func(opts *imports.Options) error {
1176 return imports.GetPackageExports(ctx, add, id.Name, c.filename, c.pkg.GetTypes().Name(), opts.Env)
1181 // unimportedScore returns a score for an unimported package that is generally
1182 // lower than other candidates.
1183 func unimportedScore(relevance float64) float64 {
1184 return (stdScore + .1*relevance) / 2
1187 func (c *completer) packageMembers(pkg *types.Package, score float64, imp *importInfo) []candidate {
1188 var candidates []candidate
1189 scope := pkg.Scope()
1190 for _, name := range scope.Names() {
1191 obj := scope.Lookup(name)
1192 candidates = append(candidates, candidate{
1196 addressable: isVar(obj),
1202 func (c *completer) methodsAndFields(typ types.Type, addressable bool, imp *importInfo) []candidate {
1203 mset := c.methodSetCache[methodSetKey{typ, addressable}]
1205 if addressable && !types.IsInterface(typ) && !isPointer(typ) {
1206 // Add methods of *T, which includes methods with receiver T.
1207 mset = types.NewMethodSet(types.NewPointer(typ))
1209 // Add methods of T.
1210 mset = types.NewMethodSet(typ)
1212 c.methodSetCache[methodSetKey{typ, addressable}] = mset
1215 var candidates []candidate
1216 for i := 0; i < mset.Len(); i++ {
1217 candidates = append(candidates, candidate{
1218 obj: mset.At(i).Obj(),
1221 addressable: addressable || isPointer(typ),
1226 eachField(typ, func(v *types.Var) {
1227 candidates = append(candidates, candidate{
1229 score: stdScore - 0.01,
1231 addressable: addressable || isPointer(typ),
1238 // lexical finds completions in the lexical environment.
1239 func (c *completer) lexical(ctx context.Context) error {
1240 scopes := source.CollectScopes(c.pkg.GetTypesInfo(), c.path, c.pos)
1241 scopes = append(scopes, c.pkg.GetTypes().Scope(), types.Universe)
1244 builtinIota = types.Universe.Lookup("iota")
1245 builtinNil = types.Universe.Lookup("nil")
1248 // Track seen variables to avoid showing completions for shadowed variables.
1249 // This works since we look at scopes from innermost to outermost.
1250 seen := make(map[string]struct{})
1252 // Process scopes innermost first.
1253 for i, scope := range scopes {
1259 for _, name := range scope.Names() {
1260 declScope, obj := scope.LookupParent(name, c.pos)
1261 if declScope != scope {
1262 continue // Name was declared in some enclosing scope, or not at all.
1265 // If obj's type is invalid, find the AST node that defines the lexical block
1266 // containing the declaration of obj. Don't resolve types for packages.
1267 if !isPkgName(obj) && !typeIsValid(obj.Type()) {
1268 // Match the scope to its ast.Node. If the scope is the package scope,
1269 // use the *ast.File as the starting node.
1271 if i < len(c.path) {
1273 } else if i == len(c.path) { // use the *ast.File for package scope
1277 if resolved := resolveInvalid(c.snapshot.FileSet(), obj, node, c.pkg.GetTypesInfo()); resolved != nil {
1283 // Don't use LHS of value spec in RHS.
1284 if vs := enclosingValueSpec(c.path); vs != nil {
1285 for _, ident := range vs.Names {
1286 if obj.Pos() == ident.Pos() {
1292 // Don't suggest "iota" outside of const decls.
1293 if obj == builtinIota && !c.inConstDecl() {
1297 // Rank outer scopes lower than inner.
1298 score := stdScore * math.Pow(.99, float64(i))
1300 // Dowrank "nil" a bit so it is ranked below more interesting candidates.
1301 if obj == builtinNil {
1305 // If we haven't already added a candidate for an object with this name.
1306 if _, ok := seen[obj.Name()]; !ok {
1307 seen[obj.Name()] = struct{}{}
1308 c.deepState.enqueue(candidate{
1311 addressable: isVar(obj),
1317 if c.inference.objType != nil {
1318 if named, _ := source.Deref(c.inference.objType).(*types.Named); named != nil {
1319 // If we expected a named type, check the type's package for
1320 // completion items. This is useful when the current file hasn't
1321 // imported the type's package yet.
1323 if named.Obj() != nil && named.Obj().Pkg() != nil {
1324 pkg := named.Obj().Pkg()
1326 // Make sure the package name isn't already in use by another
1327 // object, and that this file doesn't import the package yet.
1328 if _, ok := seen[pkg.Name()]; !ok && pkg != c.pkg.GetTypes() && !alreadyImports(c.file, pkg.Path()) {
1329 seen[pkg.Name()] = struct{}{}
1330 obj := types.NewPkgName(0, nil, pkg.Name(), pkg)
1332 importPath: pkg.Path(),
1334 if imports.ImportPathToAssumedName(pkg.Path()) != pkg.Name() {
1335 imp.name = pkg.Name()
1337 c.deepState.enqueue(candidate{
1347 if c.opts.unimported {
1348 if err := c.unimportedPackages(ctx, seen); err != nil {
1353 if t := c.inference.objType; t != nil {
1356 // If we have an expected type and it is _not_ a named type,
1357 // handle it specially. Non-named types like "[]int" will never be
1358 // considered via a lexical search, so we need to directly inject
1360 if _, named := t.(*types.Named); !named {
1361 // If our expected type is "[]int", this will add a literal
1362 // candidate of "[]int{}".
1363 c.literal(ctx, t, nil)
1365 if _, isBasic := t.(*types.Basic); !isBasic {
1366 // If we expect a non-basic type name (e.g. "[]int"), hack up
1367 // a named type whose name is literally "[]int". This allows
1368 // us to reuse our object based completion machinery.
1369 fakeNamedType := candidate{
1370 obj: types.NewTypeName(token.NoPos, nil, types.TypeString(t, c.qf), t),
1373 // Make sure the type name matches before considering
1374 // candidate. This cuts down on useless candidates.
1375 if c.matchingTypeName(&fakeNamedType) {
1376 c.deepState.enqueue(fakeNamedType)
1382 // Add keyword completion items appropriate in the current context.
1383 c.addKeywordCompletions()
1388 func (c *completer) unimportedPackages(ctx context.Context, seen map[string]struct{}) error {
1390 if c.surrounding != nil {
1391 prefix = c.surrounding.Prefix()
1395 known, err := c.snapshot.CachedImportPaths(ctx)
1400 for path, pkg := range known {
1401 if !strings.HasPrefix(pkg.GetTypes().Name(), prefix) {
1404 paths = append(paths, path)
1407 var relevances map[string]float64
1408 if len(paths) != 0 {
1409 if err := c.snapshot.RunProcessEnvFunc(ctx, func(opts *imports.Options) error {
1411 relevances, err = imports.ScoreImportPaths(ctx, opts.Env, paths)
1417 sort.Slice(paths, func(i, j int) bool {
1418 return relevances[paths[i]] > relevances[paths[j]]
1421 for _, path := range paths {
1423 if _, ok := seen[pkg.GetTypes().Name()]; ok {
1430 if imports.ImportPathToAssumedName(path) != pkg.GetTypes().Name() {
1431 imp.name = pkg.GetTypes().Name()
1433 if count >= maxUnimportedPackageNames {
1436 c.deepState.enqueue(candidate{
1437 obj: types.NewPkgName(0, nil, pkg.GetTypes().Name(), pkg.GetTypes()),
1438 score: unimportedScore(relevances[path]),
1444 ctx, cancel := context.WithCancel(ctx)
1447 add := func(pkg imports.ImportFix) {
1450 if _, ok := seen[pkg.IdentName]; ok {
1453 if _, ok := relevances[pkg.StmtInfo.ImportPath]; ok {
1457 if count >= maxUnimportedPackageNames {
1462 // Do not add the unimported packages to seen, since we can have
1463 // multiple packages of the same name as completion suggestions, since
1464 // only one will be chosen.
1465 obj := types.NewPkgName(0, nil, pkg.IdentName, types.NewPackage(pkg.StmtInfo.ImportPath, pkg.IdentName))
1466 c.deepState.enqueue(candidate{
1468 score: unimportedScore(pkg.Relevance),
1470 importPath: pkg.StmtInfo.ImportPath,
1471 name: pkg.StmtInfo.Name,
1476 c.completionCallbacks = append(c.completionCallbacks, func(opts *imports.Options) error {
1478 return imports.GetAllCandidates(ctx, add, prefix, c.filename, c.pkg.GetTypes().Name(), opts.Env)
1483 // alreadyImports reports whether f has an import with the specified path.
1484 func alreadyImports(f *ast.File, path string) bool {
1485 for _, s := range f.Imports {
1486 if source.ImportPath(s) == path {
1493 func (c *completer) inConstDecl() bool {
1494 for _, n := range c.path {
1495 if decl, ok := n.(*ast.GenDecl); ok && decl.Tok == token.CONST {
1502 // structLiteralFieldName finds completions for struct field names inside a struct literal.
1503 func (c *completer) structLiteralFieldName(ctx context.Context) error {
1504 clInfo := c.enclosingCompositeLiteral
1506 // Mark fields of the composite literal that have already been set,
1507 // except for the current field.
1508 addedFields := make(map[*types.Var]bool)
1509 for _, el := range clInfo.cl.Elts {
1510 if kvExpr, ok := el.(*ast.KeyValueExpr); ok {
1511 if clInfo.kv == kvExpr {
1515 if key, ok := kvExpr.Key.(*ast.Ident); ok {
1516 if used, ok := c.pkg.GetTypesInfo().Uses[key]; ok {
1517 if usedVar, ok := used.(*types.Var); ok {
1518 addedFields[usedVar] = true
1525 switch t := clInfo.clType.(type) {
1527 for i := 0; i < t.NumFields(); i++ {
1529 if !addedFields[field] {
1530 c.deepState.enqueue(candidate{
1537 // Add lexical completions if we aren't certain we are in the key part of a
1539 if clInfo.maybeInFieldName {
1540 return c.lexical(ctx)
1543 return c.lexical(ctx)
1549 func (cl *compLitInfo) isStruct() bool {
1550 _, ok := cl.clType.(*types.Struct)
1554 // enclosingCompositeLiteral returns information about the composite literal enclosing the
1556 func enclosingCompositeLiteral(path []ast.Node, pos token.Pos, info *types.Info) *compLitInfo {
1557 for _, n := range path {
1558 switch n := n.(type) {
1559 case *ast.CompositeLit:
1560 // The enclosing node will be a composite literal if the user has just
1561 // opened the curly brace (e.g. &x{<>) or the completion request is triggered
1562 // from an already completed composite literal expression (e.g. &x{foo: 1, <>})
1564 // The position is not part of the composite literal unless it falls within the
1565 // curly braces (e.g. "foo.Foo<>Struct{}").
1566 if !(n.Lbrace < pos && pos <= n.Rbrace) {
1567 // Keep searching since we may yet be inside a composite literal.
1568 // For example "Foo{B: Ba<>{}}".
1572 tv, ok := info.Types[n]
1577 clInfo := compLitInfo{
1579 clType: source.Deref(tv.Type).Underlying(),
1586 for _, el := range n.Elts {
1587 // Remember the expression that the position falls in, if any.
1588 if el.Pos() <= pos && pos <= el.End() {
1592 if kv, ok := el.(*ast.KeyValueExpr); ok {
1594 // If expr == el then we know the position falls in this expression,
1595 // so also record kv as the enclosing *ast.KeyValueExpr.
1603 if clInfo.kv != nil {
1604 // If in a *ast.KeyValueExpr, we know we are in the key if the position
1605 // is to the left of the colon (e.g. "Foo{F<>: V}".
1606 clInfo.inKey = pos <= clInfo.kv.Colon
1608 // If we aren't in a *ast.KeyValueExpr but the composite literal has
1609 // other *ast.KeyValueExprs, we must be on the key side of a new
1610 // *ast.KeyValueExpr (e.g. "Foo{F: V, <>}").
1613 switch clInfo.clType.(type) {
1615 if len(n.Elts) == 0 {
1616 // If the struct literal is empty, next could be a struct field
1617 // name or an expression (e.g. "Foo{<>}" could become "Foo{F:}"
1618 // or "Foo{someVar}").
1619 clInfo.maybeInFieldName = true
1620 } else if len(n.Elts) == 1 {
1621 // If there is one expression and the position is in that expression
1622 // and the expression is an identifier, we may be writing a field
1623 // name or an expression (e.g. "Foo{F<>}").
1624 _, clInfo.maybeInFieldName = expr.(*ast.Ident)
1627 // If we aren't in a *ast.KeyValueExpr we must be adding a new key
1635 if breaksExpectedTypeInference(n, pos) {
1644 // enclosingFunction returns the signature and body of the function
1645 // enclosing the given position.
1646 func enclosingFunction(path []ast.Node, info *types.Info) *funcInfo {
1647 for _, node := range path {
1648 switch t := node.(type) {
1650 if obj, ok := info.Defs[t.Name]; ok {
1652 sig: obj.Type().(*types.Signature),
1657 if typ, ok := info.Types[t]; ok {
1659 sig: typ.Type.(*types.Signature),
1668 func (c *completer) expectedCompositeLiteralType() types.Type {
1669 clInfo := c.enclosingCompositeLiteral
1670 switch t := clInfo.clType.(type) {
1673 return types.Typ[types.Int]
1678 return types.Typ[types.Int]
1687 // If we are completing a key (i.e. field name), there is no expected type.
1692 // If we are in a key-value pair, but not in the key, then we must be on the
1693 // value side. The expected type of the value will be determined from the key.
1694 if clInfo.kv != nil {
1695 if key, ok := clInfo.kv.Key.(*ast.Ident); ok {
1696 for i := 0; i < t.NumFields(); i++ {
1697 if field := t.Field(i); field.Name() == key.Name {
1703 // If we aren't in a key-value pair and aren't in the key, we must be using
1704 // implicit field names.
1706 // The order of the literal fields must match the order in the struct definition.
1707 // Find the element that the position belongs to and suggest that field's type.
1708 if i := exprAtPos(c.pos, clInfo.cl.Elts); i < t.NumFields() {
1709 return t.Field(i).Type()
1716 // typeModifier represents an operator that changes the expected type.
1717 type typeModifier struct {
1725 dereference typeMod = iota // pointer indirection: "*"
1726 reference // adds level of pointer: "&" for values, "*" for type names
1727 chanRead // channel read operator ("<-")
1728 slice // make a slice type ("[]" in "[]int")
1729 array // make an array type ("[2]" in "[2]int")
1736 kindArray objKind = 1 << iota
1753 // penalizedObj represents an object that should be disfavored as a
1754 // completion candidate.
1755 type penalizedObj struct {
1756 // objChain is the full "chain", e.g. "foo.bar().baz" becomes
1757 // []types.Object{foo, bar, baz}.
1758 objChain []types.Object
1759 // penalty is score penalty in the range (0, 1).
1763 // candidateInference holds information we have inferred about a type that can be
1764 // used at the current position.
1765 type candidateInference struct {
1766 // objType is the desired type of an object used at the query position.
1769 // objKind is a mask of expected kinds of types such as "map", "slice", etc.
1772 // variadic is true if we are completing the initial variadic
1773 // parameter. For example:
1774 // append([]T{}, <>) // objType=T variadic=true
1775 // append([]T{}, T{}, <>) // objType=T variadic=false
1778 // modifiers are prefixes such as "*", "&" or "<-" that influence how
1779 // a candidate type relates to the expected type.
1780 modifiers []typeModifier
1782 // convertibleTo is a type our candidate type must be convertible to.
1783 convertibleTo types.Type
1785 // typeName holds information about the expected type name at
1786 // position, if any.
1787 typeName typeNameInference
1789 // assignees are the types that would receive a function call's
1790 // results at the position. For example:
1795 // at "<>", the assignees are [int, <invalid>].
1796 assignees []types.Type
1798 // variadicAssignees is true if we could be completing an inner
1799 // function call that fills out an outer function call's variadic
1800 // params. For example:
1802 // func foo(int, ...string) {}
1804 // foo(<>) // variadicAssignees=true
1805 // foo(bar<>) // variadicAssignees=true
1806 // foo(bar, baz<>) // variadicAssignees=false
1807 variadicAssignees bool
1809 // penalized holds expressions that should be disfavored as
1810 // candidates. For example, it tracks expressions already used in a
1811 // switch statement's other cases. Each expression is tracked using
1812 // its entire object "chain" allowing differentiation between
1813 // "a.foo" and "b.foo" when "a" and "b" are the same type.
1814 penalized []penalizedObj
1816 // objChain contains the chain of objects representing the
1817 // surrounding *ast.SelectorExpr. For example, if we are completing
1818 // "foo.bar.ba<>", objChain will contain []types.Object{foo, bar}.
1819 objChain []types.Object
1822 // typeNameInference holds information about the expected type name at
1824 type typeNameInference struct {
1825 // wantTypeName is true if we expect the name of a type.
1828 // modifiers are prefixes such as "*", "&" or "<-" that influence how
1829 // a candidate type relates to the expected type.
1830 modifiers []typeModifier
1832 // assertableFrom is a type that must be assertable to our candidate type.
1833 assertableFrom types.Type
1835 // wantComparable is true if we want a comparable type.
1838 // seenTypeSwitchCases tracks types that have already been used by
1839 // the containing type switch.
1840 seenTypeSwitchCases []types.Type
1842 // compLitType is true if we are completing a composite literal type
1843 // name, e.g "foo<>{}".
1847 // expectedCandidate returns information about the expected candidate
1848 // for an expression at the query position.
1849 func expectedCandidate(ctx context.Context, c *completer) (inf candidateInference) {
1850 inf.typeName = expectTypeName(c)
1852 if c.enclosingCompositeLiteral != nil {
1853 inf.objType = c.expectedCompositeLiteralType()
1857 for i, node := range c.path {
1858 switch node := node.(type) {
1859 case *ast.BinaryExpr:
1860 // Determine if query position comes from left or right of op.
1862 if c.pos < node.OpPos {
1865 if tv, ok := c.pkg.GetTypesInfo().Types[e]; ok {
1867 case token.LAND, token.LOR:
1868 // Don't infer "bool" type for "&&" or "||". Often you want
1869 // to compose a boolean expression from non-boolean
1872 inf.objType = tv.Type
1876 case *ast.AssignStmt:
1877 // Only rank completions if you are on the right side of the token.
1878 if c.pos > node.TokPos {
1879 i := exprAtPos(c.pos, node.Rhs)
1880 if i >= len(node.Lhs) {
1881 i = len(node.Lhs) - 1
1883 if tv, ok := c.pkg.GetTypesInfo().Types[node.Lhs[i]]; ok {
1884 inf.objType = tv.Type
1887 // If we have a single expression on the RHS, record the LHS
1888 // assignees so we can favor multi-return function calls with
1889 // matching result values.
1890 if len(node.Rhs) <= 1 {
1891 for _, lhs := range node.Lhs {
1892 inf.assignees = append(inf.assignees, c.pkg.GetTypesInfo().TypeOf(lhs))
1895 // Otherwse, record our single assignee, even if its type is
1896 // not available. We use this info to downrank functions
1897 // with the wrong number of result values.
1898 inf.assignees = append(inf.assignees, c.pkg.GetTypesInfo().TypeOf(node.Lhs[i]))
1902 case *ast.ValueSpec:
1903 if node.Type != nil && c.pos > node.Type.End() {
1904 inf.objType = c.pkg.GetTypesInfo().TypeOf(node.Type)
1908 // Only consider CallExpr args if position falls between parens.
1909 if node.Lparen < c.pos && c.pos <= node.Rparen {
1910 // For type conversions like "int64(foo)" we can only infer our
1911 // desired type is convertible to int64.
1912 if typ := typeConversion(node, c.pkg.GetTypesInfo()); typ != nil {
1913 inf.convertibleTo = typ
1917 if tv, ok := c.pkg.GetTypesInfo().Types[node.Fun]; ok {
1918 if sig, ok := tv.Type.(*types.Signature); ok {
1919 numParams := sig.Params().Len()
1924 exprIdx := exprAtPos(c.pos, node.Args)
1926 // If we have one or zero arg expressions, we may be
1927 // completing to a function call that returns multiple
1928 // values, in turn getting passed in to the surrounding
1929 // call. Record the assignees so we can favor function
1930 // calls that return matching values.
1931 if len(node.Args) <= 1 && exprIdx == 0 {
1932 for i := 0; i < sig.Params().Len(); i++ {
1933 inf.assignees = append(inf.assignees, sig.Params().At(i).Type())
1936 // Record that we may be completing into variadic parameters.
1937 inf.variadicAssignees = sig.Variadic()
1940 // Make sure not to run past the end of expected parameters.
1941 if exprIdx >= numParams {
1942 inf.objType = sig.Params().At(numParams - 1).Type()
1944 inf.objType = sig.Params().At(exprIdx).Type()
1947 if sig.Variadic() && exprIdx >= (numParams-1) {
1948 // If we are completing a variadic param, deslice the variadic type.
1949 inf.objType = deslice(inf.objType)
1950 // Record whether we are completing the initial variadic param.
1951 inf.variadic = exprIdx == numParams-1 && len(node.Args) <= numParams
1953 // Check if we can infer object kind from printf verb.
1954 inf.objKind |= printfArgKind(c.pkg.GetTypesInfo(), node, exprIdx)
1959 if funIdent, ok := node.Fun.(*ast.Ident); ok {
1960 obj := c.pkg.GetTypesInfo().ObjectOf(funIdent)
1962 if obj != nil && obj.Parent() == types.Universe {
1963 // Defer call to builtinArgType so we can provide it the
1964 // inferred type from its parent node.
1966 inf = c.builtinArgType(obj, node, inf)
1967 inf.objKind = c.builtinArgKind(ctx, obj, node)
1970 // The expected type of builtin arguments like append() is
1971 // the expected type of the builtin call itself. For
1974 // var foo []int = append(<>)
1976 // To find the expected type at <> we "skip" the append()
1977 // node and get the expected type one level up, which is
1985 case *ast.ReturnStmt:
1986 if c.enclosingFunc != nil {
1987 sig := c.enclosingFunc.sig
1988 // Find signature result that corresponds to our return statement.
1989 if resultIdx := exprAtPos(c.pos, node.Results); resultIdx < len(node.Results) {
1990 if resultIdx < sig.Results().Len() {
1991 inf.objType = sig.Results().At(resultIdx).Type()
1996 case *ast.CaseClause:
1997 if swtch, ok := findSwitchStmt(c.path[i+1:], c.pos, node).(*ast.SwitchStmt); ok {
1998 if tv, ok := c.pkg.GetTypesInfo().Types[swtch.Tag]; ok {
1999 inf.objType = tv.Type
2001 // Record which objects have already been used in the case
2002 // statements so we don't suggest them again.
2003 for _, cc := range swtch.Body.List {
2004 for _, caseExpr := range cc.(*ast.CaseClause).List {
2005 // Don't record the expression we are currently completing.
2006 if caseExpr.Pos() < c.pos && c.pos <= caseExpr.End() {
2010 if objs := objChain(c.pkg.GetTypesInfo(), caseExpr); len(objs) > 0 {
2011 inf.penalized = append(inf.penalized, penalizedObj{objChain: objs, penalty: 0.1})
2018 case *ast.SliceExpr:
2019 // Make sure position falls within the brackets (e.g. "foo[a:<>]").
2020 if node.Lbrack < c.pos && c.pos <= node.Rbrack {
2021 inf.objType = types.Typ[types.Int]
2024 case *ast.IndexExpr:
2025 // Make sure position falls within the brackets (e.g. "foo[<>]").
2026 if node.Lbrack < c.pos && c.pos <= node.Rbrack {
2027 if tv, ok := c.pkg.GetTypesInfo().Types[node.X]; ok {
2028 switch t := tv.Type.Underlying().(type) {
2030 inf.objType = t.Key()
2031 case *types.Slice, *types.Array:
2032 inf.objType = types.Typ[types.Int]
2038 // Make sure we are on right side of arrow (e.g. "foo <- <>").
2039 if c.pos > node.Arrow+1 {
2040 if tv, ok := c.pkg.GetTypesInfo().Types[node.Chan]; ok {
2041 if ch, ok := tv.Type.Underlying().(*types.Chan); ok {
2042 inf.objType = ch.Elem()
2047 case *ast.RangeStmt:
2048 if source.NodeContains(node.X, c.pos) {
2049 inf.objKind |= kindSlice | kindArray | kindMap | kindString
2050 if node.Value == nil {
2051 inf.objKind |= kindChan
2056 inf.modifiers = append(inf.modifiers, typeModifier{mod: dereference})
2057 case *ast.UnaryExpr:
2060 inf.modifiers = append(inf.modifiers, typeModifier{mod: reference})
2062 inf.modifiers = append(inf.modifiers, typeModifier{mod: chanRead})
2064 case *ast.DeferStmt, *ast.GoStmt:
2065 inf.objKind |= kindFunc
2068 if breaksExpectedTypeInference(node, c.pos) {
2077 // objChain decomposes e into a chain of objects if possible. For
2078 // example, "foo.bar().baz" will yield []types.Object{foo, bar, baz}.
2079 // If any part can't be turned into an object, return nil.
2080 func objChain(info *types.Info, e ast.Expr) []types.Object {
2081 var objs []types.Object
2084 switch n := e.(type) {
2086 obj := info.ObjectOf(n)
2090 objs = append(objs, obj)
2092 case *ast.SelectorExpr:
2093 obj := info.ObjectOf(n.Sel)
2097 objs = append(objs, obj)
2100 if len(n.Args) > 0 {
2109 // Reverse order so the layout matches the syntactic order.
2110 for i := 0; i < len(objs)/2; i++ {
2111 objs[i], objs[len(objs)-1-i] = objs[len(objs)-1-i], objs[i]
2117 // applyTypeModifiers applies the list of type modifiers to a type.
2118 // It returns nil if the modifiers could not be applied.
2119 func (ci candidateInference) applyTypeModifiers(typ types.Type, addressable bool) types.Type {
2120 for _, mod := range ci.modifiers {
2123 // For every "*" indirection operator, remove a pointer layer
2124 // from candidate type.
2125 if ptr, ok := typ.Underlying().(*types.Pointer); ok {
2131 // For every "&" address operator, add another pointer layer to
2132 // candidate type, if the candidate is addressable.
2134 typ = types.NewPointer(typ)
2139 // For every "<-" operator, remove a layer of channelness.
2140 if ch, ok := typ.(*types.Chan); ok {
2151 // applyTypeNameModifiers applies the list of type modifiers to a type name.
2152 func (ci candidateInference) applyTypeNameModifiers(typ types.Type) types.Type {
2153 for _, mod := range ci.typeName.modifiers {
2156 typ = types.NewPointer(typ)
2158 typ = types.NewArray(typ, mod.arrayLen)
2160 typ = types.NewSlice(typ)
2166 // matchesVariadic returns true if we are completing a variadic
2167 // parameter and candType is a compatible slice type.
2168 func (ci candidateInference) matchesVariadic(candType types.Type) bool {
2169 return ci.variadic && ci.objType != nil && types.AssignableTo(candType, types.NewSlice(ci.objType))
2172 // findSwitchStmt returns an *ast.CaseClause's corresponding *ast.SwitchStmt or
2173 // *ast.TypeSwitchStmt. path should start from the case clause's first ancestor.
2174 func findSwitchStmt(path []ast.Node, pos token.Pos, c *ast.CaseClause) ast.Stmt {
2175 // Make sure position falls within a "case <>:" clause.
2176 if exprAtPos(pos, c.List) >= len(c.List) {
2179 // A case clause is always nested within a block statement in a switch statement.
2183 if _, ok := path[0].(*ast.BlockStmt); !ok {
2186 switch s := path[1].(type) {
2187 case *ast.SwitchStmt:
2189 case *ast.TypeSwitchStmt:
2196 // breaksExpectedTypeInference reports if an expression node's type is unrelated
2197 // to its child expression node types. For example, "Foo{Bar: x.Baz(<>)}" should
2198 // expect a function argument, not a composite literal value.
2199 func breaksExpectedTypeInference(n ast.Node, pos token.Pos) bool {
2200 switch n := n.(type) {
2201 case *ast.CompositeLit:
2202 // Doesn't break inference if pos is in type name.
2203 // For example: "Foo<>{Bar: 123}"
2204 return !source.NodeContains(n.Type, pos)
2206 // Doesn't break inference if pos is in func name.
2207 // For example: "Foo<>(123)"
2208 return !source.NodeContains(n.Fun, pos)
2209 case *ast.FuncLit, *ast.IndexExpr, *ast.SliceExpr:
2216 // expectTypeName returns information about the expected type name at position.
2217 func expectTypeName(c *completer) typeNameInference {
2218 var inf typeNameInference
2221 for i, p := range c.path {
2222 switch n := p.(type) {
2223 case *ast.FieldList:
2224 // Expect a type name if pos is in a FieldList. This applies to
2225 // FuncType params/results, FuncDecl receiver, StructType, and
2226 // InterfaceType. We don't need to worry about the field name
2227 // because completion bails out early if pos is in an *ast.Ident
2228 // that defines an object.
2229 inf.wantTypeName = true
2231 case *ast.CaseClause:
2232 // Expect type names in type switch case clauses.
2233 if swtch, ok := findSwitchStmt(c.path[i+1:], c.pos, n).(*ast.TypeSwitchStmt); ok {
2234 // The case clause types must be assertable from the type switch parameter.
2235 ast.Inspect(swtch.Assign, func(n ast.Node) bool {
2236 if ta, ok := n.(*ast.TypeAssertExpr); ok {
2237 inf.assertableFrom = c.pkg.GetTypesInfo().TypeOf(ta.X)
2242 inf.wantTypeName = true
2244 // Track the types that have already been used in this
2245 // switch's case statements so we don't recommend them.
2246 for _, e := range swtch.Body.List {
2247 for _, typeExpr := range e.(*ast.CaseClause).List {
2248 // Skip if type expression contains pos. We don't want to
2249 // count it as already used if the user is completing it.
2250 if typeExpr.Pos() < c.pos && c.pos <= typeExpr.End() {
2254 if t := c.pkg.GetTypesInfo().TypeOf(typeExpr); t != nil {
2255 inf.seenTypeSwitchCases = append(inf.seenTypeSwitchCases, t)
2262 return typeNameInference{}
2263 case *ast.TypeAssertExpr:
2264 // Expect type names in type assert expressions.
2265 if n.Lparen < c.pos && c.pos <= n.Rparen {
2266 // The type in parens must be assertable from the expression type.
2267 inf.assertableFrom = c.pkg.GetTypesInfo().TypeOf(n.X)
2268 inf.wantTypeName = true
2271 return typeNameInference{}
2273 inf.modifiers = append(inf.modifiers, typeModifier{mod: reference})
2274 case *ast.CompositeLit:
2275 // We want a type name if position is in the "Type" part of a
2276 // composite literal (e.g. "Foo<>{}").
2277 if n.Type != nil && n.Type.Pos() <= c.pos && c.pos <= n.Type.End() {
2278 inf.wantTypeName = true
2279 inf.compLitType = true
2281 if i < len(c.path)-1 {
2282 // Track preceding "&" operator. Technically it applies to
2283 // the composite literal and not the type name, but if
2284 // affects our type completion nonetheless.
2285 if u, ok := c.path[i+1].(*ast.UnaryExpr); ok && u.Op == token.AND {
2286 inf.modifiers = append(inf.modifiers, typeModifier{mod: reference})
2291 case *ast.ArrayType:
2292 // If we are inside the "Elt" part of an array type, we want a type name.
2293 if n.Elt.Pos() <= c.pos && c.pos <= n.Elt.End() {
2294 inf.wantTypeName = true
2296 // No "Len" expression means a slice type.
2297 inf.modifiers = append(inf.modifiers, typeModifier{mod: slice})
2299 // Try to get the array type using the constant value of "Len".
2300 tv, ok := c.pkg.GetTypesInfo().Types[n.Len]
2301 if ok && tv.Value != nil && tv.Value.Kind() == constant.Int {
2302 if arrayLen, ok := constant.Int64Val(tv.Value); ok {
2303 inf.modifiers = append(inf.modifiers, typeModifier{mod: array, arrayLen: arrayLen})
2308 // ArrayTypes can be nested, so keep going if our parent is an
2310 if i < len(c.path)-1 {
2311 if _, ok := c.path[i+1].(*ast.ArrayType); ok {
2319 inf.wantTypeName = true
2321 inf.wantComparable = source.NodeContains(n.Key, c.pos)
2323 // If the key is empty, assume we are completing the key if
2324 // pos is directly after the "map[".
2325 inf.wantComparable = c.pos == n.Pos()+token.Pos(len("map["))
2328 case *ast.ValueSpec:
2329 inf.wantTypeName = source.NodeContains(n.Type, c.pos)
2332 inf.wantTypeName = source.NodeContains(n.Type, c.pos)
2334 if breaksExpectedTypeInference(p, c.pos) {
2335 return typeNameInference{}
2343 func (c *completer) fakeObj(T types.Type) *types.Var {
2344 return types.NewVar(token.NoPos, c.pkg.GetTypes(), "", T)
2347 // anyCandType reports whether f returns true for any candidate type
2348 // derivable from c. For example, from "foo" we might derive "&foo",
2350 func (c *candidate) anyCandType(f func(t types.Type, addressable bool) bool) bool {
2351 if c.obj == nil || c.obj.Type() == nil {
2355 objType := c.obj.Type()
2357 if f(objType, c.addressable) {
2361 // If c is a func type with a single result, offer the result type.
2362 if sig, ok := objType.Underlying().(*types.Signature); ok {
2363 if sig.Results().Len() == 1 && f(sig.Results().At(0).Type(), false) {
2364 // Mark the candidate so we know to append "()" when formatting.
2365 c.expandFuncCall = true
2371 seenPtrTypes map[types.Type]bool
2376 // Check if dereferencing c would match our type inference. We loop
2377 // since c could have arbitrary levels of pointerness.
2379 ptr, ok := ptrType.Underlying().(*types.Pointer)
2386 // Avoid pointer type cycles.
2387 if seenPtrTypes[ptrType] {
2391 if _, named := ptrType.(*types.Named); named {
2392 // Lazily allocate "seen" since it isn't used normally.
2393 if seenPtrTypes == nil {
2394 seenPtrTypes = make(map[types.Type]bool)
2397 // Track named pointer types we have seen to detect cycles.
2398 seenPtrTypes[ptrType] = true
2401 if f(ptr.Elem(), false) {
2402 // Mark the candidate so we know to prepend "*" when formatting.
2403 c.dereference = ptrDepth
2407 ptrType = ptr.Elem()
2410 // Check if c is addressable and a pointer to c matches our type inference.
2411 if c.addressable && f(types.NewPointer(objType), false) {
2412 // Mark the candidate so we know to prepend "&" when formatting.
2413 c.takeAddress = true
2420 // matchingCandidate reports whether cand matches our type inferences.
2421 // It mutates cand's score in certain cases.
2422 func (c *completer) matchingCandidate(cand *candidate) bool {
2423 if c.completionContext.commentCompletion {
2427 if isTypeName(cand.obj) {
2428 return c.matchingTypeName(cand)
2429 } else if c.wantTypeName() {
2430 // If we want a type, a non-type object never matches.
2434 if c.inference.candTypeMatches(cand) {
2438 candType := cand.obj.Type()
2439 if candType == nil {
2443 if sig, ok := candType.Underlying().(*types.Signature); ok {
2444 if c.inference.assigneesMatch(cand, sig) {
2445 // Invoke the candidate if its results are multi-assignable.
2446 cand.expandFuncCall = true
2451 // Default to invoking *types.Func candidates. This is so function
2452 // completions in an empty statement (or other cases with no expected type)
2453 // are invoked by default.
2454 cand.expandFuncCall = isFunc(cand.obj)
2459 // candTypeMatches reports whether cand makes a good completion
2460 // candidate given the candidate inference. cand's score may be
2461 // mutated to downrank the candidate in certain situations.
2462 func (ci *candidateInference) candTypeMatches(cand *candidate) bool {
2464 expTypes = make([]types.Type, 0, 2)
2465 variadicType types.Type
2467 if ci.objType != nil {
2468 expTypes = append(expTypes, ci.objType)
2471 variadicType = types.NewSlice(ci.objType)
2472 expTypes = append(expTypes, variadicType)
2476 return cand.anyCandType(func(candType types.Type, addressable bool) bool {
2477 // Take into account any type modifiers on the expected type.
2478 candType = ci.applyTypeModifiers(candType, addressable)
2479 if candType == nil {
2483 if ci.convertibleTo != nil && types.ConvertibleTo(candType, ci.convertibleTo) {
2487 for _, expType := range expTypes {
2488 if isEmptyInterface(expType) {
2492 matches, untyped := ci.typeMatches(expType, candType)
2497 if expType == variadicType {
2498 cand.variadic = true
2501 // Lower candidate score for untyped conversions. This avoids
2502 // ranking untyped constants above candidates with an exact type
2503 // match. Don't lower score of builtin constants, e.g. "true".
2504 if untyped && !types.Identical(candType, expType) && cand.obj.Parent() != types.Universe {
2511 // If we don't have a specific expected type, fall back to coarser
2512 // object kind checks.
2513 if ci.objType == nil || isEmptyInterface(ci.objType) {
2514 // If we were able to apply type modifiers to our candidate type,
2515 // count that as a match. For example:
2520 // We were able to apply the "<-" type modifier to "foo", so "foo"
2522 if len(ci.modifiers) > 0 {
2526 // If we didn't have an exact type match, check if our object kind
2528 if ci.kindMatches(candType) {
2529 if ci.objKind == kindFunc {
2530 cand.expandFuncCall = true
2540 // typeMatches reports whether an object of candType makes a good
2541 // completion candidate given the expected type expType. It also
2542 // returns a second bool which is true if both types are basic types
2543 // of the same kind, and at least one is untyped.
2544 func (ci *candidateInference) typeMatches(expType, candType types.Type) (bool, bool) {
2545 // Handle untyped values specially since AssignableTo gives false negatives
2546 // for them (see https://golang.org/issue/32146).
2547 if candBasic, ok := candType.Underlying().(*types.Basic); ok {
2548 if wantBasic, ok := expType.Underlying().(*types.Basic); ok {
2549 // Make sure at least one of them is untyped.
2550 if isUntyped(candType) || isUntyped(expType) {
2551 // Check that their constant kind (bool|int|float|complex|string) matches.
2552 // This doesn't take into account the constant value, so there will be some
2553 // false positives due to integer sign and overflow.
2554 if candBasic.Info()&types.IsConstType == wantBasic.Info()&types.IsConstType {
2561 // AssignableTo covers the case where the types are equal, but also handles
2562 // cases like assigning a concrete type to an interface type.
2563 return types.AssignableTo(candType, expType), false
2566 // kindMatches reports whether candType's kind matches our expected
2567 // kind (e.g. slice, map, etc.).
2568 func (ci *candidateInference) kindMatches(candType types.Type) bool {
2569 return ci.objKind > 0 && ci.objKind&candKind(candType) > 0
2572 // assigneesMatch reports whether an invocation of sig matches the
2573 // number and type of any assignees.
2574 func (ci *candidateInference) assigneesMatch(cand *candidate, sig *types.Signature) bool {
2575 if len(ci.assignees) == 0 {
2579 // Uniresult functions are always usable and are handled by the
2580 // normal, non-assignees type matching logic.
2581 if sig.Results().Len() == 1 {
2585 var numberOfResultsCouldMatch bool
2586 if ci.variadicAssignees {
2587 numberOfResultsCouldMatch = sig.Results().Len() >= len(ci.assignees)-1
2589 numberOfResultsCouldMatch = sig.Results().Len() == len(ci.assignees)
2592 // If our signature doesn't return the right number of values, it's
2593 // not a match, so downrank it. For example:
2595 // var foo func() (int, int)
2596 // a, b, c := <> // downrank "foo()" since it only returns two values
2597 if !numberOfResultsCouldMatch {
2602 // If at least one assignee has a valid type, and all valid
2603 // assignees match the corresponding sig result value, the signature
2606 for i := 0; i < sig.Results().Len(); i++ {
2607 var assignee types.Type
2609 // If we are completing into variadic parameters, deslice the
2610 // expected variadic type.
2611 if ci.variadicAssignees && i >= len(ci.assignees)-1 {
2612 assignee = ci.assignees[len(ci.assignees)-1]
2613 if elem := deslice(assignee); elem != nil {
2617 assignee = ci.assignees[i]
2620 if assignee == nil {
2624 allMatch, _ = ci.typeMatches(assignee, sig.Results().At(i).Type())
2632 func (c *completer) matchingTypeName(cand *candidate) bool {
2633 if !c.wantTypeName() {
2637 typeMatches := func(candType types.Type) bool {
2638 // Take into account any type name modifier prefixes.
2639 candType = c.inference.applyTypeNameModifiers(candType)
2641 if from := c.inference.typeName.assertableFrom; from != nil {
2642 // Don't suggest the starting type in type assertions. For example,
2643 // if "foo" is an io.Writer, don't suggest "foo.(io.Writer)".
2644 if types.Identical(from, candType) {
2648 if intf, ok := from.Underlying().(*types.Interface); ok {
2649 if !types.AssertableTo(intf, candType) {
2655 if c.inference.typeName.wantComparable && !types.Comparable(candType) {
2659 // Skip this type if it has already been used in another type
2661 for _, seen := range c.inference.typeName.seenTypeSwitchCases {
2662 if types.Identical(candType, seen) {
2667 // We can expect a type name and have an expected type in cases like:
2672 // Where our expected type is "[]int", and we expect a type name.
2673 if c.inference.objType != nil {
2674 return types.AssignableTo(candType, c.inference.objType)
2677 // Default to saying any type name is a match.
2681 t := cand.obj.Type()
2687 if !source.IsInterface(t) && typeMatches(types.NewPointer(t)) {
2688 if c.inference.typeName.compLitType {
2689 // If we are completing a composite literal type as in
2690 // "foo<>{}", to make a pointer we must prepend "&".
2691 cand.takeAddress = true
2693 // If we are completing a normal type name such as "foo<>", to
2694 // make a pointer we must prepend "*".
2695 cand.makePointer = true
2704 // "interface { Error() string }" (i.e. error)
2705 errorIntf = types.Universe.Lookup("error").Type().Underlying().(*types.Interface)
2707 // "interface { String() string }" (i.e. fmt.Stringer)
2708 stringerIntf = types.NewInterfaceType([]*types.Func{
2709 types.NewFunc(token.NoPos, nil, "String", types.NewSignature(
2712 types.NewTuple(types.NewParam(token.NoPos, nil, "", types.Typ[types.String])),
2717 byteType = types.Universe.Lookup("byte").Type()
2720 // candKind returns the objKind of candType, if any.
2721 func candKind(candType types.Type) objKind {
2724 switch t := candType.Underlying().(type) {
2727 if t.Elem() == byteType {
2732 if t.Elem() == byteType {
2739 case *types.Pointer:
2742 // Some builtins handle array pointers as arrays, so just report a pointer
2743 // to an array as an array.
2744 if _, isArray := t.Elem().Underlying().(*types.Array); isArray {
2748 switch info := t.Info(); {
2749 case info&types.IsString > 0:
2751 case info&types.IsInteger > 0:
2753 case info&types.IsFloat > 0:
2755 case info&types.IsComplex > 0:
2757 case info&types.IsBoolean > 0:
2760 case *types.Signature:
2764 if types.Implements(candType, errorIntf) {
2768 if types.Implements(candType, stringerIntf) {
2769 kind |= kindStringer