oficina / mapa Public

History L9: draw removed blocks before added ones

Between two kept blocks, unpaired additions were drawn first and the
removals after them, the reverse of what diffBlocks says. stretch now
walks the pairs in order and, before each, draws what was removed, then
what was added; a removal falls At the block drawn after it, so the
summary's heading above it is the one it sat under.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 01242893b7db3619ac14773149401319c31d57c2 parent e0f89fb
3 files changed, +49 −31
  • internal/mapa/diff.go +27 −24
  • internal/mapa/diff_test.go +12 −0
  • internal/mapa/versions_test.go +10 −7
diff --git a/internal/mapa/diff.go b/internal/mapa/diff.go
index c13bbe0..7dc71b7 100644
--- a/internal/mapa/diff.go
+++ b/internal/mapa/diff.go
@@ -59,43 +59,45 @@ func diffBlocks(from, to []markup.Block) []blockChange {
// stretch settles the blocks between two matches: removed from the
// version before, and to's blocks start up to end. Each removed block is
// paired, in order, with the next added one alike enough to diff word
-// by word (a changed block); the rest are removed or added, in place.
+// by word (a changed block). Between pairs, the rest are drawn removed,
+// then added; a removal falls At the block drawn after it.
func stretch(removed []blockChange, start, end int,
alike func(was, now int) bool) []blockChange {
- pairs := pairUp(removed, start, end, alike)
+ pairs := append(pairUp(removed, start, end, alike),
+ blockPair{From: math.MaxInt, To: end})
var out []blockChange
- next := 0
- settle := func(before, at int) {
- for ; next < len(removed) && removed[next].From < before; next++ {
- gone := removed[next]
- gone.At = at
- out = append(out, gone)
+ gone, cursor := 0, start
+ for _, pair := range pairs {
+ for ; gone < len(removed) && removed[gone].From < pair.From; gone++ {
+ change := removed[gone]
+ change.At = cursor
+ out = append(out, change)
}
- }
- for index := start; index < end; index++ {
- old, paired := pairs[index]
- if !paired {
+ for ; cursor < pair.To; cursor++ {
out = append(out, blockChange{Kind: blockAdded, From: -1,
- To: index, At: index})
- continue
+ To: cursor, At: cursor})
+ }
+ if pair.To < end {
+ out = append(out, blockChange{Kind: blockChanged,
+ From: pair.From, To: pair.To, At: pair.To})
+ gone, cursor = gone+1, cursor+1
}
- settle(old, index)
- next++
- out = append(out, blockChange{Kind: blockChanged, From: old,
- To: index, At: index})
}
- settle(math.MaxInt, end)
return out
}
+// blockPair is a removed block, From, that changed into an added one.
+type blockPair struct{ From, To int }
+
// maxPairings bounds how many block pairs one stretch compares.
const maxPairings = 2000
-// pairUp maps an added block's index to the removed block it changed:
-// greedily, in order on both sides, so a pairing never crosses another.
+// pairUp is the removed blocks that changed into added ones in start up
+// to end, in order: greedily, in order on both sides, so a pairing
+// never crosses another.
func pairUp(removed []blockChange, start, end int,
- alike func(was, now int) bool) map[int]int {
- pairs := map[int]int{}
+ alike func(was, now int) bool) []blockPair {
+ var pairs []blockPair
cursor, tries := start, 0
for _, gone := range removed {
for candidate := cursor; candidate < end; candidate++ {
@@ -103,7 +105,8 @@ func pairUp(removed []blockChange, start, end int,
return pairs
}
if alike(gone.From, candidate) {
- pairs[candidate] = gone.From
+ pairs = append(pairs, blockPair{From: gone.From,
+ To: candidate})
cursor = candidate + 1
break
}
diff --git a/internal/mapa/diff_test.go b/internal/mapa/diff_test.go
index f8a5f03..8462323 100644
--- a/internal/mapa/diff_test.go
+++ b/internal/mapa/diff_test.go
@@ -125,3 +125,15 @@ func TestChangedBlocksDrawEmphasisAndNumbers(t *testing.T) {
t.Fatalf("got %s", got)
}
}
+
+// Review L9: between two kept blocks, what was removed is drawn before
+// what was added, as diffBlocks says.
+func TestRemovedBlocksAreDrawnBeforeAddedOnes(t *testing.T) {
+ got := diffOf(t, "Kept.\n\nAn old line about budgets.\n\nAlso kept.",
+ "Kept.\n\n# Something new\n\n- Another\n\nAlso kept.")
+ removed := strings.Index(got, `mapa-diff="removed"`)
+ added := strings.Index(got, `mapa-diff="added"`)
+ if removed < 0 || added < 0 || removed > added {
+ t.Fatalf("removed is not drawn before added:\n%s", got)
+ }
+}
diff --git a/internal/mapa/versions_test.go b/internal/mapa/versions_test.go
index f975fd2..9e44fc3 100644
--- a/internal/mapa/versions_test.go
+++ b/internal/mapa/versions_test.go
@@ -78,19 +78,22 @@ func TestALongPagesHistoryIsBounded(t *testing.T) {
}
// Past the bound, a chosen version's diff is coarse: the changed middle
-// removed and added whole, between the blocks the versions share.
-func TestATooLargeChangeIsRemovedAndAddedWhole(t *testing.T) {
+// removed and added whole, between the blocks the versions share, the
+// removed first (review L9).
+func TestATooLargeChangeIsRemovedThenAddedWhole(t *testing.T) {
old := markup.Parse("Kept first.\n\n" + longList(1100,
func(int) string { return "old" }) + "\nKept last.")
now := markup.Parse("Kept first.\n\n" + longList(1100,
func(int) string { return "new" }) + "\nKept last.")
- counts := map[string]int{}
+ var kinds []string
for _, change := range diffBlocks(old, now) {
- counts[change.Kind]++
+ if len(kinds) == 0 || kinds[len(kinds)-1] != change.Kind {
+ kinds = append(kinds, change.Kind)
+ }
}
- if counts[blockSame] != 2 || counts[blockRemoved] != 1100 ||
- counts[blockAdded] != 1100 || counts[blockChanged] != 0 {
- t.Fatalf("changes %v", counts)
+ want := []string{blockSame, blockRemoved, blockAdded, blockSame}
+ if strings.Join(kinds, " ") != strings.Join(want, " ") {
+ t.Fatalf("runs %q, want %q", kinds, want)
}
}