Skip to content

Commit 7f23bc8

Browse files
committed
gopls/internal/regtest/source/completion: reuse functionCallSnippet in
unimported completion The unimported completion logic was using ad-hoc snippet logic that didn't correctly handle "usePlaceholders", and wasn't updated for the recently added "completeFunctionCalls" setting. Refactor to re-use the functionCallSnippet helper, and add a test. Fixes golang/go#62676 Change-Id: I08277cfc556455fbe17900ad56821e269990ead6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/531458 Commit-Queue: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Alan Donovan <adonovan@google.com>
1 parent 3d03fbd commit 7f23bc8

File tree

2 files changed

+75
-25
lines changed

2 files changed

+75
-25
lines changed

gopls/internal/lsp/source/completion/completion.go

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,32 +1320,18 @@ func (c *completer) selector(ctx context.Context, sel *ast.SelectorExpr) error {
13201320

13211321
// For functions, add a parameter snippet.
13221322
if fn != nil {
1323-
var sn snippet.Builder
1324-
sn.WriteText(id.Name)
1325-
1326-
paramList := func(open, close string, list *ast.FieldList) {
1323+
paramList := func(list *ast.FieldList) []string {
1324+
var params []string
13271325
if list != nil {
13281326
var cfg printer.Config // slight overkill
1329-
var nparams int
13301327
param := func(name string, typ ast.Expr) {
1331-
if nparams > 0 {
1332-
sn.WriteText(", ")
1333-
}
1334-
nparams++
1335-
if c.opts.placeholders {
1336-
sn.WritePlaceholder(func(b *snippet.Builder) {
1337-
var buf strings.Builder
1338-
buf.WriteString(name)
1339-
buf.WriteByte(' ')
1340-
cfg.Fprint(&buf, token.NewFileSet(), typ)
1341-
b.WriteText(buf.String())
1342-
})
1343-
} else {
1344-
sn.WriteText(name)
1345-
}
1328+
var buf strings.Builder
1329+
buf.WriteString(name)
1330+
buf.WriteByte(' ')
1331+
cfg.Fprint(&buf, token.NewFileSet(), typ)
1332+
params = append(params, buf.String())
13461333
}
13471334

1348-
sn.WriteText(open)
13491335
for _, field := range list.List {
13501336
if field.Names != nil {
13511337
for _, name := range field.Names {
@@ -1355,13 +1341,14 @@ func (c *completer) selector(ctx context.Context, sel *ast.SelectorExpr) error {
13551341
param("_", field.Type)
13561342
}
13571343
}
1358-
sn.WriteText(close)
13591344
}
1345+
return params
13601346
}
13611347

1362-
paramList("[", "]", typeparams.ForFuncType(fn.Type))
1363-
paramList("(", ")", fn.Type.Params)
1364-
1348+
tparams := paramList(fn.Type.TypeParams)
1349+
params := paramList(fn.Type.Params)
1350+
var sn snippet.Builder
1351+
c.functionCallSnippet(id.Name, tparams, params, &sn)
13651352
item.snippet = &sn
13661353
}
13671354

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
This test verifies that unimported completion respects the usePlaceholders setting.
2+
3+
-- flags --
4+
-ignore_extra_diags
5+
6+
-- settings.json --
7+
{
8+
"usePlaceholders": false
9+
}
10+
11+
-- go.mod --
12+
module mod.test
13+
14+
go 1.21
15+
16+
-- foo/foo.go --
17+
package foo
18+
19+
func _() {
20+
// This uses goimports-based completion; TODO: this should insert snippets.
21+
os.Open //@acceptcompletion(re"Open()", "Open", open)
22+
}
23+
24+
func _() {
25+
// This uses metadata-based completion.
26+
errors.New //@acceptcompletion(re"New()", "New", new)
27+
}
28+
29+
-- bar/bar.go --
30+
package bar
31+
32+
import _ "errors" // important: doesn't transitively import os.
33+
34+
-- @new/foo/foo.go --
35+
package foo
36+
37+
import "errors"
38+
39+
func _() {
40+
// This uses goimports-based completion; TODO: this should insert snippets.
41+
os.Open //@acceptcompletion(re"Open()", "Open", open)
42+
}
43+
44+
func _() {
45+
// This uses metadata-based completion.
46+
errors.New(${1:}) //@acceptcompletion(re"New()", "New", new)
47+
}
48+
49+
-- @open/foo/foo.go --
50+
package foo
51+
52+
import "os"
53+
54+
func _() {
55+
// This uses goimports-based completion; TODO: this should insert snippets.
56+
os.Open //@acceptcompletion(re"Open()", "Open", open)
57+
}
58+
59+
func _() {
60+
// This uses metadata-based completion.
61+
errors.New //@acceptcompletion(re"New()", "New", new)
62+
}
63+

0 commit comments

Comments
 (0)