about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-06-05 08:29:38 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-06-05 08:29:38 -0700
commitde473046bc6ad8b33e57399e662b271cd2f8cc44 (patch)
treed693df8fdaf802f3030233d9ee789f61bcdb2d94
parent5055361209da2dc9427ffafe09c4d29933a94747 (diff)
downloadtext.love-de473046bc6ad8b33e57399e662b271cd2f8cc44.tar.gz
check for scroll when just typing
-rw-r--r--text.lua5
-rw-r--r--text_tests.lua35
2 files changed, 38 insertions, 2 deletions
diff --git a/text.lua b/text.lua
index b8d22cd..7fa4d2e 100644
--- a/text.lua
+++ b/text.lua
@@ -155,7 +155,12 @@ function Text.insert_at_cursor(t)
   Lines[Cursor1.line].data = string.sub(Lines[Cursor1.line].data, 1, byte_offset-1)..t..string.sub(Lines[Cursor1.line].data, byte_offset)
   Lines[Cursor1.line].fragments = nil
   Lines[Cursor1.line].screen_line_starting_pos = nil
+  local scroll_down = Text.le1(Screen_bottom1, Cursor1)
   Cursor1.pos = Cursor1.pos+1
+  if scroll_down then
+    Text.populate_screen_line_starting_pos(Cursor1.line)
+    Text.snap_cursor_to_bottom_of_screen()
+  end
 end
 
 -- Don't handle any keys here that would trigger love.textinput above.
diff --git a/text_tests.lua b/text_tests.lua
index 6982c04..c11ef6e 100644
--- a/text_tests.lua
+++ b/text_tests.lua
@@ -78,12 +78,11 @@ function test_edit_wrapping_text()
   Cursor1 = {line=2, pos=4}
   Screen_top1 = {line=1, pos=1}
   Screen_bottom1 = {}
+  App.draw()
   App.run_after_textinput('g')
   App.run_after_textinput('h')
   App.run_after_textinput('i')
   App.run_after_textinput('j')
-  App.run_after_textinput('k')
-  App.run_after_textinput('l')
   local y = Margin_top
   App.screen.check(y, 'abc', 'F - test_edit_wrapping_text/screen:1')
   y = y + Line_height
@@ -727,6 +726,37 @@ function test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom()
   App.screen.check(y, 'kl', 'F - test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom/screen:2')
 end
 
+function test_typing_on_bottom_line_scrolls_down()
+  io.write('\ntest_typing_on_bottom_line_scrolls_down')
+  -- display a few lines with cursor on bottom line
+  App.screen.init{width=25+30, height=60}
+  Lines = load_array{'abc', 'def', 'ghi', 'jkl'}
+  Line_width = App.screen.width
+  Cursor1 = {line=3, pos=4}
+  Screen_top1 = {line=1, pos=1}
+  Screen_bottom1 = {}
+  App.draw()
+  local y = Margin_top
+  App.screen.check(y, 'abc', 'F - test_typing_on_bottom_line_scrolls_down/baseline/screen:1')
+  y = y + Line_height
+  App.screen.check(y, 'def', 'F - test_typing_on_bottom_line_scrolls_down/baseline/screen:2')
+  y = y + Line_height
+  App.screen.check(y, 'ghi', 'F - test_typing_on_bottom_line_scrolls_down/baseline/screen:3')
+  -- after typing something the line wraps and the screen scrolls down
+  App.run_after_textinput('j')
+  App.run_after_textinput('k')
+  App.run_after_textinput('l')
+  check_eq(Screen_top1.line, 2, 'F - test_typing_on_bottom_line_scrolls_down/screen_top')
+  check_eq(Cursor1.line, 3, 'F - test_typing_on_bottom_line_scrolls_down/cursor:line')
+  check_eq(Cursor1.pos, 7, 'F - test_typing_on_bottom_line_scrolls_down/cursor:pos')
+  y = Margin_top
+  App.screen.check(y, 'def', 'F - test_typing_on_bottom_line_scrolls_down/screen:1')
+  y = y + Line_height
+  App.screen.check(y, 'ghijk', 'F - test_typing_on_bottom_line_scrolls_down/screen:2')
+  y = y + Line_height
+  App.screen.check(y, 'l', 'F - test_typing_on_bottom_line_scrolls_down/screen:3')
+end
+
 function test_position_cursor_on_recently_edited_wrapping_line()
   -- draw a line wrapping over 2 screen lines
   io.write('\ntest_position_cursor_on_recently_edited_wrapping_line')
@@ -924,6 +954,7 @@ function test_undo_insert_text()
   Screen_top1 = {line=1, pos=1}
   Screen_bottom1 = {}
   -- insert a character
+  App.draw()
   App.run_after_textinput('g')
   check_eq(Cursor1.line, 2, 'F - test_undo_insert_text/baseline/cursor:line')
   check_eq(Cursor1.pos, 5, 'F - test_undo_insert_text/baseline/cursor:pos')