internal/lsp/source: add some downranking for workspace symbols

Downrank symbols in packages outside of the workspace.

For golang/go#40548

Change-Id: Ie83f42f844e57aae5fc397d105f33858d6e44d0f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/248383
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/internal/lsp/source/workspace_symbol.go b/internal/lsp/source/workspace_symbol.go
index 828f3bb..d60a54f 100644
--- a/internal/lsp/source/workspace_symbol.go
+++ b/internal/lsp/source/workspace_symbol.go
@@ -418,10 +418,25 @@
 	if !node.Pos().IsValid() || !node.End().IsValid() {
 		return
 	}
+
+	// Arbitrary factors to apply to the match score for the purpose of
+	// downranking results.
+	//
+	// There is no science behind this, other than the principle that symbols
+	// outside of a workspace should be downranked. Adjust as necessary.
+	const (
+		nonWorkspaceFactor = 0.5
+	)
+	factor := 1.0
+	if !sc.current.isWorkspace {
+		factor *= nonWorkspaceFactor
+	}
 	symbol, score := sc.symbolizer(name, sc.current.pkg, sc.matcher)
+	score *= factor
 	if score <= sc.res[len(sc.res)-1].score {
 		return
 	}
+
 	mrng := newMappedRange(sc.current.snapshot.FileSet(), sc.curFile.Mapper, node.Pos(), node.End())
 	rng, err := mrng.Range()
 	if err != nil {