From b243e57ea8189f67f74aa2fdadabbf72db1c16fa Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Wed, 21 Sep 2022 09:28:11 -0400 Subject: [PATCH] gopls/internal/lsp/tests: simplify collectCompletionItems, remove Data.t The marker function collectCompletionItems required at least three arguments. Express this in its signature, leaving a final variadic argument for documentation. I considered just making this final argument mandatory, but opted for minimizing the diff given that there are 400+ existing @item annotations. With this change the only use of tests.Data.t is in mustRange. Since conversion to range should always succeed, I switched this usage to a panic and removed the t field. For golang/go#54845 Change-Id: I407f07cb85fa1356ceb6dba366007f69d1b6a068 Reviewed-on: https://go-review.googlesource.com/c/tools/+/432337 Reviewed-by: Alan Donovan Run-TryBot: Robert Findley gopls-CI: kokoro TryBot-Result: Gopher Robot --- gopls/internal/lsp/tests/tests.go | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/gopls/internal/lsp/tests/tests.go b/gopls/internal/lsp/tests/tests.go index cae4af25ad..689fcca94f 100644 --- a/gopls/internal/lsp/tests/tests.go +++ b/gopls/internal/lsp/tests/tests.go @@ -130,7 +130,6 @@ type Data struct { AddImport AddImport Hovers Hovers - t testing.TB fragments map[string]string dir string golden map[string]*Golden @@ -337,7 +336,6 @@ func load(t testing.TB, mode string, dir string) *Data { AddImport: make(AddImport), Hovers: make(Hovers), - t: t, dir: dir, fragments: map[string]string{}, golden: map[string]*Golden{}, @@ -1187,15 +1185,9 @@ func (data *Data) collectCompletions(typ CompletionTestType) func(span.Span, []t } } -func (data *Data) collectCompletionItems(pos token.Pos, args []string) { - if len(args) < 3 { - loc := data.Exported.ExpectFileSet.Position(pos) - data.t.Fatalf("%s:%d: @item expects at least 3 args, got %d", - loc.Filename, loc.Line, len(args)) - } - label, detail, kind := args[0], args[1], args[2] +func (data *Data) collectCompletionItems(pos token.Pos, label, detail, kind string, args []string) { var documentation string - if len(args) == 4 { + if len(args) > 3 { documentation = args[3] } data.CompletionItems[pos] = &completion.CompletionItem{ @@ -1354,14 +1346,12 @@ func (data *Data) collectSymbols(name string, selectionRng span.Span, kind, deta }) } -// mustRange converts spn into a protocol.Range, calling t.Fatal on any error. +// mustRange converts spn into a protocol.Range, panicking on any error. func (data *Data) mustRange(spn span.Span) protocol.Range { m, err := data.Mapper(spn.URI()) rng, err := m.Range(spn) if err != nil { - // TODO(rfindley): this can probably just be a panic, at which point we - // don't need to close over t. - data.t.Fatal(err) + panic(fmt.Sprintf("converting span %s to range: %v", spn, err)) } return rng }