1
0
mirror of https://github.com/golang/go synced 2024-11-19 03:24:40 -07:00
go/internal/lsp/testdata/func_rank/func_rank.go.in
Muir Manders 49797d04a8 internal/lsp/source: improve completion in type assertions
In cases like:

var foo *someType = bar.(some<>)

We will now complete "some" to "*someType". This involved two changes:

1. Properly detect expected type as *someType in above example. To do
   this I just removed *ast.TypeAssertExpr from
   breaksExpectedTypeInference() so we continue searching up the AST for
   the expected type.

2. If the given type name T doesn't match, also try *T. If *T does
   match, we mark the candidate as "makePointer=true" so we know to
   prepend the "*" when formatting the candidate.

Change-Id: I05859c68082a798141755b614673a1483d864e3e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/212717
Run-TryBot: Muir Manders <muir@mnd.rs>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-01-16 01:06:23 +00:00

45 lines
1.6 KiB
Go

package func_rank
var stringAVar = "var" //@item(stringAVar, "stringAVar", "string", "var")
func stringBFunc() string { return "str" } //@item(stringBFunc, "stringBFunc", "func() string", "func")
type stringer struct{} //@item(stringer, "stringer", "struct{...}", "struct")
func _() stringer //@complete("tr", stringer, stringAVar, stringBFunc)
func _(val stringer) {} //@complete("tr", stringer, stringAVar, stringBFunc)
func (stringer) _() {} //@complete("tr", stringer, stringAVar, stringBFunc)
func _() {
var s struct {
AA int //@item(rankAA, "AA", "int", "field")
AB string //@item(rankAB, "AB", "string", "field")
AC int //@item(rankAC, "AC", "int", "field")
}
fnStr := func(string) {}
fnStr(s.A) //@complete(")", rankAB, rankAA, rankAC)
fnStr("" + s.A) //@complete(")", rankAB, rankAA, rankAC)
fnInt := func(int) {}
fnInt(-s.A) //@complete(")", rankAA, rankAC, rankAB)
// no expected type
fnInt(func() int { s.A }) //@complete(" }", rankAA, rankAB, rankAC)
fnInt(s.A()) //@complete("()", rankAA, rankAB, rankAC)
fnInt([]int{}[s.A]) //@complete("])", rankAA, rankAC, rankAB)
fnInt([]int{}[:s.A]) //@complete("])", rankAA, rankAC, rankAB)
fnInt(s.A.(int)) //@complete(".(", rankAA, rankAC, rankAB)
fnPtr := func(*string) {}
fnPtr(&s.A) //@complete(")", rankAB, rankAA, rankAC)
var aaPtr *string //@item(rankAAPtr, "aaPtr", "*string", "var")
var abPtr *int //@item(rankABPtr, "abPtr", "*int", "var")
fnInt(*a) //@complete(")", rankABPtr, rankAAPtr)
_ = func() string {
return s.A //@complete(" //", rankAB, rankAA, rankAC)
}
}