Merge text.love

This commit is contained in:
Kartik K. Agaram 2023-11-18 12:49:34 -08:00
commit c3effcc850
5 changed files with 28 additions and 35 deletions

View File

@ -105,14 +105,8 @@ end
-- return y drawn until
function edit.draw(State)
App.color(Text_color)
if #State.lines ~= #State.line_cache then
print(('line_cache is out of date; %d when it should be %d'):format(#State.line_cache, #State.lines))
assert(false)
end
if not Text.le1(State.screen_top1, State.cursor1) then
print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
assert(false)
end
assert(#State.lines == #State.line_cache, ('line_cache is out of date; %d elements when it should be %d'):format(#State.line_cache, #State.lines))
assert(Text.le1(State.screen_top1, State.cursor1), ('screen_top (line=%d,pos=%d) is below cursor (line=%d,pos=%d)'):format(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos))
State.cursor_x = nil
State.cursor_y = nil
local y = State.top

View File

@ -139,7 +139,7 @@ function rfind(s, pat, i, plain)
local rendpos = rs:find(rpat, ri, plain)
if rendpos == nil then return nil end
local endpos = #s - rendpos + 1
assert (endpos >= #pat)
assert (endpos >= #pat, ('rfind: endpos %d should be >= #pat %d at this point'):format(endpos, #pat))
return endpos-#pat+1
end

View File

@ -33,13 +33,13 @@ function Text.clip_selection(State, line_index, apos, bpos)
-- fully contained
return apos,bpos
elseif a_ge then
assert(maxl == line_index)
assert(maxl == line_index, ('maxl %d not equal to line_index %d'):format(maxl, line_index))
return apos,maxp
elseif b_lt then
assert(minl == line_index)
assert(minl == line_index, ('minl %d not equal to line_index %d'):format(minl, line_index))
return minp,bpos
else
assert(minl == maxl and minl == line_index)
assert(minl == maxl and minl == line_index, ('minl %d, maxl %d and line_index %d are not all equal'):format(minl, maxl, line_index))
return minp,maxp
end
end
@ -125,7 +125,7 @@ function Text.delete_selection_without_undo(State)
State.lines[minl].data = State.lines[minl].data:sub(1, min_offset-1)..State.lines[minl].data:sub(max_offset)
return
end
assert(minl < maxl)
assert(minl < maxl, ('minl %d not < maxl %d'):format(minl, maxl))
local rhs = State.lines[maxl].data:sub(max_offset)
for i=maxl,minl+1,-1 do
table.remove(State.lines, i)
@ -152,7 +152,7 @@ function Text.selection(State)
if minl == maxl then
return State.lines[minl].data:sub(min_offset, max_offset-1)
end
assert(minl < maxl)
assert(minl < maxl, ('minl %d not < maxl %d'):format(minl, maxl))
local result = {State.lines[minl].data:sub(min_offset)}
for i=minl+1,maxl-1 do
table.insert(result, State.lines[i].data)

View File

@ -17,7 +17,7 @@ function Text.draw(State, line_index, y, startpos)
-- wrap long lines
local final_screen_line_starting_pos = startpos -- track value to return
Text.populate_screen_line_starting_pos(State, line_index)
assert(#line_cache.screen_line_starting_pos >= 1)
assert(#line_cache.screen_line_starting_pos >= 1, 'line cache missing screen line info')
for i=1,#line_cache.screen_line_starting_pos do
local pos = line_cache.screen_line_starting_pos[i]
if pos < startpos then
@ -210,7 +210,7 @@ function Text.keychord_press(State, chord)
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
end
Text.clear_screen_line_cache(State, State.cursor1.line)
assert(Text.le1(State.screen_top1, State.cursor1))
assert(Text.le1(State.screen_top1, State.cursor1), ('screen_top (line=%d,pos=%d) is below cursor (line=%d,pos=%d)'):format(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos))
schedule_save(State)
record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
elseif chord == 'delete' then
@ -403,7 +403,7 @@ function Text.up(State)
end
else
-- move up one screen line in current line
assert(screen_line_index > 1)
assert(screen_line_index > 1, 'bumped up against top screen line in line')
local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index-1]
local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
@ -421,7 +421,7 @@ end
function Text.down(State)
--? print('down', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
assert(State.cursor1.pos)
assert(State.cursor1.pos, 'cursor has no pos')
if Text.cursor_at_final_screen_line(State) then
-- line is done, skip to next text line
--? print('cursor at final screen line of its line')
@ -489,7 +489,7 @@ function Text.word_left(State)
if State.cursor1.pos == 1 then
break
end
assert(State.cursor1.pos > 1)
assert(State.cursor1.pos > 1, 'bumped up against start of line')
if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos-1, '%s') then
break
end
@ -523,9 +523,8 @@ end
function Text.match(s, pos, pat)
local start_offset = Text.offset(s, pos)
assert(start_offset)
local end_offset = Text.offset(s, pos+1)
assert(end_offset > start_offset)
assert(end_offset > start_offset, ('end_offset %d not > start_offset %d'):format(end_offset, start_offset))
local curr = s:sub(start_offset, end_offset-1)
return curr:match(pat)
end
@ -572,7 +571,7 @@ function Text.pos_at_start_of_screen_line(State, loc1)
return spos,i
end
end
assert(false)
assert(false, ('invalid pos %d'):format(loc1.pos))
end
function Text.pos_at_end_of_screen_line(State, loc1)
@ -586,7 +585,7 @@ function Text.pos_at_end_of_screen_line(State, loc1)
end
most_recent_final_pos = spos-1
end
assert(false)
assert(false, ('invalid pos %d'):format(loc1.pos))
end
function Text.cursor_at_final_screen_line(State)
@ -644,7 +643,7 @@ end
function Text.to_pos_on_line(State, line_index, mx, my)
local line = State.lines[line_index]
local line_cache = State.line_cache[line_index]
assert(my >= line_cache.starty)
assert(my >= line_cache.starty, 'failed to map y pixel to line')
-- duplicate some logic from Text.draw
local y = line_cache.starty
local start_screen_line_index = Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos)
@ -667,7 +666,7 @@ function Text.to_pos_on_line(State, line_index, mx, my)
end
y = nexty
end
assert(false)
assert(false, 'failed to map y pixel to line')
end
function Text.screen_line_width(State, line_index, i)
@ -733,7 +732,7 @@ function Text.nearest_cursor_pos(line, x, left)
leftpos = curr
end
end
assert(false)
assert(false, 'failed to map x pixel to pos')
end
-- return the nearest index of line (in utf8 code points) which lies entirely
@ -764,7 +763,7 @@ function Text.nearest_pos_less_than(line, x)
left = curr
end
end
assert(false)
assert(false, 'failed to map x pixel to pos')
end
function Text.x_after(s, pos)
@ -792,7 +791,7 @@ function Text.to2(State, loc1)
break
end
end
assert(result.screen_pos)
assert(result.screen_pos, 'failed to convert schema-1 coordinate to schema-2')
return result
end
@ -834,7 +833,7 @@ function Text.offset(s, pos1)
if result == nil then
print(pos1, #s, s)
end
assert(result)
assert(result, "Text.offset returned nil; this is likely a failure to handle utf8")
return result
end

View File

@ -36,11 +36,11 @@ end
-- Make copies of objects; the rest of the app may mutate them in place, but undo requires immutable histories.
function snapshot(State, s,e)
-- Snapshot everything by default, but subset if requested.
assert(s)
assert(s, 'failed to snapshot operation for undo history')
if e == nil then
e = s
end
assert(#State.lines > 0)
assert(#State.lines > 0, 'failed to snapshot operation for undo history')
if s < 1 then s = 1 end
if s > #State.lines then s = #State.lines end
if e < 1 then e = 1 end
@ -71,22 +71,22 @@ function patch(lines, from, to)
--? lines[from.start_line] = to.lines[1]
--? return
--? end
assert(from.start_line == to.start_line)
assert(from.start_line == to.start_line, 'failed to patch undo operation')
for i=from.end_line,from.start_line,-1 do
table.remove(lines, i)
end
assert(#to.lines == to.end_line-to.start_line+1)
assert(#to.lines == to.end_line-to.start_line+1, 'failed to patch undo operation')
for i=1,#to.lines do
table.insert(lines, to.start_line+i-1, to.lines[i])
end
end
function patch_placeholders(line_cache, from, to)
assert(from.start_line == to.start_line)
assert(from.start_line == to.start_line, 'failed to patch undo operation')
for i=from.end_line,from.start_line,-1 do
table.remove(line_cache, i)
end
assert(#to.lines == to.end_line-to.start_line+1)
assert(#to.lines == to.end_line-to.start_line+1, 'failed to patch undo operation')
for i=1,#to.lines do
table.insert(line_cache, to.start_line+i-1, {})
end