avoid saving fragments in lines

Now we render lines one screen line at a time rather than one word at a
time.

I can't port the source side just yet; I need to fix hyperlinks first..
This commit is contained in:
Kartik K. Agaram 2023-04-01 20:03:43 -07:00
parent d0d39797cf
commit 29f1687f3c
3 changed files with 49 additions and 70 deletions

View File

@ -373,7 +373,7 @@ end
-- prepend file/line/test -- prepend file/line/test
function prepend_debug_info_to_test_failure(test_name, err) function prepend_debug_info_to_test_failure(test_name, err)
local err_without_line_number = err:gsub('^[^:]*:[^:]*: ', '') local err_without_line_number = err:gsub('^[^:]*:[^:]*: ', '')
local stack_trace = debug.traceback('', --[[stack frame]]4) local stack_trace = debug.traceback('', --[[stack frame]]5)
local file_and_line_number = stack_trace:gsub('stack traceback:\n', ''):gsub(': .*', '') local file_and_line_number = stack_trace:gsub('stack traceback:\n', ''):gsub(': .*', '')
local full_error = file_and_line_number..':'..test_name..' -- '..err_without_line_number local full_error = file_and_line_number..':'..test_name..' -- '..err_without_line_number
--? local full_error = file_and_line_number..':'..test_name..' -- '..err_without_line_number..'\t\t'..stack_trace:gsub('\n', '\n\t\t') --? local full_error = file_and_line_number..':'..test_name..' -- '..err_without_line_number..'\t\t'..stack_trace:gsub('\n', '\n\t\t')

115
text.lua
View File

