mirror of
https://github.com/golang/go
synced 2024-11-18 13:04:46 -07:00
internal/lsp: remove edit fixups from rename now diff is functional
Change-Id: I9b26388abe4b1470605852c71c397c7fddf7fdef Reviewed-on: https://go-review.googlesource.com/c/tools/+/199740 Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
parent
a26de0c301
commit
e5ffc44a6f
@ -12,7 +12,6 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/tools/internal/lsp/diff"
|
||||
"golang.org/x/tools/internal/lsp/protocol"
|
||||
@ -112,9 +111,7 @@ func (r *rename) Run(ctx context.Context, args ...string) error {
|
||||
}
|
||||
ioutil.WriteFile(filename, []byte(newContent), 0644)
|
||||
case r.Diff:
|
||||
// myersEdits := diff.ComputeEdits(cmdFile.uri, string(cmdFile.mapper.Content), string(newContent))
|
||||
myersEdits := toMyersTextEdits(renameEdits, cmdFile.mapper)
|
||||
diffs := diff.ToUnified(filename+".orig", filename, string(cmdFile.mapper.Content), myersEdits)
|
||||
diffs := diff.ToUnified(filename+".orig", filename, string(cmdFile.mapper.Content), renameEdits)
|
||||
fmt.Print(diffs)
|
||||
default:
|
||||
if len(keys) > 1 {
|
||||
@ -129,94 +126,3 @@ func (r *rename) Run(ctx context.Context, args ...string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type editPair [2]diff.TextEdit // container for a del/ins TextEdit pair
|
||||
|
||||
// toMyersTextEdits converts the "word-oriented" textEdits returned by
|
||||
// source.Rename into the "line-oriented" textEdits that
|
||||
// diff.ToUnified() (aka myers.toUnified()) expects.
|
||||
func toMyersTextEdits(edits []diff.TextEdit, mapper *protocol.ColumnMapper) []diff.TextEdit {
|
||||
var myersEdits []diff.TextEdit
|
||||
|
||||
if len(edits) == 0 {
|
||||
return myersEdits
|
||||
}
|
||||
|
||||
contentByLine := strings.Split(string(mapper.Content), "\n")
|
||||
|
||||
// gather all of the edits on a line, create an editPair from them,
|
||||
// and append it to the list of pairs
|
||||
var pairs []editPair
|
||||
var pending []diff.TextEdit
|
||||
currentLine := edits[0].Span.Start().Line()
|
||||
for i := 0; i < len(edits); i++ {
|
||||
if edits[i].Span.Start().Line() != currentLine {
|
||||
pairs = append(pairs, toEditPair(pending, contentByLine[currentLine-1]))
|
||||
currentLine = edits[i].Span.Start().Line()
|
||||
pending = pending[:0] // clear it, leaking not a problem...
|
||||
}
|
||||
pending = append(pending, edits[i])
|
||||
}
|
||||
pairs = append(pairs, toEditPair(pending, contentByLine[currentLine-1]))
|
||||
|
||||
// reorder contiguous del/ins pairs into blocks of del and ins
|
||||
myersEdits = reorderEdits(pairs)
|
||||
return myersEdits
|
||||
}
|
||||
|
||||
// toEditPair takes one or more "word" diff.TextEdit(s) that occur
|
||||
// on a single line and creates a single equivalent
|
||||
// delete-line/insert-line pair of diff.TextEdit.
|
||||
func toEditPair(edits []diff.TextEdit, before string) editPair {
|
||||
// interleave retained bits of old line with new text from edits
|
||||
p := 0 // position in old line
|
||||
after := ""
|
||||
for i := 0; i < len(edits); i++ {
|
||||
after += before[p:edits[i].Span.Start().Column()-1] + edits[i].NewText
|
||||
p = edits[i].Span.End().Column() - 1
|
||||
}
|
||||
after += before[p:] + "\n"
|
||||
|
||||
// seems we can get away w/out providing offsets
|
||||
u := edits[0].Span.URI()
|
||||
l := edits[0].Span.Start().Line()
|
||||
newEdits := editPair{
|
||||
diff.TextEdit{Span: span.New(u, span.NewPoint(l, 1, -1), span.NewPoint(l+1, 1, -1))},
|
||||
diff.TextEdit{Span: span.New(u, span.NewPoint(l+1, 1, -1), span.NewPoint(l+1, 1, -1)), NewText: after},
|
||||
}
|
||||
return newEdits
|
||||
}
|
||||
|
||||
// reorderEdits reorders blocks of delete/insert pairs so that all of
|
||||
// the deletes come first, resetting the spans for the insert records
|
||||
// to keep them "sorted". It assumes that each entry is a "del/ins"
|
||||
// pair.
|
||||
func reorderEdits(e []editPair) []diff.TextEdit {
|
||||
var r []diff.TextEdit // reordered edits
|
||||
var p []diff.TextEdit // pending insert edits, waiting for end of dels
|
||||
|
||||
r = append(r, e[0][0])
|
||||
p = append(p, e[0][1])
|
||||
|
||||
for i := 1; i < len(e); i++ {
|
||||
if e[i][0].Span.Start().Line() != r[len(r)-1].Span.Start().Line()+1 {
|
||||
unpend(&r, &p)
|
||||
p = p[:0] // clear it, leaking not a problem...
|
||||
}
|
||||
r = append(r, e[i][0])
|
||||
p = append(p, e[i][1])
|
||||
}
|
||||
unpend(&r, &p)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// unpend sets the spans of the pending TextEdits to point to the last
|
||||
// line in the associated block of deletes then appends them to r.
|
||||
func unpend(r, p *[]diff.TextEdit) {
|
||||
for j := 0; j < len(*p); j++ {
|
||||
prev := (*r)[len(*r)-1]
|
||||
(*p)[j].Span = span.New(prev.Span.URI(), prev.Span.End(), prev.Span.End())
|
||||
}
|
||||
*r = append(*r, (*p)...)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user