Skip to content

Commit

Permalink
internal/lsp/source: fix default param name generation
Browse files Browse the repository at this point in the history
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 <muir@mnd.rs>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Trust: Rebecca Stambler <rstambler@golang.org>
Trust: Robert Findley <rfindley@google.com>
  • Loading branch information
muirdm authored and marwan-at-work committed Dec 18, 2020
1 parent b472908 commit 4ea36ca
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
5 changes: 1 addition & 4 deletions internal/lsp/source/types_format.go
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions internal/lsp/testdata/snippets/literal_snippets.go.in
Expand Up @@ -2,6 +2,7 @@ package snippets

import (
"bytes"
"go/ast"
"net/http"
"sort"

Expand Down Expand Up @@ -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 _() {
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/testdata/summary.txt.golden
Expand Up @@ -2,7 +2,7 @@
CallHierarchyCount = 1
CodeLensCount = 5
CompletionsCount = 255
CompletionSnippetCount = 87
CompletionSnippetCount = 88
UnimportedCompletionsCount = 5
DeepCompletionsCount = 5
FuzzyCompletionsCount = 8
Expand Down

0 comments on commit 4ea36ca

Please sign in to comment.