mirror of
https://github.com/golang/go
synced 2024-11-19 02:54:42 -07:00
4298585011
Deep completion refers to searching through an object's fields and methods for more completion candidates. For example: func wantsInt(int) { } var s struct { i int } wantsInt(<>) Will now give a candidate for "s.i" since its type matches the expected type. We limit to three deep completion results. In some cases there are many useless deep completion matches. Showing too many options defeats the purpose of "smart" completions. We also lower a completion item's score according to its depth so that we favor shallower options. For now we do not continue searching past function calls to limit our search scope. In other words, we are not able to suggest results with any chained fields/methods after the first method call. Deep completions are behind the "useDeepCompletions" LSP config flag for now. Change-Id: I1b888c82e5c4b882f9718177ce07811e2bccbf22 GitHub-Last-Rev: 26522363730036e0b382a7bcd10aa1ed825f6866 GitHub-Pull-Request: golang/tools#100 Reviewed-on: https://go-review.googlesource.com/c/tools/+/177622 Reviewed-by: Rebecca Stambler <rstambler@golang.org> Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
85 lines
2.3 KiB
Go
85 lines
2.3 KiB
Go
// Copyright 2019 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.
|
|
|
|
package source
|
|
|
|
import (
|
|
"go/types"
|
|
"strings"
|
|
)
|
|
|
|
// deepCompletionState stores our state as we search for deep completions.
|
|
// "deep completion" refers to searching into objects' fields and methods to
|
|
// find more completion candidates.
|
|
type deepCompletionState struct {
|
|
// enabled is true if deep completions are enabled.
|
|
enabled bool
|
|
|
|
// chain holds the traversal path as we do a depth-first search through
|
|
// objects' members looking for exact type matches.
|
|
chain []types.Object
|
|
|
|
// chainNames holds the names of the chain objects. This allows us to
|
|
// save allocations as we build many deep completion items.
|
|
chainNames []string
|
|
}
|
|
|
|
// push pushes obj onto our search stack.
|
|
func (s *deepCompletionState) push(obj types.Object) {
|
|
s.chain = append(s.chain, obj)
|
|
s.chainNames = append(s.chainNames, obj.Name())
|
|
}
|
|
|
|
// pop pops the last object off our search stack.
|
|
func (s *deepCompletionState) pop() {
|
|
s.chain = s.chain[:len(s.chain)-1]
|
|
s.chainNames = s.chainNames[:len(s.chainNames)-1]
|
|
}
|
|
|
|
// chainString joins the chain of objects' names together on ".".
|
|
func (s *deepCompletionState) chainString(finalName string) string {
|
|
s.chainNames = append(s.chainNames, finalName)
|
|
chainStr := strings.Join(s.chainNames, ".")
|
|
s.chainNames = s.chainNames[:len(s.chainNames)-1]
|
|
return chainStr
|
|
}
|
|
|
|
func (c *completer) inDeepCompletion() bool {
|
|
return len(c.deepState.chain) > 0
|
|
}
|
|
|
|
// deepSearch searches through obj's subordinate objects for more
|
|
// completion items.
|
|
func (c *completer) deepSearch(obj types.Object) {
|
|
if !c.deepState.enabled {
|
|
return
|
|
}
|
|
|
|
// Don't search into type names.
|
|
if isTypeName(obj) {
|
|
return
|
|
}
|
|
|
|
// Don't search embedded fields because they were already included in their
|
|
// parent's fields.
|
|
if v, ok := obj.(*types.Var); ok && v.Embedded() {
|
|
return
|
|
}
|
|
|
|
// Push this object onto our search stack.
|
|
c.deepState.push(obj)
|
|
|
|
switch obj := obj.(type) {
|
|
case *types.PkgName:
|
|
c.packageMembers(obj)
|
|
default:
|
|
// For now it is okay to assume obj is addressable since we don't search beyond
|
|
// function calls.
|
|
c.methodsAndFields(obj.Type(), true)
|
|
}
|
|
|
|
// Pop the object off our search stack.
|
|
c.deepState.pop()
|
|
}
|