From 3114176ebd5b9895b5f67c759a2a705f673a4ac0 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 3 Jun 2023 10:36:05 -0700 Subject: 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} -- cgit 1.4.1-2-gfad0