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 <rfindley@google.com>
Run-TryBot: Peter Weinberger <pjw@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Peter Weinberger <pjw@google.com>
This commit is contained in:
Marwan Sulaiman 2022-01-15 02:12:46 -05:00 committed by Peter Weinberger
parent 80963bc8f5
commit 9f83dd3691
3 changed files with 25 additions and 2 deletions

View File

@ -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.

View File

@ -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\\}")
}

View File

@ -1,8 +1,8 @@
-- summary --
CallHierarchyCount = 2
CodeLensCount = 5
CompletionsCount = 265
CompletionSnippetCount = 103
CompletionsCount = 266
CompletionSnippetCount = 104
UnimportedCompletionsCount = 5
DeepCompletionsCount = 5
FuzzyCompletionsCount = 8