From a472d218f21cc0787a9e52c6cb03819b27a247e9 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 20 Jul 2022 07:08:28 -0700 Subject: allow Text.nearest_pos_less_than to return 0 This eliminates another case of overflowing margins. --- text.lua | 7 ++----- text_tests.lua | 4 ++-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/text.lua b/text.lua index 1f10a1b..c83a177 100644 --- a/text.lua +++ b/text.lua @@ -104,7 +104,7 @@ function Text.compute_fragments(State, line_index) -- We're not going to reimplement TeX here. local bpos = Text.nearest_pos_less_than(frag, State.right - x) --? print('bpos', bpos) - assert(bpos > 0) -- avoid infinite loop when window is too narrow + if bpos == 0 then break end -- avoid infinite loop when window is too narrow local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos --? print('space for '..tostring(bpos)..' graphemes, '..tostring(boffset-1)..' bytes') local frag1 = string.sub(frag, 1, boffset-1) @@ -800,15 +800,12 @@ end -- within x pixels of the left margin function Text.nearest_pos_less_than(line, x) --? print('', '-- nearest_pos_less_than', line, x) - if x == 0 then - return 1 - end local len = utf8.len(line) local max_x = Text.x(line, len) if x > max_x then return len+1 end - local left, right = 1, len+1 + local left, right = 0, len+1 while true do local curr = math.floor((left+right)/2) local currxmin = Text.x(line, curr+1) diff --git a/text_tests.lua b/text_tests.lua index aaeacc8..837fe3a 100644 --- a/text_tests.lua +++ b/text_tests.lua @@ -1044,9 +1044,9 @@ function test_pagedown_can_start_from_middle_of_long_wrapping_line() y = Editor_state.top App.screen.check(y, 'ghi ', 'F - test_pagedown_can_start_from_middle_of_long_wrapping_line/screen:1') y = y + Editor_state.line_height - App.screen.check(y, 'jkl m', 'F - test_pagedown_can_start_from_middle_of_long_wrapping_line/screen:2') + App.screen.check(y, 'jkl ', 'F - test_pagedown_can_start_from_middle_of_long_wrapping_line/screen:2') y = y + Editor_state.line_height - App.screen.check(y, 'no ', 'F - test_pagedown_can_start_from_middle_of_long_wrapping_line/screen:3') + App.screen.check(y, 'mno ', 'F - test_pagedown_can_start_from_middle_of_long_wrapping_line/screen:3') end function test_down_arrow_moves_cursor() -- cgit 1.4.1-2-gfad0