Every scroll<->content mapping site (window start, sub-line shift, tap
mapping, max-scroll clamp, selection menu/handle positions) assumed
1 logical line = 1 visual line. When the viewport top crossed the
bottom of a wrapped line, the view jumped past the wrapped remainder
(jump magnitude (count-1)*lh) instead of moving pixel-by-pixel.
- WrapIndex (internal/editor/wrap_index.go): Fenwick tree of
per-logical-line visual-line counts, parallel to the LineIndex;
built at index-build time, bookkept by the same
UpdateLineIndexAfter{Insert,Delete} hooks (never under-stale: every
touched line resets to the estimate, the next shaping pass
re-corrects it).
- scrollVisualDecompose: the scroll offset lives in visual-line space:
k = LineForVisual(floor(s/lh)), r = s - V(k)*lh. All mapping sites
go through it, so the viewport top is always exactly s into the
document's visual space (V(k)*lh + r = s) — the jump invariant.
All-ones index reduces to the legacy 1:1 mapping (pre-shaping and
non-wrapped behavior unchanged by construction).
- Correction pipeline: the renderer's per-frame VisualLineStarts are
grouped per logical line and written back (applyWrapCounts). The
layout feedback now carries the exact window text the layout was
shaped for (carried in the frame) plus the window start line and the
content-edit counter; corrections apply only on edit-counter match,
and grouping over the current window text (wrong after a scroll moved
the window) is no longer possible.
- bytePosToScreenXY now applies the sub-line shift and the scaled line
pitch: the selection menu/handles were off by up to a full line.
- maxScroll uses TotalVisuals() with the effective (font-scaled) line
height; the bottom clamp lands exactly on the file end for wrapped
content.
- VisibleByteRange returns the real start line (was hardcoded 0).
- emitFrame: replace the unread handoff frame with the newer snapshot
instead of dropping it — a dropped final frame was never re-emitted
(emission is event-driven), leaving the consumer one state behind
forever; fixes the pre-existing TestRealFile_ShiftSelectionInsert
failure. Still non-blocking.
Tests (mutation-verified where practical): wrap_index_test.go (Fenwick
vs naive model, 3000 ops), wrap_bookkeeping_test.go (edit hooks vs
shadow-string oracle, 400 ops — caught a real m=0 under-marking),
wrap_mapping_test.go (the jump regression: V(k)*lh + r == s over sweeps
+ random offsets; legacy-identity pin; boundary sweep), wrap_apply_test.go
(VisualLineStarts grouping + guards — the first version exposed the
always-true WindowStartByte guard that blocked all post-scroll
corrections). go test -race ./... green.
On-device (emulator, 60 wrapped lines): dp sweep 0/17/50/67/134/340
lands on LINE000-vl0/1/3, LINE001-vl0, LINE002-vl0, LINE005-vl0 —
pixel-exact 1:1, no jump (dp 134 is where the old code jumped to
LINE008); bottom clamp exact.
Docs: architecture.md §6.2 (visual-line space invariant),
development_plan.md (Phase 13), spec.md (wrap + clamp lines).
141 lines
4.7 KiB
Go
141 lines
4.7 KiB
Go
package editor
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"strings"
|
|
"testing"
|
|
|
|
"pad/internal/io/pool/types"
|
|
)
|
|
|
|
// TestWrapIndex_EditBookkeeping verifies the WrapIndex maintenance inside
|
|
// UpdateLineIndexAfterInsert/Delete against an independent oracle built from
|
|
// a shadow copy of the file content.
|
|
//
|
|
// The oracle works on the shadow's lines:
|
|
// - after each edit, the surviving lines are identified by maximal
|
|
// prefix/suffix content alignment (a single contiguous edit has a unique
|
|
// changed block);
|
|
// - every line inside the changed block must have been reset to the
|
|
// estimate (1) — missing that stamp leaves a stale wrap count, which is
|
|
// exactly the scroll-jump bug this whole mechanism exists to prevent;
|
|
// - surviving lines must keep their pre-edit count (or be conservatively
|
|
// re-stamped to 1, which the next shaping pass corrects).
|
|
//
|
|
// Structural invariant checked every op: WrapIndex.Len() ==
|
|
// LineIndex.LineCount() (the two indexes must track the same line set).
|
|
func TestWrapIndex_EditBookkeeping(t *testing.T) {
|
|
// Distinct-ish line contents so content alignment is reliable.
|
|
words := []string{"alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel"}
|
|
var named []string
|
|
for i := 0; i < 40; i++ {
|
|
named = append(named, fmt.Sprintf("%s-%d-%s", words[i%len(words)], i, words[(i*3)%len(words)]))
|
|
}
|
|
content := strings.Join(named, "\n") + "\n"
|
|
// The LineIndex convention: a trailing "\n" opens an empty trailing line
|
|
// ("a\nb\n" has three lines: "a", "b", ""). strings.Split matches it.
|
|
lines := strings.Split(content, "\n")
|
|
|
|
cb := NewChunkedBuffer("/bookkeeping.txt", DefaultChunkSize, nil, "")
|
|
cb.SetContent([]byte(content))
|
|
offsets := []int32{0}
|
|
for i := 0; i < len(content); i++ {
|
|
if content[i] == '\n' {
|
|
offsets = append(offsets, int32(i+1))
|
|
}
|
|
}
|
|
cb.LineIndex = types.NewLineIndex(offsets, 0, int64(len(content)))
|
|
w := NewWrapIndex(len(lines))
|
|
// Seed with a non-trivial pattern so a lost/shifted count is visible.
|
|
for i := range lines {
|
|
w.Set(i, int32(1+(i*7)%5))
|
|
}
|
|
cb.WrapIndex = w
|
|
|
|
rng := rand.New(rand.NewSource(99))
|
|
texts := []string{"x", "xy", "x\ny", "x\ny\nz", "\n", "a\nb", "no-newline-here"}
|
|
|
|
for op := 0; op < 400; op++ {
|
|
// Snapshot pre-edit state.
|
|
oldCounts := make([]int32, w.Len())
|
|
for i := range oldCounts {
|
|
oldCounts[i] = w.Get(i)
|
|
}
|
|
oldLines := splitLines(content)
|
|
nBefore := w.Len()
|
|
|
|
var newContent string
|
|
if op%2 == 0 {
|
|
// Insert
|
|
pos := rng.Intn(len(content) + 1)
|
|
text := texts[rng.Intn(len(texts))]
|
|
content = content[:pos] + text + content[pos:]
|
|
cb.Insert(pos, text)
|
|
cb.UpdateLineIndexAfterInsert(pos, text)
|
|
newContent = content
|
|
} else {
|
|
// Delete
|
|
a := rng.Intn(len(content))
|
|
b := a + 1 + rng.Intn(min(8, len(content)-a))
|
|
content = content[:a] + content[b:]
|
|
cb.Delete(a, b-a)
|
|
cb.UpdateLineIndexAfterDelete(a, b)
|
|
newContent = content
|
|
}
|
|
|
|
// Structural: the wrap index must track the line index.
|
|
if w.Len() != cb.LineIndex.LineCount() {
|
|
t.Fatalf("op %d: WrapIndex.Len() = %d, LineIndex.LineCount() = %d", op, w.Len(), cb.LineIndex.LineCount())
|
|
}
|
|
newLines := splitLines(newContent)
|
|
if w.Len() != len(newLines) {
|
|
t.Fatalf("op %d: WrapIndex.Len() = %d, shadow lines = %d", op, w.Len(), len(newLines))
|
|
}
|
|
|
|
// Oracle: maximal prefix/suffix alignment of old vs new lines.
|
|
j := 0
|
|
for j < len(oldLines) && j < len(newLines) && oldLines[j] == newLines[j] {
|
|
j++
|
|
}
|
|
d := len(newLines) - len(oldLines)
|
|
tail := 0
|
|
for tail < len(newLines)-j && j+tail < len(newLines) && newLines[len(newLines)-1-tail] == oldLines[len(oldLines)-1-tail] {
|
|
tail++
|
|
}
|
|
// Survivors: new[0:j] == old[0:j]; new[len-tail:] == old[len_old-tail:].
|
|
// Changed block: new[j : len(new)-tail].
|
|
for i := 0; i < len(newLines); i++ {
|
|
var want int32
|
|
var survivorOld int // -1 = changed block
|
|
if i < j {
|
|
survivorOld = i
|
|
} else if i >= len(newLines)-tail {
|
|
survivorOld = i - d
|
|
} else {
|
|
survivorOld = -1
|
|
}
|
|
got := w.Get(i)
|
|
if survivorOld < 0 {
|
|
// Changed line: must have been reset to the estimate.
|
|
if got != 1 {
|
|
t.Fatalf("op %d: line %d changed (content %q) but count = %d, want 1 (stale count would cause a scroll jump)", op, i, newLines[i], got)
|
|
}
|
|
} else {
|
|
want = oldCounts[survivorOld]
|
|
if got != want && got != 1 {
|
|
t.Fatalf("op %d: line %d is a survivor (content %q) but count = %d, want %d (or 1)", op, i, newLines[i], got, want)
|
|
}
|
|
}
|
|
}
|
|
_ = nBefore
|
|
}
|
|
}
|
|
|
|
// splitLines splits content into its logical lines using the LineIndex
|
|
// convention: each '\n' opens a new line, so a trailing '\n' leaves an empty
|
|
// trailing line ("a\nb\n" -> ["a","b",""] and "" -> [""]).
|
|
func splitLines(content string) []string {
|
|
return strings.Split(content, "\n")
|
|
}
|