Skip to content

Commit 27097e8

Browse files
committed
gopls/internal/lsp/source/completion: skip index overload func by funcId
1 parent 0424bd2 commit 27097e8

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ type gopCompleter struct {
7171
// seen is the map that ensures we do not return duplicate results.
7272
seen map[types.Object]bool
7373

74+
// skipFunc is the map skip index overload func id
75+
skipFunc map[string]bool
76+
7477
// items is the list of completion items returned.
7578
items []CompletionItem
7679

@@ -383,6 +386,7 @@ func GopCompletion(ctx context.Context, snapshot source.Snapshot, fh source.File
383386
path: path,
384387
pos: pos,
385388
seen: make(map[types.Object]bool),
389+
skipFunc: make(map[string]bool),
386390
enclosingFunc: gopEnclosingFunction(path, pkg.GopTypesInfo()),
387391
enclosingCompositeLiteral: gopEnclosingCompositeLiteral(path, pos, pkg.GopTypesInfo()),
388392
deepState: deepCompletionState{

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ func (c *gopCompleter) deepSearch(ctx context.Context, start time.Time, deadline
8080
}
8181
}
8282

83+
// skip index overload func
84+
if fn, ok := obj.(*types.Func); ok && c.skipFunc[funcId(fn)] {
85+
continue
86+
}
87+
8388
c.addCandidate(ctx, &cand)
8489

8590
c.deepState.candidateCount++

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,38 @@ import (
2424
"golang.org/x/tools/internal/typeparams"
2525
)
2626

27+
func writeFuncName(buf *bytes.Buffer, f *types.Func) {
28+
if f.Type() != nil {
29+
sig := f.Type().(*types.Signature)
30+
if recv := sig.Recv(); recv != nil {
31+
buf.WriteByte('(')
32+
switch t := recv.Type().(type) {
33+
case *types.Interface:
34+
buf.WriteString("interface")
35+
case *types.Named:
36+
obj := t.Origin().Obj()
37+
if obj.Pkg() != nil {
38+
buf.WriteString(obj.Pkg().Path())
39+
buf.WriteByte('.')
40+
}
41+
buf.WriteString(obj.Name())
42+
}
43+
buf.WriteByte(')')
44+
buf.WriteByte('.')
45+
} else if f.Pkg() != nil {
46+
buf.WriteString(f.Pkg().Path())
47+
buf.WriteByte('.')
48+
}
49+
}
50+
buf.WriteString(f.Name())
51+
}
52+
53+
func funcId(fn *types.Func) string {
54+
var buf bytes.Buffer
55+
writeFuncName(&buf, fn)
56+
return buf.String()
57+
}
58+
2759
// item formats a candidate to a CompletionItem.
2860
func (c *gopCompleter) item(ctx context.Context, cand candidate) (CompletionItem, error) {
2961
obj := cand.obj
@@ -144,7 +176,7 @@ Suffixes:
144176
buf.WriteString("Go+ overload funcs\n")
145177
for _, o := range objs {
146178
if isIndexOverload(o.Name(), obj.Name()) {
147-
c.seen[o] = true
179+
c.skipFunc[funcId(o.(*types.Func))] = true
148180
}
149181
s, err := source.NewSignature(ctx, c.snapshot, c.pkg, o.Type().(*types.Signature), nil, c.qf, c.mq)
150182
if err != nil {

0 commit comments

Comments
 (0)