about summary refs log tree commit diff stats
path: root/text_tests.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-09-20 13:39:29 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-09-20 13:39:29 -0700
commitbd6f7d48e76182218877564e8ca672e657f4ef56 (patch)
treee2ed5bd8592ad604612fb2d8f1b49079b0103ae0 /text_tests.lua
parentc43d884b6ffb94803bee9f9e788e4b3a2f74f23b (diff)
downloadtext.love-bd6f7d48e76182218877564e8ca672e657f4ef56.tar.gz
bugfix: clear selection when clicking above or below lines
Matt Wynne pointed out that snap.love would crash when a node went off
screen. While debugging it I noticed that selection1 was being set when
it shouldn't be.

Turns out I introduced a bug when I fixed the inscript bug back in June
(commit 9656e137742). One invariant I want to preserve is: selection1
should be unset after a mouse click (press and release without
intervening drag). This invariant was violated in my bugfix back in
June. I was concerned only with selection back then, and I didn't
realize I was breaking the mouse click case (in a fairly subtle way; you
can have selection set, and when it's set identically to the cursor
everything looks the same).

I think there might still be an issue in snap.love after this fix. I
noticed screen_bottom1.pos was nil, and as far as I recall that should
never happen.
Diffstat (limited to 'text_tests.lua')
-rw-r--r--text_tests.lua24
1 files changed, 24 insertions, 0 deletions
diff --git a/text_tests.lua b/text_tests.lua
index 21a085a..bee6c31 100644
--- a/text_tests.lua
+++ b/text_tests.lua
@@ -275,6 +275,7 @@ function test_click_to_left_of_line()
   Editor_state.cursor1 = {line=1, pos=3}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
+  Editor_state.selection1 = {}
   -- click to the left of the line
   edit.draw(Editor_state)
   edit.run_after_mouse_click(Editor_state, Editor_state.left-4,Editor_state.top+5, 1)
@@ -294,6 +295,7 @@ function test_click_takes_margins_into_account()
   Editor_state.cursor1 = {line=2, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
+  Editor_state.selection1 = {}
   -- click on the other line
   edit.draw(Editor_state)
   edit.run_after_mouse_click(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
@@ -312,11 +314,33 @@ function test_click_on_empty_line()
   Editor_state.cursor1 = {line=2, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
+  Editor_state.selection1 = {}
   -- click on the empty line
   edit.draw(Editor_state)
   edit.run_after_mouse_click(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
   -- cursor moves
   check_eq(Editor_state.cursor1.line, 1, 'cursor')
+  -- selection remains empty
+  check_nil(Editor_state.selection1.line, 'selection is empty to avoid perturbing future edits')
+end
+
+function test_click_below_all_lines()
+  -- display one line
+  App.screen.init{width=50, height=80}
+  Editor_state = edit.initialize_test_state()
+  Editor_state.lines = load_array{'abc'}
+  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 = {}
+  -- click below first line
+  edit.draw(Editor_state)
+  edit.run_after_mouse_click(Editor_state, Editor_state.left+8,Editor_state.top+50, 1)
+  -- cursor doesn't move
+  check_eq(Editor_state.cursor1.line, 1, 'cursor')
+  -- selection remains empty
+  check_nil(Editor_state.selection1.line, 'selection is empty to avoid perturbing future edits')
 end
 
 function test_draw_text()