Skip to content

Commit 756ed79

Browse files
committed
gopls/internal/golang/completion: report types in unimported completion
pkgIDmatches handled only the Function, Variable, Struct and Constant symbol kinds, so an interface or a named non-struct type from a workspace package was dropped, and a struct type was reported as "var". Report every type as "type (from ...)", as the other two search paths already do. All three paths gave a type VariableCompletion, so editors drew a variable icon beside an item whose detail read "type". Use InterfaceCompletion for an interface and ClassCompletion otherwise. The standard library and module cache indexes do not record whether a type is an interface, so those two paths always use ClassCompletion. Fixes golang/go#81369
1 parent 9e18529 commit 756ed79

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

gopls/internal/golang/completion/unimported.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,15 @@ func (c *completer) pkgIDmatches(ctx context.Context, ids []metadata.PackageID,
233233
}
234234
kind = protocol.FunctionCompletion
235235
detail = fmt.Sprintf("func (from %q)", pkg.PkgPath)
236-
case protocol.Variable, protocol.Struct:
236+
case protocol.Variable:
237237
kind = protocol.VariableCompletion
238238
detail = fmt.Sprintf("var (from %q)", pkg.PkgPath)
239+
case protocol.Interface:
240+
kind = protocol.InterfaceCompletion
241+
detail = fmt.Sprintf("type (from %q)", pkg.PkgPath)
242+
case protocol.Struct, protocol.Class:
243+
kind = protocol.ClassCompletion
244+
detail = fmt.Sprintf("type (from %q)", pkg.PkgPath)
239245
case protocol.Constant:
240246
kind = protocol.ConstantCompletion
241247
detail = fmt.Sprintf("const (from %q)", pkg.PkgPath)
@@ -284,7 +290,7 @@ func (c *completer) stdlibMatches(pkgs []metadata.PackagePath, pkg metadata.Pack
284290
kind = protocol.VariableCompletion
285291
detail = fmt.Sprintf("var (from %q)", candpkg)
286292
case stdlib.Type:
287-
kind = protocol.VariableCompletion
293+
kind = protocol.ClassCompletion
288294
detail = fmt.Sprintf("type (from %q)", candpkg)
289295
default:
290296
continue
@@ -331,7 +337,7 @@ func (c *completer) modcacheMatches(pkg metadata.PackageName, prefix string) ([]
331337
kind = protocol.ConstantCompletion
332338
detail = fmt.Sprintf("const (from %s)", cand.ImportPath)
333339
case modindex.Type: // might be a type alias
334-
kind = protocol.VariableCompletion
340+
kind = protocol.ClassCompletion
335341
detail = fmt.Sprintf("type (from %s)", cand.ImportPath)
336342
default:
337343
continue

gopls/internal/test/marker/testdata/completion/unimported.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ func Foo() {}
2121
package foo
2222

2323
type StructFoo struct{ F int }
24+
type ArrayFoo [16]byte
25+
type InterfaceFoo interface{}
26+
type FuncFoo func(int)
27+
28+
const ConstFoo = 1
29+
30+
var VarFoo = 2
2431

2532
-- baz/baz.go --
2633
package baz
@@ -41,6 +48,10 @@ func _() {
4148
signature.Foo //@complete(re"()Foo", signaturefoo)
4249

4350
context.Bac //@complete(re"() \\/\\/", contextBackground)
51+
52+
// Every kind of exported declaration of a workspace package that is not
53+
// imported yet.
54+
foo. //@complete(re"foo.() \\/\\/", fooArray, fooConst, fooFunc, fooInterface, fooStruct, fooVar)
4455
}
4556

4657
// Create markers for unimported std lib packages. Only for use by this test.
@@ -49,7 +60,14 @@ func _() {
4960
/* httptrace */ //@item(httptrace, "httptrace", "\"net/http/httptrace\"", "package")
5061
/* httputil */ //@item(httputil, "httputil", "\"net/http/httputil\"", "package")
5162

52-
/* ring.Ring */ //@item(ringring, "Ring", "type (from \"container/ring\")", "var")
63+
/* ring.Ring */ //@item(ringring, "Ring", "type (from \"container/ring\")", "type")
64+
65+
/* foo.ArrayFoo */ //@item(fooArray, "ArrayFoo", "type (from \"unimported.test/foo\")", "type")
66+
/* foo.ConstFoo */ //@item(fooConst, "ConstFoo", "const (from \"unimported.test/foo\")", "const")
67+
/* foo.FuncFoo */ //@item(fooFunc, "FuncFoo", "func (from \"unimported.test/foo\")", "func")
68+
/* foo.InterfaceFoo */ //@item(fooInterface, "InterfaceFoo", "type (from \"unimported.test/foo\")", "interface")
69+
/* foo.StructFoo */ //@item(fooStruct, "StructFoo", "type (from \"unimported.test/foo\")", "type")
70+
/* foo.VarFoo */ //@item(fooVar, "VarFoo", "var (from \"unimported.test/foo\")", "var")
5371

5472
/* signature.Foo */ //@item(signaturefoo, "Foo", "func (from \"unimported.test/signature\")", "func")
5573

0 commit comments

Comments
 (0)