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"
|
|
|
|
|
|
|
|
"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-01 16:06:10 -06:00
|
|
|
type ComputeEdits func(uri span.URI, before, after string) []TextEdit
|
|
|
|
|
2019-10-01 16:08:04 -06:00
|
|
|
type OpKind int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Delete OpKind = iota
|
|
|
|
Insert
|
|
|
|
Equal
|
|
|
|
)
|
|
|
|
|
|
|
|
func (k OpKind) String() string {
|
|
|
|
switch k {
|
|
|
|
case Delete:
|
|
|
|
return "delete"
|
|
|
|
case Insert:
|
|
|
|
return "insert"
|
|
|
|
case Equal:
|
|
|
|
return "equal"
|
|
|
|
default:
|
|
|
|
panic("unknown operation kind")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
})
|
|
|
|
}
|