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