Skip to content

Commit

Permalink
gopls/internal/lsp/regtest: add @rename marker tests
Browse files Browse the repository at this point in the history
This change adds rudimentary support for @rename
markers, which make an LSP 'rename' request and
assert that the expected file changes are obtained.
We also introduce a separate marker, @renameerr,
for expectations of failure.

The treatment both of changed files and of expected errors
should be used by other markers, for consistency.

Also:
- Env.Mapper returns an error.
- add diff.ApplyBytes and compare.Bytes convenience wrappers.
- simplify fake.ApplyEdits by using ApplyProtocolEdits.
- change ApplyProtocolEdits to use []byte.

Change-Id: Ib64b3d8e12c025f01dd0783b4faee94f082b4895
Reviewed-on: https://go-review.googlesource.com/c/tools/+/466144
Run-TryBot: Alan Donovan <adonovan@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
adonovan authored and gopherbot committed Feb 16, 2023
1 parent a30296b commit e5c9e63
Show file tree
Hide file tree
Showing 18 changed files with 552 additions and 126 deletions.
8 changes: 4 additions & 4 deletions go/analysis/analysistest/analysistest.go
Expand Up @@ -184,7 +184,7 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns
for _, vf := range ar.Files {
if vf.Name == sf {
found = true
out, err := diff.Apply(string(orig), edits)
out, err := diff.ApplyBytes(orig, edits)
if err != nil {
t.Errorf("%s: error applying fixes: %v", file.Name(), err)
continue
Expand All @@ -194,7 +194,7 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns
// between files in the archive. normalize
// this to a single newline.
want := string(bytes.TrimRight(vf.Data, "\n")) + "\n"
formatted, err := format.Source([]byte(out))
formatted, err := format.Source(out)
if err != nil {
t.Errorf("%s: error formatting edited source: %v\n%s", file.Name(), err, out)
continue
Expand All @@ -218,14 +218,14 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns
catchallEdits = append(catchallEdits, edits...)
}

out, err := diff.Apply(string(orig), catchallEdits)
out, err := diff.ApplyBytes(orig, catchallEdits)
if err != nil {
t.Errorf("%s: error applying fixes: %v", file.Name(), err)
continue
}
want := string(ar.Comment)

formatted, err := format.Source([]byte(out))
formatted, err := format.Source(out)
if err != nil {
t.Errorf("%s: error formatting resulting source: %v\n%s", file.Name(), err, out)
continue
Expand Down
3 changes: 1 addition & 2 deletions go/analysis/internal/checker/checker.go
Expand Up @@ -424,11 +424,10 @@ func applyFixes(roots []*action) error {
return err
}

applied, err := diff.Apply(string(contents), edits)
out, err := diff.ApplyBytes(contents, edits)
if err != nil {
return err
}
out := []byte(applied)

// Try to format the file.
if formatted, err := format.Source(out); err == nil {
Expand Down
5 changes: 3 additions & 2 deletions gopls/internal/lsp/cmd/format.go
Expand Up @@ -9,6 +9,7 @@ import (
"flag"
"fmt"
"io/ioutil"
"os"

"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/lsp/source"
Expand Down Expand Up @@ -90,7 +91,7 @@ func (c *format) Run(ctx context.Context, args ...string) error {
if c.Write {
printIt = false
if len(edits) > 0 {
ioutil.WriteFile(filename, []byte(formatted), 0644)
ioutil.WriteFile(filename, formatted, 0644)
}
}
if c.Diff {
Expand All @@ -102,7 +103,7 @@ func (c *format) Run(ctx context.Context, args ...string) error {
fmt.Print(unified)
}
if printIt {
fmt.Print(formatted)
os.Stdout.Write(formatted)
}
}
return nil
Expand Down
5 changes: 3 additions & 2 deletions gopls/internal/lsp/cmd/imports.go
Expand Up @@ -9,6 +9,7 @@ import (
"flag"
"fmt"
"io/ioutil"
"os"

"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/lsp/source"
Expand Down Expand Up @@ -89,7 +90,7 @@ func (t *imports) Run(ctx context.Context, args ...string) error {
switch {
case t.Write:
if len(edits) > 0 {
ioutil.WriteFile(filename, []byte(newContent), 0644)
ioutil.WriteFile(filename, newContent, 0644)
}
case t.Diff:
unified, err := diff.ToUnified(filename+".orig", filename, string(file.mapper.Content), sedits)
Expand All @@ -98,7 +99,7 @@ func (t *imports) Run(ctx context.Context, args ...string) error {
}
fmt.Print(unified)
default:
fmt.Print(string(newContent))
os.Stdout.Write(newContent)
}
return nil
}
4 changes: 2 additions & 2 deletions gopls/internal/lsp/cmd/rename.go
Expand Up @@ -108,7 +108,7 @@ func (r *rename) Run(ctx context.Context, args ...string) error {
return fmt.Errorf("%v: %v", edits, err)
}
}
ioutil.WriteFile(filename, []byte(newContent), 0644)
ioutil.WriteFile(filename, newContent, 0644)
case r.Diff:
unified, err := diff.ToUnified(filename+".orig", filename, string(cmdFile.mapper.Content), renameEdits)
if err != nil {
Expand All @@ -119,7 +119,7 @@ func (r *rename) Run(ctx context.Context, args ...string) error {
if len(orderedURIs) > 1 {
fmt.Printf("%s:\n", filepath.Base(filename))
}
fmt.Print(string(newContent))
os.Stdout.Write(newContent)
if changeCount > 1 { // if this wasn't last change, print newline
fmt.Println()
}
Expand Down
5 changes: 3 additions & 2 deletions gopls/internal/lsp/cmd/suggested_fix.go
Expand Up @@ -9,6 +9,7 @@ import (
"flag"
"fmt"
"io/ioutil"
"os"

"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/lsp/source"
Expand Down Expand Up @@ -151,7 +152,7 @@ func (s *suggestedFix) Run(ctx context.Context, args ...string) error {
switch {
case s.Write:
if len(edits) > 0 {
ioutil.WriteFile(filename, []byte(newContent), 0644)
ioutil.WriteFile(filename, newContent, 0644)
}
case s.Diff:
diffs, err := diff.ToUnified(filename+".orig", filename, string(file.mapper.Content), sedits)
Expand All @@ -160,7 +161,7 @@ func (s *suggestedFix) Run(ctx context.Context, args ...string) error {
}
fmt.Print(diffs)
default:
fmt.Print(string(newContent))
os.Stdout.Write(newContent)
}
return nil
}
20 changes: 5 additions & 15 deletions gopls/internal/lsp/fake/edit.go
Expand Up @@ -6,6 +6,7 @@ package fake

import (
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/lsp/source"
"golang.org/x/tools/internal/diff"
)

Expand Down Expand Up @@ -35,25 +36,14 @@ func EditToChangeEvent(e protocol.TextEdit) protocol.TextDocumentContentChangeEv
// and returns a new slice containing the lines of the patched file.
// It is a wrapper around diff.Apply; see that function for preconditions.
func applyEdits(mapper *protocol.Mapper, edits []protocol.TextEdit, windowsLineEndings bool) ([]byte, error) {
// Convert fake.Edits to diff.Edits
diffEdits := make([]diff.Edit, len(edits))
for i, edit := range edits {
start, end, err := mapper.RangeOffsets(edit.Range)
if err != nil {
return nil, err
}
diffEdits[i] = diff.Edit{
Start: start,
End: end,
New: edit.NewText,
}
diffEdits, err := source.FromProtocolEdits(mapper, edits)
if err != nil {
return nil, err
}

patchedString, err := diff.Apply(string(mapper.Content), diffEdits)
patched, err := diff.ApplyBytes(mapper.Content, diffEdits)
if err != nil {
return nil, err
}
patched := []byte(patchedString)
if windowsLineEndings {
patched = toWindowsLineEndings(patched)
}
Expand Down
10 changes: 4 additions & 6 deletions gopls/internal/lsp/fake/editor.go
Expand Up @@ -702,17 +702,15 @@ func (e *Editor) BufferText(name string) (string, bool) {
return buf.text(), true
}

// Mapper returns the protocol.Mapper for the given buffer name.
//
// If there is no open buffer with that name, it returns nil.
func (e *Editor) Mapper(name string) *protocol.Mapper {
// Mapper returns the protocol.Mapper for the given buffer name, if it is open.
func (e *Editor) Mapper(name string) (*protocol.Mapper, error) {
e.mu.Lock()
defer e.mu.Unlock()
buf, ok := e.buffers[name]
if !ok {
return nil
return nil, fmt.Errorf("no mapper for %q", name)
}
return buf.mapper
return buf.mapper, nil
}

// BufferVersion returns the current version of the buffer corresponding to
Expand Down

0 comments on commit e5c9e63

Please sign in to comment.