about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-07-11 19:18:54 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-07-11 19:18:54 -0700
commit6c4483976e49c3f63944a9ee6c00c585bc1105be (patch)
tree663ce3778f45c100a97cf89cb2a47ab294e8ae50
parentbc2c14c89921d255e99778e6c6b2c4f2e755d08f (diff)
downloadview.love-6c4483976e49c3f63944a9ee6c00c585bc1105be.tar.gz
skip multiple consecutive whitespace
-rw-r--r--text.lua29
-rw-r--r--text_tests.lua22
2 files changed, 49 insertions, 2 deletions
diff --git a/text.lua b/text.lua
index 098129d..6661fd8 100644
--- a/text.lua
+++ b/text.lua
@@ -515,9 +515,22 @@ function Text.end_of_line(left, right)
 end
 
 function Text.word_left(left, right)
+  -- skip some whitespace
   while true do
+    if Cursor1.pos == 1 then
+      break
+    end
+    if Text.match(Lines[Cursor1.line].data, Cursor1.pos-1, '%S') then
+      break
+    end
     Text.left(left, right)
-    if Cursor1.pos == 1 then break end
+  end
+  -- skip some non-whitespace
+  while true do
+    Text.left(left, right)
+    if Cursor1.pos == 1 then
+      break
+    end
     assert(Cursor1.pos > 1)
     if Text.match(Lines[Cursor1.line].data, Cursor1.pos-1, '%s') then
       break
@@ -526,9 +539,21 @@ function Text.word_left(left, right)
 end
 
 function Text.word_right(left, right)
+  -- skip some whitespace
   while true do
+    if Cursor1.pos > utf8.len(Lines[Cursor1.line].data) then
+      break
+    end
+    if Text.match(Lines[Cursor1.line].data, Cursor1.pos, '%S') then
+      break
+    end
     Text.right_without_scroll()
-    if Cursor1.pos > utf8.len(Lines[Cursor1.line].data) then break end
+  end
+  while true do
+    Text.right_without_scroll()
+    if Cursor1.pos > utf8.len(Lines[Cursor1.line].data) then
+      break
+    end
     if Text.match(Lines[Cursor1.line].data, Cursor1.pos, '%s') then
       break
     end
diff --git a/text_tests.lua b/text_tests.lua
index 1d508bc..49c737d 100644
--- a/text_tests.lua
+++ b/text_tests.lua
@@ -152,6 +152,17 @@ function test_skip_past_tab_to_previous_word()
   check_eq(Cursor1.pos, 9, 'F - test_skip_past_tab_to_previous_word')
 end
 
+function test_skip_multiple_spaces_to_previous_word()
+  io.write('\ntest_skip_multiple_spaces_to_previous_word')
+  App.screen.init{width=120, height=60}
+  Lines = load_array{'abc  def'}
+  Cursor1 = {line=1, pos=6}  -- at the start of second word
+  Margin_right = 0; Margin_width = Margin_left
+  App.draw()
+  App.run_after_keychord('M-left')
+  check_eq(Cursor1.pos, 1, 'F - test_skip_multiple_spaces_to_previous_word')
+end
+
 function test_move_to_start_of_word_on_previous_line()
   io.write('\ntest_move_to_start_of_word_on_previous_line')
   App.screen.init{width=120, height=60}
@@ -197,6 +208,17 @@ function test_skip_past_tab_to_next_word()
   check_eq(Cursor1.pos, 4, 'F - test_skip_past_tab_to_next_word')
 end
 
+function test_skip_multiple_spaces_to_next_word()
+  io.write('\ntest_skip_multiple_spaces_to_next_word')
+  App.screen.init{width=120, height=60}
+  Lines = load_array{'abc  def'}
+  Cursor1 = {line=1, pos=4}  -- at the start of second word
+  Margin_right = 0; Margin_width = Margin_left
+  App.draw()
+  App.run_after_keychord('M-right')
+  check_eq(Cursor1.pos, 9, 'F - test_skip_multiple_spaces_to_next_word')
+end
+
 function test_move_past_end_of_word_on_next_line()
   io.write('\ntest_move_past_end_of_word_on_next_line')
   App.screen.init{width=120, height=60}