mirror of
https://github.com/golang/go.git
synced 2025-05-05 23:53:05 +00:00
This makes it so the diff package is depended on by all implementations, rather than the diff package having to depend on the default myers implementation. Change-Id: I04b9caee6ff1017fa8e5476a7434e4b0e17753c3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/198379 Reviewed-by: Rebecca Stambler <rstambler@golang.org>
57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
// 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
|
|
}
|
|
|
|
type ComputeEdits func(uri span.URI, before, after string) []TextEdit
|
|
|
|
var (
|
|
ToUnified func(from, to string, before string, edits []TextEdit) string
|
|
)
|
|
|
|
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.
|
|
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
|
|
})
|
|
}
|