dymokomi/luce-ui
Source of release 0.19.1
0.19.1 / src / text / projection_tests.lucb
## Tests for folding and the line projection: allocation failures leave both
## usable, incremental refreshes match a full scan through edits and undo, soft
## wrap breaks lines into word-wrapped visual rows, and wide East Asian scalars
## take two columns in the projection, the caret and click mapping.
import memory
from text.cells import grid
from text.document import Document
from text.folding import FoldRange, Folding
from text.projection import Projection
import text.editing
from text.style import Swatch
struct Tracker: memory.Allocator:
var upstream: memory.Allocator
var live: usize
var remaining: usize?
pub func allocate(size: usize, alignment: usize) -> u8[]?:
if let remaining = self.remaining:
if remaining == 0:
return none
self.remaining = remaining - 1
let block = self.upstream.allocate(size, alignment) else return none
self.live += 1
return block
pub func resize(block: u8[], size: usize) -> bool:
return self.upstream.resize(block, size)
pub func release(block: u8[]):
assert(self.live > 0)
self.live -= 1
self.upstream.release(block)
func restore(previous: memory.Allocator):
memory.heap = previous
## Failed replacement keeps the old fold set and line cache usable. Retry after
## memory pressure must rebuild the edited projection without leaked buffers.
func text_allocations(tracker: Tracker*) -> !:
var document = try Document("head:\n body\nafter")
defer document.close()
var folds = Folding()
defer folds.close()
try folds.replace([FoldRange(0, 15)], &document)
assert(folds.set(0, true))
for stage in 0..<2:
let baseline = tracker.live
tracker.remaining = (usize)stage
var refused = false
folds.replace([FoldRange(0, (i64)document.length())], &document) catch failure:
assert(failure.code == memory.exhausted)
refused = true
tracker.remaining = none
assert(refused and tracker.live == baseline)
assert(folds.values().length == 1 and folds.values()[0].end == 15 and folds.values()[0].collapsed)
var projection = Projection()
defer projection.close()
try projection.refresh(&document, &folds)
assert(projection.visible_count() == 2)
try document.replace(document.length(), document.length(), "\n\t😀")
folds.transform(document.last_change())
for stage in 0..<2:
let baseline = tracker.live
tracker.remaining = (usize)stage
var refused = false
projection.refresh(&document, &folds) catch failure:
assert(failure.code == memory.exhausted)
refused = true
tracker.remaining = none
assert(refused and tracker.live == baseline)
assert(projection.line_count() == 3 and projection.visible_count() == 2)
try projection.refresh(&document, &folds)
assert(projection.line_count() == 4 and projection.visible_count() == 3)
func check_projection(document: const Document*, projection: const Projection*):
var row: usize = 0
var start: usize = 0
var columns: usize = 0
var widest: usize = 0
for offset in 0..<(document.length() + 1):
if offset == document.length() or document.scalar(offset) == 10:
let line = projection.line(row)
assert(line.start == start and line.end == offset and line.columns == columns)
assert(projection.row_at(start) == row and projection.physical_row(row) == row)
if columns > widest:
widest = columns
start = offset + 1
row += 1
columns = 0
else:
columns += grid(document.scalar(offset), columns)
assert(projection.line_count() == row and projection.visible_count() == row)
assert(projection.maximum_columns() == widest)
func projection_edits() -> !:
var document = try Document("α\n\t😀\nlast")
defer document.close()
var folds = Folding()
defer folds.close()
var projection = Projection()
defer projection.close()
let replacements: str[5] = ["\né\n", "", "\t😀", "longer", "\n\n"]
try projection.refresh(&document, &folds)
check_projection(&document, &projection)
for turn in 0..<300:
let start = (usize)(turn * 17) % (document.length() + 1)
let proposed = start + (usize)(turn % 7)
let finish = proposed if proposed < document.length() else document.length()
try document.replace(start, finish, replacements[(usize)turn % replacements.length])
try projection.refresh(&document, &folds)
check_projection(&document, &projection)
if turn % 3 == 0:
assert(try document.undo())
try projection.refresh(&document, &folds)
check_projection(&document, &projection)
assert(try document.redo())
try projection.refresh(&document, &folds)
check_projection(&document, &projection)
func wrapping() -> !:
var document = try Document("aaaa bbbb cccc dddd\nshort\n")
defer document.close()
var folds = Folding()
defer folds.close()
var projection = Projection()
defer projection.close()
try projection.refresh(&document, &folds, 10)
assert(projection.line_count() == 3 and projection.visible_count() == 4)
let first = projection.row(0)
let second = projection.row(1)
assert(first.physical == 0 and first.start == 0 and first.end == 10 and first.column == 0 and first.width == 10 and first.leading)
assert(second.physical == 0 and second.start == 10 and second.end == 19 and second.column == 10 and second.width == 9 and not second.leading)
assert(projection.row(2).physical == 1 and projection.row(2).start == 20 and projection.row(2).leading)
assert(projection.row(3).physical == 2 and projection.row(3).start == 26)
# Every visible scalar lands in the segment that draws it, no wider than the wrap.
for offset in 0..<document.length():
if document.scalar(offset) == 10:
continue
let visual = projection.visual_row(offset)
let segment = projection.row(visual)
assert(segment.start <= offset and offset < segment.end)
assert(segment.physical == projection.row_at(offset) and segment.width <= 10)
# A single word longer than the wrap still advances, one hard break at a time.
try document.set_text("wwwwwwwwwwww\n")
try projection.refresh(&document, &folds, 5)
assert(projection.visible_count() == 4)
assert(projection.row(0).width == 5 and projection.row(1).width == 5 and projection.row(2).width == 2)
assert(projection.row(3).physical == 1 and projection.row(3).start == 13)
# Turning wrap off restores one visual row per physical line.
try projection.refresh(&document, &folds, 0)
assert(projection.visible_count() == 2)
## Mixed ASCII and CJK: each wide scalar is two columns wide, marks none. Columns,
## clicks (`at_x`), vertical moves (`at_column`) and wrapping all agree.
func wide_columns() -> !:
var document = try Document("a田中b\n会議の件\ne\u{301}x")
defer document.close()
var folds = Folding()
defer folds.close()
var projection = Projection()
defer projection.close()
try projection.refresh(&document, &folds)
assert(projection.line(0).columns == 6 and projection.line(1).columns == 8 and projection.line(2).columns == 2)
assert(projection.maximum_columns() == 8)
var chips: Swatch[]
# The column before each offset of "a田中b": a 0, 田 1, 中 3, b 5, end 6.
let expected: usize[5] = [0, 1, 3, 5, 6]
for offset in 0..<5:
assert(editing.column(&document, (usize)offset, chips) == expected[offset])
# "会議の件" starts at offset 5: two columns a scalar.
for offset in 0..<5:
assert(editing.column(&document, 5 + (usize)offset, chips) == (usize)offset * 2)
# A combining mark adds no column.
assert(editing.column(&document, 11, chips) == 1 and editing.column(&document, 12, chips) == 1)
# Clicks land on the nearest boundary; past a wide scalar's middle is after it.
assert(editing.at_x(&document, 0, 0.4, chips) == 0)
assert(editing.at_x(&document, 0, 1.9, chips) == 1)
assert(editing.at_x(&document, 0, 2.1, chips) == 2)
assert(editing.at_x(&document, 0, 3.9, chips) == 2)
assert(editing.at_x(&document, 0, 4.1, chips) == 3)
assert(editing.at_x(&document, 0, 5.6, chips) == 4)
assert(editing.at_x(&document, 0, 40.0, chips) == 4)
assert(editing.at_x(&document, 1, 5.2, chips) == 8)
# A click never parks the caret between a base and its mark.
assert(editing.at_x(&document, 2, 0.8, chips) == 12)
# Vertical moves keep the column or stop before a wide scalar that holds it.
assert(editing.at_column(&document, 1, 3, chips) == 6)
assert(editing.at_column(&document, 1, 4, chips) == 7)
assert(editing.at_column(&document, 0, 2, chips) == 1)
assert(editing.at_column(&document, 2, 1, chips) == 12)
# Wrapping at 7 columns: "aaa田中" fills it exactly; the next 田 starts row two,
# and at 6 the 中 would straddle the edge, so it moves down whole.
try document.set_text("aaa田中田\n")
try projection.refresh(&document, &folds, 7)
assert(projection.visible_count() == 3)
assert(projection.row(0).end == 5 and projection.row(0).width == 7)
assert(projection.row(1).start == 5 and projection.row(1).column == 7 and projection.row(1).width == 2)
try projection.refresh(&document, &folds, 6)
assert(projection.row(0).end == 4 and projection.row(0).width == 5)
assert(projection.row(1).start == 4 and projection.row(1).width == 4)
for index in 0..<projection.visible_count():
assert(projection.row(index).width <= 6)
# A run of wide scalars wraps on cell counts, two columns each.
try document.set_text("会議の件会議の件")
try projection.refresh(&document, &folds, 5)
assert(projection.visible_count() == 4 and projection.row(0).end == 2 and projection.row(0).width == 4)
test "fold and projection allocation failures preserve usable storage":
var tracker = Tracker(upstream = memory.heap)
let previous = memory.heap
memory.heap = &tracker
defer restore(previous)
with tracker:
try text_allocations(&tracker)
assert(tracker.live == 0)
test "incremental line projection matches complete scans through Unicode edits and history":
try projection_edits()
test "soft wrap splits physical lines into word-broken visual rows":
try wrapping()
test "wide scalars take two columns in projection, caret, clicks and wrapping":
try wide_columns()