bugfix: inscript's bug

To fix this I have to first stop incrementally updating screen_bottom1
in the middle of a frame. Now it always has a good value from the end of
a frame.

I'm also running into some limitations in the test I'd ideally like to
write (that are documented in a comment), but I still get some sort of
automated test for this bugfix.
This commit is contained in:
Kartik K. Agaram 2023-06-04 12:20:24 -07:00
parent cf0ba7c154
commit 9656e13774
4 changed files with 65 additions and 16 deletions

View File

@ -152,12 +152,13 @@ function edit.draw(State)
State.cursor_x = nil
State.cursor_y = nil
local y = State.top
local screen_bottom1 = {line=nil, pos=nil}
--? print('== draw')
for line_index = State.screen_top1.line,#State.lines do
local line = State.lines[line_index]
--? print('draw:', y, line_index, line)
if y + State.line_height > App.screen.height then break end
State.screen_bottom1 = {line=line_index, pos=nil}
screen_bottom1.line = line_index
if line.mode == 'text' then
--? print('text.draw', y, line_index)
local startpos = 1
@ -180,7 +181,7 @@ function edit.draw(State)
end,
})
end
y, State.screen_bottom1.pos = Text.draw(State, line_index, y, startpos)
y, screen_bottom1.pos = Text.draw(State, line_index, y, startpos)
--? print('=> y', y)
elseif line.mode == 'drawing' then
y = y+Drawing_padding_top
@ -191,6 +192,7 @@ function edit.draw(State)
assert(false)
end
end
State.screen_bottom1 = screen_bottom1
if State.search_term then
Text.draw_search_bar(State)
end
@ -221,7 +223,7 @@ end
function edit.mouse_press(State, x,y, mouse_button)
if State.search_term then return end
print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
--? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
if mouse_press_consumed_by_any_button_handler(State, x,y, mouse_button) then
-- press on a button and it returned 'true' to short-circuit
return
@ -258,7 +260,7 @@ function edit.mouse_press(State, x,y, mouse_button)
line=line_index,
pos=Text.to_pos_on_line(State, line_index, x, y),
}
break
return
end
elseif line.mode == 'drawing' then
local line_cache = State.line_cache[line_index]
@ -267,15 +269,24 @@ function edit.mouse_press(State, x,y, mouse_button)
State.lines.current_drawing = line
Drawing.before = snapshot(State, line_index)
Drawing.mouse_press(State, line_index, x,y, mouse_button)
break
return
end
end
end
-- still here? click is below all screen lines
State.old_cursor1 = State.cursor1
State.old_selection1 = State.selection1
State.mousepress_shift = App.shift_down()
State.selection1 = {
line=State.screen_bottom1.line,
pos=Text.pos_at_end_of_screen_line(State, State.screen_bottom1),
}
end
function edit.mouse_release(State, x,y, mouse_button)
if State.search_term then return end
print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
--? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
if State.lines.current_drawing then
Drawing.mouse_release(State, x,y, mouse_button)
schedule_save(State)
@ -284,16 +295,16 @@ function edit.mouse_release(State, x,y, mouse_button)
Drawing.before = nil
end
else
print_and_log('edit.mouse_release: no current drawing')
--? print_and_log('edit.mouse_release: no current drawing')
for line_index,line in ipairs(State.lines) do
if line.mode == 'text' then
if Text.in_line(State, line_index, x,y) then
print_and_log(('edit.mouse_release: in line %d'):format(line_index))
--? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
State.cursor1 = {
line=line_index,
pos=Text.to_pos_on_line(State, line_index, x, y),
}
print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
--? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
if State.mousepress_shift then
if State.old_selection1.line == nil then
State.selection1 = State.old_cursor1
@ -309,7 +320,7 @@ function edit.mouse_release(State, x,y, mouse_button)
end
end
end
print_and_log(('edit.mouse_release: finally selection %s,%s cursor %d,%d'):format(tostring(State.selection1.line), tostring(State.selection1.pos), State.cursor1.line, State.cursor1.pos))
--? print_and_log(('edit.mouse_release: finally selection %s,%s cursor %d,%d'):format(tostring(State.selection1.line), tostring(State.selection1.pos), State.cursor1.line, State.cursor1.pos))
end
end

View File

@ -67,13 +67,8 @@ function Text.draw_highlight(State, line, x,y, pos, lo,hi)
end
end
-- inefficient for some reason, so don't do it on every frame
function Text.mouse_pos(State)
local line,pos = Text.to_pos(State, App.mouse_x(), App.mouse_y())
return line, pos
end
function Text.to_pos(State, x,y)
local x,y = App.mouse_x(), App.mouse_y()
if y < State.line_cache[State.screen_top1.line].starty then
return State.screen_top1.line, State.screen_top1.pos
end
@ -84,6 +79,7 @@ function Text.to_pos(State, x,y)
end
end
end
return State.screen_bottom1.line, Text.pos_at_end_of_screen_line(State, State.screen_bottom1)
end
function Text.cut_selection(State)

View File

@ -591,6 +591,7 @@ function Text.right_without_scroll(State)
end
end
-- result: pos, index of screen line
function Text.pos_at_start_of_screen_line(State, loc1)
Text.populate_screen_line_starting_pos(State, loc1.line)
local line_cache = State.line_cache[loc1.line]
@ -603,6 +604,20 @@ function Text.pos_at_start_of_screen_line(State, loc1)
assert(false)
end
function Text.pos_at_end_of_screen_line(State, loc1)
Text.populate_screen_line_starting_pos(State, loc1.line)
local line_cache = State.line_cache[loc1.line]
local most_recent_final_pos = utf8.len(State.lines[loc1.line].data)+1
for i=#line_cache.screen_line_starting_pos,1,-1 do
local spos = line_cache.screen_line_starting_pos[i]
if spos <= loc1.pos then
return most_recent_final_pos
end
most_recent_final_pos = spos-1
end
assert(false)
end
function Text.cursor_at_final_screen_line(State)
Text.populate_screen_line_starting_pos(State, State.cursor1.line)
local screen_lines = State.line_cache[State.cursor1.line].screen_line_starting_pos

View File

@ -864,6 +864,33 @@ function test_select_text_using_mouse_starting_above_text_wrapping_line()
check_eq(Editor_state.selection1.pos, 3, 'selection:pos')
end
function test_select_text_using_mouse_starting_below_text()
-- I'd like to test what happens when a mouse click is below some page of
-- text, potentially even in the middle of a line.
-- However, it's brittle to set up a text line boundary just right.
-- So I'm going to just check things below the bottom of the final line of
-- text when it's in the middle of the screen.
-- final screen line ends in the middle of screen
App.screen.init{width=50, height=60}
Editor_state = edit.initialize_test_state()
Editor_state.lines = load_array{'abcde'}
Text.redraw_all(Editor_state)
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw(Editor_state)
local y = Editor_state.top
App.screen.check(y, 'ab', 'baseline:screen:1')
y = y + Editor_state.line_height
App.screen.check(y, 'cde', 'baseline:screen:2')
-- press mouse above first line of text
edit.run_after_mouse_press(Editor_state, 5,App.screen.height-5, 1)
-- selection is past bottom-most text in screen
check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil')
check_eq(Editor_state.selection1.line, 1, 'selection:line')
check_eq(Editor_state.selection1.pos, 6, 'selection:pos')
end
function test_select_text_using_mouse_and_shift()
App.screen.init{width=50, height=60}
Editor_state = edit.initialize_test_state()