go/internal/lsp/testdata/rank
Muir Manders ca1c149215 internal/lsp: offer type converted completion candidates
For example:

    func wantsInt64(int64)            {}
    func wantsDuration(time.Duration) {}

    func _() {
      for i := range []string{} {
        // inserts "i" as "int64(i)"
        wantsInt64(i<>)

        // inserts "i" as "time.Duration(i)"
        wantsDuration(i<>)
      }
    }

Type converted candidates have a significant score penalty so they
should never outrank a directly assignable candidate, other factors
being equal.

To minimize false positive completions, we don't offer converted
candidates if either:
- The candidate is a deep completion into another package. This avoids
  random "float64(somepkg.SomeVar)" popping up when completing a
  float64.
- Don't offer to convert ints to strings since that's not normally
  what the user wants.

After going back and forth I decided not to include the type
conversion in the candidate label. This means you will just see the
candidate "i" in the completion popup instead of "float64(i)". Type
names add a lot of noise to the candidate list.

I also tweaked the untyped constant penalty to interplay better with
the new type conversion penalty. I wanted untyped constants to be
ranked above type conversion candidates since they are directly
assignable, so I reduced untyped constants' penalty. However, to
continue suppressing untyped constants from random other packages, I
applied a similar heuristic to above where we give a bigger penalty if
the untyped constant is a deep completion from another package.

Fixes golang/go#42764.

Updates golang/go#39207 (untyped constant penalty tweak may improve
this situation).

Change-Id: I565114b3a1c9628285d07200b9dfc45c61e5d898
Reviewed-on: https://go-review.googlesource.com/c/tools/+/272286
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Trust: Robert Findley <rfindley@google.com>
Trust: Rebecca Stambler <rstambler@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-11-30 20:04:59 +00:00
..
struct internal/lsp: use a marker test for struct field ranking test 2020-11-18 00:33:11 +00:00
assign_rank.go.in internal/lsp: remove multi-module support in tests 2020-09-22 16:28:30 +00:00
binexpr_rank.go.in internal/lsp: remove multi-module support in tests 2020-09-22 16:28:30 +00:00
boolexpr_rank.go internal/lsp: remove multi-module support in tests 2020-09-22 16:28:30 +00:00
convert_rank.go.in internal/lsp: offer type converted completion candidates 2020-11-30 20:04:59 +00:00
switch_rank.go.in internal/lsp: remove multi-module support in tests 2020-09-22 16:28:30 +00:00
type_assert_rank.go.in internal/lsp: remove multi-module support in tests 2020-09-22 16:28:30 +00:00
type_switch_rank.go.in internal/lsp: remove multi-module support in tests 2020-09-22 16:28:30 +00:00