Skip to content

Commit

Permalink
gopls/internal/lsp: simplify prepareRename tests
Browse files Browse the repository at this point in the history
- simplify collection to use an expected span.Span and use mustRange
- remove the source_test.go version of the test, as it is redundant

For golang/go#54845

Change-Id: I3a7da8547e27dc157fb513486a151031ec135746
Reviewed-on: https://go-review.googlesource.com/c/tools/+/432138
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
findleyr committed Sep 23, 2022
1 parent b9adce9 commit c7ac942
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 54 deletions.
9 changes: 7 additions & 2 deletions gopls/internal/lsp/lsp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,12 @@ func (r *runner) PrepareRename(t *testing.T, src span.Span, want *source.Prepare
t.Errorf("prepare rename failed for %v: got error: %v", src, err)
return
}
// we all love typed nils

// TODO(rfindley): can we consolidate on a single representation for
// PrepareRename results, and use cmp.Diff here?

// PrepareRename may fail with no error if there was no object found at the
// position.
if got == nil {
if want.Text != "" { // expected an ident.
t.Errorf("prepare rename failed for %v: got nil", src)
Expand All @@ -1088,7 +1093,7 @@ func (r *runner) PrepareRename(t *testing.T, src span.Span, want *source.Prepare
t.Errorf("prepare rename failed: incorrect point, got %v want %v", got.Range.Start, want.Range.Start)
}
} else {
if protocol.CompareRange(got.Range, want.Range) != 0 {
if got.Range != want.Range {
t.Errorf("prepare rename failed: incorrect range got %v want %v", got.Range, want.Range)
}
}
Expand Down
6 changes: 6 additions & 0 deletions gopls/internal/lsp/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ func (s *Server) rename(ctx context.Context, params *protocol.RenameParams) (*pr
}, nil
}

// prepareRename implements the textDocument/prepareRename handler. It may
// return (nil, nil) if there is no rename at the cursor position, but it is
// not desirable to display an error to the user.
//
// TODO(rfindley): why wouldn't we want to show an error to the user, if the
// user initiated a rename request at the cursor?
func (s *Server) prepareRename(ctx context.Context, params *protocol.PrepareRenameParams) (*protocol.PrepareRename2Gn, error) {
snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.Go)
defer release()
Expand Down
38 changes: 1 addition & 37 deletions gopls/internal/lsp/source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,43 +839,7 @@ func applyEdits(contents string, edits []diff.TextEdit) string {
}

func (r *runner) PrepareRename(t *testing.T, src span.Span, want *source.PrepareItem) {
_, srcRng, err := spanToRange(r.data, src)
if err != nil {
t.Fatal(err)
}
// Find the identifier at the position.
fh, err := r.snapshot.GetFile(r.ctx, src.URI())
if err != nil {
t.Fatal(err)
}
item, _, err := source.PrepareRename(r.ctx, r.snapshot, fh, srcRng.Start)
if err != nil {
if want.Text != "" { // expected an ident.
t.Errorf("prepare rename failed for %v: got error: %v", src, err)
}
return
}
if item == nil {
if want.Text != "" {
t.Errorf("prepare rename failed for %v: got nil", src)
}
return
}
if want.Text == "" {
t.Errorf("prepare rename failed for %v: expected nil, got %v", src, item)
return
}
if item.Range.Start == item.Range.End {
// Special case for 0-length ranges. Marks can't specify a 0-length range,
// so just compare the start.
if item.Range.Start != want.Range.Start {
t.Errorf("prepare rename failed: incorrect point, got %v want %v", item.Range.Start, want.Range.Start)
}
} else {
if protocol.CompareRange(item.Range, want.Range) != 0 {
t.Errorf("prepare rename failed: incorrect range got %v want %v", item.Range, want.Range)
}
}
// Removed in favor of just using the lsp_test implementation. See ../lsp_test.go
}

func (r *runner) Symbols(t *testing.T, uri span.URI, expectedSymbols []protocol.DocumentSymbol) {
Expand Down
17 changes: 2 additions & 15 deletions gopls/internal/lsp/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -1365,22 +1365,9 @@ func (data *Data) collectRenames(src span.Span, newText string) {
data.Renames[src] = newText
}

func (data *Data) collectPrepareRenames(src span.Span, rng span.Range, placeholder string) {
m, err := data.Mapper(src.URI())
if err != nil {
data.t.Fatal(err)
}
// Convert range to span and then to protocol.Range.
spn, err := rng.Span()
if err != nil {
data.t.Fatal(err)
}
prng, err := m.Range(spn)
if err != nil {
data.t.Fatal(err)
}
func (data *Data) collectPrepareRenames(src, spn span.Span, placeholder string) {
data.PrepareRenames[src] = &source.PrepareItem{
Range: prng,
Range: data.mustRange(spn),
Text: placeholder,
}
}
Expand Down

0 comments on commit c7ac942

Please sign in to comment.