From fd66c7521c0f4ce8d1e990fd59af0b65bb6fc000 Mon Sep 17 00:00:00 2001 From: Heschi Kreinick Date: Mon, 23 Dec 2019 13:19:17 -0500 Subject: [PATCH] internal/lsp/source: don't get unnecessary unimported completions Unimported completions are always low-priority. If the user already has 100 completion options, the unimported ones are probably not useful. There's no point in calculating any of them. Also, only do unimported completions for package members when they're enabled. Oops. Change-Id: I7535a22ad56bed869dceb6cd0ffdfc6390cf8eb5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/212629 Run-TryBot: Heschi Kreinick TryBot-Result: Gobot Gobot Reviewed-by: Rebecca Stambler --- internal/lsp/source/completion.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/internal/lsp/source/completion.go b/internal/lsp/source/completion.go index a1cf59417f..11c55301e2 100644 --- a/internal/lsp/source/completion.go +++ b/internal/lsp/source/completion.go @@ -601,7 +601,7 @@ func (c *completer) wantTypeName() bool { } // See https://golang.org/issue/36001. Unimported completions are expensive. -const maxUnimported = 20 +const unimportedTarget = 100 // selector finds completions for the specified selector expression. func (c *completer) selector(sel *ast.SelectorExpr) error { @@ -620,15 +620,14 @@ func (c *completer) selector(sel *ast.SelectorExpr) error { } // Try unimported packages. - if id, ok := sel.X.(*ast.Ident); ok { + if id, ok := sel.X.(*ast.Ident); ok && c.opts.Unimported && len(c.items) < unimportedTarget { pkgExports, err := PackageExports(c.ctx, c.snapshot.View(), id.Name, c.filename) if err != nil { return err } known := c.snapshot.KnownImportPaths() - startingItems := len(c.items) for _, pkgExport := range pkgExports { - if len(c.items)-startingItems >= maxUnimported { + if len(c.items) >= unimportedTarget { break } // If we've seen this import path, use the fully-typed version. @@ -828,7 +827,7 @@ func (c *completer) lexical() error { } } - if c.opts.Unimported { + if c.opts.Unimported && len(c.items) < unimportedTarget { // Suggest packages that have not been imported yet. pkgs, err := CandidateImports(c.ctx, c.snapshot.View(), c.filename) if err != nil { @@ -838,9 +837,8 @@ func (c *completer) lexical() error { // Rank unimported packages significantly lower than other results. score *= 0.07 - startingItems := len(c.items) for _, pkg := range pkgs { - if len(c.items)-startingItems >= maxUnimported { + if len(c.items) >= unimportedTarget { break } if _, ok := seen[pkg.IdentName]; !ok {