@ -4,63 +4,64 @@ Text = {}
-- draw a line starting from startpos to screen at y between State.left and State.right -- draw a line starting from startpos to screen at y between State.left and State.right
-- return the final y, and position of start of final screen line drawn -- return the final y, and position of start of final screen line drawn
function Text.draw(State, line_index, y, startpos) function Text.draw(State, line_index, y, startpos)
--? print('text.draw', line_index, y)
local line = State.lines[line_index] local line = State.lines[line_index]
local line_cache = State.line_cache[line_index] local line_cache = State.line_cache[line_index]
line_cache.starty = y line_cache.starty = y
line_cache.startpos = startpos line_cache.startpos = startpos
-- wrap long lines -- wrap long lines
local x = State.left local final_screen_line_starting_pos = startpos -- track value to return
local pos = 1
local screen_line_starting_pos = startpos
Text.populate_screen_line_starting_pos(State, line_index) Text.populate_screen_line_starting_pos(State, line_index)
for _, f in ipairs(line_cache.fragments) do assert(#line_cache.screen_line_starting_pos >= 1)
App.color(Text_color) for i=1,#line_cache.screen_line_starting_pos do
local frag_len = utf8.len(f) local pos = line_cache.screen_line_starting_pos[i]
--? print('text.draw:', f, 'at', line_index,pos, 'after', x,y)
if pos < startpos then if pos < startpos then
-- render nothing -- render nothing
--? print('skipping', f) --? print('skipping', f)
else else
final_screen_line_starting_pos = pos
local f = Text.screen_line(line, line_cache, i)
--? print('text.draw:', f, 'at', line_index,pos, 'after', x,y)
local frag_len = utf8.len(f)
-- render fragment -- render fragment
local frag_width = App.width(f)
if x + frag_width > State.right then
assert(x > State.left) -- no overfull lines
y = y + State.line_height
if y + State.line_height > App.screen.height then
return y, screen_line_starting_pos
end
screen_line_starting_pos = pos
x = State.left
end
if State.selection1.line then if State.selection1.line then
local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len) local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
Text.draw_highlight(State, line, x,y, pos, lo,hi) Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
end end
App.screen.print(f, x,y) App.color(Text_color)
App.screen.print(f, State.left,y)
-- render cursor if necessary -- render cursor if necessary
if line_index == State.cursor1.line then if line_index == State.cursor1.line then
if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
if State.search_term then if State.search_term then
if State.lines[State.cursor1.line].data:sub(State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term)-1) == State.search_term then if State.lines[State.cursor1.line].data:sub(State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term)-1) == State.search_term then
local lo_px = Text.draw_highlight(State, line, x,y, pos, State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term)) local lo_px = Text.draw_highlight(State, line, State.left,y, pos, State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term))
App.color(Text_color) App.color(Text_color)
love.graphics.print(State.search_term, x+lo_px,y) love.graphics.print(State.search_term, State.left+lo_px,y)
end end
else else
Text.draw_cursor(State, x+Text.x(f, State.cursor1.pos-pos+1), y) Text.draw_cursor(State, State.left+Text.x(f, State.cursor1.pos-pos+1), y)
end end
end end
end end
x = x + frag_width y = y + State.line_height
end if y >= App.screen.height then
pos = pos + frag_len break
end end
if State.search_term == nil then
if line_index == State.cursor1.line and State.cursor1.pos == pos then
Text.draw_cursor(State, x, y)
end end
end end
return y, screen_line_starting_pos return y - State.line_height, final_screen_line_starting_pos
end
function Text.screen_line(line, line_cache, i)
local pos = line_cache.screen_line_starting_pos[i]
local offset = Text.offset(line.data, pos)
if i >= #line_cache.screen_line_starting_pos then
return line.data:sub(offset)
end
local endpos = line_cache.screen_line_starting_pos[i+1]-1
local end_offset = Text.offset(line.data, endpos)
return line.data:sub(offset, end_offset)
end end
function Text.draw_cursor(State, x, y) function Text.draw_cursor(State, x, y)
@ -81,55 +82,34 @@ function Text.populate_screen_line_starting_pos(State, line_index)
if line_cache.screen_line_starting_pos then if line_cache.screen_line_starting_pos then
return return
end end
-- duplicate some logic from Text.draw
Text.compute_fragments(State, line_index)
line_cache.screen_line_starting_pos = {1} line_cache.screen_line_starting_pos = {1}
local x = State.left local x = 0
local pos = 1 local pos = 1
for _, f in ipairs(line_cache.fragments) do
-- render fragment
local frag_width = App.width(f)
if x + frag_width > State.right then
x = State.left
table.insert(line_cache.screen_line_starting_pos, pos)
end
x = x + frag_width
pos = pos + utf8.len(f)
end
end
function Text.compute_fragments(State, line_index)
local line = State.lines[line_index]
if line.mode ~= 'text' then return end
local line_cache = State.line_cache[line_index]
if line_cache.fragments then
return
end
line_cache.fragments = {}
local x = State.left
-- try to wrap at word boundaries -- try to wrap at word boundaries
for frag in line.data:gmatch('%S*%s*') do for frag in line.data:gmatch('%S*%s*') do
local frag_width = App.width(frag) local frag_width = App.width(frag)
while x + frag_width > State.right do --? print('-- frag:', frag, pos, x, frag_width, State.width)
if (x-State.left) < 0.8 * (State.right-State.left) then while x + frag_width > State.width do
--? print('frag:', frag, pos, x, frag_width, State.width)
if x < 0.8 * State.width then
-- long word; chop it at some letter -- long word; chop it at some letter
-- We're not going to reimplement TeX here. -- We're not going to reimplement TeX here.
local bpos = Text.nearest_pos_less_than(frag, State.right - x) local bpos = Text.nearest_pos_less_than(frag, State.width - x)
if bpos == 0 then break end -- avoid infinite loop when window is too narrow -- everything works if bpos == 0, but is a little inefficient
pos = pos + bpos
local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos
local frag1 = string.sub(frag, 1, boffset-1)
local frag1_width = App.width(frag1)
assert(x + frag1_width <= State.right)
table.insert(line_cache.fragments, frag1)
frag = string.sub(frag, boffset) frag = string.sub(frag, boffset)
--? if bpos > 0 then
--? print('after chop:', frag)
--? end
frag_width = App.width(frag) frag_width = App.width(frag)
end end
x = State.left -- new line --? print('screen line:', pos)
end table.insert(line_cache.screen_line_starting_pos, pos)
if #frag > 0 then x = 0 -- new screen line
table.insert(line_cache.fragments, frag)
end end
x = x + frag_width x = x + frag_width
pos = pos + utf8.len(frag)
end end
end end
@ -974,7 +954,6 @@ function Text.redraw_all(State)
end end
function Text.clear_screen_line_cache(State, line_index) function Text.clear_screen_line_cache(State, line_index)
State.line_cache[line_index].fragments = nil
State.line_cache[line_index].screen_line_starting_pos = nil State.line_cache[line_index].screen_line_starting_pos = nil
end end

View File

@ -1007,7 +1007,7 @@ function test_pagedown_can_start_from_middle_of_long_wrapping_line()
y = y + Editor_state.line_height y = y + Editor_state.line_height
App.screen.check(y, 'jkl ', 'screen:2') App.screen.check(y, 'jkl ', 'screen:2')
y = y + Editor_state.line_height y = y + Editor_state.line_height
App.screen.check(y, 'mno ', 'screen:3') App.screen.check(y, 'mn', 'screen:3')
end end
function test_pagedown_never_moves_up() function test_pagedown_never_moves_up()