From f1886391c56e530bb1bb362a450aa9ab6cb8485c Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 1 Jun 2023 12:20:34 -0700 Subject: [PATCH 01/11] some temporary logging to catch a bug The bug has been spotted twice: 1. In snap.love, I selected text in one node, then another, and hit: Error: text.lua:789: attempt to compare nil with number stack traceback: text.lua:789: in function 'lt1' select.lua:19: in function 'clip_selection' text.lua:32: in function 'draw' edit.lua:117: in function 'draw' [string "REPL"]:21: in function 'draw' main.lua:152: in function 'draw' app.lua:102: in function [C]: in function 'xpcall' app.lua:112: in function [C]: in function 'xpcall' Couldn't reproduce. 2. In text.love, inscript selected all text in a small buffer and then clicked outside the text. And got: Error: text.lua:784: attempt to compare nil with number Traceback [love "callbacks.lua"]:228: in function 'handler' text.lua:784: in function 'lt1' select.lua:19: in function 'clip_selection' text.lua:27: in function 'draw' edit.lua:117: in function 'draw' run.lua:136: in function 'draw' main.lua:148: in function 'draw' app.lua:42: in function [C]: in function 'xpcall' This is reproducible, and also across forks. --- edit.lua | 14 ++++++++------ run.lua | 5 +++++ select.lua | 4 ++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/edit.lua b/edit.lua index 0f5bef5..7140997 100644 --- a/edit.lua +++ b/edit.lua @@ -223,7 +223,7 @@ end function edit.mouse_press(State, x,y, mouse_button) if State.search_term then return end ---? print('press', State.cursor1.line) + print_and_log(('edit.mouse_press: cursor at %d,%d').format(State.cursor1.line, State.cursor1.pos)) if mouse_press_consumed_by_any_button_handler(State, x,y, mouse_button) then -- press on a button and it returned 'true' to short-circuit return @@ -241,6 +241,7 @@ function edit.mouse_press(State, x,y, mouse_button) -- press and hold to start a selection: sets selection on press, cursor on release -- press and hold, then press shift: ignore shift -- i.e. mouse_release should never look at shift state + print_and_log(('edit.mouse_press: in line %d').format(line_index)) State.old_cursor1 = State.cursor1 State.old_selection1 = State.selection1 State.mousepress_shift = App.shift_down() @@ -248,7 +249,7 @@ function edit.mouse_press(State, x,y, mouse_button) line=line_index, pos=Text.to_pos_on_line(State, line_index, x, y), } ---? print('selection', State.selection1.line, State.selection1.pos) + print_and_log(('edit.mouse_press: selection now %d,%d').format(State.selection1.line, State.selection1.pos)) break end elseif line.mode == 'drawing' then @@ -266,7 +267,7 @@ end function edit.mouse_release(State, x,y, mouse_button) if State.search_term then return end ---? print('release', State.cursor1.line) + print_and_log(('edit.mouse_release: cursor at %d,%d').format(State.cursor1.line, State.cursor1.pos)) if State.lines.current_drawing then Drawing.mouse_release(State, x,y, mouse_button) schedule_save(State) @@ -275,15 +276,16 @@ function edit.mouse_release(State, x,y, mouse_button) Drawing.before = nil end else + print_and_log('edit.mouse_release: no current drawing') for line_index,line in ipairs(State.lines) do if line.mode == 'text' then if Text.in_line(State, line_index, x,y) then ---? print('reset selection') + print_and_log(('edit.mouse_release: in line %d').format(line_index)) State.cursor1 = { line=line_index, pos=Text.to_pos_on_line(State, line_index, x, y), } ---? print('cursor', State.cursor1.line, State.cursor1.pos) + print_and_log(('edit.mouse_release: cursor now %d,%d').format(State.cursor1.line, State.cursor1.pos)) if State.mousepress_shift then if State.old_selection1.line == nil then State.selection1 = State.old_cursor1 @@ -299,7 +301,7 @@ function edit.mouse_release(State, x,y, mouse_button) end end end ---? print('selection:', State.selection1.line, State.selection1.pos) + print_and_log(('edit.mouse_release: finally selection %s,%s cursor %d,%d').format(tostring(State.selection1.line), tostring(State.selection1.pos), State.cursor1.line, State.cursor1.pos)) end end diff --git a/run.lua b/run.lua index d029bed..5f35a0b 100644 --- a/run.lua +++ b/run.lua @@ -48,6 +48,11 @@ function run.initialize(arg) end end +function print_and_log(s) + print(s) + log(3, s) +end + function run.load_settings() love.graphics.setFont(love.graphics.newFont(Settings.font_height)) -- determine default dimensions and flags diff --git a/select.lua b/select.lua index efb6909..094cca9 100644 --- a/select.lua +++ b/select.lua @@ -8,13 +8,17 @@ -- Result: positions spos,epos between apos,bpos. function Text.clip_selection(State, line_index, apos, bpos) if State.selection1.line == nil then return nil,nil end + print_and_log('text.clip_selection') -- min,max = sorted(State.selection1,State.cursor1) local minl,minp = State.selection1.line,State.selection1.pos + print_and_log(('text.clip_selection: one end from selection: %d,%d'):format(minl,minp)) local maxl,maxp if App.mouse_down(1) then maxl,maxp = Text.mouse_pos(State) + print_and_log(('text.clip_selection: other end from mouse: %d,%d'):format(maxl,maxp)) else maxl,maxp = State.cursor1.line,State.cursor1.pos + print_and_log(('text.clip_selection: other end from cursor: %d,%d'):format(maxl,maxp)) end if Text.lt1({line=maxl, pos=maxp}, {line=minl, pos=minp}) then From 3e848efb081797d2341254dbdfdcccc307cf5170 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 1 Jun 2023 12:33:22 -0700 Subject: [PATCH 02/11] ah, I see the problem Text.mouse_pos can sometimes set recent_mouse.time but not recent_mouse.x/y. I'd assumed x/y is never nil in those situations, but that's violated. It's most easily seen when typing C-a and then clicking. --- edit.lua | 14 +++++++------- select.lua | 7 +------ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/edit.lua b/edit.lua index 7140997..f9eb076 100644 --- a/edit.lua +++ b/edit.lua @@ -223,7 +223,7 @@ end function edit.mouse_press(State, x,y, mouse_button) if State.search_term then return end - print_and_log(('edit.mouse_press: cursor at %d,%d').format(State.cursor1.line, State.cursor1.pos)) + print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos)) if mouse_press_consumed_by_any_button_handler(State, x,y, mouse_button) then -- press on a button and it returned 'true' to short-circuit return @@ -241,7 +241,7 @@ function edit.mouse_press(State, x,y, mouse_button) -- press and hold to start a selection: sets selection on press, cursor on release -- press and hold, then press shift: ignore shift -- i.e. mouse_release should never look at shift state - print_and_log(('edit.mouse_press: in line %d').format(line_index)) + print_and_log(('edit.mouse_press: in line %d'):format(line_index)) State.old_cursor1 = State.cursor1 State.old_selection1 = State.selection1 State.mousepress_shift = App.shift_down() @@ -249,7 +249,7 @@ function edit.mouse_press(State, x,y, mouse_button) line=line_index, pos=Text.to_pos_on_line(State, line_index, x, y), } - print_and_log(('edit.mouse_press: selection now %d,%d').format(State.selection1.line, State.selection1.pos)) + print_and_log(('edit.mouse_press: selection now %d,%d'):format(State.selection1.line, State.selection1.pos)) break end elseif line.mode == 'drawing' then @@ -267,7 +267,7 @@ end function edit.mouse_release(State, x,y, mouse_button) if State.search_term then return end - print_and_log(('edit.mouse_release: cursor at %d,%d').format(State.cursor1.line, State.cursor1.pos)) + print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos)) if State.lines.current_drawing then Drawing.mouse_release(State, x,y, mouse_button) schedule_save(State) @@ -280,12 +280,12 @@ function edit.mouse_release(State, x,y, mouse_button) for line_index,line in ipairs(State.lines) do if line.mode == 'text' then if Text.in_line(State, line_index, x,y) then - print_and_log(('edit.mouse_release: in line %d').format(line_index)) + print_and_log(('edit.mouse_release: in line %d'):format(line_index)) State.cursor1 = { line=line_index, pos=Text.to_pos_on_line(State, line_index, x, y), } - print_and_log(('edit.mouse_release: cursor now %d,%d').format(State.cursor1.line, State.cursor1.pos)) + print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos)) if State.mousepress_shift then if State.old_selection1.line == nil then State.selection1 = State.old_cursor1 @@ -301,7 +301,7 @@ function edit.mouse_release(State, x,y, mouse_button) end end end - print_and_log(('edit.mouse_release: finally selection %s,%s cursor %d,%d').format(tostring(State.selection1.line), tostring(State.selection1.pos), State.cursor1.line, State.cursor1.pos)) + print_and_log(('edit.mouse_release: finally selection %s,%s cursor %d,%d'):format(tostring(State.selection1.line), tostring(State.selection1.pos), State.cursor1.line, State.cursor1.pos)) end end diff --git a/select.lua b/select.lua index 094cca9..3b36b52 100644 --- a/select.lua +++ b/select.lua @@ -8,17 +8,13 @@ -- Result: positions spos,epos between apos,bpos. function Text.clip_selection(State, line_index, apos, bpos) if State.selection1.line == nil then return nil,nil end - print_and_log('text.clip_selection') -- min,max = sorted(State.selection1,State.cursor1) local minl,minp = State.selection1.line,State.selection1.pos - print_and_log(('text.clip_selection: one end from selection: %d,%d'):format(minl,minp)) local maxl,maxp if App.mouse_down(1) then maxl,maxp = Text.mouse_pos(State) - print_and_log(('text.clip_selection: other end from mouse: %d,%d'):format(maxl,maxp)) else maxl,maxp = State.cursor1.line,State.cursor1.pos - print_and_log(('text.clip_selection: other end from cursor: %d,%d'):format(maxl,maxp)) end if Text.lt1({line=maxl, pos=maxp}, {line=minl, pos=minp}) then @@ -33,7 +29,6 @@ function Text.clip_selection(State, line_index, apos, bpos) -- compare bounds more carefully (start inclusive, end exclusive) local a_ge = Text.le1({line=minl, pos=minp}, {line=line_index, pos=apos}) local b_lt = Text.lt1({line=line_index, pos=bpos}, {line=maxl, pos=maxp}) ---? print(minl,line_index,maxl, '--', minp,apos,bpos,maxp, '--', a_ge,b_lt) if a_ge and b_lt then -- fully contained return apos,bpos @@ -64,7 +59,6 @@ function Text.draw_highlight(State, line, x,y, pos, lo,hi) local before = line.data:sub(pos_offset, lo_offset-1) lo_px = App.width(before) end ---? print(lo,pos,hi, '--', lo_offset,pos_offset,hi_offset, '--', lo_px) local s = line.data:sub(lo_offset, hi_offset-1) App.color(Highlight_color) love.graphics.rectangle('fill', x+lo_px,y, App.width(s),State.line_height) @@ -77,6 +71,7 @@ end function Text.mouse_pos(State) local time = love.timer.getTime() if State.recent_mouse.time and State.recent_mouse.time > time-0.1 then + print_and_log(('text.mouse_pos: returning recent value %d,%d'):format(State.recent_mouse.line, State.recent_mouse.pos)) return State.recent_mouse.line, State.recent_mouse.pos end State.recent_mouse.time = time From 9b3fa33f5bdd1237c2ea13e40c8aae9e837dd54f Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 1 Jun 2023 12:57:47 -0700 Subject: [PATCH 03/11] idea: set recent_mouse on mouse events This helps, but doesn't address the C-a case. As it stands, literally my first click of the mouse might need access to recent_mouse.line/pos --- edit.lua | 3 ++- select.lua | 5 ++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/edit.lua b/edit.lua index f9eb076..82d9542 100644 --- a/edit.lua +++ b/edit.lua @@ -249,7 +249,7 @@ function edit.mouse_press(State, x,y, mouse_button) line=line_index, pos=Text.to_pos_on_line(State, line_index, x, y), } - print_and_log(('edit.mouse_press: selection now %d,%d'):format(State.selection1.line, State.selection1.pos)) + State.recent_mouse = {time=Current_time, line=State.selection1.line, pos=State.selection1.pos} break end elseif line.mode == 'drawing' then @@ -286,6 +286,7 @@ function edit.mouse_release(State, x,y, mouse_button) pos=Text.to_pos_on_line(State, line_index, x, y), } print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos)) + State.recent_mouse = {time=Current_time, line=State.cursor1.line, pos=State.cursor1.pos} if State.mousepress_shift then if State.old_selection1.line == nil then State.selection1 = State.old_cursor1 diff --git a/select.lua b/select.lua index 3b36b52..d0509ef 100644 --- a/select.lua +++ b/select.lua @@ -69,12 +69,11 @@ end -- inefficient for some reason, so don't do it on every frame function Text.mouse_pos(State) - local time = love.timer.getTime() - if State.recent_mouse.time and State.recent_mouse.time > time-0.1 then + if State.recent_mouse.time and State.recent_mouse.time > Current_time-0.1 then print_and_log(('text.mouse_pos: returning recent value %d,%d'):format(State.recent_mouse.line, State.recent_mouse.pos)) return State.recent_mouse.line, State.recent_mouse.pos end - State.recent_mouse.time = time + State.recent_mouse.time = Current_time local line,pos = Text.to_pos(State, App.mouse_x(), App.mouse_y()) if line then State.recent_mouse.line = line From 9b27a4d816bbfc23f122698e65f660d89c7664e4 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 1 Jun 2023 21:59:09 -0700 Subject: [PATCH 04/11] failing test now looks realistic --- text_tests.lua | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/text_tests.lua b/text_tests.lua index 8b646a4..bd449ac 100644 --- a/text_tests.lua +++ b/text_tests.lua @@ -882,6 +882,52 @@ function test_select_text_repeatedly_using_mouse_and_shift() check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos') end +function test_select_all_text() + -- display a single line of text + App.screen.init{width=75, height=80} + Editor_state = edit.initialize_test_state() + Editor_state.lines = load_array{'abc def'} + Text.redraw_all(Editor_state) + Editor_state.cursor1 = {line=1, pos=1} + Editor_state.screen_top1 = {line=1, pos=1} + Editor_state.screen_bottom1 = {} + edit.draw(Editor_state) + -- select all + App.fake_key_press('lctrl') + edit.run_after_keychord(Editor_state, 'C-a') + App.fake_key_release('lctrl') + edit.key_release(Editor_state, 'lctrl') + -- selection + check_eq(Editor_state.selection1.line, 1, 'selection:line') + check_eq(Editor_state.selection1.pos, 1, 'selection:pos') + check_eq(Editor_state.cursor1.line, 1, 'cursor:line') + check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos') +end + +function test_select_all_text_then_mouse_press_outside_text() + -- display a single line of text + App.screen.init{width=75, height=80} + Editor_state = edit.initialize_test_state() + Editor_state.lines = load_array{'abc def'} + Text.redraw_all(Editor_state) + Editor_state.cursor1 = {line=1, pos=1} + Editor_state.screen_top1 = {line=1, pos=1} + Editor_state.screen_bottom1 = {} + edit.draw(Editor_state) + -- select all + App.fake_key_press('lctrl') + edit.run_after_keychord(Editor_state, 'C-a') + App.fake_key_release('lctrl') + edit.key_release(Editor_state, 'lctrl') + -- selection + check_eq(Editor_state.selection1.line, 1, 'selection:line') + check_eq(Editor_state.selection1.pos, 1, 'selection:pos') + check_eq(Editor_state.cursor1.line, 1, 'cursor:line') + check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos') + -- part of a mouse click outside the selected line + edit.run_after_mouse_press(Editor_state, 45, Margin_top + Editor_state.line_height + 10, --[[mouse button]] 1) +end + function test_cut_without_selection() -- display a few lines App.screen.init{width=Editor_state.left+30, height=60} From cdef37c4191e48228d1ea83eb590e1f3109e3c8b Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 1 Jun 2023 22:12:12 -0700 Subject: [PATCH 05/11] get rid of recent_mouse It's a hack: - if you start selecting from below final line the start of the selection is the most recent click even if it was forever ago - (the crash we're currently fixing) if you start up and immediately select all then click below final line => crash. recent_mouse was never set. - getting rid of it breaks no tests (except the crash we're currently fixing) --- edit.lua | 4 ---- select.lua | 11 +---------- text_tests.lua | 46 +++++++++++++++++++++++----------------------- 3 files changed, 24 insertions(+), 37 deletions(-) diff --git a/edit.lua b/edit.lua index 82d9542..3e92a54 100644 --- a/edit.lua +++ b/edit.lua @@ -75,8 +75,6 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c old_cursor1 = nil, old_selection1 = nil, mousepress_shift = nil, - -- when selecting text, avoid recomputing some state on every single frame - recent_mouse = {}, -- cursor coordinates in pixels cursor_x = 0, @@ -249,7 +247,6 @@ function edit.mouse_press(State, x,y, mouse_button) line=line_index, pos=Text.to_pos_on_line(State, line_index, x, y), } - State.recent_mouse = {time=Current_time, line=State.selection1.line, pos=State.selection1.pos} break end elseif line.mode == 'drawing' then @@ -286,7 +283,6 @@ function edit.mouse_release(State, x,y, mouse_button) pos=Text.to_pos_on_line(State, line_index, x, y), } print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos)) - State.recent_mouse = {time=Current_time, line=State.cursor1.line, pos=State.cursor1.pos} if State.mousepress_shift then if State.old_selection1.line == nil then State.selection1 = State.old_cursor1 diff --git a/select.lua b/select.lua index d0509ef..dbd551b 100644 --- a/select.lua +++ b/select.lua @@ -69,17 +69,8 @@ end -- inefficient for some reason, so don't do it on every frame function Text.mouse_pos(State) - if State.recent_mouse.time and State.recent_mouse.time > Current_time-0.1 then - print_and_log(('text.mouse_pos: returning recent value %d,%d'):format(State.recent_mouse.line, State.recent_mouse.pos)) - return State.recent_mouse.line, State.recent_mouse.pos - end - State.recent_mouse.time = Current_time local line,pos = Text.to_pos(State, App.mouse_x(), App.mouse_y()) - if line then - State.recent_mouse.line = line - State.recent_mouse.pos = pos - end - return State.recent_mouse.line, State.recent_mouse.pos + return line, pos end function Text.to_pos(State, x,y) diff --git a/text_tests.lua b/text_tests.lua index bd449ac..eec3098 100644 --- a/text_tests.lua +++ b/text_tests.lua @@ -904,29 +904,29 @@ function test_select_all_text() check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos') end -function test_select_all_text_then_mouse_press_outside_text() - -- display a single line of text - App.screen.init{width=75, height=80} - Editor_state = edit.initialize_test_state() - Editor_state.lines = load_array{'abc def'} - Text.redraw_all(Editor_state) - Editor_state.cursor1 = {line=1, pos=1} - Editor_state.screen_top1 = {line=1, pos=1} - Editor_state.screen_bottom1 = {} - edit.draw(Editor_state) - -- select all - App.fake_key_press('lctrl') - edit.run_after_keychord(Editor_state, 'C-a') - App.fake_key_release('lctrl') - edit.key_release(Editor_state, 'lctrl') - -- selection - check_eq(Editor_state.selection1.line, 1, 'selection:line') - check_eq(Editor_state.selection1.pos, 1, 'selection:pos') - check_eq(Editor_state.cursor1.line, 1, 'cursor:line') - check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos') - -- part of a mouse click outside the selected line - edit.run_after_mouse_press(Editor_state, 45, Margin_top + Editor_state.line_height + 10, --[[mouse button]] 1) -end +--? function test_select_all_text_then_mouse_press_outside_text() +--? -- display a single line of text +--? App.screen.init{width=75, height=80} +--? Editor_state = edit.initialize_test_state() +--? Editor_state.lines = load_array{'abc def'} +--? Text.redraw_all(Editor_state) +--? Editor_state.cursor1 = {line=1, pos=1} +--? Editor_state.screen_top1 = {line=1, pos=1} +--? Editor_state.screen_bottom1 = {} +--? edit.draw(Editor_state) +--? -- select all +--? App.fake_key_press('lctrl') +--? edit.run_after_keychord(Editor_state, 'C-a') +--? App.fake_key_release('lctrl') +--? edit.key_release(Editor_state, 'lctrl') +--? -- selection +--? check_eq(Editor_state.selection1.line, 1, 'selection:line') +--? check_eq(Editor_state.selection1.pos, 1, 'selection:pos') +--? check_eq(Editor_state.cursor1.line, 1, 'cursor:line') +--? check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos') +--? -- part of a mouse click outside the selected line +--? edit.run_after_mouse_press(Editor_state, 45, Margin_top + Editor_state.line_height + 10, --[[mouse button]] 1) +--? end function test_cut_without_selection() -- display a few lines From e0462ea2d866588522501875840af464b75b4ece Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 2 Jun 2023 07:43:20 -0700 Subject: [PATCH 06/11] save the list of tests in repo --- text_tests | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 text_tests diff --git a/text_tests b/text_tests new file mode 100644 index 0000000..b14f634 --- /dev/null +++ b/text_tests @@ -0,0 +1,113 @@ +== Summary of tests for the text editor + +This doesn't include all tests and might not change between forks, but it's +intended to be the "timeless core" of a text editor widget that shouldn't +change across forks. + +# basic +initial state +draw text +draw wrapping text +draw word wrapping text +draw text wrapping within word +draw wrapping text containing non ascii + +# mouse +click moves cursor +click to left of line +click takes margins into account +click on empty line +click on wrapping line +click on wrapping line takes margins into account +click on wrapping line +click on wrapping line rendered from partway at top of screen +click past end of wrapping line +click past end of wrapping line containing non ascii +click past end of word wrapping line + +# cursor movement +move left + move left to previous line +move right + move right to next line + +move to start of word + move to start of previous word + move to start of word on previous line +move past end of word + move past end of word on next line +skip to previous word + skip past tab to previous word + skip multiple spaces to previous word +skip to next word + skip past tab to next word + skip multiple spaces to next word + +# mutating text +insert first character +edit wrapping text +insert newline +insert newline at start of line +insert from clipboard +backspace from start of final line +backspace past line boundary +backspace over selection +backspace over selection reverse +backspace over multiple lines +backspace to end of line +backspace to start of line + +# scroll +pagedown +pagedown often shows start of wrapping line +pagedown can start from middle of long wrapping line +pagedown never moves up +down arrow moves cursor +down arrow scrolls down by one line +down arrow scrolls down by one screen line +down arrow scrolls down by one screen line after splitting within word +pagedown followed by down arrow does not scroll screen up +up arrow moves cursor +up arrow scrolls up by one line +up arrow scrolls up by one screen line +up arrow scrolls up to final screen line +up arrow scrolls up to empty line +pageup +pageup scrolls up by screen line +pageup scrolls up from middle screen line +enter on bottom line scrolls down +enter on final line avoids scrolling down when not at bottom +inserting text on final line avoids scrolling down when not at bottom +typing on bottom line scrolls down +left arrow scrolls up in wrapped line +right arrow scrolls down in wrapped line +home scrolls up in wrapped line +end scrolls down in wrapped line +position cursor on recently edited wrapping line +backspace can scroll up +backspace can scroll up screen line + +# selection +select text using shift and cursor movement operations +select text using mouse +select text using mouse and shift +select text repeatedly using mouse and shift +cursor movement without shift resets selection +edit deletes selection +edit with shift key deletes selection +deleting selection may scroll +copy does not reset selection +cut +cut without selection +paste replaces selection + +# search +search +search upwards +search wrap +search wrap upwards + +# undo +undo insert text +undo delete text +undo restores selection From 541519a9986fe5c85d6b03fc8cf993ba8e950b0c Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 2 Jun 2023 07:46:45 -0700 Subject: [PATCH 07/11] desired behavior now looks good --- text_tests | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/text_tests b/text_tests index b14f634..d571368 100644 --- a/text_tests +++ b/text_tests @@ -90,9 +90,14 @@ backspace can scroll up screen line # selection select text using shift and cursor movement operations select text using mouse + clicking to left of a line = start of line + clicking to right of a line = end of line + clicking above topmost line = top of screen + clicking below bottom-most line = bottom of screen select text using mouse and shift select text repeatedly using mouse and shift cursor movement without shift resets selection +mouse click without shift resets selection edit deletes selection edit with shift key deletes selection deleting selection may scroll From 3114176ebd5b9895b5f67c759a2a705f673a4ac0 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 3 Jun 2023 10:36:05 -0700 Subject: [PATCH 08/11] change how we handle clicks above top margin --- app.lua | 1 + edit.lua | 11 +++++++++++ select.lua | 3 +++ text_tests.lua | 43 +++++++++++++++++++------------------------ 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/app.lua b/app.lua index 1d4a72b..584b17b 100644 --- a/app.lua +++ b/app.lua @@ -361,6 +361,7 @@ function App.run_tests() for _,name in ipairs(sorted_names) do App.initialize_for_test() --? print('=== '..name) +--? _G[name]() xpcall(_G[name], function(err) prepend_debug_info_to_test_failure(name, err) end) end -- clean up all test methods diff --git a/edit.lua b/edit.lua index 3e92a54..ddd17c1 100644 --- a/edit.lua +++ b/edit.lua @@ -227,6 +227,17 @@ function edit.mouse_press(State, x,y, mouse_button) return end + if y < State.top then + State.old_cursor1 = State.cursor1 + State.old_selection1 = State.selection1 + State.mousepress_shift = App.shift_down() + State.selection1 = { + line=1, + pos=1, + } + return + end + for line_index,line in ipairs(State.lines) do if line.mode == 'text' then if Text.in_line(State, line_index, x,y) then diff --git a/select.lua b/select.lua index dbd551b..c2fe7ad 100644 --- a/select.lua +++ b/select.lua @@ -74,6 +74,9 @@ function Text.mouse_pos(State) end function Text.to_pos(State, x,y) + if y < State.line_cache[State.screen_top1.line].starty then + return State.screen_top1.line, State.screen_top1.pos + end for line_index,line in ipairs(State.lines) do if line.mode == 'text' then if Text.in_line(State, line_index, x,y) then diff --git a/text_tests.lua b/text_tests.lua index eec3098..d5368a8 100644 --- a/text_tests.lua +++ b/text_tests.lua @@ -1,4 +1,6 @@ -- major tests for text editing flows +-- Arguably this should be called edit_tests.lua, +-- but that would mess up the git blame at this point. function test_initial_state() App.screen.init{width=120, height=60} @@ -828,6 +830,23 @@ function test_select_text_using_mouse() check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos') end +function test_select_text_using_mouse_starting_above_text() + App.screen.init{width=50, height=60} + Editor_state = edit.initialize_test_state() + Editor_state.lines = load_array{'abc', 'def', 'xyz'} + Text.redraw_all(Editor_state) + Editor_state.cursor1 = {line=1, pos=1} + Editor_state.screen_top1 = {line=1, pos=1} + Editor_state.screen_bottom1 = {} + Editor_state.selection1 = {} + edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache + -- press mouse above first line of text + edit.run_after_mouse_press(Editor_state, Editor_state.left+8,5, 1) + check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil') + check_eq(Editor_state.selection1.line, 1, 'selection:line') + check_eq(Editor_state.selection1.pos, 1, 'selection:pos') +end + function test_select_text_using_mouse_and_shift() App.screen.init{width=50, height=60} Editor_state = edit.initialize_test_state() @@ -904,30 +923,6 @@ function test_select_all_text() check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos') end ---? function test_select_all_text_then_mouse_press_outside_text() ---? -- display a single line of text ---? App.screen.init{width=75, height=80} ---? Editor_state = edit.initialize_test_state() ---? Editor_state.lines = load_array{'abc def'} ---? Text.redraw_all(Editor_state) ---? Editor_state.cursor1 = {line=1, pos=1} ---? Editor_state.screen_top1 = {line=1, pos=1} ---? Editor_state.screen_bottom1 = {} ---? edit.draw(Editor_state) ---? -- select all ---? App.fake_key_press('lctrl') ---? edit.run_after_keychord(Editor_state, 'C-a') ---? App.fake_key_release('lctrl') ---? edit.key_release(Editor_state, 'lctrl') ---? -- selection ---? check_eq(Editor_state.selection1.line, 1, 'selection:line') ---? check_eq(Editor_state.selection1.pos, 1, 'selection:pos') ---? check_eq(Editor_state.cursor1.line, 1, 'cursor:line') ---? check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos') ---? -- part of a mouse click outside the selected line ---? edit.run_after_mouse_press(Editor_state, 45, Margin_top + Editor_state.line_height + 10, --[[mouse button]] 1) ---? end - function test_cut_without_selection() -- display a few lines App.screen.init{width=Editor_state.left+30, height=60} From cf0ba7c15431221e90f6aee87ea5b69ef0b18ea4 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 3 Jun 2023 10:43:20 -0700 Subject: [PATCH 09/11] handle wrapping lines --- edit.lua | 4 ++-- text_tests.lua | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/edit.lua b/edit.lua index ddd17c1..5918165 100644 --- a/edit.lua +++ b/edit.lua @@ -232,8 +232,8 @@ function edit.mouse_press(State, x,y, mouse_button) State.old_selection1 = State.selection1 State.mousepress_shift = App.shift_down() State.selection1 = { - line=1, - pos=1, + line=State.screen_top1.line, + pos=State.screen_top1.pos, } return end diff --git a/text_tests.lua b/text_tests.lua index d5368a8..a01ad3e 100644 --- a/text_tests.lua +++ b/text_tests.lua @@ -847,6 +847,23 @@ function test_select_text_using_mouse_starting_above_text() check_eq(Editor_state.selection1.pos, 1, 'selection:pos') end +function test_select_text_using_mouse_starting_above_text_wrapping_line() + -- first screen line starts in the middle of a line + App.screen.init{width=50, height=60} + Editor_state = edit.initialize_test_state() + Editor_state.lines = load_array{'abc', 'defgh', 'xyz'} + Text.redraw_all(Editor_state) + Editor_state.cursor1 = {line=2, pos=5} + Editor_state.screen_top1 = {line=2, pos=3} + Editor_state.screen_bottom1 = {} + -- press mouse above first line of text + edit.run_after_mouse_press(Editor_state, Editor_state.left+8,5, 1) + -- selection is at screen top + check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil') + check_eq(Editor_state.selection1.line, 2, 'selection:line') + check_eq(Editor_state.selection1.pos, 3, 'selection:pos') +end + function test_select_text_using_mouse_and_shift() App.screen.init{width=50, height=60} Editor_state = edit.initialize_test_state() From 9656e137742eb442e9ce013dd3f25cf6df8c9fad Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sun, 4 Jun 2023 12:20:24 -0700 Subject: [PATCH 10/11] bugfix: inscript's bug To fix this I have to first stop incrementally updating screen_bottom1 in the middle of a frame. Now it always has a good value from the end of a frame. I'm also running into some limitations in the test I'd ideally like to write (that are documented in a comment), but I still get some sort of automated test for this bugfix. --- edit.lua | 31 +++++++++++++++++++++---------- select.lua | 8 ++------ text.lua | 15 +++++++++++++++ text_tests.lua | 27 +++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 16 deletions(-) diff --git a/edit.lua b/edit.lua index 5918165..f436f36 100644 --- a/edit.lua +++ b/edit.lua @@ -152,12 +152,13 @@ function edit.draw(State) State.cursor_x = nil State.cursor_y = nil local y = State.top + local screen_bottom1 = {line=nil, pos=nil} --? print('== draw') for line_index = State.screen_top1.line,#State.lines do local line = State.lines[line_index] --? print('draw:', y, line_index, line) if y + State.line_height > App.screen.height then break end - State.screen_bottom1 = {line=line_index, pos=nil} + screen_bottom1.line = line_index if line.mode == 'text' then --? print('text.draw', y, line_index) local startpos = 1 @@ -180,7 +181,7 @@ function edit.draw(State) end, }) end - y, State.screen_bottom1.pos = Text.draw(State, line_index, y, startpos) + y, screen_bottom1.pos = Text.draw(State, line_index, y, startpos) --? print('=> y', y) elseif line.mode == 'drawing' then y = y+Drawing_padding_top @@ -191,6 +192,7 @@ function edit.draw(State) assert(false) end end + State.screen_bottom1 = screen_bottom1 if State.search_term then Text.draw_search_bar(State) end @@ -221,7 +223,7 @@ end function edit.mouse_press(State, x,y, mouse_button) if State.search_term then return end - print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos)) +--? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos)) if mouse_press_consumed_by_any_button_handler(State, x,y, mouse_button) then -- press on a button and it returned 'true' to short-circuit return @@ -258,7 +260,7 @@ function edit.mouse_press(State, x,y, mouse_button) line=line_index, pos=Text.to_pos_on_line(State, line_index, x, y), } - break + return end elseif line.mode == 'drawing' then local line_cache = State.line_cache[line_index] @@ -267,15 +269,24 @@ function edit.mouse_press(State, x,y, mouse_button) State.lines.current_drawing = line Drawing.before = snapshot(State, line_index) Drawing.mouse_press(State, line_index, x,y, mouse_button) - break + return end end end + + -- still here? click is below all screen lines + State.old_cursor1 = State.cursor1 + State.old_selection1 = State.selection1 + State.mousepress_shift = App.shift_down() + State.selection1 = { + line=State.screen_bottom1.line, + pos=Text.pos_at_end_of_screen_line(State, State.screen_bottom1), + } end function edit.mouse_release(State, x,y, mouse_button) if State.search_term then return end - print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos)) +--? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos)) if State.lines.current_drawing then Drawing.mouse_release(State, x,y, mouse_button) schedule_save(State) @@ -284,16 +295,16 @@ function edit.mouse_release(State, x,y, mouse_button) Drawing.before = nil end else - print_and_log('edit.mouse_release: no current drawing') +--? print_and_log('edit.mouse_release: no current drawing') for line_index,line in ipairs(State.lines) do if line.mode == 'text' then if Text.in_line(State, line_index, x,y) then - print_and_log(('edit.mouse_release: in line %d'):format(line_index)) +--? print_and_log(('edit.mouse_release: in line %d'):format(line_index)) State.cursor1 = { line=line_index, pos=Text.to_pos_on_line(State, line_index, x, y), } - print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos)) +--? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos)) if State.mousepress_shift then if State.old_selection1.line == nil then State.selection1 = State.old_cursor1 @@ -309,7 +320,7 @@ function edit.mouse_release(State, x,y, mouse_button) end end end - print_and_log(('edit.mouse_release: finally selection %s,%s cursor %d,%d'):format(tostring(State.selection1.line), tostring(State.selection1.pos), State.cursor1.line, State.cursor1.pos)) +--? print_and_log(('edit.mouse_release: finally selection %s,%s cursor %d,%d'):format(tostring(State.selection1.line), tostring(State.selection1.pos), State.cursor1.line, State.cursor1.pos)) end end diff --git a/select.lua b/select.lua index c2fe7ad..9b2a278 100644 --- a/select.lua +++ b/select.lua @@ -67,13 +67,8 @@ function Text.draw_highlight(State, line, x,y, pos, lo,hi) end end --- inefficient for some reason, so don't do it on every frame function Text.mouse_pos(State) - local line,pos = Text.to_pos(State, App.mouse_x(), App.mouse_y()) - return line, pos -end - -function Text.to_pos(State, x,y) + local x,y = App.mouse_x(), App.mouse_y() if y < State.line_cache[State.screen_top1.line].starty then return State.screen_top1.line, State.screen_top1.pos end @@ -84,6 +79,7 @@ function Text.to_pos(State, x,y) end end end + return State.screen_bottom1.line, Text.pos_at_end_of_screen_line(State, State.screen_bottom1) end function Text.cut_selection(State) diff --git a/text.lua b/text.lua index b6389e4..d89fb63 100644 --- a/text.lua +++ b/text.lua @@ -591,6 +591,7 @@ function Text.right_without_scroll(State) end end +-- result: pos, index of screen line function Text.pos_at_start_of_screen_line(State, loc1) Text.populate_screen_line_starting_pos(State, loc1.line) local line_cache = State.line_cache[loc1.line] @@ -603,6 +604,20 @@ function Text.pos_at_start_of_screen_line(State, loc1) assert(false) end +function Text.pos_at_end_of_screen_line(State, loc1) + Text.populate_screen_line_starting_pos(State, loc1.line) + local line_cache = State.line_cache[loc1.line] + local most_recent_final_pos = utf8.len(State.lines[loc1.line].data)+1 + for i=#line_cache.screen_line_starting_pos,1,-1 do + local spos = line_cache.screen_line_starting_pos[i] + if spos <= loc1.pos then + return most_recent_final_pos + end + most_recent_final_pos = spos-1 + end + assert(false) +end + function Text.cursor_at_final_screen_line(State) Text.populate_screen_line_starting_pos(State, State.cursor1.line) local screen_lines = State.line_cache[State.cursor1.line].screen_line_starting_pos diff --git a/text_tests.lua b/text_tests.lua index a01ad3e..21a085a 100644 --- a/text_tests.lua +++ b/text_tests.lua @@ -864,6 +864,33 @@ function test_select_text_using_mouse_starting_above_text_wrapping_line() check_eq(Editor_state.selection1.pos, 3, 'selection:pos') end +function test_select_text_using_mouse_starting_below_text() + -- I'd like to test what happens when a mouse click is below some page of + -- text, potentially even in the middle of a line. + -- However, it's brittle to set up a text line boundary just right. + -- So I'm going to just check things below the bottom of the final line of + -- text when it's in the middle of the screen. + -- final screen line ends in the middle of screen + App.screen.init{width=50, height=60} + Editor_state = edit.initialize_test_state() + Editor_state.lines = load_array{'abcde'} + Text.redraw_all(Editor_state) + Editor_state.cursor1 = {line=1, pos=1} + Editor_state.screen_top1 = {line=1, pos=1} + Editor_state.screen_bottom1 = {} + edit.draw(Editor_state) + local y = Editor_state.top + App.screen.check(y, 'ab', 'baseline:screen:1') + y = y + Editor_state.line_height + App.screen.check(y, 'cde', 'baseline:screen:2') + -- press mouse above first line of text + edit.run_after_mouse_press(Editor_state, 5,App.screen.height-5, 1) + -- selection is past bottom-most text in screen + check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil') + check_eq(Editor_state.selection1.line, 1, 'selection:line') + check_eq(Editor_state.selection1.pos, 6, 'selection:pos') +end + function test_select_text_using_mouse_and_shift() App.screen.init{width=50, height=60} Editor_state = edit.initialize_test_state() From 637e28f300def4172459dd1d6fdf6efd53200ea8 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sun, 4 Jun 2023 12:33:23 -0700 Subject: [PATCH 11/11] port inscript's bugfix to source editor --- edit.lua | 2 +- source.lua | 5 +++ source_edit.lua | 45 +++++++++++++++++------ source_select.lua | 22 ++--------- source_text.lua | 15 ++++++++ source_text_tests.lua | 85 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 143 insertions(+), 31 deletions(-) diff --git a/edit.lua b/edit.lua index f436f36..e1a7fd5 100644 --- a/edit.lua +++ b/edit.lua @@ -252,7 +252,7 @@ function edit.mouse_press(State, x,y, mouse_button) -- press and hold to start a selection: sets selection on press, cursor on release -- press and hold, then press shift: ignore shift -- i.e. mouse_release should never look at shift state - print_and_log(('edit.mouse_press: in line %d'):format(line_index)) +--? print_and_log(('edit.mouse_press: in line %d'):format(line_index)) State.old_cursor1 = State.cursor1 State.old_selection1 = State.selection1 State.mousepress_shift = App.shift_down() diff --git a/source.lua b/source.lua index cab4512..b4489e1 100644 --- a/source.lua +++ b/source.lua @@ -113,6 +113,11 @@ function source.initialize_edit_side() end end +function print_and_log(s) + print(s) + log(3, s) +end + function source.load_settings() local settings = Settings.source love.graphics.setFont(love.graphics.newFont(settings.font_height)) diff --git a/source_edit.lua b/source_edit.lua index 58b1ccc..565b989 100644 --- a/source_edit.lua +++ b/source_edit.lua @@ -76,8 +76,6 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c old_cursor1 = nil, old_selection1 = nil, mousepress_shift = nil, - -- when selecting text, avoid recomputing some state on every single frame - recent_mouse = {}, -- cursor coordinates in pixels cursor_x = 0, @@ -155,12 +153,13 @@ function edit.draw(State, hide_cursor) State.cursor_x = nil State.cursor_y = nil local y = State.top + local screen_bottom1 = {line=nil, pos=nil} --? print('== draw') for line_index = State.screen_top1.line,#State.lines do local line = State.lines[line_index] --? print('draw:', y, line_index, line) if y + State.line_height > App.screen.height then break end - State.screen_bottom1 = {line=line_index, pos=nil} + screen_bottom1.line = line_index if line.mode == 'text' then --? print('text.draw', y, line_index) local startpos = 1 @@ -183,7 +182,7 @@ function edit.draw(State, hide_cursor) end, }) end - y, State.screen_bottom1.pos = Text.draw(State, line_index, y, startpos, hide_cursor) + y, screen_bottom1.pos = Text.draw(State, line_index, y, startpos, hide_cursor) --? print('=> y', y) elseif line.mode == 'drawing' then y = y+Drawing_padding_top @@ -194,6 +193,7 @@ function edit.draw(State, hide_cursor) assert(false) end end + State.screen_bottom1 = screen_bottom1 if State.search_term then Text.draw_search_bar(State) end @@ -224,12 +224,23 @@ end function edit.mouse_press(State, x,y, mouse_button) if State.search_term then return end ---? print('press', State.cursor1.line) +--? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos)) if mouse_press_consumed_by_any_button_handler(State, x,y, mouse_button) then -- press on a button and it returned 'true' to short-circuit return end + if y < State.top then + State.old_cursor1 = State.cursor1 + State.old_selection1 = State.selection1 + State.mousepress_shift = App.shift_down() + State.selection1 = { + line=State.screen_top1.line, + pos=State.screen_top1.pos, + } + return + end + for line_index,line in ipairs(State.lines) do if line.mode == 'text' then if Text.in_line(State, line_index, x,y) then @@ -242,6 +253,7 @@ function edit.mouse_press(State, x,y, mouse_button) -- press and hold to start a selection: sets selection on press, cursor on release -- press and hold, then press shift: ignore shift -- i.e. mouse_release should never look at shift state +--? print_and_log(('edit.mouse_press: in line %d'):format(line_index)) State.old_cursor1 = State.cursor1 State.old_selection1 = State.selection1 State.mousepress_shift = App.shift_down() @@ -249,8 +261,7 @@ function edit.mouse_press(State, x,y, mouse_button) line=line_index, pos=Text.to_pos_on_line(State, line_index, x, y), } ---? print('selection', State.selection1.line, State.selection1.pos) - break + return end elseif line.mode == 'drawing' then local line_cache = State.line_cache[line_index] @@ -259,15 +270,24 @@ function edit.mouse_press(State, x,y, mouse_button) State.lines.current_drawing = line Drawing.before = snapshot(State, line_index) Drawing.mouse_press(State, line_index, x,y, mouse_button) - break + return end end end + + -- still here? click is below all screen lines + State.old_cursor1 = State.cursor1 + State.old_selection1 = State.selection1 + State.mousepress_shift = App.shift_down() + State.selection1 = { + line=State.screen_bottom1.line, + pos=Text.pos_at_end_of_screen_line(State, State.screen_bottom1), + } end function edit.mouse_release(State, x,y, mouse_button) if State.search_term then return end ---? print('release', State.cursor1.line) +--? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos)) if State.lines.current_drawing then Drawing.mouse_release(State, x,y, mouse_button) schedule_save(State) @@ -276,15 +296,16 @@ function edit.mouse_release(State, x,y, mouse_button) Drawing.before = nil end else +--? print_and_log('edit.mouse_release: no current drawing') for line_index,line in ipairs(State.lines) do if line.mode == 'text' then if Text.in_line(State, line_index, x,y) then ---? print('reset selection') +--? print_and_log(('edit.mouse_release: in line %d'):format(line_index)) State.cursor1 = { line=line_index, pos=Text.to_pos_on_line(State, line_index, x, y), } ---? print('cursor', State.cursor1.line, State.cursor1.pos) +--? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos)) if State.mousepress_shift then if State.old_selection1.line == nil then State.selection1 = State.old_cursor1 @@ -300,7 +321,7 @@ function edit.mouse_release(State, x,y, mouse_button) end end end ---? print('selection:', State.selection1.line, State.selection1.pos) +--? print_and_log(('edit.mouse_release: finally selection %s,%s cursor %d,%d'):format(tostring(State.selection1.line), tostring(State.selection1.pos), State.cursor1.line, State.cursor1.pos)) end end diff --git a/source_select.lua b/source_select.lua index 2298513..9b2a278 100644 --- a/source_select.lua +++ b/source_select.lua @@ -1,6 +1,4 @@ -- helpers for selecting portions of text --- To keep things simple, we'll ignore the B side when selections start on the --- A side, and stick to within a single B side selections start in. -- Return any intersection of the region from State.selection1 to State.cursor1 (or -- current mouse, if mouse is pressed; or recent mouse if mouse is pressed and @@ -31,7 +29,6 @@ function Text.clip_selection(State, line_index, apos, bpos) -- compare bounds more carefully (start inclusive, end exclusive) local a_ge = Text.le1({line=minl, pos=minp}, {line=line_index, pos=apos}) local b_lt = Text.lt1({line=line_index, pos=bpos}, {line=maxl, pos=maxp}) ---? print(minl,line_index,maxl, '--', minp,apos,bpos,maxp, '--', a_ge,b_lt) if a_ge and b_lt then -- fully contained return apos,bpos @@ -62,7 +59,6 @@ function Text.draw_highlight(State, line, x,y, pos, lo,hi) local before = line.data:sub(pos_offset, lo_offset-1) lo_px = App.width(before) end ---? print(lo,pos,hi, '--', lo_offset,pos_offset,hi_offset, '--', lo_px) local s = line.data:sub(lo_offset, hi_offset-1) App.color(Highlight_color) love.graphics.rectangle('fill', x+lo_px,y, App.width(s),State.line_height) @@ -71,22 +67,11 @@ function Text.draw_highlight(State, line, x,y, pos, lo,hi) end end --- inefficient for some reason, so don't do it on every frame function Text.mouse_pos(State) - local time = love.timer.getTime() - if State.recent_mouse.time and State.recent_mouse.time > time-0.1 then - return State.recent_mouse.line, State.recent_mouse.pos + local x,y = App.mouse_x(), App.mouse_y() + if y < State.line_cache[State.screen_top1.line].starty then + return State.screen_top1.line, State.screen_top1.pos end - State.recent_mouse.time = time - local line,pos = Text.to_pos(State, App.mouse_x(), App.mouse_y()) - if line then - State.recent_mouse.line = line - State.recent_mouse.pos = pos - end - return State.recent_mouse.line, State.recent_mouse.pos -end - -function Text.to_pos(State, x,y) for line_index,line in ipairs(State.lines) do if line.mode == 'text' then if Text.in_line(State, line_index, x,y) then @@ -94,6 +79,7 @@ function Text.to_pos(State, x,y) end end end + return State.screen_bottom1.line, Text.pos_at_end_of_screen_line(State, State.screen_bottom1) end function Text.cut_selection(State) diff --git a/source_text.lua b/source_text.lua index 290d982..1a42310 100644 --- a/source_text.lua +++ b/source_text.lua @@ -650,6 +650,7 @@ function Text.right_without_scroll(State) end end +-- result: pos, index of screen line function Text.pos_at_start_of_screen_line(State, loc1) Text.populate_screen_line_starting_pos(State, loc1.line) local line_cache = State.line_cache[loc1.line] @@ -662,6 +663,20 @@ function Text.pos_at_start_of_screen_line(State, loc1) assert(false) end +function Text.pos_at_end_of_screen_line(State, loc1) + Text.populate_screen_line_starting_pos(State, loc1.line) + local line_cache = State.line_cache[loc1.line] + local most_recent_final_pos = utf8.len(State.lines[loc1.line].data)+1 + for i=#line_cache.screen_line_starting_pos,1,-1 do + local spos = line_cache.screen_line_starting_pos[i] + if spos <= loc1.pos then + return most_recent_final_pos + end + most_recent_final_pos = spos-1 + end + assert(false) +end + function Text.cursor_at_final_screen_line(State) Text.populate_screen_line_starting_pos(State, State.cursor1.line) local screen_lines = State.line_cache[State.cursor1.line].screen_line_starting_pos diff --git a/source_text_tests.lua b/source_text_tests.lua index 11e7613..c2e054a 100644 --- a/source_text_tests.lua +++ b/source_text_tests.lua @@ -1,4 +1,6 @@ -- major tests for text editing flows +-- Arguably this should be called source_edit_tests.lua, +-- but that would mess up the git blame at this point. function test_initial_state() App.screen.init{width=120, height=60} @@ -828,6 +830,67 @@ function test_select_text_using_mouse() check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos') end +function test_select_text_using_mouse_starting_above_text() + App.screen.init{width=50, height=60} + Editor_state = edit.initialize_test_state() + Editor_state.lines = load_array{'abc', 'def', 'xyz'} + Text.redraw_all(Editor_state) + Editor_state.cursor1 = {line=1, pos=1} + Editor_state.screen_top1 = {line=1, pos=1} + Editor_state.screen_bottom1 = {} + Editor_state.selection1 = {} + edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache + -- press mouse above first line of text + edit.run_after_mouse_press(Editor_state, Editor_state.left+8,5, 1) + check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil') + check_eq(Editor_state.selection1.line, 1, 'selection:line') + check_eq(Editor_state.selection1.pos, 1, 'selection:pos') +end + +function test_select_text_using_mouse_starting_above_text_wrapping_line() + -- first screen line starts in the middle of a line + App.screen.init{width=50, height=60} + Editor_state = edit.initialize_test_state() + Editor_state.lines = load_array{'abc', 'defgh', 'xyz'} + Text.redraw_all(Editor_state) + Editor_state.cursor1 = {line=2, pos=5} + Editor_state.screen_top1 = {line=2, pos=3} + Editor_state.screen_bottom1 = {} + -- press mouse above first line of text + edit.run_after_mouse_press(Editor_state, Editor_state.left+8,5, 1) + -- selection is at screen top + check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil') + check_eq(Editor_state.selection1.line, 2, 'selection:line') + check_eq(Editor_state.selection1.pos, 3, 'selection:pos') +end + +function test_select_text_using_mouse_starting_below_text() + -- I'd like to test what happens when a mouse click is below some page of + -- text, potentially even in the middle of a line. + -- However, it's brittle to set up a text line boundary just right. + -- So I'm going to just check things below the bottom of the final line of + -- text when it's in the middle of the screen. + -- final screen line ends in the middle of screen + App.screen.init{width=50, height=60} + Editor_state = edit.initialize_test_state() + Editor_state.lines = load_array{'abcde'} + Text.redraw_all(Editor_state) + Editor_state.cursor1 = {line=1, pos=1} + Editor_state.screen_top1 = {line=1, pos=1} + Editor_state.screen_bottom1 = {} + edit.draw(Editor_state) + local y = Editor_state.top + App.screen.check(y, 'ab', 'baseline:screen:1') + y = y + Editor_state.line_height + App.screen.check(y, 'cde', 'baseline:screen:2') + -- press mouse above first line of text + edit.run_after_mouse_press(Editor_state, 5,App.screen.height-5, 1) + -- selection is past bottom-most text in screen + check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil') + check_eq(Editor_state.selection1.line, 1, 'selection:line') + check_eq(Editor_state.selection1.pos, 6, 'selection:pos') +end + function test_select_text_using_mouse_and_shift() App.screen.init{width=50, height=60} Editor_state = edit.initialize_test_state() @@ -882,6 +945,28 @@ function test_select_text_repeatedly_using_mouse_and_shift() check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos') end +function test_select_all_text() + -- display a single line of text + App.screen.init{width=75, height=80} + Editor_state = edit.initialize_test_state() + Editor_state.lines = load_array{'abc def'} + Text.redraw_all(Editor_state) + Editor_state.cursor1 = {line=1, pos=1} + Editor_state.screen_top1 = {line=1, pos=1} + Editor_state.screen_bottom1 = {} + edit.draw(Editor_state) + -- select all + App.fake_key_press('lctrl') + edit.run_after_keychord(Editor_state, 'C-a') + App.fake_key_release('lctrl') + edit.key_release(Editor_state, 'lctrl') + -- selection + check_eq(Editor_state.selection1.line, 1, 'selection:line') + check_eq(Editor_state.selection1.pos, 1, 'selection:pos') + check_eq(Editor_state.cursor1.line, 1, 'cursor:line') + check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos') +end + function test_cut_without_selection() -- display a few lines App.screen.init{width=Editor_state.left+30, height=60}