diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2023-06-04 12:33:23 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2023-06-04 12:33:23 -0700 |
commit | 637e28f300def4172459dd1d6fdf6efd53200ea8 (patch) | |
tree | b53422ef58db46b96564b83b468e1689a1a51fba | |
parent | 9656e137742eb442e9ce013dd3f25cf6df8c9fad (diff) | |
download | view.love-637e28f300def4172459dd1d6fdf6efd53200ea8.tar.gz |
port inscript's bugfix to source editor
-rw-r--r-- | edit.lua | 2 | ||||
-rw-r--r-- | source.lua | 5 | ||||
-rw-r--r-- | source_edit.lua | 45 | ||||
-rw-r--r-- | source_select.lua | 22 | ||||
-rw-r--r-- | source_text.lua | 15 | ||||
-rw-r--r-- | source_text_tests.lua | 85 |
6 files changed, 143 insertions, 31 deletions
diff --git a/edit.lua b/edit.lua index f436f36..e1a7fd5 100644 --- a/edit.lua +++ b/edit.lua @@ -252,7 +252,7 @@ function edit.mouse_press(State, x,y, mouse_button) -- press and hold to start a selection: sets selection on press, cursor on release -- press and hold, then press shift: ignore shift -- i.e. mouse_release should never look at shift state - print_and_log(('edit.mouse_press: in line %d'):format(line_index)) +--? print_and_log(('edit.mouse_press: in line %d'):format(line_index)) State.old_cursor1 = State.cursor1 State.old_selection1 = State.selection1 State.mousepress_shift = App.shift_down() diff --git a/source.lua b/source.lua index cab4512..b4489e1 100644 --- a/source.lua +++ b/source.lua @@ -113,6 +113,11 @@ function source.initialize_edit_side() end end +function print_and_log(s) + print(s) + log(3, s) +end + function source.load_settings() local settings = Settings.source love.graphics.setFont(love.graphics.newFont(settings.font_height)) diff --git a/source_edit.lua b/source_edit.lua index 58b1ccc..565b989 100644 --- a/source_edit.lua +++ b/source_edit.lua @@ -76,8 +76,6 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c old_cursor1 = nil, old_selection1 = nil, mousepress_shift = nil, - -- when selecting text, avoid recomputing some state on every single frame - recent_mouse = {}, -- cursor coordinates in pixels cursor_x = 0, @@ -155,12 +153,13 @@ function edit.draw(State, hide_cursor) 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 @@ -183,7 +182,7 @@ function edit.draw(State, hide_cursor) end, }) end - y, State.screen_bottom1.pos = Text.draw(State, line_index, y, startpos, hide_cursor) + y, screen_bottom1.pos = Text.draw(State, line_index, y, startpos, hide_cursor) --? print('=> y', y) elseif line.mode == 'drawing' then y = y+Drawing_padding_top @@ -194,6 +193,7 @@ function edit.draw(State, hide_cursor) assert(false) end end + State.screen_bottom1 = screen_bottom1 if State.search_term then Text.draw_search_bar(State) end @@ -224,12 +224,23 @@ end function edit.mouse_press(State, x,y, mouse_button) if State.search_term then return end ---? print('press', State.cursor1.line) +--? 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 end + if y < State.top then + State.old_cursor1 = State.cursor1 + State.old_selection1 = State.selection1 + State.mousepress_shift = App.shift_down() + State.selection1 = { + line=State.screen_top1.line, + pos=State.screen_top1.pos, + } + return + end + for line_index,line in ipairs(State.lines) do if line.mode == 'text' then if Text.in_line(State, line_index, x,y) then @@ -242,6 +253,7 @@ function edit.mouse_press(State, x,y, mouse_button) -- press and hold to start a selection: sets selection on press, cursor on release -- press and hold, then press shift: ignore shift -- i.e. mouse_release should never look at shift state +--? print_and_log(('edit.mouse_press: in line %d'):format(line_index)) State.old_cursor1 = State.cursor1 State.old_selection1 = State.selection1 State.mousepress_shift = App.shift_down() @@ -249,8 +261,7 @@ function edit.mouse_press(State, x,y, mouse_button) line=line_index, pos=Text.to_pos_on_line(State, line_index, x, y), } ---? print('selection', State.selection1.line, State.selection1.pos) - break + return end elseif line.mode == 'drawing' then local line_cache = State.line_cache[line_index] @@ -259,15 +270,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('release', State.cursor1.line) +--? 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) @@ -276,15 +296,16 @@ function edit.mouse_release(State, x,y, mouse_button) Drawing.before = nil end else +--? 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('reset selection') +--? 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('cursor', 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 @@ -300,7 +321,7 @@ function edit.mouse_release(State, x,y, mouse_button) end end end ---? print('selection:', State.selection1.line, State.selection1.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 diff --git a/source_select.lua b/source_select.lua index 2298513..9b2a278 100644 --- a/source_select.lua +++ b/source_select.lua @@ -1,6 +1,4 @@ -- helpers for selecting portions of text --- To keep things simple, we'll ignore the B side when selections start on the --- A side, and stick to within a single B side selections start in. -- Return any intersection of the region from State.selection1 to State.cursor1 (or -- current mouse, if mouse is pressed; or recent mouse if mouse is pressed and @@ -31,7 +29,6 @@ function Text.clip_selection(State, line_index, apos, bpos) -- compare bounds more carefully (start inclusive, end exclusive) local a_ge = Text.le1({line=minl, pos=minp}, {line=line_index, pos=apos}) local b_lt = Text.lt1({line=line_index, pos=bpos}, {line=maxl, pos=maxp}) ---? print(minl,line_index,maxl, '--', minp,apos,bpos,maxp, '--', a_ge,b_lt) if a_ge and b_lt then -- fully contained return apos,bpos @@ -62,7 +59,6 @@ function Text.draw_highlight(State, line, x,y, pos, lo,hi) local before = line.data:sub(pos_offset, lo_offset-1) lo_px = App.width(before) end ---? print(lo,pos,hi, '--', lo_offset,pos_offset,hi_offset, '--', lo_px) local s = line.data:sub(lo_offset, hi_offset-1) App.color(Highlight_color) love.graphics.rectangle('fill', x+lo_px,y, App.width(s),State.line_height) @@ -71,22 +67,11 @@ 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 time = love.timer.getTime() - if State.recent_mouse.time and State.recent_mouse.time > time-0.1 then - return State.recent_mouse.line, State.recent_mouse.pos + 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 - State.recent_mouse.time = time - local line,pos = Text.to_pos(State, App.mouse_x(), App.mouse_y()) - if line then - State.recent_mouse.line = line - State.recent_mouse.pos = pos - end - return State.recent_mouse.line, State.recent_mouse.pos -end - -function Text.to_pos(State, x,y) for line_index,line in ipairs(State.lines) do if line.mode == 'text' then if Text.in_line(State, line_index, x,y) then @@ -94,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) diff --git a/source_text.lua b/source_text.lua index 290d982..1a42310 100644 --- a/source_text.lua +++ b/source_text.lua @@ -650,6 +650,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] @@ -662,6 +663,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 diff --git a/source_text_tests.lua b/source_text_tests.lua index 11e7613..c2e054a 100644 --- a/source_text_tests.lua +++ b/source_text_tests.lua @@ -1,4 +1,6 @@ -- major tests for text editing flows +-- Arguably this should be called source_edit_tests.lua, +-- but that would mess up the git blame at this point. function test_initial_state() App.screen.init{width=120, height=60} @@ -828,6 +830,67 @@ function test_select_text_using_mouse() check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos') end +function test_select_text_using_mouse_starting_above_text() + App.screen.init{width=50, height=60} + Editor_state = edit.initialize_test_state() + Editor_state.lines = load_array{'abc', 'def', 'xyz'} + Text.redraw_all(Editor_state) + Editor_state.cursor1 = {line=1, pos=1} + Editor_state.screen_top1 = {line=1, pos=1} + Editor_state.screen_bottom1 = {} + Editor_state.selection1 = {} + edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache + -- press mouse above first line of text + edit.run_after_mouse_press(Editor_state, Editor_state.left+8,5, 1) + 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, 1, 'selection:pos') +end + +function test_select_text_using_mouse_starting_above_text_wrapping_line() + -- first screen line starts in the middle of a line + App.screen.init{width=50, height=60} + Editor_state = edit.initialize_test_state() + Editor_state.lines = load_array{'abc', 'defgh', 'xyz'} + Text.redraw_all(Editor_state) + Editor_state.cursor1 = {line=2, pos=5} + Editor_state.screen_top1 = {line=2, pos=3} + Editor_state.screen_bottom1 = {} + -- press mouse above first line of text + edit.run_after_mouse_press(Editor_state, Editor_state.left+8,5, 1) + -- selection is at screen top + check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil') + check_eq(Editor_state.selection1.line, 2, 'selection: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() @@ -882,6 +945,28 @@ function test_select_text_repeatedly_using_mouse_and_shift() check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos') end +function test_select_all_text() + -- display a single line of text + App.screen.init{width=75, height=80} + Editor_state = edit.initialize_test_state() + Editor_state.lines = load_array{'abc def'} + 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) + -- select all + App.fake_key_press('lctrl') + edit.run_after_keychord(Editor_state, 'C-a') + App.fake_key_release('lctrl') + edit.key_release(Editor_state, 'lctrl') + -- selection + check_eq(Editor_state.selection1.line, 1, 'selection:line') + check_eq(Editor_state.selection1.pos, 1, 'selection:pos') + check_eq(Editor_state.cursor1.line, 1, 'cursor:line') + check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos') +end + function test_cut_without_selection() -- display a few lines App.screen.init{width=Editor_state.left+30, height=60} |