diff --git a/internal/lsp/source/completion.go b/internal/lsp/source/completion.go index f1f50dd936..a2242aa552 100644 --- a/internal/lsp/source/completion.go +++ b/internal/lsp/source/completion.go @@ -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 diff --git a/internal/lsp/source/completion_builtin.go b/internal/lsp/source/completion_builtin.go index da2c5f8ff8..f6046c55be 100644 --- a/internal/lsp/source/completion_builtin.go +++ b/internal/lsp/source/completion_builtin.go @@ -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 } diff --git a/internal/lsp/testdata/builtins/builtin_args.go b/internal/lsp/testdata/builtins/builtin_args.go index 040d7e396a..8b3d10a46a 100644 --- a/internal/lsp/testdata/builtins/builtin_args.go +++ b/internal/lsp/testdata/builtins/builtin_args.go @@ -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) diff --git a/internal/lsp/testdata/summary.txt.golden b/internal/lsp/testdata/summary.txt.golden index bbb39094cd..e85b9f839b 100644 --- a/internal/lsp/testdata/summary.txt.golden +++ b/internal/lsp/testdata/summary.txt.golden @@ -4,7 +4,7 @@ CompletionSnippetCount = 66 UnimportedCompletionsCount = 9 DeepCompletionsCount = 5 FuzzyCompletionsCount = 8 -RankedCompletionsCount = 66 +RankedCompletionsCount = 68 CaseSensitiveCompletionsCount = 4 DiagnosticsCount = 37 FoldingRangesCount = 2