From 7bb39e4ca9ac137795f5e78c1ce8b6a6f97a823f Mon Sep 17 00:00:00 2001 From: Muir Manders Date: Fri, 4 Dec 2020 20:50:10 -0800 Subject: [PATCH] internal/lsp/source: fix default param name generation When generating a default param name based on a type name, we want to ignore any package qualifier. For example, if the type is "ast.Node" we want to generate the variable name "n", so we must ignore the "ast." qualifier. To do this we use a types.Qualifier that always returns empty, but qualifyExpr wasn't respecting an empty qualifier because it is doing manual ast manipulation. However, it seems like things "just work" if you set a SelectorExpr's "X" to empty string (i.e. only "Sel" is output when formatting). Fixes golang/go#42794. Change-Id: Ied86b9511e4a9550590417c5a506fe35d561d4f9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/275519 Run-TryBot: Muir Manders gopls-CI: kokoro TryBot-Result: Go Bot Reviewed-by: Rebecca Stambler Trust: Rebecca Stambler Trust: Robert Findley --- internal/lsp/source/types_format.go | 5 +---- internal/lsp/testdata/snippets/literal_snippets.go.in | 3 +++ internal/lsp/testdata/summary.txt.golden | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/lsp/source/types_format.go b/internal/lsp/source/types_format.go index 5ddd8e078f..a7bd3c4c30 100644 --- a/internal/lsp/source/types_format.go +++ b/internal/lsp/source/types_format.go @@ -282,10 +282,7 @@ func qualifyExpr(expr ast.Expr, srcpkg, pkg Package, clonedInfo map[token.Pos]*t if !ok { return false } - pkgName := qf(obj.Imported()) - if pkgName != "" { - x.Name = pkgName - } + x.Name = qf(obj.Imported()) return false case *ast.Ident: if srcpkg == pkg { diff --git a/internal/lsp/testdata/snippets/literal_snippets.go.in b/internal/lsp/testdata/snippets/literal_snippets.go.in index 4a505e38f8..8deab2abd9 100644 --- a/internal/lsp/testdata/snippets/literal_snippets.go.in +++ b/internal/lsp/testdata/snippets/literal_snippets.go.in @@ -2,6 +2,7 @@ package snippets import ( "bytes" + "go/ast" "net/http" "sort" @@ -159,6 +160,8 @@ func _() { const two = 2 var builtinTypes func([]int, [two]bool, map[string]string, struct{ i int }, interface{ foo() }, <-chan int) builtinTypes = f //@snippet(" //", litFunc, "func(i1 []int, b [two]bool, m map[string]string, s struct{ i int \\}, i2 interface{ foo() \\}, c <-chan int) {$0\\}", "func(${1:i1} []int, ${2:b} [two]bool, ${3:m} map[string]string, ${4:s} struct{ i int \\}, ${5:i2} interface{ foo() \\}, ${6:c} <-chan int) {$0\\}") + + var _ func(ast.Node) = f //@snippet(" //", litFunc, "func(n ast.Node) {$0\\}", "func(${1:n} ast.Node) {$0\\}") } func _() { diff --git a/internal/lsp/testdata/summary.txt.golden b/internal/lsp/testdata/summary.txt.golden index f9af913617..8d095cbb51 100644 --- a/internal/lsp/testdata/summary.txt.golden +++ b/internal/lsp/testdata/summary.txt.golden @@ -2,7 +2,7 @@ CallHierarchyCount = 1 CodeLensCount = 5 CompletionsCount = 255 -CompletionSnippetCount = 87 +CompletionSnippetCount = 88 UnimportedCompletionsCount = 5 DeepCompletionsCount = 5 FuzzyCompletionsCount = 8