Skip to content

Commit

Permalink
internal/lsp/source: use the real type parameter in snippet completions
Browse files Browse the repository at this point in the history
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 <[email protected]>
Run-TryBot: Peter Weinberger <[email protected]>
gopls-CI: kokoro <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Peter Weinberger <[email protected]>
  • Loading branch information
marwan-at-work authored and pjweinbgo committed Jan 21, 2022
1 parent 80963bc commit 9f83dd3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
9 changes: 9 additions & 0 deletions internal/lsp/source/types_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions internal/lsp/testdata/snippets/literal_snippets118.go.in
Original file line number Diff line number Diff line change
@@ -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\\}")
}
4 changes: 2 additions & 2 deletions internal/lsp/testdata/summary_go1.18.txt.golden
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-- summary --
CallHierarchyCount = 2
CodeLensCount = 5
CompletionsCount = 265
CompletionSnippetCount = 103
CompletionsCount = 266
CompletionSnippetCount = 104
UnimportedCompletionsCount = 5
DeepCompletionsCount = 5
FuzzyCompletionsCount = 8
Expand Down

0 comments on commit 9f83dd3

Please sign in to comment.