From 9f83dd3691c2ae718b327780b8602dd0ce3fb597 Mon Sep 17 00:00:00 2001 From: Marwan Sulaiman Date: Sat, 15 Jan 2022 02:12:46 -0500 Subject: [PATCH] internal/lsp/source: use the real type parameter in snippet completions This change makes source.FormatVarType return the instantiated type parameter instead of the actual AST expression because that will just refer to the abstract type parameter and not the insantiated one. I am not sure if this covers all edge cases or if there's a better solution but I'm happy to adjust to whatever is appropriate. Fixes golang/go#50623 Change-Id: I6e0b2d93c47c9a0502f2995ca8d3668a9a31c056 Reviewed-on: https://go-review.googlesource.com/c/tools/+/378714 Trust: Robert Findley Run-TryBot: Peter Weinberger gopls-CI: kokoro TryBot-Result: Gopher Robot Reviewed-by: Peter Weinberger --- internal/lsp/source/types_format.go | 9 +++++++++ .../testdata/snippets/literal_snippets118.go.in | 14 ++++++++++++++ internal/lsp/testdata/summary_go1.18.txt.golden | 4 ++-- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 internal/lsp/testdata/snippets/literal_snippets118.go.in diff --git a/internal/lsp/source/types_format.go b/internal/lsp/source/types_format.go index c3f17b0f304..734764851ad 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 00000000000..8251a6384a3 --- /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 61ca3b14189..a7afe2df664 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