1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:14:39 -07:00

internal/lsp/source: prefer funcs when completing go/defer

Now we prefer functions when completing "go" and "defer" statements.
Previously we had no preference for the type of object. Further, we
will now also properly invoke functions.

    var f1 int
    var f2 func()
    go f<> // prefers "f2" and expands to "f2()"

Change-Id: I213551b74ba453c337ac89e825b5d495659e9d65
Reviewed-on: https://go-review.googlesource.com/c/tools/+/246359
Run-TryBot: Muir Manders <muir@mnd.rs>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Muir Manders 2020-08-01 14:18:10 -07:00 committed by Rebecca Stambler
parent 23e6869392
commit 990129eca5
5 changed files with 19 additions and 7 deletions

View File

@ -1511,6 +1511,7 @@ const (
kindMap
kindStruct
kindString
kindFunc
)
// candidateInference holds information we have inferred about a type that can be
@ -1639,7 +1640,7 @@ Nodes:
return inf
case *ast.CallExpr:
// Only consider CallExpr args if position falls between parens.
if node.Lparen <= c.pos && c.pos <= node.Rparen {
if node.Lparen < c.pos && c.pos <= node.Rparen {
// For type conversions like "int64(foo)" we can only infer our
// desired type is convertible to int64.
if typ := typeConversion(node, c.pkg.GetTypesInfo()); typ != nil {
@ -1709,8 +1710,9 @@ Nodes:
continue Nodes
}
}
return inf
}
return inf
case *ast.ReturnStmt:
if c.enclosingFunc != nil {
sig := c.enclosingFunc.sig
@ -1775,6 +1777,9 @@ Nodes:
case token.ARROW:
inf.modifiers = append(inf.modifiers, typeModifier{mod: chanRead})
}
case *ast.DeferStmt, *ast.GoStmt:
inf.objKind |= kindFunc
return inf
default:
if breaksExpectedTypeInference(node) {
return inf
@ -2137,7 +2142,12 @@ func (ci *candidateInference) candTypeMatches(cand *candidate) bool {
// If we have no expected type, fall back to checking the
// expected "kind" of object, if available.
return ci.kindMatches(candType)
if ci.kindMatches(candType) {
if ci.objKind == kindFunc {
cand.expandFuncCall = true
}
return true
}
}
for _, expType := range expTypes {
@ -2326,6 +2336,8 @@ func candKind(candType types.Type) objKind {
if t.Info()&types.IsString > 0 {
return kindString
}
case *types.Signature:
return kindFunc
}
return 0

View File

@ -5,5 +5,5 @@ import (
)
func _() {
go foo. //@rank(" //", Foo)
go foo. //@rank(" //", Foo, IntFoo),snippet(" //", Foo, "Foo()", "Foo()")
}

View File

@ -6,6 +6,6 @@ import (
func _() {
go func() {
defer foo. //@rank(" //", Foo)
defer foo. //@rank(" //", Foo, IntFoo)
}
}

View File

@ -27,7 +27,7 @@ func _() {
// no expected type
fnInt(func() int { s.A }) //@complete(" }", rankAA, rankAB, rankAC)
fnInt(s.A()) //@complete("()", rankAA, rankAB, rankAC)
fnInt(s.A()) //@complete("()", rankAA, rankAC, rankAB)
fnInt([]int{}[s.A]) //@complete("])", rankAA, rankAC, rankAB)
fnInt([]int{}[:s.A]) //@complete("])", rankAA, rankAC, rankAB)

View File

@ -1,7 +1,7 @@
-- summary --
CodeLensCount = 4
CompletionsCount = 241
CompletionSnippetCount = 80
CompletionSnippetCount = 81
UnimportedCompletionsCount = 6
DeepCompletionsCount = 5
FuzzyCompletionsCount = 8