From e2c1bbe4e53bb0369471296caedf81ff6a059ae0 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 17 Mar 2023 09:36:38 -0700 Subject: [PATCH 1/2] more robust state validation --- edit.lua | 26 ++++++++++++++++++++++++-- run.lua | 7 ++----- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/edit.lua b/edit.lua index 8f629a4..1d8e0b3 100644 --- a/edit.lua +++ b/edit.lua @@ -109,10 +109,32 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c return result end -- App.initialize_state -function edit.fixup_cursor(State) +function edit.check_locs(State) + -- if State is inconsistent (i.e. file changed by some other program), + -- throw away all cursor state entirely + if edit.invalid1(State, State.screen_top1) + or edit.invalid1(State, State.cursor1) + or not edit.cursor_on_text(State) + or not Text.le1(State.screen_top1, State.cursor1) then + State.screen_top1 = {line=1, pos=1} + edit.put_cursor_on_first_text_line(State) + end +end + +function edit.invalid1(State, loc1) + return loc1.line > #State.lines + or loc1.pos > #State.lines[loc1.line].data +end + +function edit.cursor_on_text(State) + return State.cursor1.line <= #State.lines + and State.lines[State.cursor1.line].mode == 'text' +end + +function edit.put_cursor_on_first_text_line(State) for i,line in ipairs(State.lines) do if line.mode == 'text' then - State.cursor1.line = i + State.cursor1 = {line=i, pos=1} break end end diff --git a/run.lua b/run.lua index ec6a509..4c29860 100644 --- a/run.lua +++ b/run.lua @@ -28,14 +28,11 @@ function run.initialize(arg) Text.redraw_all(Editor_state) Editor_state.screen_top1 = {line=1, pos=1} Editor_state.cursor1 = {line=1, pos=1} - edit.fixup_cursor(Editor_state) else load_from_disk(Editor_state) Text.redraw_all(Editor_state) - if Editor_state.cursor1.line > #Editor_state.lines or Editor_state.lines[Editor_state.cursor1.line].mode ~= 'text' then - edit.fixup_cursor(Editor_state) - end end + edit.check_locs(Editor_state) love.window.setTitle('lines.love - '..Editor_state.filename) if #arg > 1 then @@ -112,7 +109,7 @@ function run.file_drop(file) Editor_state.lines = load_from_file(file) file:close() Text.redraw_all(Editor_state) - edit.fixup_cursor(Editor_state) + edit.check_locs(Editor_state) love.window.setTitle('lines.love - '..Editor_state.filename) end From d65b7950a152cf2ffe3149707f5e2040b305d8bd Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 17 Mar 2023 10:46:50 -0700 Subject: [PATCH 2/2] state validation in source editor as well --- source.lua | 1 + source_edit.lua | 26 ++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/source.lua b/source.lua index eeaea21..d0836cb 100644 --- a/source.lua +++ b/source.lua @@ -87,6 +87,7 @@ function source.initialize_edit_side() Editor_state.screen_top1 = {line=1, pos=1} Editor_state.cursor1 = {line=1, pos=1} end + edit.check_locs(Editor_state) -- We currently start out with side B collapsed. -- Other options: diff --git a/source_edit.lua b/source_edit.lua index e17f2f2..f340ab3 100644 --- a/source_edit.lua +++ b/source_edit.lua @@ -115,10 +115,32 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c return result end -- App.initialize_state -function edit.fixup_cursor(State) +function edit.check_locs(State) + -- if State is inconsistent (i.e. file changed by some other program), + -- throw away all cursor state entirely + if edit.invalid1(State, State.screen_top1) + or edit.invalid1(State, State.cursor1) + or not edit.cursor_on_text(State) + or not Text.le1(State.screen_top1, State.cursor1) then + State.screen_top1 = {line=1, pos=1} + edit.put_cursor_on_first_text_line(State) + end +end + +function edit.invalid1(State, loc1) + return loc1.line > #State.lines + or loc1.pos > #State.lines[loc1.line].data +end + +function edit.cursor_on_text(State) + return State.cursor1.line <= #State.lines + and State.lines[State.cursor1.line].mode == 'text' +end + +function edit.put_cursor_on_first_text_line(State) for i,line in ipairs(State.lines) do if line.mode == 'text' then - State.cursor1.line = i + State.cursor1 = {line=i, pos=1} break end end