1
0
mirror of https://github.com/golang/go synced 2024-11-18 10:14:45 -07:00

internal/lsp/source: fix function completion ranking

I noticed an annoying completion ranking issue:

   // ranks "HandlerFunc" over "HandleFunc"
   http.HandleFunc<>

This was due to us downranking function calls to prefer fields/vars. I
tweaked the logic to only downrank methods (with a receiver).

Change-Id: Ia4040dc8a35f641be2d7d934bf555090831219ac
Reviewed-on: https://go-review.googlesource.com/c/tools/+/246357
Run-TryBot: Muir Manders <muir@mnd.rs>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Muir Manders 2020-08-01 12:34:17 -07:00 committed by Rebecca Stambler
parent 9099c90ca5
commit 41a9f6dc66
11 changed files with 30 additions and 18 deletions

View File

@ -340,9 +340,11 @@ func (c *completer) found(ctx context.Context, cand candidate) {
}
}
// Lower score of function calls so we prefer fields and vars over calls.
// Lower score of method calls so we prefer fields and vars over calls.
if cand.expandFuncCall {
cand.score *= 0.9
if sig, ok := obj.Type().Underlying().(*types.Signature); ok && sig.Recv() != nil {
cand.score *= 0.9
}
}
// Prefer private objects over public ones.

View File

@ -8,7 +8,7 @@ type stateFunc func() stateFunc //@item(stateFunc, "stateFunc", "func() stateFun
var a unknown //@item(global_a, "a", "unknown", "var"),diag("unknown", "compiler", "undeclared name: unknown", "error")
func random() int { //@item(random, "random", "func() int", "func")
//@complete("", global_a, bob, stateFunc, random, random2, random3, stuff)
//@complete("", global_a, bob, random, random2, random3, stateFunc, stuff)
return 0
}
@ -16,18 +16,18 @@ func random2(y int) int { //@item(random2, "random2", "func(y int) int", "func")
x := 6 //@item(x, "x", "int", "var"),diag("x", "compiler", "x declared but not used", "error")
var q blah //@item(q, "q", "blah", "var"),diag("q", "compiler", "q declared but not used", "error"),diag("blah", "compiler", "undeclared name: blah", "error")
var t **blob //@item(t, "t", "**blob", "var"),diag("t", "compiler", "t declared but not used", "error"),diag("blob", "compiler", "undeclared name: blob", "error")
//@complete("", q, t, x, bad_y_param, global_a, bob, stateFunc, random, random2, random3, stuff)
//@complete("", q, t, x, bad_y_param, global_a, bob, random, random2, random3, stateFunc, stuff)
return y
}
func random3(y ...int) { //@item(random3, "random3", "func(y ...int)", "func"),item(y_variadic_param, "y", "[]int", "var")
//@complete("", y_variadic_param, global_a, bob, stateFunc, random, random2, random3, stuff)
//@complete("", y_variadic_param, global_a, bob, random, random2, random3, stateFunc, stuff)
var ch chan (favType1) //@item(ch, "ch", "chan (favType1)", "var"),diag("ch", "compiler", "ch declared but not used", "error"),diag("favType1", "compiler", "undeclared name: favType1", "error")
var m map[keyType]int //@item(m, "m", "map[keyType]int", "var"),diag("m", "compiler", "m declared but not used", "error"),diag("keyType", "compiler", "undeclared name: keyType", "error")
var arr []favType2 //@item(arr, "arr", "[]favType2", "var"),diag("arr", "compiler", "arr declared but not used", "error"),diag("favType2", "compiler", "undeclared name: favType2", "error")
var fn1 func() badResult //@item(fn1, "fn1", "func() badResult", "var"),diag("fn1", "compiler", "fn1 declared but not used", "error"),diag("badResult", "compiler", "undeclared name: badResult", "error")
var fn2 func(badParam) //@item(fn2, "fn2", "func(badParam)", "var"),diag("fn2", "compiler", "fn2 declared but not used", "error"),diag("badParam", "compiler", "undeclared name: badParam", "error")
//@complete("", arr, ch, fn1, fn2, m, y_variadic_param, global_a, bob, stateFunc, random, random2, random3, stuff)
//@complete("", arr, ch, fn1, fn2, m, y_variadic_param, global_a, bob, random, random2, random3, stateFunc, stuff)
}

