internal/lsp/source: improve completion for "make()" args

Fix type inference to expect a type name for the first "make()"
parameter and an integer for later parameters. For example:

   // Previously we expected "[]int{}", now we expect "[]int".
   var _ []int = make(<>)

Note that we don't currently support actually completing to unnamed
type names like "[]int", but this improvement at least eliminates
nonsensical completion suggestions.

   // Previously we had no expectation, now we expect an int.
   var _ []int = make([]int, <>)

Change-Id: Ifd349767662ab6902d3a3ea9e52de7df70cb37c7
Reviewed-on: https://go-review.googlesource.com/c/tools/+/217310
Run-TryBot: Muir Manders <muir@mnd.rs>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ingo Oeser <nightlyone@googlemail.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Muir Manders 2020-02-01 21:45:01 -08:00 committed by Rebecca Stambler
parent ecb101ed65
commit bc0b458b10
4 changed files with 17 additions and 12 deletions

View File

@ -1375,8 +1375,6 @@ Nodes:
} else {
inf.objType = sig.Params().At(exprIdx).Type()
}
break Nodes
}
}
@ -1386,14 +1384,10 @@ Nodes:
if obj != nil && obj.Parent() == types.Universe {
inf.objKind |= c.builtinArgKind(obj, node)
if obj.Name() == "new" {
inf.typeName.wantTypeName = true
}
// Defer call to builtinArgType so we can provide it the
// inferred type from its parent node.
defer func() {
inf.objType, inf.variadic = c.builtinArgType(obj, node, inf.objType)
inf.objType, inf.typeName.wantTypeName, inf.variadic = c.builtinArgType(obj, node, inf.objType)
}()
// The expected type of builtin arguments like append() is

View File

@ -51,7 +51,7 @@ func (c *completer) builtinArgKind(obj types.Object, call *ast.CallExpr) objKind
// builtinArgType infers the type of an argument to a builtin
// function. "parentType" is the inferred type for the builtin call's
// parent node.
func (c *completer) builtinArgType(obj types.Object, call *ast.CallExpr, parentType types.Type) (infType types.Type, variadic bool) {
func (c *completer) builtinArgType(obj types.Object, call *ast.CallExpr, parentType types.Type) (infType types.Type, wantType, variadic bool) {
exprIdx := exprAtPos(c.pos, call.Args)
switch obj.Name() {
@ -93,6 +93,7 @@ func (c *completer) builtinArgType(obj types.Object, call *ast.CallExpr, parentT
infType = t2
}
case "new":
wantType = true
if parentType != nil {
// Expected type for "new" is the de-pointered parent type.
if ptr, ok := parentType.Underlying().(*types.Pointer); ok {
@ -101,9 +102,12 @@ func (c *completer) builtinArgType(obj types.Object, call *ast.CallExpr, parentT
}
case "make":
if exprIdx == 0 {
wantType = true
infType = parentType
} else {
infType = types.Typ[types.Int]
}
}
return infType, variadic
return infType, wantType, variadic
}

View File

@ -12,6 +12,12 @@ func _() {
aInt int //@item(builtinInt, "aInt", "int", "var")
)
type (
aSliceType []int //@item(builtinSliceType, "aSliceType", "[]int", "type")
aChanType chan int //@item(builtinChanType, "aChanType", "chan int", "type")
aMapType map[string]int //@item(builtinMapType, "aMapType", "map[string]int", "type")
)
close() //@rank(")", builtinChan, builtinSlice)
append() //@rank(")", builtinSlice, builtinChan)
@ -27,9 +33,10 @@ func _() {
cap() //@rank(")", builtinSlice, builtinMap),rank(")", builtinArray, builtinString),rank(")", builtinArrayPtr, builtinPtr),rank(")", builtinChan, builtinInt)
make() //@rank(")", builtinMap, builtinInt),rank(")", builtinChan, builtinInt),rank(")", builtinSlice, builtinInt)
make() //@rank(")", builtinMapType, int),rank(")", builtinChanType, int),rank(")", builtinSliceType, int),rank(")", builtinMapType, builtinMap)
make(aSliceType, a) //@rank(")", builtinInt, builtinSlice)
var _ []int = make() //@rank(")", builtinSlice, builtinMap)
var _ []int = make() //@rank(")", builtinSliceType, builtinSlice)
type myStruct struct{} //@item(builtinStructType, "myStruct", "struct{...}", "struct")
new() //@rank(")", builtinStructType, builtinInt)

View File

@ -4,7 +4,7 @@ CompletionSnippetCount = 66
UnimportedCompletionsCount = 9
DeepCompletionsCount = 5
FuzzyCompletionsCount = 8
RankedCompletionsCount = 66
RankedCompletionsCount = 68
CaseSensitiveCompletionsCount = 4
DiagnosticsCount = 37
FoldingRangesCount = 2