Skip to content

Commit

Permalink
internal/diff: unexport various identifiers
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
adonovan committed May 25, 2023
1 parent 827f5aa commit ed90c6d
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 67 deletions.
34 changes: 28 additions & 6 deletions internal/diff/myers/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func ComputeEdits(before, after string) []diff.Edit {
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})
Expand All @@ -45,8 +45,30 @@ func ComputeEdits(before, after string) []diff.Edit {
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)
Expand All @@ -72,7 +94,7 @@ func operations(a, b []string) []*operation {
return
}
op.I2 = i2
if op.Kind == diff.Insert {
if op.Kind == opInsert {
op.Content = b[op.J1:j2]
}
solution[i] = op
Expand All @@ -88,7 +110,7 @@ func operations(a, b []string) []*operation {
for snake[0]-snake[1] > x-y {
if op == nil {
op = &operation{
Kind: diff.Delete,
Kind: opDelete,
I1: x,
J1: y,
}
Expand All @@ -104,7 +126,7 @@ func operations(a, b []string) []*operation {
for snake[0]-snake[1] < x-y {
if op == nil {
op = &operation{
Kind: diff.Insert,
Kind: opInsert,
I1: x,
J1: y,
}
Expand Down
121 changes: 60 additions & 61 deletions internal/diff/unified.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,58 +36,57 @@ func ToUnified(oldLabel, newLabel, content string, edits []Edit) (string, error)

// 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")
Expand All @@ -103,8 +102,8 @@ const (
// 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
Expand Down Expand Up @@ -138,21 +137,21 @@ func toUnified(fromName, toName string, content string, edits []Edit) (unified,
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 != "" {
Expand All @@ -163,18 +162,18 @@ func toUnified(fromName, toName string, content string, edits []Edit) (unified,
// 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
}
Expand All @@ -196,7 +195,7 @@ func addEqualLines(h *hunk, lines []string, start, end int) int {
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
Expand All @@ -205,19 +204,19 @@ func addEqualLines(h *hunk, lines []string, start, end int) int {
// 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++
Expand All @@ -226,32 +225,32 @@ func (u unified) String() string {
}
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")
}
}
Expand Down

0 comments on commit ed90c6d

Please sign in to comment.