View File

@ -20,6 +20,7 @@ func _() {
func _() {
defer func() {
foo.F //@complete(" //", Foo),snippet(" //", Foo, "Foo()", "Foo()")
foo. //@complete(" //", IntFoo, StructFoo, Foo)
foo. //@rank(" //", Foo)
}
}

View File

@ -5,5 +5,5 @@ import (
)
func _() {
defer func() { foo. } //@complete(" }", IntFoo, StructFoo, Foo)
defer func() { foo. } //@rank(" }", Foo)
}

View File

@ -5,5 +5,5 @@ import (
)
func _() {
go foo. //@complete(" //", IntFoo, StructFoo, Foo)
go foo. //@rank(" //", Foo)
}

View File

@ -6,6 +6,6 @@ import (
func _() {
go func() {
defer foo. //@complete(" //", IntFoo, StructFoo, Foo)
defer foo. //@rank(" //", Foo)
}
}

View File

@ -15,9 +15,9 @@ func _() {
// Bar is a function.
func Bar() { //@item(Bar, "Bar", "func()", "func", "Bar is a function.")
foo.Foo() //@complete("F", IntFoo, StructFoo, Foo)
foo.Foo() //@complete("F", Foo, IntFoo, StructFoo)
var _ foo.IntFoo //@complete("I", IntFoo, StructFoo)
foo.() //@complete("(", IntFoo, StructFoo, Foo)
foo.() //@complete("(", Foo, IntFoo, StructFoo)
}
func _() {

View File

@ -129,7 +129,7 @@ type baz struct {
}
func (b baz) _() {
b.Thing().val //@item(deepBazMethVal, "b.Thing().val", "int", "field")
b.thing.val //@item(deepBazFieldVal, "b.thing.val", "int", "field")
var _ int = b //@rank(" //", deepBazFieldVal, deepBazMethVal)
b.Thing().val //@item(deepBazMethVal, "b.Thing().val", "int", "field")
b.thing.val //@item(deepBazFieldVal, "b.thing.val", "int", "field")
var _ int = bval //@rank(" //", deepBazFieldVal, deepBazMethVal)
}

View File

@ -1,5 +1,7 @@
package func_rank
import "net/http"
var stringAVar = "var" //@item(stringAVar, "stringAVar", "string", "var")
func stringBFunc() string { return "str" } //@item(stringBFunc, "stringBFunc", "func() string", "func")
type stringer struct{} //@item(stringer, "stringer", "struct{...}", "struct")
@ -59,3 +61,10 @@ func (foo) FooPublicMethod() int { //@item(rankFooPubMeth, "FooPublicMethod", "f
func _() {
var _ int = foo{}. //@rank(" //", rankFooPrivField, rankFooPubField),rank(" //", rankFooPrivMeth, rankFooPubMeth),rank(" //", rankFooPrivField, rankFooPrivMeth)
}
func _() {
HandleFunc //@item(httpHandleFunc, "HandleFunc", "func(pattern string, handler func(ResponseWriter, *Request))", "func")
HandlerFunc //@item(httpHandlerFunc, "HandlerFunc", "func(http.ResponseWriter, *http.Request)", "type")
http.HandleFunc //@rank(" //", httpHandleFunc, httpHandlerFunc)
}

View File

@ -5,5 +5,5 @@ import (
)
func _() {
foo.Foo() //@complete("F", IntFoo, StructFoo, Foo)
foo.Foo() //@complete("F", Foo, IntFoo, StructFoo)
}

View File

@ -1,11 +1,11 @@
-- summary --
CodeLensCount = 4
CompletionsCount = 245
CompletionsCount = 241
CompletionSnippetCount = 80
UnimportedCompletionsCount = 6
DeepCompletionsCount = 5
FuzzyCompletionsCount = 8
RankedCompletionsCount = 122
RankedCompletionsCount = 127
CaseSensitiveCompletionsCount = 4
DiagnosticsCount = 44
FoldingRangesCount = 2