2019-08-19 17:28:08 -06:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package diff supports a pluggable diff algorithm.
|
|
|
|
package diff
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
2019-10-03 12:44:07 -06:00
|
|
|
"strings"
|
2019-08-19 17:28:08 -06:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TextEdit represents a change to a section of a document.
|
|
|
|
// The text within the specified span should be replaced by the supplied new text.
|
|
|
|
type TextEdit struct {
|
|
|
|
Span span.Span
|
|
|
|
NewText string
|
|
|
|
}
|
|
|
|
|
2019-10-03 12:18:24 -06:00
|
|
|
// ComputeEdits is the type for a function that produces a set of edits that
|
|
|
|
// convert from the before content to the after content.
|
2019-10-01 16:06:10 -06:00
|
|
|
type ComputeEdits func(uri span.URI, before, after string) []TextEdit
|
|
|
|
|
2019-10-01 16:08:04 -06:00
|
|
|
// SortTextEdits attempts to order all edits by their starting points.
|
|
|
|
// The sort is stable so that edits with the same starting point will not
|
|
|
|
// be reordered.
|
2019-08-19 17:28:08 -06:00
|
|
|
func SortTextEdits(d []TextEdit) {
|
|
|
|
// Use a stable sort to maintain the order of edits inserted at the same position.
|
|
|
|
sort.SliceStable(d, func(i int, j int) bool {
|
|
|
|
return span.Compare(d[i].Span, d[j].Span) < 0
|
|
|
|
})
|
|
|
|
}
|
2019-10-03 12:18:24 -06:00
|
|
|
|
|
|
|
// ApplyEdits applies the set of edits to the before and returns the resulting
|
|
|
|
// content.
|
|
|
|
// It may panic or produce garbage if the edits are not valid for the provided
|
|
|
|
// before content.
|
|
|
|
func ApplyEdits(before string, edits []TextEdit) string {
|
|
|
|
// Preconditions:
|
|
|
|
// - all of the edits apply to before
|
|
|
|
// - and all the spans for each TextEdit have the same URI
|
2019-10-03 12:44:07 -06:00
|
|
|
if len(edits) == 0 {
|
|
|
|
return before
|
2019-10-03 12:18:24 -06:00
|
|
|
}
|
2019-10-03 12:44:07 -06:00
|
|
|
edits = prepareEdits(edits)
|
|
|
|
c := span.NewContentConverter("", []byte(before))
|
|
|
|
after := strings.Builder{}
|
|
|
|
last := 0
|
2019-10-03 12:18:24 -06:00
|
|
|
for _, edit := range edits {
|
2019-10-03 12:44:07 -06:00
|
|
|
spn, _ := edit.Span.WithAll(c)
|
|
|
|
start := spn.Start().Offset()
|
|
|
|
if start > last {
|
|
|
|
after.WriteString(before[last:start])
|
|
|
|
last = start
|
2019-10-03 12:18:24 -06:00
|
|
|
}
|
|
|
|
after.WriteString(edit.NewText)
|
2019-10-03 12:44:07 -06:00
|
|
|
last = spn.End().Offset()
|
2019-10-03 12:18:24 -06:00
|
|
|
}
|
2019-10-03 12:44:07 -06:00
|
|
|
if last < len(before) {
|
|
|
|
after.WriteString(before[last:])
|
2019-10-03 12:18:24 -06:00
|
|
|
}
|
|
|
|
return after.String()
|
|
|
|
}
|
2019-10-03 12:44:07 -06:00
|
|
|
|
|
|
|
// prepareEdits returns a sorted copy of the edits
|
|
|
|
func prepareEdits(edits []TextEdit) []TextEdit {
|
|
|
|
copied := make([]TextEdit, len(edits))
|
|
|
|
copy(copied, edits)
|
|
|
|
SortTextEdits(copied)
|
|
|
|
return copied
|
|
|
|
}
|