internal/diff: unexport various identifiers
They were exported only because of unnecessary coupling
with another package, solved by copying.
Change-Id: I5f08ad9091b8fce10c2bac6383e020a3c45426f6
Reviewed-on: https://go-review.googlesource.com/c/tools/+/498257
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/internal/diff/myers/diff.go b/internal/diff/myers/diff.go
index 7c2d435..c0f6cce 100644
--- a/internal/diff/myers/diff.go
+++ b/internal/diff/myers/diff.go
@@ -32,10 +32,10 @@
for _, op := range ops {
start, end := lineOffsets[op.I1], lineOffsets[op.I2]
switch op.Kind {
- case diff.Delete:
+ case opDelete:
// Delete: before[I1:I2] is deleted.
edits = append(edits, diff.Edit{Start: start, End: end})
- case diff.Insert:
+ case opInsert:
// Insert: after[J1:J2] is inserted at before[I1:I1].
if content := strings.Join(op.Content, ""); content != "" {
edits = append(edits, diff.Edit{Start: start, End: end, New: content})
@@ -45,8 +45,30 @@
return edits
}
+// opKind is used to denote the type of operation a line represents.
+type opKind int
+
+const (
+ opDelete opKind = iota // line deleted from input (-)
+ opInsert // line inserted into output (+)
+ opEqual // line present in input and output
+)
+
+func (kind opKind) String() string {
+ switch kind {
+ case opDelete:
+ return "delete"
+ case opInsert:
+ return "insert"
+ case opEqual:
+ return "equal"
+ default:
+ panic("unknown opKind")
+ }
+}
+
type operation struct {
- Kind diff.OpKind
+ Kind opKind
Content []string // content from b
I1, I2 int // indices of the line in a
J1 int // indices of the line in b, J2 implied by len(Content)
@@ -72,7 +94,7 @@
return
}
op.I2 = i2
- if op.Kind == diff.Insert {
+ if op.Kind == opInsert {
op.Content = b[op.J1:j2]
}
solution[i] = op
@@ -88,7 +110,7 @@
for snake[0]-snake[1] > x-y {
if op == nil {
op = &operation{
- Kind: diff.Delete,
+ Kind: opDelete,
I1: x,
J1: y,
}
@@ -104,7 +126,7 @@
for snake[0]-snake[1] < x-y {
if op == nil {
op = &operation{
- Kind: diff.Insert,
+ Kind: opInsert,
I1: x,
J1: y,
}
diff --git a/internal/diff/unified.go b/internal/diff/unified.go
index ed2c22e..3522e1e 100644
--- a/internal/diff/unified.go
+++ b/internal/diff/unified.go
@@ -36,58 +36,57 @@
// unified represents a set of edits as a unified diff.
type unified struct {
- // From is the name of the original file.
- From string
- // To is the name of the modified file.
- To string
- // Hunks is the set of edit hunks needed to transform the file content.
- Hunks []*hunk
+ // from is the name of the original file.
+ from string
+ // to is the name of the modified file.
+ to string
+ // hunks is the set of edit hunks needed to transform the file content.
+ hunks []*hunk
}
// Hunk represents a contiguous set of line edits to apply.
type hunk struct {
// The line in the original source where the hunk starts.
- FromLine int
+ fromLine int
// The line in the original source where the hunk finishes.
- ToLine int
+ toLine int
// The set of line based edits to apply.
- Lines []line
+ lines []line
}
// Line represents a single line operation to apply as part of a Hunk.
type line struct {
- // Kind is the type of line this represents, deletion, insertion or copy.
- Kind OpKind
- // Content is the content of this line.
+ // kind is the type of line this represents, deletion, insertion or copy.
+ kind opKind
+ // content is the content of this line.
// For deletion it is the line being removed, for all others it is the line
// to put in the output.
- Content string
+ content string
}
-// OpKind is used to denote the type of operation a line represents.
-// TODO(adonovan): hide this once the myers package no longer references it.
-type OpKind int
+// opKind is used to denote the type of operation a line represents.
+type opKind int
const (
- // Delete is the operation kind for a line that is present in the input
+ // opDelete is the operation kind for a line that is present in the input
// but not in the output.
- Delete OpKind = iota
- // Insert is the operation kind for a line that is new in the output.
- Insert
- // Equal is the operation kind for a line that is the same in the input and
+ opDelete opKind = iota
+ // opInsert is the operation kind for a line that is new in the output.
+ opInsert
+ // opEqual is the operation kind for a line that is the same in the input and
// output, often used to provide context around edited lines.
- Equal
+ opEqual
)
// String returns a human readable representation of an OpKind. It is not
// intended for machine processing.
-func (k OpKind) String() string {
+func (k opKind) String() string {
switch k {
- case Delete:
+ case opDelete:
return "delete"
- case Insert:
+ case opInsert:
return "insert"
- case Equal:
+ case opEqual:
return "equal"
default:
panic("unknown operation kind")
@@ -103,8 +102,8 @@
// a unified diff that represents those edits.
func toUnified(fromName, toName string, content string, edits []Edit) (unified, error) {
u := unified{
- From: fromName,
- To: toName,
+ from: fromName,
+ to: toName,
}
if len(edits) == 0 {
return u, nil
@@ -138,21 +137,21 @@
if h != nil {
// add the edge to the previous hunk
addEqualLines(h, lines, last, last+edge)
- u.Hunks = append(u.Hunks, h)
+ u.hunks = append(u.hunks, h)
}
toLine += start - last
h = &hunk{
- FromLine: start + 1,
- ToLine: toLine + 1,
+ fromLine: start + 1,
+ toLine: toLine + 1,
}
// add the edge to the new hunk
delta := addEqualLines(h, lines, start-edge, start)
- h.FromLine -= delta
- h.ToLine -= delta
+ h.fromLine -= delta
+ h.toLine -= delta
}
last = start
for i := start; i < end; i++ {
- h.Lines = append(h.Lines, line{Kind: Delete, Content: lines[i]})
+ h.lines = append(h.lines, line{kind: opDelete, content: lines[i]})
last++
}
if edit.New != "" {
@@ -163,18 +162,18 @@
// that is easiest to fix by postprocessing.
// e.g. issue #59232: ("aaa\nccc\n", "aaa\nbbb\nccc")
// -> [Delete "aaa\n", Insert "aaa\n", Insert "bbb\n", ...].
- if i == 0 && last > start && h.Lines[len(h.Lines)-1].Content == content {
- h.Lines[len(h.Lines)-1].Kind = Equal
+ if i == 0 && last > start && h.lines[len(h.lines)-1].content == content {
+ h.lines[len(h.lines)-1].kind = opEqual
continue
}
- h.Lines = append(h.Lines, line{Kind: Insert, Content: content})
+ h.lines = append(h.lines, line{kind: opInsert, content: content})
}
}
}
if h != nil {
// add the edge to the final hunk
addEqualLines(h, lines, last, last+edge)
- u.Hunks = append(u.Hunks, h)
+ u.hunks = append(u.hunks, h)
}
return u, nil
}
@@ -196,7 +195,7 @@
if i >= len(lines) {
return delta
}
- h.Lines = append(h.Lines, line{Kind: Equal, Content: lines[i]})
+ h.lines = append(h.lines, line{kind: opEqual, content: lines[i]})
delta++
}
return delta
@@ -205,19 +204,19 @@
// String converts a unified diff to the standard textual form for that diff.
// The output of this function can be passed to tools like patch.
func (u unified) String() string {
- if len(u.Hunks) == 0 {
+ if len(u.hunks) == 0 {
return ""
}
b := new(strings.Builder)
- fmt.Fprintf(b, "--- %s\n", u.From)
- fmt.Fprintf(b, "+++ %s\n", u.To)
- for _, hunk := range u.Hunks {
+ fmt.Fprintf(b, "--- %s\n", u.from)
+ fmt.Fprintf(b, "+++ %s\n", u.to)
+ for _, hunk := range u.hunks {
fromCount, toCount := 0, 0
- for _, l := range hunk.Lines {
- switch l.Kind {
- case Delete:
+ for _, l := range hunk.lines {
+ switch l.kind {
+ case opDelete:
fromCount++
- case Insert:
+ case opInsert:
toCount++
default:
fromCount++
@@ -226,32 +225,32 @@
}
fmt.Fprint(b, "@@")
if fromCount > 1 {
- fmt.Fprintf(b, " -%d,%d", hunk.FromLine, fromCount)
- } else if hunk.FromLine == 1 && fromCount == 0 {
+ fmt.Fprintf(b, " -%d,%d", hunk.fromLine, fromCount)
+ } else if hunk.fromLine == 1 && fromCount == 0 {
// Match odd GNU diff -u behavior adding to empty file.
fmt.Fprintf(b, " -0,0")
} else {
- fmt.Fprintf(b, " -%d", hunk.FromLine)
+ fmt.Fprintf(b, " -%d", hunk.fromLine)
}
if toCount > 1 {
- fmt.Fprintf(b, " +%d,%d", hunk.ToLine, toCount)
- } else if hunk.ToLine == 1 && toCount == 0 {
+ fmt.Fprintf(b, " +%d,%d", hunk.toLine, toCount)
+ } else if hunk.toLine == 1 && toCount == 0 {
// Match odd GNU diff -u behavior adding to empty file.
fmt.Fprintf(b, " +0,0")
} else {
- fmt.Fprintf(b, " +%d", hunk.ToLine)
+ fmt.Fprintf(b, " +%d", hunk.toLine)
}
fmt.Fprint(b, " @@\n")
- for _, l := range hunk.Lines {
- switch l.Kind {
- case Delete:
- fmt.Fprintf(b, "-%s", l.Content)
- case Insert:
- fmt.Fprintf(b, "+%s", l.Content)
+ for _, l := range hunk.lines {
+ switch l.kind {
+ case opDelete:
+ fmt.Fprintf(b, "-%s", l.content)
+ case opInsert:
+ fmt.Fprintf(b, "+%s", l.content)
default:
- fmt.Fprintf(b, " %s", l.Content)
+ fmt.Fprintf(b, " %s", l.content)
}
- if !strings.HasSuffix(l.Content, "\n") {
+ if !strings.HasSuffix(l.content, "\n") {
fmt.Fprintf(b, "\n\\ No newline at end of file\n")
}
}