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