aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2026-04-12 19:52:06 +0700
committerShulhan <ms@kilabit.info>2026-04-12 19:52:06 +0700
commitc708197eceaee50d29801cff63124a146139e969 (patch)
treecfd55f6d8d8a3ab98c7a71fc463e42317fc65b4d
parent26a02a8396684fcb58f4ba431ae5ae799eb35d68 (diff)
downloadpakakeh.go-c708197eceaee50d29801cff63124a146139e969.tar.xz
text/diff: rename LineChange to LineChunk
In the struct Data we also rename field Changes to LineChunks.
-rw-r--r--lib/text/diff/diff.go23
-rw-r--r--lib/text/diff/line_chunk.go52
-rw-r--r--lib/text/diff/linechange.go49
3 files changed, 65 insertions, 59 deletions
diff --git a/lib/text/diff/diff.go b/lib/text/diff/diff.go
index 1c0ae70d..0f2faac7 100644
--- a/lib/text/diff/diff.go
+++ b/lib/text/diff/diff.go
@@ -20,9 +20,9 @@ type Data struct {
oldlines []Line
newlines []Line
- Adds []Line
- Dels []Line
- Changes []LineChange
+ Adds []Line
+ Dels []Line
+ LineChunks []LineChunk
// Unified contains the result of [Unified] diff, without the label
// ("@@ ... @@") and context lines.
@@ -184,10 +184,10 @@ func Lines(oldlines, newlines []Line, level int) (diff *Data) {
if level == LevelWords {
// Process each changes to find modified chunkes.
- for x, change := range diff.Changes {
+ for x, change := range diff.LineChunks {
adds, dels := Bytes(change.Old.Val, change.New.Val, 0, 0)
- diff.Changes[x].Adds = adds
- diff.Changes[x].Dels = dels
+ diff.LineChunks[x].Adds = adds
+ diff.LineChunks[x].Dels = dels
}
}
@@ -215,7 +215,7 @@ func (diff *Data) checkIsMatched() {
if len(diff.Dels) != 0 {
return
}
- if len(diff.Changes) != 0 {
+ if len(diff.LineChunks) != 0 {
return
}
diff.IsMatched = true
@@ -227,14 +227,17 @@ func (diff *Data) PushAdd(new Line) {
diff.Adds = append(diff.Adds, new)
}
-// PushChange adds the old and new line to Dels, Adds, and [Data.Changes]
+// PushChange adds the old and new line to Dels, Adds, and [Data.LineChunks]
// respectively.
func (diff *Data) PushChange(old, new Line) {
if old.Num != new.Num {
old.NumOther, new.NumOther = new.Num, old.Num
}
- change := NewLineChange(old, new)
- diff.Changes = append(diff.Changes, *change)
+ change := LineChunk{
+ Old: old,
+ New: new,
+ }
+ diff.LineChunks = append(diff.LineChunks, change)
diff.PushDel(old)
diff.PushAdd(new)
}
diff --git a/lib/text/diff/line_chunk.go b/lib/text/diff/line_chunk.go
new file mode 100644
index 00000000..16145883
--- /dev/null
+++ b/lib/text/diff/line_chunk.go
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: BSD-3-Clause
+// SPDX-FileCopyrightText: 2018 Shulhan <ms@kilabit.info>
+
+package diff
+
+import (
+ "fmt"
+ "strings"
+
+ "git.sr.ht/~shulhan/pakakeh.go/lib/text"
+)
+
+// LineChunk represents a changes inside a line.
+// Each chunk in Adds and Dels contains the position and value of string that
+// is added in New or deleted in Old line.
+type LineChunk struct {
+ Adds text.Chunks
+ Dels text.Chunks
+
+ Old Line
+ New Line
+}
+
+// String returns formatted content of LineChunk.
+// The format is as follow,
+//
+// <OldLine>
+// <NewLine>
+// <StartAt>: <Kind><QuotedVal>
+//
+// The OldLine and NewLine print the line being compared, following the format
+// from [Line.String].
+// The StartAt print the beginning position where characters differences found
+// between old and new line.
+// The Kind print '-' for deletion or '+' for deletion.
+// The QuoteVal print the string difference.
+func (lchunk LineChunk) String() string {
+ var sb strings.Builder
+
+ sb.WriteString(lchunk.Old.String())
+ sb.WriteByte('\n')
+ sb.WriteString(lchunk.New.String())
+ sb.WriteByte('\n')
+
+ for _, chunk := range lchunk.Dels {
+ fmt.Fprintf(&sb, "^%-4d: -%q\n", chunk.StartAt, chunk.V)
+ }
+ for _, chunk := range lchunk.Adds {
+ fmt.Fprintf(&sb, "^%-4d: +%q\n", chunk.StartAt, chunk.V)
+ }
+ return sb.String()
+}
diff --git a/lib/text/diff/linechange.go b/lib/text/diff/linechange.go
deleted file mode 100644
index 44425353..00000000
--- a/lib/text/diff/linechange.go
+++ /dev/null
@@ -1,49 +0,0 @@
-// SPDX-License-Identifier: BSD-3-Clause
-// SPDX-FileCopyrightText: 2018 Shulhan <ms@kilabit.info>
-
-package diff
-
-import (
- "fmt"
- "strings"
-
- "git.sr.ht/~shulhan/pakakeh.go/lib/text"
-)
-
-// LineChange represent one change in text.
-type LineChange struct {
- Adds text.Chunks
- Dels text.Chunks
-
- Old Line
- New Line
-}
-
-// NewLineChange create a pointer to new LineChange object.
-func NewLineChange(old, new Line) *LineChange {
- return &LineChange{
- Adds: text.Chunks{},
- Dels: text.Chunks{},
- Old: old,
- New: new,
- }
-}
-
-// String return formatted content of LineChange.
-func (change LineChange) String() string {
- var (
- sb strings.Builder
- chunk text.Chunk
- )
-
- fmt.Fprintf(&sb, "%d - %q\n", change.Old.Num, change.Old.Val)
- fmt.Fprintf(&sb, "%d + %q\n", change.New.Num, change.New.Val)
-
- for _, chunk = range change.Dels {
- fmt.Fprintf(&sb, "^%d - %q\n", chunk.StartAt, chunk.V)
- }
- for _, chunk = range change.Adds {
- fmt.Fprintf(&sb, "^%d + %q\n", chunk.StartAt, chunk.V)
- }
- return sb.String()
-}