Merge driver.love

This commit is contained in:
Kartik K. Agaram 2024-07-09 23:05:14 -07:00
commit 90c125641b
4 changed files with 12 additions and 27 deletions

View File

@ -277,7 +277,7 @@ function edit.keychord_press(State, chord, key, readonly)
State.cursor1 = State.search_backup.cursor State.cursor1 = State.search_backup.cursor
State.screen_top1 = State.search_backup.screen_top State.screen_top1 = State.search_backup.screen_top
State.search_backup = nil State.search_backup = nil
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
elseif chord == 'return' then elseif chord == 'return' then
State.search_term = nil State.search_term = nil
State.search_backup = nil State.search_backup = nil
@ -322,9 +322,7 @@ function edit.keychord_press(State, chord, key, readonly)
State.cursor1 = deepcopy(src.cursor) State.cursor1 = deepcopy(src.cursor)
State.selection1 = deepcopy(src.selection) State.selection1 = deepcopy(src.selection)
patch(State.lines, event.after, event.before) patch(State.lines, event.after, event.before)
patch_placeholders(State.line_cache, event.after, event.before) Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
-- if we're scrolling, reclaim all fragments to avoid memory leaks
Text.redraw_all(State)
schedule_save(State) schedule_save(State)
end end
elseif not readonly and chord == 'C-y' then elseif not readonly and chord == 'C-y' then
@ -335,8 +333,7 @@ function edit.keychord_press(State, chord, key, readonly)
State.cursor1 = deepcopy(src.cursor) State.cursor1 = deepcopy(src.cursor)
State.selection1 = deepcopy(src.selection) State.selection1 = deepcopy(src.selection)
patch(State.lines, event.before, event.after) patch(State.lines, event.before, event.after)
-- if we're scrolling, reclaim all fragments to avoid memory leaks Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
Text.redraw_all(State)
schedule_save(State) schedule_save(State)
end end
-- clipboard -- clipboard

View File

@ -386,6 +386,7 @@ function live.handle_error(err)
local callstack = debug.traceback('', --[[stack frame]]2) local callstack = debug.traceback('', --[[stack frame]]2)
local cleaned_up_error = 'Error: ' .. cleaned_up_frame(tostring(err))..'\n'..cleaned_up_callstack(callstack) local cleaned_up_error = 'Error: ' .. cleaned_up_frame(tostring(err))..'\n'..cleaned_up_callstack(callstack)
live.send_run_time_error_to_driver(cleaned_up_error) live.send_run_time_error_to_driver(cleaned_up_error)
love.graphics.setFont(love.graphics.newFont(20))
Error_message = 'Something is wrong. Sorry!\n\n'..cleaned_up_error..'\n\n'.. Error_message = 'Something is wrong. Sorry!\n\n'..cleaned_up_error..'\n\n'..
"(Note: function names above don't include outer tables. So functions like on.draw might show up as just 'draw', etc.)\n\n".. "(Note: function names above don't include outer tables. So functions like on.draw might show up as just 'draw', etc.)\n\n"..
'Options:\n'.. 'Options:\n'..

View File

@ -231,7 +231,7 @@ function Text.keychord_press(State, chord, readonly)
line=State.cursor1.line, line=State.cursor1.line,
pos=Text.pos_at_start_of_screen_line(State, State.cursor1), pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
} }
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
end end
Text.clear_screen_line_cache(State, State.cursor1.line) Text.clear_screen_line_cache(State, State.cursor1.line)
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)) 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))
@ -368,7 +368,7 @@ function Text.pageup(State)
State.screen_top1 = Text.previous_screen_top1(State) State.screen_top1 = Text.previous_screen_top1(State)
State.cursor1 = deepcopy(State.screen_top1) State.cursor1 = deepcopy(State.screen_top1)
Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State) Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
end end
-- return the top y coordinate of a given line_index, -- return the top y coordinate of a given line_index,
@ -406,7 +406,7 @@ function Text.pagedown(State)
State.screen_top1 = Text.screen_bottom1(State) State.screen_top1 = Text.screen_bottom1(State)
State.cursor1 = deepcopy(State.screen_top1) State.cursor1 = deepcopy(State.screen_top1)
Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State) Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
end end
-- return the location of the start of the bottom-most line on screen -- return the location of the start of the bottom-most line on screen
@ -459,7 +459,7 @@ function Text.up(State)
line=State.cursor1.line, line=State.cursor1.line,
pos=Text.pos_at_start_of_screen_line(State, State.cursor1), pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
} }
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
end end
end end
@ -588,7 +588,7 @@ function Text.left(State)
line=State.cursor1.line, line=State.cursor1.line,
pos=Text.pos_at_start_of_screen_line(State, State.cursor1), pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
} }
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
end end
end end
@ -682,7 +682,7 @@ function Text.snap_cursor_to_bottom_of_screen(State)
State.screen_top1 = Text.to1(State, top2) State.screen_top1 = Text.to1(State, top2)
--? print('top1 finally:', State.screen_top1.line, State.screen_top1.pos) --? print('top1 finally:', State.screen_top1.line, State.screen_top1.pos)
--? print('snap =>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos) --? print('snap =>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
end end
function Text.in_line(State, line_index, x,y) function Text.in_line(State, line_index, x,y)
@ -964,7 +964,7 @@ function Text.cursor_out_of_screen(State)
end end
function Text.redraw_all(State) function Text.redraw_all(State)
--? print('clearing fragments') --? print('clearing line caches')
-- Perform some early sanity checking here, in hopes that we correctly call -- Perform some early sanity checking here, in hopes that we correctly call
-- this whenever we change editor state. -- this whenever we change editor state.
if State.right <= State.left then if State.right <= State.left then

View File

@ -55,10 +55,8 @@ function snapshot(State, s,e)
end_line=e, end_line=e,
-- no filename; undo history is cleared when filename changes -- no filename; undo history is cleared when filename changes
} }
-- deep copy lines without cached stuff like text fragments
for i=s,e do for i=s,e do
local line = State.lines[i] table.insert(event.lines, deepcopy(State.lines[i]))
table.insert(event.lines, {data=line.data}) -- I've forgotten: should we deepcopy(line.data)?
end end
return event return event
end end
@ -81,17 +79,6 @@ function patch(lines, from, to)
end end
end end
function patch_placeholders(line_cache, from, to)
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, 'failed to patch undo operation')
for i=1,#to.lines do
table.insert(line_cache, to.start_line+i-1, {})
end
end
-- https://stackoverflow.com/questions/640642/how-do-you-copy-a-lua-table-by-value/26367080#26367080 -- https://stackoverflow.com/questions/640642/how-do-you-copy-a-lua-table-by-value/26367080#26367080
function deepcopy(obj, seen) function deepcopy(obj, seen)
if type(obj) ~= 'table' then return obj end if type(obj) ~= 'table' then return obj end