2019-04-24 17:26:34 -06:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-11-07 18:57:08 -07:00
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
|
|
|
"go/types"
|
|
|
|
|
|
|
|
"golang.org/x/tools/go/ast/astutil"
|
2019-07-01 15:08:29 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/fuzzy"
|
2019-04-28 21:19:54 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/snippet"
|
2019-06-21 14:28:23 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry/trace"
|
2019-05-17 11:45:59 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2018-11-07 18:57:08 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type CompletionItem struct {
|
2019-04-28 21:19:54 -06:00
|
|
|
// Label is the primary text the user sees for this completion item.
|
|
|
|
Label string
|
|
|
|
|
2019-04-29 17:47:54 -06:00
|
|
|
// Detail is supplemental information to present to the user.
|
|
|
|
// This often contains the type or return type of the completion item.
|
2019-04-28 21:19:54 -06:00
|
|
|
Detail string
|
|
|
|
|
2019-04-29 17:47:54 -06:00
|
|
|
// InsertText is the text to insert if this item is selected.
|
|
|
|
// Any of the prefix that has already been typed is not trimmed.
|
|
|
|
// The insert text does not contain snippets.
|
|
|
|
InsertText string
|
2019-04-28 21:19:54 -06:00
|
|
|
|
|
|
|
Kind CompletionItemKind
|
|
|
|
|
2019-06-27 11:50:01 -06:00
|
|
|
// Depth is how many levels were searched to find this completion.
|
|
|
|
// For example when completing "foo<>", "fooBar" is depth 0, and
|
|
|
|
// "fooBar.Baz" is depth 1.
|
|
|
|
Depth int
|
|
|
|
|
2019-04-29 17:47:54 -06:00
|
|
|
// Score is the internal relevance score.
|
|
|
|
// A higher score indicates that this completion item is more relevant.
|
2019-04-28 21:19:54 -06:00
|
|
|
Score float64
|
|
|
|
|
2019-04-29 17:47:54 -06:00
|
|
|
// Snippet is the LSP snippet for the completion item, without placeholders.
|
|
|
|
// The LSP specification contains details about LSP snippets.
|
|
|
|
// For example, a snippet for a function with the following signature:
|
|
|
|
//
|
|
|
|
// func foo(a, b, c int)
|
|
|
|
//
|
|
|
|
// would be:
|
|
|
|
//
|
|
|
|
// foo(${1:})
|
|
|
|
//
|
2019-05-14 12:15:18 -06:00
|
|
|
plainSnippet *snippet.Builder
|
2019-04-29 17:47:54 -06:00
|
|
|
|
|
|
|
// PlaceholderSnippet is the LSP snippet for the completion ite, containing
|
|
|
|
// placeholders. The LSP specification contains details about LSP snippets.
|
|
|
|
// For example, a placeholder snippet for a function with the following signature:
|
|
|
|
//
|
|
|
|
// func foo(a, b, c int)
|
|
|
|
//
|
|
|
|
// would be:
|
|
|
|
//
|
|
|
|
// foo(${1:a int}, ${2: b int}, ${3: c int})
|
|
|
|
//
|
2019-05-14 12:15:18 -06:00
|
|
|
placeholderSnippet *snippet.Builder
|
2019-07-02 15:31:31 -06:00
|
|
|
|
|
|
|
// Documentation is the documentation for the completion item.
|
|
|
|
Documentation string
|
2019-05-14 12:15:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Snippet is a convenience function that determines the snippet that should be
|
|
|
|
// used for an item, depending on if the callee wants placeholders or not.
|
|
|
|
func (i *CompletionItem) Snippet(usePlaceholders bool) string {
|
|
|
|
if usePlaceholders {
|
|
|
|
if i.placeholderSnippet != nil {
|
|
|
|
return i.placeholderSnippet.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i.plainSnippet != nil {
|
|
|
|
return i.plainSnippet.String()
|
|
|
|
}
|
|
|
|
return i.InsertText
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type CompletionItemKind int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Unknown CompletionItemKind = iota
|
|
|
|
InterfaceCompletionItem
|
|
|
|
StructCompletionItem
|
|
|
|
TypeCompletionItem
|
|
|
|
ConstantCompletionItem
|
|
|
|
FieldCompletionItem
|
|
|
|
ParameterCompletionItem
|
|
|
|
VariableCompletionItem
|
|
|
|
FunctionCompletionItem
|
|
|
|
MethodCompletionItem
|
|
|
|
PackageCompletionItem
|
|
|
|
)
|
|
|
|
|
2019-04-24 17:26:34 -06:00
|
|
|
// Scoring constants are used for weighting the relevance of different candidates.
|
|
|
|
const (
|
|
|
|
// stdScore is the base score for all completion items.
|
|
|
|
stdScore float64 = 1.0
|
|
|
|
|
|
|
|
// highScore indicates a very relevant completion item.
|
|
|
|
highScore float64 = 10.0
|
|
|
|
|
|
|
|
// lowScore indicates an irrelevant or not useful completion item.
|
|
|
|
lowScore float64 = 0.01
|
|
|
|
)
|
|
|
|
|
|
|
|
// completer contains the necessary information for a single completion request.
|
|
|
|
type completer struct {
|
|
|
|
// Package-specific fields.
|
|
|
|
types *types.Package
|
|
|
|
info *types.Info
|
|
|
|
qf types.Qualifier
|
2019-07-02 15:31:31 -06:00
|
|
|
opts CompletionOptions
|
2019-04-29 19:08:16 -06:00
|
|
|
|
|
|
|
// view is the View associated with this completion request.
|
|
|
|
view View
|
|
|
|
|
|
|
|
// ctx is the context associated with this completion request.
|
|
|
|
ctx context.Context
|
2019-04-24 17:26:34 -06:00
|
|
|
|
|
|
|
// pos is the position at which the request was triggered.
|
|
|
|
pos token.Pos
|
2018-12-05 15:00:36 -07:00
|
|
|
|
2019-04-24 17:26:34 -06:00
|
|
|
// path is the path of AST nodes enclosing the position.
|
|
|
|
path []ast.Node
|
|
|
|
|
|
|
|
// seen is the map that ensures we do not return duplicate results.
|
|
|
|
seen map[types.Object]bool
|
|
|
|
|
|
|
|
// items is the list of completion items returned.
|
|
|
|
items []CompletionItem
|
|
|
|
|
2019-05-17 11:45:59 -06:00
|
|
|
// surrounding describes the identifier surrounding the position.
|
|
|
|
surrounding *Selection
|
2019-04-24 17:26:34 -06:00
|
|
|
|
2019-06-17 12:11:13 -06:00
|
|
|
// expectedType conains information about the type we expect the completion
|
|
|
|
// candidate to be. It will be the zero value if no information is available.
|
|
|
|
expectedType typeInference
|
2019-04-24 17:26:34 -06:00
|
|
|
|
|
|
|
// enclosingFunction is the function declaration enclosing the position.
|
|
|
|
enclosingFunction *types.Signature
|
|
|
|
|
2019-05-13 15:37:08 -06:00
|
|
|
// enclosingCompositeLiteral contains information about the composite literal
|
|
|
|
// enclosing the position.
|
|
|
|
enclosingCompositeLiteral *compLitInfo
|
2019-06-27 11:50:01 -06:00
|
|
|
|
|
|
|
// deepState contains the current state of our deep completion search.
|
|
|
|
deepState deepCompletionState
|
2019-07-01 15:08:29 -06:00
|
|
|
|
|
|
|
// matcher does fuzzy matching of the candidates for the surrounding prefix.
|
|
|
|
matcher *fuzzy.Matcher
|
2019-05-13 15:37:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type compLitInfo struct {
|
|
|
|
// cl is the *ast.CompositeLit enclosing the position.
|
|
|
|
cl *ast.CompositeLit
|
|
|
|
|
|
|
|
// clType is the type of cl.
|
|
|
|
clType types.Type
|
2019-04-28 21:19:54 -06:00
|
|
|
|
2019-05-13 15:37:08 -06:00
|
|
|
// kv is the *ast.KeyValueExpr enclosing the position, if any.
|
|
|
|
kv *ast.KeyValueExpr
|
2019-04-28 21:19:54 -06:00
|
|
|
|
2019-05-13 15:37:08 -06:00
|
|
|
// inKey is true if we are certain the position is in the key side
|
|
|
|
// of a key-value pair.
|
|
|
|
inKey bool
|
|
|
|
|
|
|
|
// maybeInFieldName is true if inKey is false and it is possible
|
|
|
|
// we are completing a struct field name. For example,
|
|
|
|
// "SomeStruct{<>}" will be inKey=false, but maybeInFieldName=true
|
|
|
|
// because we _could_ be completing a field name.
|
|
|
|
maybeInFieldName bool
|
2019-04-24 17:26:34 -06:00
|
|
|
}
|
|
|
|
|
2019-05-17 11:45:59 -06:00
|
|
|
// A Selection represents the cursor position and surrounding identifier.
|
|
|
|
type Selection struct {
|
|
|
|
Content string
|
|
|
|
Range span.Range
|
|
|
|
Cursor token.Pos
|
2019-05-13 14:49:29 -06:00
|
|
|
}
|
|
|
|
|
2019-05-17 11:45:59 -06:00
|
|
|
func (p Selection) Prefix() string {
|
|
|
|
return p.Content[:p.Cursor-p.Range.Start]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *completer) setSurrounding(ident *ast.Ident) {
|
|
|
|
if c.surrounding != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !(ident.Pos() <= c.pos && c.pos <= ident.End()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.surrounding = &Selection{
|
|
|
|
Content: ident.Name,
|
2019-05-17 08:51:19 -06:00
|
|
|
Range: span.NewRange(c.view.Session().Cache().FileSet(), ident.Pos(), ident.End()),
|
2019-05-17 11:45:59 -06:00
|
|
|
Cursor: c.pos,
|
|
|
|
}
|
2019-07-01 15:08:29 -06:00
|
|
|
if c.surrounding.Prefix() != "" {
|
|
|
|
c.matcher = fuzzy.NewMatcher(c.surrounding.Prefix(), fuzzy.Symbol)
|
|
|
|
}
|
2019-05-17 11:45:59 -06:00
|
|
|
}
|
2019-05-13 14:49:29 -06:00
|
|
|
|
2019-06-27 11:50:01 -06:00
|
|
|
// found adds a candidate completion. We will also search through the object's
|
|
|
|
// members for more candidates.
|
2019-07-02 15:31:31 -06:00
|
|
|
func (c *completer) found(obj types.Object, score float64) error {
|
2019-04-24 17:26:34 -06:00
|
|
|
if obj.Pkg() != nil && obj.Pkg() != c.types && !obj.Exported() {
|
2019-08-06 13:13:11 -06:00
|
|
|
return errors.Errorf("%s is inaccessible from %s", obj.Name(), c.types.Path())
|
2019-04-24 17:26:34 -06:00
|
|
|
}
|
2019-06-27 11:50:01 -06:00
|
|
|
|
|
|
|
if c.inDeepCompletion() {
|
|
|
|
// When searching deep, just make sure we don't have a cycle in our chain.
|
|
|
|
// We don't dedupe by object because we want to allow both "foo.Baz" and
|
|
|
|
// "bar.Baz" even though "Baz" is represented the same types.Object in both.
|
|
|
|
for _, seenObj := range c.deepState.chain {
|
|
|
|
if seenObj == obj {
|
2019-07-02 15:31:31 -06:00
|
|
|
return nil
|
2019-06-27 11:50:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// At the top level, dedupe by object.
|
|
|
|
if c.seen[obj] {
|
2019-07-02 15:31:31 -06:00
|
|
|
return nil
|
2019-06-27 11:50:01 -06:00
|
|
|
}
|
|
|
|
c.seen[obj] = true
|
2019-04-24 17:26:34 -06:00
|
|
|
}
|
2019-06-19 16:24:05 -06:00
|
|
|
|
|
|
|
cand := candidate{
|
|
|
|
obj: obj,
|
|
|
|
score: score,
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.matchingType(&cand) {
|
|
|
|
cand.score *= highScore
|
2019-04-24 17:26:34 -06:00
|
|
|
}
|
2019-06-19 16:24:05 -06:00
|
|
|
|
2019-06-27 11:50:01 -06:00
|
|
|
// Favor shallow matches by lowering weight according to depth.
|
|
|
|
cand.score -= stdScore * float64(len(c.deepState.chain))
|
2019-07-02 15:31:31 -06:00
|
|
|
item, err := c.item(cand)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.items = append(c.items, item)
|
2019-06-27 11:50:01 -06:00
|
|
|
|
|
|
|
c.deepSearch(obj)
|
2019-07-02 15:31:31 -06:00
|
|
|
return nil
|
2019-06-19 16:24:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// candidate represents a completion candidate.
|
|
|
|
type candidate struct {
|
|
|
|
// obj is the types.Object to complete to.
|
|
|
|
obj types.Object
|
|
|
|
|
|
|
|
// score is used to rank candidates.
|
|
|
|
score float64
|
|
|
|
|
|
|
|
// expandFuncCall is true if obj should be invoked in the completion.
|
|
|
|
// For example, expandFuncCall=true yields "foo()", expandFuncCall=false yields "foo".
|
|
|
|
expandFuncCall bool
|
2019-04-24 17:26:34 -06:00
|
|
|
}
|
2018-12-05 15:00:36 -07:00
|
|
|
|
2019-06-27 11:50:01 -06:00
|
|
|
type CompletionOptions struct {
|
2019-07-02 15:31:31 -06:00
|
|
|
DeepComplete bool
|
|
|
|
WantDocumentaton bool
|
2019-06-27 11:50:01 -06:00
|
|
|
}
|
|
|
|
|
2018-12-05 15:00:36 -07:00
|
|
|
// Completion returns a list of possible candidates for completion, given a
|
2019-04-24 17:26:34 -06:00
|
|
|
// a file and a position.
|
|
|
|
//
|
2019-05-17 11:45:59 -06:00
|
|
|
// The selection is computed based on the preceding identifier and can be used by
|
2019-04-24 17:26:34 -06:00
|
|
|
// the client to score the quality of the completion. For instance, some clients
|
|
|
|
// may tolerate imperfect matches as valid completion results, since users may make typos.
|
2019-06-27 11:50:01 -06:00
|
|
|
func Completion(ctx context.Context, view View, f GoFile, pos token.Pos, opts CompletionOptions) ([]CompletionItem, *Selection, error) {
|
2019-06-26 20:46:12 -06:00
|
|
|
ctx, done := trace.StartSpan(ctx, "source.Completion")
|
|
|
|
defer done()
|
2019-07-11 19:05:55 -06:00
|
|
|
|
|
|
|
file, err := f.GetAST(ctx, ParseFull)
|
2019-05-20 13:23:02 -06:00
|
|
|
if file == nil {
|
2019-07-11 19:05:55 -06:00
|
|
|
return nil, nil, err
|
2019-05-20 13:23:02 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
pkg, err := f.GetPackage(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2019-03-11 15:14:55 -06:00
|
|
|
}
|
2019-04-23 16:00:40 -06:00
|
|
|
// Completion is based on what precedes the cursor.
|
2019-04-24 17:26:34 -06:00
|
|
|
// Find the path to the position before pos.
|
2019-04-23 16:00:40 -06:00
|
|
|
path, _ := astutil.PathEnclosingInterval(file, pos-1, pos-1)
|
2018-11-07 18:57:08 -07:00
|
|
|
if path == nil {
|
2019-08-06 13:13:11 -06:00
|
|
|
return nil, nil, errors.Errorf("cannot find node enclosing position")
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
2019-04-23 16:34:23 -06:00
|
|
|
// Skip completion inside comments.
|
2019-04-24 17:26:34 -06:00
|
|
|
for _, g := range file.Comments {
|
|
|
|
if g.Pos() <= pos && pos <= g.End() {
|
2019-05-17 11:45:59 -06:00
|
|
|
return nil, nil, nil
|
2019-04-24 17:26:34 -06:00
|
|
|
}
|
2019-04-23 16:34:23 -06:00
|
|
|
}
|
|
|
|
// Skip completion inside any kind of literal.
|
|
|
|
if _, ok := path[0].(*ast.BasicLit); ok {
|
2019-05-17 11:45:59 -06:00
|
|
|
return nil, nil, nil
|
2019-01-15 23:36:31 -07:00
|
|
|
}
|
|
|
|
|
2019-05-13 15:37:08 -06:00
|
|
|
clInfo := enclosingCompositeLiteral(path, pos, pkg.GetTypesInfo())
|
2019-04-24 17:26:34 -06:00
|
|
|
c := &completer{
|
2019-04-28 21:19:54 -06:00
|
|
|
types: pkg.GetTypes(),
|
|
|
|
info: pkg.GetTypesInfo(),
|
|
|
|
qf: qualifier(file, pkg.GetTypes(), pkg.GetTypesInfo()),
|
2019-06-21 15:00:02 -06:00
|
|
|
view: view,
|
2019-04-29 19:08:16 -06:00
|
|
|
ctx: ctx,
|
2019-04-28 21:19:54 -06:00
|
|
|
path: path,
|
|
|
|
pos: pos,
|
|
|
|
seen: make(map[types.Object]bool),
|
|
|
|
enclosingFunction: enclosingFunction(path, pos, pkg.GetTypesInfo()),
|
2019-05-13 15:37:08 -06:00
|
|
|
enclosingCompositeLiteral: clInfo,
|
2019-07-02 15:31:31 -06:00
|
|
|
opts: opts,
|
2019-04-24 17:26:34 -06:00
|
|
|
}
|
2019-01-31 23:40:03 -07:00
|
|
|
|
2019-06-27 11:50:01 -06:00
|
|
|
c.deepState.enabled = opts.DeepComplete
|
|
|
|
|
2019-05-17 11:45:59 -06:00
|
|
|
// Set the filter surrounding.
|
2019-05-13 15:37:08 -06:00
|
|
|
if ident, ok := path[0].(*ast.Ident); ok {
|
2019-05-17 11:45:59 -06:00
|
|
|
c.setSurrounding(ident)
|
2019-05-13 15:37:08 -06:00
|
|
|
}
|
2019-05-10 10:26:15 -06:00
|
|
|
|
2019-05-13 15:37:08 -06:00
|
|
|
c.expectedType = expectedType(c)
|
2019-04-24 17:26:34 -06:00
|
|
|
|
2019-05-13 15:37:08 -06:00
|
|
|
// Struct literals are handled entirely separately.
|
|
|
|
if c.wantStructFieldCompletions() {
|
|
|
|
if err := c.structLiteralFieldName(); err != nil {
|
2019-05-17 11:45:59 -06:00
|
|
|
return nil, nil, err
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
2019-05-17 11:45:59 -06:00
|
|
|
return c.items, c.surrounding, nil
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
switch n := path[0].(type) {
|
|
|
|
case *ast.Ident:
|
|
|
|
// Is this the Sel part of a selector?
|
|
|
|
if sel, ok := path[1].(*ast.SelectorExpr); ok && sel.Sel == n {
|
2019-04-24 17:26:34 -06:00
|
|
|
if err := c.selector(sel); err != nil {
|
2019-05-17 11:45:59 -06:00
|
|
|
return nil, nil, err
|
2019-04-24 17:26:34 -06:00
|
|
|
}
|
2019-05-17 11:45:59 -06:00
|
|
|
return c.items, c.surrounding, nil
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
// reject defining identifiers
|
2019-03-06 14:33:47 -07:00
|
|
|
if obj, ok := pkg.GetTypesInfo().Defs[n]; ok {
|
2018-11-07 18:57:08 -07:00
|
|
|
if v, ok := obj.(*types.Var); ok && v.IsField() {
|
|
|
|
// An anonymous field is also a reference to a type.
|
|
|
|
} else {
|
|
|
|
of := ""
|
|
|
|
if obj != nil {
|
2019-03-06 14:33:47 -07:00
|
|
|
qual := types.RelativeTo(pkg.GetTypes())
|
2018-11-07 18:57:08 -07:00
|
|
|
of += ", of " + types.ObjectString(obj, qual)
|
|
|
|
}
|
2019-08-06 13:13:11 -06:00
|
|
|
return nil, nil, errors.Errorf("this is a definition%s", of)
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
}
|
2019-04-24 17:26:34 -06:00
|
|
|
if err := c.lexical(); err != nil {
|
2019-05-17 11:45:59 -06:00
|
|
|
return nil, nil, err
|
2019-04-24 17:26:34 -06:00
|
|
|
}
|
2018-11-07 18:57:08 -07:00
|
|
|
|
|
|
|
// The function name hasn't been typed yet, but the parens are there:
|
|
|
|
// recv.‸(arg)
|
|
|
|
case *ast.TypeAssertExpr:
|
|
|
|
// Create a fake selector expression.
|
2019-04-24 17:26:34 -06:00
|
|
|
if err := c.selector(&ast.SelectorExpr{X: n.X}); err != nil {
|
2019-05-17 11:45:59 -06:00
|
|
|
return nil, nil, err
|
2019-04-24 17:26:34 -06:00
|
|
|
}
|
2018-11-07 18:57:08 -07:00
|
|
|
|
|
|
|
case *ast.SelectorExpr:
|
2019-05-20 15:31:13 -06:00
|
|
|
// The go parser inserts a phantom "_" Sel node when the selector is
|
|
|
|
// not followed by an identifier or a "(". The "_" isn't actually in
|
|
|
|
// the text, so don't think it is our surrounding.
|
|
|
|
// TODO: Find a way to differentiate between phantom "_" and real "_",
|
|
|
|
// perhaps by checking if "_" is present in file contents.
|
|
|
|
if n.Sel.Name != "_" || c.pos != n.Sel.Pos() {
|
|
|
|
c.setSurrounding(n.Sel)
|
|
|
|
}
|
2019-05-17 11:45:59 -06:00
|
|
|
|
2019-04-24 17:26:34 -06:00
|
|
|
if err := c.selector(n); err != nil {
|
2019-05-17 11:45:59 -06:00
|
|
|
return nil, nil, err
|
2019-04-24 17:26:34 -06:00
|
|
|
}
|
2018-11-07 18:57:08 -07:00
|
|
|
|
|
|
|
default:
|
|
|
|
// fallback to lexical completions
|
2019-04-24 17:26:34 -06:00
|
|
|
if err := c.lexical(); err != nil {
|
2019-05-17 11:45:59 -06:00
|
|
|
return nil, nil, err
|
2019-04-24 17:26:34 -06:00
|
|
|
}
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
2019-05-13 15:37:08 -06:00
|
|
|
|
2019-05-17 11:45:59 -06:00
|
|
|
return c.items, c.surrounding, nil
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
|
2019-05-13 15:37:08 -06:00
|
|
|
func (c *completer) wantStructFieldCompletions() bool {
|
|
|
|
clInfo := c.enclosingCompositeLiteral
|
|
|
|
if clInfo == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return clInfo.isStruct() && (clInfo.inKey || clInfo.maybeInFieldName)
|
|
|
|
}
|
|
|
|
|
2019-06-17 12:11:13 -06:00
|
|
|
func (c *completer) wantTypeName() bool {
|
|
|
|
return c.expectedType.wantTypeName
|
|
|
|
}
|
|
|
|
|
2019-04-24 17:26:34 -06:00
|
|
|
// selector finds completions for the specified selector expression.
|
|
|
|
func (c *completer) selector(sel *ast.SelectorExpr) error {
|
2018-11-07 18:57:08 -07:00
|
|
|
// Is sel a qualified identifier?
|
|
|
|
if id, ok := sel.X.(*ast.Ident); ok {
|
2019-04-24 17:26:34 -06:00
|
|
|
if pkgname, ok := c.info.Uses[id].(*types.PkgName); ok {
|
2019-06-27 11:50:01 -06:00
|
|
|
c.packageMembers(pkgname)
|
2019-04-24 17:26:34 -06:00
|
|
|
return nil
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 17:26:34 -06:00
|
|
|
// Invariant: sel is a true selector.
|
|
|
|
tv, ok := c.info.Types[sel.X]
|
2018-11-07 18:57:08 -07:00
|
|
|
if !ok {
|
2019-08-06 13:13:11 -06:00
|
|
|
return errors.Errorf("cannot resolve %s", sel.X)
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
|
2019-06-27 11:50:01 -06:00
|
|
|
return c.methodsAndFields(tv.Type, tv.Addressable())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *completer) packageMembers(pkg *types.PkgName) {
|
|
|
|
scope := pkg.Imported().Scope()
|
|
|
|
for _, name := range scope.Names() {
|
|
|
|
c.found(scope.Lookup(name), stdScore)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *completer) methodsAndFields(typ types.Type, addressable bool) error {
|
|
|
|
var mset *types.MethodSet
|
|
|
|
|
|
|
|
if addressable && !types.IsInterface(typ) && !isPointer(typ) {
|
|
|
|
// Add methods of *T, which includes methods with receiver T.
|
|
|
|
mset = types.NewMethodSet(types.NewPointer(typ))
|
|
|
|
} else {
|
|
|
|
// Add methods of T.
|
|
|
|
mset = types.NewMethodSet(typ)
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
|
2019-06-27 11:50:01 -06:00
|
|
|
for i := 0; i < mset.Len(); i++ {
|
|
|
|
c.found(mset.At(i).Obj(), stdScore)
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
|
2019-04-24 17:26:34 -06:00
|
|
|
// Add fields of T.
|
2019-06-27 11:50:01 -06:00
|
|
|
for _, f := range fieldSelections(typ) {
|
2019-04-24 17:26:34 -06:00
|
|
|
c.found(f, stdScore)
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
2019-04-24 17:26:34 -06:00
|
|
|
return nil
|
2019-01-31 23:40:03 -07:00
|
|
|
}
|
|
|
|
|
2018-11-07 18:57:08 -07:00
|
|
|
// lexical finds completions in the lexical environment.
|
2019-04-24 17:26:34 -06:00
|
|
|
func (c *completer) lexical() error {
|
2018-11-07 18:57:08 -07:00
|
|
|
var scopes []*types.Scope // scopes[i], where i<len(path), is the possibly nil Scope of path[i].
|
2019-04-24 17:26:34 -06:00
|
|
|
for _, n := range c.path {
|
2018-11-07 18:57:08 -07:00
|
|
|
switch node := n.(type) {
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
n = node.Type
|
|
|
|
case *ast.FuncLit:
|
|
|
|
n = node.Type
|
|
|
|
}
|
2019-04-24 17:26:34 -06:00
|
|
|
scopes = append(scopes, c.info.Scopes[n])
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
2019-04-24 17:26:34 -06:00
|
|
|
scopes = append(scopes, c.types.Scope(), types.Universe)
|
2018-11-07 18:57:08 -07:00
|
|
|
|
2019-04-25 15:19:24 -06:00
|
|
|
// Track seen variables to avoid showing completions for shadowed variables.
|
|
|
|
// This works since we look at scopes from innermost to outermost.
|
|
|
|
seen := make(map[string]struct{})
|
|
|
|
|
2018-11-07 18:57:08 -07:00
|
|
|
// Process scopes innermost first.
|
|
|
|
for i, scope := range scopes {
|
|
|
|
if scope == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, name := range scope.Names() {
|
2019-04-24 17:26:34 -06:00
|
|
|
declScope, obj := scope.LookupParent(name, c.pos)
|
2018-11-07 18:57:08 -07:00
|
|
|
if declScope != scope {
|
|
|
|
continue // Name was declared in some enclosing scope, or not at all.
|
|
|
|
}
|
|
|
|
// If obj's type is invalid, find the AST node that defines the lexical block
|
|
|
|
// containing the declaration of obj. Don't resolve types for packages.
|
|
|
|
if _, ok := obj.(*types.PkgName); !ok && obj.Type() == types.Typ[types.Invalid] {
|
|
|
|
// Match the scope to its ast.Node. If the scope is the package scope,
|
|
|
|
// use the *ast.File as the starting node.
|
|
|
|
var node ast.Node
|
2019-04-24 17:26:34 -06:00
|
|
|
if i < len(c.path) {
|
|
|
|
node = c.path[i]
|
|
|
|
} else if i == len(c.path) { // use the *ast.File for package scope
|
|
|
|
node = c.path[i-1]
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
if node != nil {
|
2019-04-24 17:26:34 -06:00
|
|
|
if resolved := resolveInvalid(obj, node, c.info); resolved != nil {
|
2018-11-07 18:57:08 -07:00
|
|
|
obj = resolved
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 09:13:53 -07:00
|
|
|
score := stdScore
|
2018-11-07 18:57:08 -07:00
|
|
|
// Rank builtins significantly lower than other results.
|
|
|
|
if scope == types.Universe {
|
|
|
|
score *= 0.1
|
|
|
|
}
|
2019-07-11 19:05:55 -06:00
|
|
|
|
2019-04-25 15:19:24 -06:00
|
|
|
// If we haven't already added a candidate for an object with this name.
|
|
|
|
if _, ok := seen[obj.Name()]; !ok {
|
|
|
|
seen[obj.Name()] = struct{}{}
|
2019-04-24 17:26:34 -06:00
|
|
|
c.found(obj, score)
|
2019-04-25 15:19:24 -06:00
|
|
|
}
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
}
|
2019-04-24 17:26:34 -06:00
|
|
|
return nil
|
2019-01-15 23:36:31 -07:00
|
|
|
}
|
|
|
|
|
2019-05-13 15:37:08 -06:00
|
|
|
// structLiteralFieldName finds completions for struct field names inside a struct literal.
|
|
|
|
func (c *completer) structLiteralFieldName() error {
|
|
|
|
clInfo := c.enclosingCompositeLiteral
|
|
|
|
|
2018-11-07 18:57:08 -07:00
|
|
|
// Mark fields of the composite literal that have already been set,
|
|
|
|
// except for the current field.
|
|
|
|
addedFields := make(map[*types.Var]bool)
|
2019-05-13 15:37:08 -06:00
|
|
|
for _, el := range clInfo.cl.Elts {
|
2019-04-24 17:26:34 -06:00
|
|
|
if kvExpr, ok := el.(*ast.KeyValueExpr); ok {
|
2019-05-13 15:37:08 -06:00
|
|
|
if clInfo.kv == kvExpr {
|
2018-11-07 18:57:08 -07:00
|
|
|
continue
|
|
|
|
}
|
2019-04-24 17:26:34 -06:00
|
|
|
|
|
|
|
if key, ok := kvExpr.Key.(*ast.Ident); ok {
|
|
|
|
if used, ok := c.info.Uses[key]; ok {
|
2018-11-07 18:57:08 -07:00
|
|
|
if usedVar, ok := used.(*types.Var); ok {
|
|
|
|
addedFields[usedVar] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-13 15:37:08 -06:00
|
|
|
|
|
|
|
switch t := clInfo.clType.(type) {
|
|
|
|
case *types.Struct:
|
|
|
|
for i := 0; i < t.NumFields(); i++ {
|
|
|
|
field := t.Field(i)
|
|
|
|
if !addedFields[field] {
|
|
|
|
c.found(field, highScore)
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
2019-05-13 15:37:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add lexical completions if we aren't certain we are in the key part of a
|
|
|
|
// key-value pair.
|
|
|
|
if clInfo.maybeInFieldName {
|
2019-04-24 17:26:34 -06:00
|
|
|
return c.lexical()
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
2019-05-13 15:37:08 -06:00
|
|
|
default:
|
|
|
|
return c.lexical()
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
2019-05-13 15:37:08 -06:00
|
|
|
|
2019-04-24 17:26:34 -06:00
|
|
|
return nil
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
|
2019-05-13 15:37:08 -06:00
|
|
|
func (cl *compLitInfo) isStruct() bool {
|
|
|
|
_, ok := cl.clType.(*types.Struct)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// enclosingCompositeLiteral returns information about the composite literal enclosing the
|
|
|
|
// position.
|
|
|
|
func enclosingCompositeLiteral(path []ast.Node, pos token.Pos, info *types.Info) *compLitInfo {
|
2019-04-28 21:19:54 -06:00
|
|
|
for _, n := range path {
|
2019-04-23 16:00:40 -06:00
|
|
|
switch n := n.(type) {
|
|
|
|
case *ast.CompositeLit:
|
2019-04-24 17:26:34 -06:00
|
|
|
// The enclosing node will be a composite literal if the user has just
|
|
|
|
// opened the curly brace (e.g. &x{<>) or the completion request is triggered
|
|
|
|
// from an already completed composite literal expression (e.g. &x{foo: 1, <>})
|
|
|
|
//
|
|
|
|
// The position is not part of the composite literal unless it falls within the
|
|
|
|
// curly braces (e.g. "foo.Foo<>Struct{}").
|
2019-05-13 15:37:08 -06:00
|
|
|
if !(n.Lbrace <= pos && pos <= n.Rbrace) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tv, ok := info.Types[n]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
clInfo := compLitInfo{
|
|
|
|
cl: n,
|
|
|
|
clType: tv.Type.Underlying(),
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
expr ast.Expr
|
|
|
|
hasKeys bool
|
|
|
|
)
|
|
|
|
for _, el := range n.Elts {
|
|
|
|
// Remember the expression that the position falls in, if any.
|
|
|
|
if el.Pos() <= pos && pos <= el.End() {
|
|
|
|
expr = el
|
|
|
|
}
|
|
|
|
|
|
|
|
if kv, ok := el.(*ast.KeyValueExpr); ok {
|
|
|
|
hasKeys = true
|
|
|
|
// If expr == el then we know the position falls in this expression,
|
|
|
|
// so also record kv as the enclosing *ast.KeyValueExpr.
|
|
|
|
if expr == el {
|
|
|
|
clInfo.kv = kv
|
|
|
|
break
|
|
|
|
}
|
2019-04-23 16:00:40 -06:00
|
|
|
}
|
|
|
|
}
|
2019-05-13 15:37:08 -06:00
|
|
|
|
|
|
|
if clInfo.kv != nil {
|
|
|
|
// If in a *ast.KeyValueExpr, we know we are in the key if the position
|
|
|
|
// is to the left of the colon (e.g. "Foo{F<>: V}".
|
|
|
|
clInfo.inKey = pos <= clInfo.kv.Colon
|
|
|
|
} else if hasKeys {
|
|
|
|
// If we aren't in a *ast.KeyValueExpr but the composite literal has
|
|
|
|
// other *ast.KeyValueExprs, we must be on the key side of a new
|
|
|
|
// *ast.KeyValueExpr (e.g. "Foo{F: V, <>}").
|
|
|
|
clInfo.inKey = true
|
|
|
|
} else {
|
|
|
|
switch clInfo.clType.(type) {
|
|
|
|
case *types.Struct:
|
|
|
|
if len(n.Elts) == 0 {
|
|
|
|
// If the struct literal is empty, next could be a struct field
|
|
|
|
// name or an expression (e.g. "Foo{<>}" could become "Foo{F:}"
|
|
|
|
// or "Foo{someVar}").
|
|
|
|
clInfo.maybeInFieldName = true
|
|
|
|
} else if len(n.Elts) == 1 {
|
|
|
|
// If there is one expression and the position is in that expression
|
|
|
|
// and the expression is an identifier, we may be writing a field
|
|
|
|
// name or an expression (e.g. "Foo{F<>}").
|
|
|
|
_, clInfo.maybeInFieldName = expr.(*ast.Ident)
|
|
|
|
}
|
|
|
|
case *types.Map:
|
|
|
|
// If we aren't in a *ast.KeyValueExpr we must be adding a new key
|
|
|
|
// to the map.
|
|
|
|
clInfo.inKey = true
|
|
|
|
}
|
2019-04-24 17:26:34 -06:00
|
|
|
}
|
2019-05-13 15:37:08 -06:00
|
|
|
|
|
|
|
return &clInfo
|
2019-05-10 10:26:15 -06:00
|
|
|
default:
|
|
|
|
if breaksExpectedTypeInference(n) {
|
2019-05-13 15:37:08 -06:00
|
|
|
return nil
|
2019-05-10 10:26:15 -06:00
|
|
|
}
|
2019-04-23 16:00:40 -06:00
|
|
|
}
|
|
|
|
}
|
2019-05-13 15:37:08 -06:00
|
|
|
|
|
|
|
return nil
|
2019-04-23 16:00:40 -06:00
|
|
|
}
|
|
|
|
|
2019-04-24 17:26:34 -06:00
|
|
|
// enclosingFunction returns the signature of the function enclosing the given position.
|
2018-11-07 18:57:08 -07:00
|
|
|
func enclosingFunction(path []ast.Node, pos token.Pos, info *types.Info) *types.Signature {
|
|
|
|
for _, node := range path {
|
|
|
|
switch t := node.(type) {
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
if obj, ok := info.Defs[t.Name]; ok {
|
|
|
|
return obj.Type().(*types.Signature)
|
|
|
|
}
|
|
|
|
case *ast.FuncLit:
|
|
|
|
if typ, ok := info.Types[t]; ok {
|
|
|
|
return typ.Type.(*types.Signature)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-13 15:37:08 -06:00
|
|
|
func (c *completer) expectedCompositeLiteralType() types.Type {
|
|
|
|
clInfo := c.enclosingCompositeLiteral
|
|
|
|
switch t := clInfo.clType.(type) {
|
2019-04-23 16:00:40 -06:00
|
|
|
case *types.Slice:
|
2019-05-13 15:37:08 -06:00
|
|
|
if clInfo.inKey {
|
|
|
|
return types.Typ[types.Int]
|
|
|
|
}
|
2019-04-23 16:00:40 -06:00
|
|
|
return t.Elem()
|
|
|
|
case *types.Array:
|
2019-05-13 15:37:08 -06:00
|
|
|
if clInfo.inKey {
|
|
|
|
return types.Typ[types.Int]
|
|
|
|
}
|
2019-04-23 16:00:40 -06:00
|
|
|
return t.Elem()
|
|
|
|
case *types.Map:
|
2019-05-13 15:37:08 -06:00
|
|
|
if clInfo.inKey {
|
2019-04-23 16:00:40 -06:00
|
|
|
return t.Key()
|
|
|
|
}
|
|
|
|
return t.Elem()
|
|
|
|
case *types.Struct:
|
2019-05-13 15:37:08 -06:00
|
|
|
// If we are completing a key (i.e. field name), there is no expected type.
|
|
|
|
if clInfo.inKey {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are in a key-value pair, but not in the key, then we must be on the
|
|
|
|
// value side. The expected type of the value will be determined from the key.
|
|
|
|
if clInfo.kv != nil {
|
|
|
|
if key, ok := clInfo.kv.Key.(*ast.Ident); ok {
|
2019-04-23 16:00:40 -06:00
|
|
|
for i := 0; i < t.NumFields(); i++ {
|
2019-04-24 17:26:34 -06:00
|
|
|
if field := t.Field(i); field.Name() == key.Name {
|
2019-04-23 16:00:40 -06:00
|
|
|
return field.Type()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-13 15:37:08 -06:00
|
|
|
} else {
|
|
|
|
// If we aren't in a key-value pair and aren't in the key, we must be using
|
|
|
|
// implicit field names.
|
|
|
|
|
|
|
|
// The order of the literal fields must match the order in the struct definition.
|
|
|
|
// Find the element that the position belongs to and suggest that field's type.
|
|
|
|
if i := indexExprAtPos(c.pos, clInfo.cl.Elts); i < t.NumFields() {
|
|
|
|
return t.Field(i).Type()
|
2019-04-23 16:00:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-15 16:29:19 -06:00
|
|
|
// typeModifier represents an operator that changes the expected type.
|
|
|
|
type typeModifier int
|
|
|
|
|
|
|
|
const (
|
2019-06-26 16:30:01 -06:00
|
|
|
star typeModifier = iota // dereference operator for expressions, pointer indicator for types
|
|
|
|
reference // reference ("&") operator
|
|
|
|
chanRead // channel read ("<-") operator
|
2019-05-15 16:29:19 -06:00
|
|
|
)
|
|
|
|
|
2019-06-17 12:11:13 -06:00
|
|
|
// typeInference holds information we have inferred about a type that can be
|
|
|
|
// used at the current position.
|
|
|
|
type typeInference struct {
|
|
|
|
// objType is the desired type of an object used at the query position.
|
|
|
|
objType types.Type
|
|
|
|
|
|
|
|
// wantTypeName is true if we expect the name of a type.
|
|
|
|
wantTypeName bool
|
2019-06-19 10:12:57 -06:00
|
|
|
|
|
|
|
// modifiers are prefixes such as "*", "&" or "<-" that influence how
|
|
|
|
// a candidate type relates to the expected type.
|
|
|
|
modifiers []typeModifier
|
2019-06-26 16:30:01 -06:00
|
|
|
|
|
|
|
// assertableFrom is a type that must be assertable to our candidate type.
|
|
|
|
assertableFrom types.Type
|
2019-06-27 16:37:50 -06:00
|
|
|
|
|
|
|
// convertibleTo is a type our candidate type must be convertible to.
|
|
|
|
convertibleTo types.Type
|
2019-06-17 12:11:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// expectedType returns information about the expected type for an expression at
|
|
|
|
// the query position.
|
|
|
|
func expectedType(c *completer) typeInference {
|
|
|
|
if ti := expectTypeName(c); ti.wantTypeName {
|
|
|
|
return ti
|
|
|
|
}
|
|
|
|
|
2019-05-13 15:37:08 -06:00
|
|
|
if c.enclosingCompositeLiteral != nil {
|
2019-06-17 12:11:13 -06:00
|
|
|
return typeInference{objType: c.expectedCompositeLiteralType()}
|
2019-05-13 15:37:08 -06:00
|
|
|
}
|
|
|
|
|
2019-05-10 10:26:15 -06:00
|
|
|
var (
|
2019-06-27 16:37:50 -06:00
|
|
|
modifiers []typeModifier
|
|
|
|
typ types.Type
|
|
|
|
convertibleTo types.Type
|
2019-05-10 10:26:15 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
Nodes:
|
2019-05-15 16:29:19 -06:00
|
|
|
for i, node := range c.path {
|
|
|
|
switch node := node.(type) {
|
2018-11-07 18:57:08 -07:00
|
|
|
case *ast.BinaryExpr:
|
|
|
|
// Determine if query position comes from left or right of op.
|
2019-05-15 16:29:19 -06:00
|
|
|
e := node.X
|
|
|
|
if c.pos < node.OpPos {
|
|
|
|
e = node.Y
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
2019-05-10 10:26:15 -06:00
|
|
|
if tv, ok := c.info.Types[e]; ok {
|
|
|
|
typ = tv.Type
|
|
|
|
break Nodes
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
case *ast.AssignStmt:
|
|
|
|
// Only rank completions if you are on the right side of the token.
|
2019-05-15 16:29:19 -06:00
|
|
|
if c.pos > node.TokPos {
|
|
|
|
i := indexExprAtPos(c.pos, node.Rhs)
|
|
|
|
if i >= len(node.Lhs) {
|
|
|
|
i = len(node.Lhs) - 1
|
2019-05-10 10:26:15 -06:00
|
|
|
}
|
2019-05-15 16:29:19 -06:00
|
|
|
if tv, ok := c.info.Types[node.Lhs[i]]; ok {
|
2019-05-10 10:26:15 -06:00
|
|
|
typ = tv.Type
|
|
|
|
break Nodes
|
|
|
|
}
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
2019-06-17 12:11:13 -06:00
|
|
|
return typeInference{}
|
2018-11-07 18:57:08 -07:00
|
|
|
case *ast.CallExpr:
|
2019-05-10 10:26:15 -06:00
|
|
|
// Only consider CallExpr args if position falls between parens.
|
2019-05-15 16:29:19 -06:00
|
|
|
if node.Lparen <= c.pos && c.pos <= node.Rparen {
|
2019-06-27 16:37:50 -06:00
|
|
|
// For type conversions like "int64(foo)" we can only infer our
|
|
|
|
// desired type is convertible to int64.
|
|
|
|
if typ := typeConversion(node, c.info); typ != nil {
|
|
|
|
convertibleTo = typ
|
|
|
|
break Nodes
|
|
|
|
}
|
|
|
|
|
2019-05-15 16:29:19 -06:00
|
|
|
if tv, ok := c.info.Types[node.Fun]; ok {
|
2019-05-10 10:26:15 -06:00
|
|
|
if sig, ok := tv.Type.(*types.Signature); ok {
|
|
|
|
if sig.Params().Len() == 0 {
|
2019-06-17 12:11:13 -06:00
|
|
|
return typeInference{}
|
2019-05-10 10:26:15 -06:00
|
|
|
}
|
2019-05-15 16:29:19 -06:00
|
|
|
i := indexExprAtPos(c.pos, node.Args)
|
2019-05-10 10:26:15 -06:00
|
|
|
// Make sure not to run past the end of expected parameters.
|
|
|
|
if i >= sig.Params().Len() {
|
|
|
|
i = sig.Params().Len() - 1
|
|
|
|
}
|
|
|
|
typ = sig.Params().At(i).Type()
|
|
|
|
break Nodes
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
2019-05-10 10:26:15 -06:00
|
|
|
}
|
|
|
|
}
|
2019-06-17 12:11:13 -06:00
|
|
|
return typeInference{}
|
2019-05-10 10:26:15 -06:00
|
|
|
case *ast.ReturnStmt:
|
|
|
|
if sig := c.enclosingFunction; sig != nil {
|
2019-05-15 16:29:19 -06:00
|
|
|
// Find signature result that corresponds to our return statement.
|
|
|
|
if resultIdx := indexExprAtPos(c.pos, node.Results); resultIdx < len(node.Results) {
|
2019-05-10 10:26:15 -06:00
|
|
|
if resultIdx < sig.Results().Len() {
|
|
|
|
typ = sig.Results().At(resultIdx).Type()
|
|
|
|
break Nodes
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-17 12:11:13 -06:00
|
|
|
return typeInference{}
|
2019-05-15 16:29:19 -06:00
|
|
|
case *ast.CaseClause:
|
|
|
|
if swtch, ok := findSwitchStmt(c.path[i+1:], c.pos, node).(*ast.SwitchStmt); ok {
|
|
|
|
if tv, ok := c.info.Types[swtch.Tag]; ok {
|
|
|
|
typ = tv.Type
|
|
|
|
break Nodes
|
|
|
|
}
|
|
|
|
}
|
2019-06-17 12:11:13 -06:00
|
|
|
return typeInference{}
|
2019-05-15 16:29:19 -06:00
|
|
|
case *ast.SliceExpr:
|
|
|
|
// Make sure position falls within the brackets (e.g. "foo[a:<>]").
|
|
|
|
if node.Lbrack < c.pos && c.pos <= node.Rbrack {
|
|
|
|
typ = types.Typ[types.Int]
|
|
|
|
break Nodes
|
|
|
|
}
|
2019-06-17 12:11:13 -06:00
|
|
|
return typeInference{}
|
2019-05-15 16:29:19 -06:00
|
|
|
case *ast.IndexExpr:
|
|
|
|
// Make sure position falls within the brackets (e.g. "foo[<>]").
|
|
|
|
if node.Lbrack < c.pos && c.pos <= node.Rbrack {
|
|
|
|
if tv, ok := c.info.Types[node.X]; ok {
|
|
|
|
switch t := tv.Type.Underlying().(type) {
|
|
|
|
case *types.Map:
|
|
|
|
typ = t.Key()
|
|
|
|
case *types.Slice, *types.Array:
|
|
|
|
typ = types.Typ[types.Int]
|
|
|
|
default:
|
2019-06-17 12:11:13 -06:00
|
|
|
return typeInference{}
|
2019-05-15 16:29:19 -06:00
|
|
|
}
|
|
|
|
break Nodes
|
|
|
|
}
|
|
|
|
}
|
2019-06-17 12:11:13 -06:00
|
|
|
return typeInference{}
|
2019-05-15 16:29:19 -06:00
|
|
|
case *ast.SendStmt:
|
|
|
|
// Make sure we are on right side of arrow (e.g. "foo <- <>").
|
|
|
|
if c.pos > node.Arrow+1 {
|
|
|
|
if tv, ok := c.info.Types[node.Chan]; ok {
|
|
|
|
if ch, ok := tv.Type.Underlying().(*types.Chan); ok {
|
|
|
|
typ = ch.Elem()
|
|
|
|
break Nodes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-17 12:11:13 -06:00
|
|
|
return typeInference{}
|
2019-05-10 10:26:15 -06:00
|
|
|
case *ast.StarExpr:
|
2019-06-26 16:30:01 -06:00
|
|
|
modifiers = append(modifiers, star)
|
2019-05-10 10:26:15 -06:00
|
|
|
case *ast.UnaryExpr:
|
2019-05-15 16:29:19 -06:00
|
|
|
switch node.Op {
|
|
|
|
case token.AND:
|
|
|
|
modifiers = append(modifiers, reference)
|
|
|
|
case token.ARROW:
|
|
|
|
modifiers = append(modifiers, chanRead)
|
2019-05-10 10:26:15 -06:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
if breaksExpectedTypeInference(node) {
|
2019-06-17 12:11:13 -06:00
|
|
|
return typeInference{}
|
2019-05-10 10:26:15 -06:00
|
|
|
}
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
}
|
2019-05-10 10:26:15 -06:00
|
|
|
|
2019-06-19 10:12:57 -06:00
|
|
|
return typeInference{
|
2019-06-27 16:37:50 -06:00
|
|
|
objType: typ,
|
|
|
|
modifiers: modifiers,
|
|
|
|
convertibleTo: convertibleTo,
|
2019-05-10 10:26:15 -06:00
|
|
|
}
|
2019-06-19 10:12:57 -06:00
|
|
|
}
|
2019-05-10 10:26:15 -06:00
|
|
|
|
2019-06-19 10:12:57 -06:00
|
|
|
// applyTypeModifiers applies the list of type modifiers to a type.
|
|
|
|
func (ti typeInference) applyTypeModifiers(typ types.Type) types.Type {
|
|
|
|
for _, mod := range ti.modifiers {
|
|
|
|
switch mod {
|
2019-06-26 16:30:01 -06:00
|
|
|
case star:
|
2019-06-19 10:12:57 -06:00
|
|
|
// For every "*" deref operator, remove a pointer layer from candidate type.
|
|
|
|
typ = deref(typ)
|
|
|
|
case reference:
|
|
|
|
// For every "&" ref operator, add another pointer layer to candidate type.
|
|
|
|
typ = types.NewPointer(typ)
|
|
|
|
case chanRead:
|
|
|
|
// For every "<-" operator, remove a layer of channelness.
|
|
|
|
if ch, ok := typ.(*types.Chan); ok {
|
|
|
|
typ = ch.Elem()
|
|
|
|
}
|
|
|
|
}
|
2019-06-17 12:11:13 -06:00
|
|
|
}
|
2019-06-19 10:12:57 -06:00
|
|
|
return typ
|
2019-05-10 10:26:15 -06:00
|
|
|
}
|
|
|
|
|
2019-06-26 16:30:01 -06:00
|
|
|
// applyTypeNameModifiers applies the list of type modifiers to a type name.
|
|
|
|
func (ti typeInference) applyTypeNameModifiers(typ types.Type) types.Type {
|
|
|
|
for _, mod := range ti.modifiers {
|
|
|
|
switch mod {
|
|
|
|
case star:
|
|
|
|
// For every "*" indicator, add a pointer layer to type name.
|
|
|
|
typ = types.NewPointer(typ)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
2019-05-15 16:29:19 -06:00
|
|
|
// findSwitchStmt returns an *ast.CaseClause's corresponding *ast.SwitchStmt or
|
|
|
|
// *ast.TypeSwitchStmt. path should start from the case clause's first ancestor.
|
|
|
|
func findSwitchStmt(path []ast.Node, pos token.Pos, c *ast.CaseClause) ast.Stmt {
|
|
|
|
// Make sure position falls within a "case <>:" clause.
|
|
|
|
if exprAtPos(pos, c.List) == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// A case clause is always nested within a block statement in a switch statement.
|
|
|
|
if len(path) < 2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if _, ok := path[0].(*ast.BlockStmt); !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch s := path[1].(type) {
|
|
|
|
case *ast.SwitchStmt:
|
|
|
|
return s
|
|
|
|
case *ast.TypeSwitchStmt:
|
|
|
|
return s
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 10:26:15 -06:00
|
|
|
// breaksExpectedTypeInference reports if an expression node's type is unrelated
|
|
|
|
// to its child expression node types. For example, "Foo{Bar: x.Baz(<>)}" should
|
|
|
|
// expect a function argument, not a composite literal value.
|
|
|
|
func breaksExpectedTypeInference(n ast.Node) bool {
|
|
|
|
switch n.(type) {
|
|
|
|
case *ast.FuncLit, *ast.CallExpr, *ast.TypeAssertExpr, *ast.IndexExpr, *ast.SliceExpr, *ast.CompositeLit:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
|
2019-06-17 12:11:13 -06:00
|
|
|
// expectTypeName returns information about the expected type name at position.
|
|
|
|
func expectTypeName(c *completer) typeInference {
|
2019-06-26 16:30:01 -06:00
|
|
|
var (
|
|
|
|
wantTypeName bool
|
|
|
|
modifiers []typeModifier
|
|
|
|
assertableFrom types.Type
|
|
|
|
)
|
2019-06-17 12:11:13 -06:00
|
|
|
|
|
|
|
Nodes:
|
|
|
|
for i, p := range c.path {
|
2019-04-24 17:26:34 -06:00
|
|
|
switch n := p.(type) {
|
|
|
|
case *ast.FuncDecl:
|
2019-06-17 12:11:13 -06:00
|
|
|
// Expect type names in a function declaration receiver, params and results.
|
|
|
|
|
|
|
|
if r := n.Recv; r != nil && r.Pos() <= c.pos && c.pos <= r.End() {
|
|
|
|
wantTypeName = true
|
|
|
|
break Nodes
|
2019-04-24 17:26:34 -06:00
|
|
|
}
|
|
|
|
if t := n.Type; t != nil {
|
2019-06-17 12:11:13 -06:00
|
|
|
if p := t.Params; p != nil && p.Pos() <= c.pos && c.pos <= p.End() {
|
|
|
|
wantTypeName = true
|
|
|
|
break Nodes
|
2019-04-24 17:26:34 -06:00
|
|
|
}
|
2019-06-17 12:11:13 -06:00
|
|
|
if r := t.Results; r != nil && r.Pos() <= c.pos && c.pos <= r.End() {
|
|
|
|
wantTypeName = true
|
|
|
|
break Nodes
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
}
|
2019-06-17 12:11:13 -06:00
|
|
|
return typeInference{}
|
2019-05-15 16:29:19 -06:00
|
|
|
case *ast.CaseClause:
|
2019-06-17 12:11:13 -06:00
|
|
|
// Expect type names in type switch case clauses.
|
2019-06-26 16:30:01 -06:00
|
|
|
if swtch, ok := findSwitchStmt(c.path[i+1:], c.pos, n).(*ast.TypeSwitchStmt); ok {
|
|
|
|
// The case clause types must be assertable from the type switch parameter.
|
|
|
|
ast.Inspect(swtch.Assign, func(n ast.Node) bool {
|
|
|
|
if ta, ok := n.(*ast.TypeAssertExpr); ok {
|
|
|
|
assertableFrom = c.info.TypeOf(ta.X)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2019-06-17 12:11:13 -06:00
|
|
|
wantTypeName = true
|
|
|
|
break Nodes
|
|
|
|
}
|
|
|
|
return typeInference{}
|
2019-05-15 16:29:19 -06:00
|
|
|
case *ast.TypeAssertExpr:
|
2019-06-17 12:11:13 -06:00
|
|
|
// Expect type names in type assert expressions.
|
|
|
|
if n.Lparen < c.pos && c.pos <= n.Rparen {
|
2019-06-26 16:30:01 -06:00
|
|
|
// The type in parens must be assertable from the expression type.
|
|
|
|
assertableFrom = c.info.TypeOf(n.X)
|
2019-06-17 12:11:13 -06:00
|
|
|
wantTypeName = true
|
|
|
|
break Nodes
|
|
|
|
}
|
|
|
|
return typeInference{}
|
2019-06-26 16:30:01 -06:00
|
|
|
case *ast.StarExpr:
|
|
|
|
modifiers = append(modifiers, star)
|
2019-06-17 12:11:13 -06:00
|
|
|
default:
|
|
|
|
if breaksExpectedTypeInference(p) {
|
|
|
|
return typeInference{}
|
2019-05-15 16:29:19 -06:00
|
|
|
}
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 12:11:13 -06:00
|
|
|
return typeInference{
|
2019-06-26 16:30:01 -06:00
|
|
|
wantTypeName: wantTypeName,
|
|
|
|
modifiers: modifiers,
|
|
|
|
assertableFrom: assertableFrom,
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
2019-06-17 12:11:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// matchingType reports whether an object is a good completion candidate
|
|
|
|
// in the context of the expected type.
|
2019-06-19 16:24:05 -06:00
|
|
|
func (c *completer) matchingType(cand *candidate) bool {
|
2019-06-26 16:30:01 -06:00
|
|
|
if isTypeName(cand.obj) {
|
|
|
|
return c.matchingTypeName(cand)
|
|
|
|
}
|
|
|
|
|
2019-06-19 16:24:05 -06:00
|
|
|
objType := cand.obj.Type()
|
|
|
|
|
|
|
|
// Default to invoking *types.Func candidates. This is so function
|
|
|
|
// completions in an empty statement (or other cases with no expected type)
|
|
|
|
// are invoked by default.
|
|
|
|
cand.expandFuncCall = isFunc(cand.obj)
|
|
|
|
|
2019-07-01 15:28:46 -06:00
|
|
|
typeMatches := func(candType types.Type) bool {
|
2019-06-19 16:24:05 -06:00
|
|
|
// Take into account any type modifiers on the expected type.
|
2019-07-01 15:28:46 -06:00
|
|
|
candType = c.expectedType.applyTypeModifiers(candType)
|
2019-06-19 16:24:05 -06:00
|
|
|
|
|
|
|
if c.expectedType.objType != nil {
|
2019-07-01 15:28:46 -06:00
|
|
|
wantType := types.Default(c.expectedType.objType)
|
|
|
|
|
|
|
|
// Handle untyped values specially since AssignableTo gives false negatives
|
|
|
|
// for them (see https://golang.org/issue/32146).
|
|
|
|
if candBasic, ok := candType.(*types.Basic); ok && candBasic.Info()&types.IsUntyped > 0 {
|
|
|
|
if wantBasic, ok := wantType.Underlying().(*types.Basic); ok {
|
|
|
|
// Check that their constant kind (bool|int|float|complex|string) matches.
|
|
|
|
// This doesn't take into account the constant value, so there will be some
|
|
|
|
// false positives due to integer sign and overflow.
|
|
|
|
return candBasic.Info()&types.IsConstType == wantBasic.Info()&types.IsConstType
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-06-19 16:24:05 -06:00
|
|
|
// AssignableTo covers the case where the types are equal, but also handles
|
|
|
|
// cases like assigning a concrete type to an interface type.
|
2019-07-01 15:28:46 -06:00
|
|
|
return types.AssignableTo(candType, wantType)
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
2019-06-19 16:24:05 -06:00
|
|
|
|
|
|
|
return false
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
2019-06-17 12:11:13 -06:00
|
|
|
|
2019-06-19 16:24:05 -06:00
|
|
|
if typeMatches(objType) {
|
|
|
|
// If obj's type matches, we don't want to expand to an invocation of obj.
|
|
|
|
cand.expandFuncCall = false
|
|
|
|
return true
|
|
|
|
}
|
2019-06-19 10:12:57 -06:00
|
|
|
|
2019-06-19 16:24:05 -06:00
|
|
|
// Try using a function's return type as its type.
|
|
|
|
if sig, ok := objType.Underlying().(*types.Signature); ok && sig.Results().Len() == 1 {
|
|
|
|
if typeMatches(sig.Results().At(0).Type()) {
|
|
|
|
// If obj's return value matches the expected type, we need to invoke obj
|
|
|
|
// in the completion.
|
|
|
|
cand.expandFuncCall = true
|
|
|
|
return true
|
|
|
|
}
|
2019-06-17 12:11:13 -06:00
|
|
|
}
|
|
|
|
|
2019-06-27 16:37:50 -06:00
|
|
|
if c.expectedType.convertibleTo != nil {
|
|
|
|
return types.ConvertibleTo(objType, c.expectedType.convertibleTo)
|
|
|
|
}
|
|
|
|
|
2019-06-17 12:11:13 -06:00
|
|
|
return false
|
2018-11-07 18:57:08 -07:00
|
|
|
}
|
2019-06-26 16:30:01 -06:00
|
|
|
|
|
|
|
func (c *completer) matchingTypeName(cand *candidate) bool {
|
|
|
|
if !c.wantTypeName() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take into account any type name modifier prefixes.
|
|
|
|
actual := c.expectedType.applyTypeNameModifiers(cand.obj.Type())
|
|
|
|
|
|
|
|
if c.expectedType.assertableFrom != nil {
|
|
|
|
// Don't suggest the starting type in type assertions. For example,
|
|
|
|
// if "foo" is an io.Writer, don't suggest "foo.(io.Writer)".
|
|
|
|
if types.Identical(c.expectedType.assertableFrom, actual) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if intf, ok := c.expectedType.assertableFrom.Underlying().(*types.Interface); ok {
|
|
|
|
if !types.AssertableTo(intf, actual) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default to saying any type name is a match.
|
|
|
|
return true
|
|
|
|
}
|