bugfix: check after cursor on same line when searching upwards

This commit is contained in:
Kartik K. Agaram 2022-08-11 22:23:04 -07:00
parent 8b880f4fe8
commit f3df1cda0f
2 changed files with 28 additions and 1 deletions

View File

@ -68,12 +68,13 @@ function Text.search_next(State)
end
function Text.search_previous(State)
-- search current line
-- search current line before cursor
local pos = rfind(State.lines[State.cursor1.line].data, State.search_term, State.cursor1.pos-1)
if pos then
State.cursor1.pos = pos
end
if pos == nil then
-- search lines above cursor
for i=State.cursor1.line-1,1,-1 do
pos = rfind(State.lines[i].data, State.search_term)
if pos then
@ -94,6 +95,13 @@ function Text.search_previous(State)
end
end
end
if pos == nil then
-- search current line after cursor
pos = rfind(State.lines[State.cursor1.line].data, State.search_term)
if pos and pos > State.cursor1.pos then
State.cursor1.pos = pos
end
end
if pos == nil then
State.cursor1.line = State.search_backup.cursor.line
State.cursor1.pos = State.search_backup.cursor.pos

View File

@ -2083,3 +2083,22 @@ function test_search_wrap()
check_eq(Editor_state.cursor1.line, 1, 'F - test_search_wrap/1/cursor:line')
check_eq(Editor_state.cursor1.pos, 1, 'F - test_search_wrap/1/cursor:pos')
end
function test_search_wrap_upwards()
io.write('\ntest_search_wrap_upwards')
App.screen.init{width=120, height=60}
Editor_state = edit.initialize_test_state()
Editor_state.lines = load_array{'abc abd'}
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)
-- search upwards for a string
edit.run_after_keychord(Editor_state, 'C-f')
edit.run_after_textinput(Editor_state, 'a')
edit.run_after_keychord(Editor_state, 'up')
-- cursor wraps
check_eq(Editor_state.cursor1.line, 1, 'F - test_search_wrap_upwards/1/cursor:line')
check_eq(Editor_state.cursor1.pos, 5, 'F - test_search_wrap_upwards/1/cursor:pos')
end