about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-07-17 22:15:49 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-07-17 22:29:37 -0700
commit82cdd9ddd17f2ba1a637232fe22f790c5a769728 (patch)
tree0a31556e0cf705911bb9d2c9e4fcba36276b0720
parent29dac6a6eccf5e5f09debe789fb4f314e460088b (diff)
downloadtext.love-82cdd9ddd17f2ba1a637232fe22f790c5a769728.tar.gz
bugfix: couple of margin-relative computations
-rw-r--r--text.lua4
-rw-r--r--text_tests.lua62
2 files changed, 63 insertions, 3 deletions
diff --git a/text.lua b/text.lua
index bafc097..e28a9dc 100644
--- a/text.lua
+++ b/text.lua
@@ -728,7 +728,7 @@ function Text.to_pos_on_line(State, line_index, mx, my)
       -- On all wrapped screen lines but the final one, clicks past end of
       -- line position cursor on final character of screen line.
       -- (The final screen line positions past end of screen line as always.)
-      if screen_line_index < #line_cache.screen_line_starting_pos and mx > Text.screen_line_width(State, line_index, screen_line_index) then
+      if screen_line_index < #line_cache.screen_line_starting_pos and mx > State.left + Text.screen_line_width(State, line_index, screen_line_index) then
 --?         print('past end of non-final line; return')
         return line_cache.screen_line_starting_pos[screen_line_index+1]-1
       end
@@ -978,7 +978,7 @@ function Text.tweak_screen_top_and_cursor(State)
 --?       print('tweak')
       State.cursor1 = {
           line=State.screen_bottom1.line,
-          pos=Text.to_pos_on_line(State, State.screen_bottom1.line, App.screen.width-5, App.screen.height-5),
+          pos=Text.to_pos_on_line(State, State.screen_bottom1.line, State.right-5, App.screen.height-5),
       }
     end
   end
diff --git a/text_tests.lua b/text_tests.lua
index 37deec9..e9741c9 100644
--- a/text_tests.lua
+++ b/text_tests.lua
@@ -265,10 +265,31 @@ function test_click_with_mouse()
   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, 'F - test_click_with_mouse/cursor')
+  check_eq(Editor_state.cursor1.line, 1, 'F - test_click_with_mouse/cursor:line')
+  check_eq(Editor_state.cursor1.pos, 2, 'F - test_click_with_mouse/cursor:pos')
   check_nil(Editor_state.selection1.line, 'F - test_click_with_mouse/selection is empty to avoid perturbing future edits')
 end
 
+function test_click_with_mouse_takes_margins_into_account()
+  io.write('\ntest_click_with_mouse_takes_margins_into_account')
+  -- display two lines with cursor on one of them
+  App.screen.init{width=100, height=80}
+  Editor_state = edit.initialize_test_state()
+  Editor_state.left = 50  -- occupy only right side of screen
+  Editor_state.lines = load_array{'abc', 'def'}
+  Text.redraw_all(Editor_state)
+  Editor_state.cursor1 = {line=2, pos=1}
+  Editor_state.screen_top1 = {line=1, pos=1}
+  Editor_state.screen_bottom1 = {}
+  -- 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)
+  -- cursor moves
+  check_eq(Editor_state.cursor1.line, 1, 'F - test_click_with_mouse_takes_margins_into_account/cursor:line')
+  check_eq(Editor_state.cursor1.pos, 2, 'F - test_click_with_mouse_takes_margins_into_account/cursor:pos')
+  check_nil(Editor_state.selection1.line, 'F - test_click_with_mouse_takes_margins_into_account/selection is empty to avoid perturbing future edits')
+end
+
 function test_click_with_mouse_on_empty_line()
   io.write('\ntest_click_with_mouse_on_empty_line')
   -- display two lines with the first one empty
@@ -340,6 +361,45 @@ function test_draw_word_wrapping_text()
   App.screen.check(y, 'ghi', 'F - test_draw_word_wrapping_text/screen:3')
 end
 
+function test_click_with_mouse_on_wrapping_line()
+  io.write('\ntest_click_with_mouse_on_wrapping_line')
+  -- display two lines with cursor on one of them
+  App.screen.init{width=50, height=80}
+  Editor_state = edit.initialize_test_state()
+  Editor_state.lines = load_array{'abc def ghi jkl mno pqr stu'}
+  Text.redraw_all(Editor_state)
+  Editor_state.cursor1 = {line=1, pos=20}
+  Editor_state.screen_top1 = {line=1, pos=1}
+  Editor_state.screen_bottom1 = {}
+  -- 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)
+  -- cursor moves
+  check_eq(Editor_state.cursor1.line, 1, 'F - test_click_with_mouse_on_wrapping_line/cursor:line')
+  check_eq(Editor_state.cursor1.pos, 2, 'F - test_click_with_mouse_on_wrapping_line/cursor:pos')
+  check_nil(Editor_state.selection1.line, 'F - test_click_with_mouse_on_wrapping_line/selection is empty to avoid perturbing future edits')
+end
+
+function test_click_with_mouse_on_wrapping_line_takes_margins_into_account()
+  io.write('\ntest_click_with_mouse_on_wrapping_line_takes_margins_into_account')
+  -- display two lines with cursor on one of them
+  App.screen.init{width=100, height=80}
+  Editor_state = edit.initialize_test_state()
+  Editor_state.left = 50  -- occupy only right side of screen
+  Editor_state.lines = load_array{'abc def ghi jkl mno pqr stu'}
+  Text.redraw_all(Editor_state)
+  Editor_state.cursor1 = {line=1, pos=20}
+  Editor_state.screen_top1 = {line=1, pos=1}
+  Editor_state.screen_bottom1 = {}
+  -- 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)
+  -- cursor moves
+  check_eq(Editor_state.cursor1.line, 1, 'F - test_click_with_mouse_on_wrapping_line_takes_margins_into_account/cursor:line')
+  check_eq(Editor_state.cursor1.pos, 2, 'F - test_click_with_mouse_on_wrapping_line_takes_margins_into_account/cursor:pos')
+  check_nil(Editor_state.selection1.line, 'F - test_click_with_mouse_on_wrapping_line_takes_margins_into_account/selection is empty to avoid perturbing future edits')
+end
+
 function test_draw_text_wrapping_within_word()
   -- arrange a screen line that needs to be split within a word
   io.write('\ntest_draw_text_wrapping_within_word')