diff --git a/internal/lsp/source/types_format.go b/internal/lsp/source/types_format.go index c3f17b0f30..734764851a 100644 --- a/internal/lsp/source/types_format.go +++ b/internal/lsp/source/types_format.go @@ -18,6 +18,7 @@ import ( "golang.org/x/tools/internal/event" "golang.org/x/tools/internal/lsp/debug/tag" "golang.org/x/tools/internal/lsp/protocol" + "golang.org/x/tools/internal/typeparams" ) // FormatType returns the detail and kind for a types.Type. @@ -229,6 +230,14 @@ func FormatVarType(ctx context.Context, snapshot Snapshot, srcpkg Package, obj * return types.TypeString(obj.Type(), qf) } + // If the given expr refers to a type parameter, then use the + // object's Type instead of the type parameter declaration. This helps + // format the instantiated type as opposed to the original undeclared + // generic type. + if typeparams.IsTypeParam(pkg.GetTypesInfo().Types[expr].Type) { + return types.TypeString(obj.Type(), qf) + } + // The type names in the AST may not be correctly qualified. // Determine the package name to use based on the package that originated // the query and the package in which the type is declared. diff --git a/internal/lsp/testdata/snippets/literal_snippets118.go.in b/internal/lsp/testdata/snippets/literal_snippets118.go.in new file mode 100644 index 0000000000..8251a6384a --- /dev/null +++ b/internal/lsp/testdata/snippets/literal_snippets118.go.in @@ -0,0 +1,14 @@ +// +build go1.18 +//go:build go1.18 + +package snippets + +type Tree[T any] struct{} + +func (tree Tree[T]) Do(f func(s T)) {} + +func _() { + _ = "func(...) {}" //@item(litFunc, "func(...) {}", "", "var") + var t Tree[string] + t.Do(fun) //@complete(")", litFunc),snippet(")", litFunc, "func(s string) {$0\\}", "func(s string) {$0\\}") +} diff --git a/internal/lsp/testdata/summary_go1.18.txt.golden b/internal/lsp/testdata/summary_go1.18.txt.golden index 61ca3b1418..a7afe2df66 100644 --- a/internal/lsp/testdata/summary_go1.18.txt.golden +++ b/internal/lsp/testdata/summary_go1.18.txt.golden @@ -1,8 +1,8 @@ -- summary -- CallHierarchyCount = 2 CodeLensCount = 5 -CompletionsCount = 265 -CompletionSnippetCount = 103 +CompletionsCount = 266 +CompletionSnippetCount = 104 UnimportedCompletionsCount = 5 DeepCompletionsCount = 5 FuzzyCompletionsCount = 8