From 0fcbe310a21dc581a29d22366e20629fc9e321ac Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 8 Jul 2024 00:22:56 -0700 Subject: simplify some code now that lines contains no derived data --- source_undo.lua | 10 +--------- undo.lua | 10 +--------- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/source_undo.lua b/source_undo.lua index e5dea93..8f21a9c 100644 --- a/source_undo.lua +++ b/source_undo.lua @@ -57,16 +57,8 @@ function snapshot(State, s,e) end_line=e, -- no filename; undo history is cleared when filename changes } - -- deep copy lines without cached stuff like text fragments for i=s,e do - local line = State.lines[i] - if line.mode == 'text' then - table.insert(event.lines, {mode='text', data=line.data}) -- I've forgotten: should we deepcopy(line.data)? - elseif line.mode == 'drawing' then - table.insert(event.lines, {mode='drawing', h=line.h, points=deepcopy(line.points), shapes=deepcopy(line.shapes), pending={}}) - else - assert(false, ('unknown line mode %s'):format(line.mode)) - end + table.insert(event.lines, deepcopy(State.lines[i])) end return event end diff --git a/undo.lua b/undo.lua index e5dea93..8f21a9c 100644 --- a/undo.lua +++ b/undo.lua @@ -57,16 +57,8 @@ function snapshot(State, s,e) end_line=e, -- no filename; undo history is cleared when filename changes } - -- deep copy lines without cached stuff like text fragments for i=s,e do - local line = State.lines[i] - if line.mode == 'text' then - table.insert(event.lines, {mode='text', data=line.data}) -- I've forgotten: should we deepcopy(line.data)? - elseif line.mode == 'drawing' then - table.insert(event.lines, {mode='drawing', h=line.h, points=deepcopy(line.points), shapes=deepcopy(line.shapes), pending={}}) - else - assert(false, ('unknown line mode %s'):format(line.mode)) - end + table.insert(event.lines, deepcopy(State.lines[i])) end return event end -- cgit 1.4.1-2-gfad0 From 5a74b6938e5a37b8b2d1f8e49039cf1cbae46a50 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 8 Jul 2024 00:26:54 -0700 Subject: purge obsolete term 'fragment' --- edit.lua | 8 +++----- source_edit.lua | 11 ++++------- source_text.lua | 14 +++++++------- text.lua | 16 ++++++++-------- 4 files changed, 22 insertions(+), 27 deletions(-) diff --git a/edit.lua b/edit.lua index 219689d..8424a9b 100644 --- a/edit.lua +++ b/edit.lua @@ -395,7 +395,7 @@ function edit.keychord_press(State, chord, key) State.cursor1 = State.search_backup.cursor State.screen_top1 = State.search_backup.screen_top 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 State.search_term = nil State.search_backup = nil @@ -443,8 +443,7 @@ function edit.keychord_press(State, chord, key) patch_placeholders(State.line_cache, event.after, event.before) -- invalidate various cached bits of lines State.lines.current_drawing = nil - -- if we're scrolling, reclaim all fragments to avoid memory leaks - Text.redraw_all(State) + Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks schedule_save(State) end elseif chord == 'C-y' then @@ -457,8 +456,7 @@ function edit.keychord_press(State, chord, key) patch(State.lines, event.before, event.after) -- invalidate various cached bits of lines State.lines.current_drawing = nil - -- if we're scrolling, reclaim all fragments to avoid memory leaks - Text.redraw_all(State) + Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks schedule_save(State) end -- clipboard diff --git a/source_edit.lua b/source_edit.lua index 34e12c3..9b3b7ba 100644 --- a/source_edit.lua +++ b/source_edit.lua @@ -53,8 +53,7 @@ function edit.initialize_state(top, left, right, font, font_height, line_height) -- rendering wrapped text lines needs some additional short-lived data per line: -- startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen - -- fragments: snippets of the line guaranteed to not straddle screen lines - -- screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line + -- screen_line_starting_pos: optional array of codepoint indices if it wraps over more than one screen line line_cache = {}, -- Given wrapping, any potential location for the text cursor can be described in two ways: @@ -400,7 +399,7 @@ function edit.keychord_press(State, chord, key) State.cursor1 = State.search_backup.cursor State.screen_top1 = State.search_backup.screen_top 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 State.search_term = nil State.search_backup = nil @@ -445,8 +444,7 @@ function edit.keychord_press(State, chord, key) patch_placeholders(State.line_cache, event.after, event.before) -- invalidate various cached bits of lines State.lines.current_drawing = nil - -- if we're scrolling, reclaim all fragments to avoid memory leaks - Text.redraw_all(State) + Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks schedule_save(State) end elseif chord == 'C-y' then @@ -459,8 +457,7 @@ function edit.keychord_press(State, chord, key) patch(State.lines, event.before, event.after) -- invalidate various cached bits of lines State.lines.current_drawing = nil - -- if we're scrolling, reclaim all fragments to avoid memory leaks - Text.redraw_all(State) + Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks schedule_save(State) end -- clipboard diff --git a/source_text.lua b/source_text.lua index 931dd00..70bb610 100644 --- a/source_text.lua +++ b/source_text.lua @@ -289,7 +289,7 @@ function Text.keychord_press(State, chord) line=State.cursor1.line, 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 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)) @@ -427,7 +427,7 @@ function Text.pageup(State) State.screen_top1 = Text.previous_screen_top1(State) State.cursor1 = deepcopy(State.screen_top1) 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 -- return the top y coordinate of a given line_index, @@ -476,7 +476,7 @@ function Text.pagedown(State) State.screen_top1 = Text.screen_bottom1(State) State.cursor1 = deepcopy(State.screen_top1) 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 -- return the location of the start of the bottom-most line on screen @@ -538,7 +538,7 @@ function Text.up(State) line=State.cursor1.line, 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 @@ -684,7 +684,7 @@ function Text.left(State) line=State.cursor1.line, 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 @@ -829,7 +829,7 @@ function Text.snap_cursor_to_bottom_of_screen(State) State.screen_top1 = Text.to1(State, top2) --? 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) - 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 function Text.in_line(State, line_index, x,y) @@ -1118,7 +1118,7 @@ function Text.cursor_out_of_screen(State) end function Text.redraw_all(State) ---? print('clearing fragments') +--? print('clearing line caches') -- Perform some early sanity checking here, in hopes that we correctly call -- this whenever we change editor state. if State.right <= State.left then diff --git a/text.lua b/text.lua index e50b154..f7f6de1 100644 --- a/text.lua +++ b/text.lua @@ -47,7 +47,7 @@ function Text.draw(State, line_index, y, startpos) end end end - -- render fragment + -- render screen line App.color(Text_color) App.screen.print(screen_line, State.left,y) y = y + State.line_height @@ -215,7 +215,7 @@ function Text.keychord_press(State, chord) line=State.cursor1.line, 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 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)) @@ -353,7 +353,7 @@ function Text.pageup(State) State.screen_top1 = Text.previous_screen_top1(State) State.cursor1 = deepcopy(State.screen_top1) 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 -- return the top y coordinate of a given line_index, @@ -402,7 +402,7 @@ function Text.pagedown(State) State.screen_top1 = Text.screen_bottom1(State) State.cursor1 = deepcopy(State.screen_top1) 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 -- return the location of the start of the bottom-most line on screen @@ -464,7 +464,7 @@ function Text.up(State) line=State.cursor1.line, 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 @@ -610,7 +610,7 @@ function Text.left(State) line=State.cursor1.line, 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 @@ -755,7 +755,7 @@ function Text.snap_cursor_to_bottom_of_screen(State) State.screen_top1 = Text.to1(State, top2) --? 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) - 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 function Text.in_line(State, line_index, x,y) @@ -1044,7 +1044,7 @@ function Text.cursor_out_of_screen(State) end function Text.redraw_all(State) ---? print('clearing fragments') +--? print('clearing line caches') -- Perform some early sanity checking here, in hopes that we correctly call -- this whenever we change editor state. if State.right <= State.left then -- cgit 1.4.1-2-gfad0 From c064f0a97df56c28d9d76a226ce152d97e40450a Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 8 Jul 2024 14:10:49 -0700 Subject: delete some seemingly redundant code We're patching line_cache and then immediately clearing it using Text.redraw_all. --- edit.lua | 1 - source_edit.lua | 1 - source_undo.lua | 11 ----------- undo.lua | 11 ----------- 4 files changed, 24 deletions(-) diff --git a/edit.lua b/edit.lua index 8424a9b..69756b7 100644 --- a/edit.lua +++ b/edit.lua @@ -440,7 +440,6 @@ function edit.keychord_press(State, chord, key) State.cursor1 = deepcopy(src.cursor) State.selection1 = deepcopy(src.selection) patch(State.lines, event.after, event.before) - patch_placeholders(State.line_cache, event.after, event.before) -- invalidate various cached bits of lines State.lines.current_drawing = nil Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks diff --git a/source_edit.lua b/source_edit.lua index 9b3b7ba..fd8e321 100644 --- a/source_edit.lua +++ b/source_edit.lua @@ -441,7 +441,6 @@ function edit.keychord_press(State, chord, key) State.cursor1 = deepcopy(src.cursor) State.selection1 = deepcopy(src.selection) patch(State.lines, event.after, event.before) - patch_placeholders(State.line_cache, event.after, event.before) -- invalidate various cached bits of lines State.lines.current_drawing = nil Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks diff --git a/source_undo.lua b/source_undo.lua index 8f21a9c..d91fecd 100644 --- a/source_undo.lua +++ b/source_undo.lua @@ -81,17 +81,6 @@ function patch(lines, from, to) 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 function deepcopy(obj, seen) if type(obj) ~= 'table' then return obj end diff --git a/undo.lua b/undo.lua index 8f21a9c..d91fecd 100644 --- a/undo.lua +++ b/undo.lua @@ -81,17 +81,6 @@ function patch(lines, from, to) 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 function deepcopy(obj, seen) if type(obj) ~= 'table' then return obj end -- cgit 1.4.1-2-gfad0