From 3be413602a59ed735a33712380cedbdb8f5603df Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 20 Jun 2022 10:48:35 -0700 Subject: [PATCH] bugfix: typing should delete highlighted text The test harness now also mimics real usage more precisely. --- app.lua | 9 +++++++-- main.lua | 12 +++++------- text_tests.lua | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/app.lua b/app.lua index a887588..e5be2b0 100644 --- a/app.lua +++ b/app.lua @@ -232,14 +232,19 @@ function App.mouse_y() return App.fake_mouse_state.y end +-- all textinput events are also keypresses function App.run_after_textinput(t) + App.keypressed(t) App.textinput(t) + App.keyreleased(t) App.screen.contents = {} App.draw() end -function App.run_after_keychord(key) - App.keychord_pressed(key) +-- not all keys are textinput +function App.run_after_keychord(chord) + App.keychord_pressed(chord) + App.keyreleased(chord) App.screen.contents = {} App.draw() end diff --git a/main.lua b/main.lua index 2ad8c76..df68da5 100644 --- a/main.lua +++ b/main.lua @@ -407,9 +407,6 @@ function App.textinput(t) Text.textinput(t) end schedule_save() - if not App.shift_down() then - Selection1 = {} - end end function App.keychord_pressed(chord) @@ -540,22 +537,23 @@ function App.keychord_pressed(chord) local p = drawing.points[drawing.pending.target_point] if chord == 'escape' then p.name = nil + record_undo_event({before=before, after=snapshot(Lines.current_drawing_index)}) elseif chord == 'backspace' then local len = utf8.len(p.name) local byte_offset = Text.offset(p.name, len-1) p.name = string.sub(p.name, 1, byte_offset) + record_undo_event({before=before, after=snapshot(Lines.current_drawing_index)}) end - record_undo_event({before=before, after=snapshot(Lines.current_drawing_index)}) end schedule_save() else for _,line in ipairs(Lines) do line.y = nil end -- just in case we scroll Text.keychord_pressed(chord) end +end + +function App.keyreleased(key, scancode) if not App.shift_down() then Selection1 = {} end end - -function App.keyreleased(key, scancode) -end diff --git a/text_tests.lua b/text_tests.lua index f835d8d..95fbd78 100644 --- a/text_tests.lua +++ b/text_tests.lua @@ -204,6 +204,23 @@ function test_edit_after_click_resets_selection() check_nil(Selection1.line, 'F - test_edit_after_click_resets_selection') end +function test_edit_deletes_selection() + io.write('\ntest_edit_deletes_selection') + -- display a line of text with some part selected + App.screen.init{width=80, height=80} + Lines = load_array{'abc'} + Line_width = 75 + Cursor1 = {line=1, pos=1} + Selection1 = {line=1, pos=2} + Screen_top1 = {line=1, pos=1} + Screen_bottom1 = {} + App.draw() + -- press a key + App.run_after_textinput('x') + -- selected text is deleted and replaced with the key + check_eq(Lines[1].data, 'xbc', 'F - test_edit_deletes_selection') +end + function test_edit_wrapping_text() io.write('\ntest_edit_wrapping_text') App.screen.init{width=50, height=60}