[ruby/reline] Fix incremental search cancel bug

(https://github.com/ruby/reline/pull/748)

https://github.com/ruby/reline/commit/bf0f8fa333
This commit is contained in:
tomoya ishida 2024-10-03 02:46:59 +09:00 committed by git
parent 8f4277f405
commit 09761e4789
2 changed files with 42 additions and 47 deletions

View File

@ -1665,57 +1665,29 @@ class Reline::LineEditor
end end
private def incremental_search_history(key) private def incremental_search_history(key)
unless @history_pointer backup = @buffer_of_lines.dup, @line_index, @byte_pointer, @history_pointer, @line_backup_in_history
@line_backup_in_history = whole_buffer
end
searcher = generate_searcher(key) searcher = generate_searcher(key)
@searching_prompt = "(reverse-i-search)`': " @searching_prompt = "(reverse-i-search)`': "
termination_keys = ["\C-j".ord] termination_keys = ["\C-j".ord]
termination_keys.concat(@config.isearch_terminators&.chars&.map(&:ord)) if @config.isearch_terminators termination_keys.concat(@config.isearch_terminators.chars.map(&:ord)) if @config.isearch_terminators
@waiting_proc = ->(k) { @waiting_proc = ->(k) {
case k
when *termination_keys
if @history_pointer
buffer = Reline::HISTORY[@history_pointer]
else
buffer = @line_backup_in_history
end
@buffer_of_lines = buffer.split("\n")
@buffer_of_lines = [String.new(encoding: @encoding)] if @buffer_of_lines.empty?
@line_index = @buffer_of_lines.size - 1
@searching_prompt = nil
@waiting_proc = nil
@byte_pointer = 0
when "\C-g".ord
@buffer_of_lines = @line_backup_in_history.split("\n")
@buffer_of_lines = [String.new(encoding: @encoding)] if @buffer_of_lines.empty?
@line_index = @buffer_of_lines.size - 1
move_history(nil, line: :end, cursor: :end, save_buffer: false)
@searching_prompt = nil
@waiting_proc = nil
@byte_pointer = 0
else
chr = k.is_a?(String) ? k : k.chr(Encoding::ASCII_8BIT) chr = k.is_a?(String) ? k : k.chr(Encoding::ASCII_8BIT)
if chr.match?(/[[:print:]]/) or k == "\C-h".ord or k == "\C-?".ord or k == "\C-r".ord or k == "\C-s".ord if k == "\C-g".ord
# cancel search and restore buffer
@buffer_of_lines, @line_index, @byte_pointer, @history_pointer, @line_backup_in_history = backup
@searching_prompt = nil
@waiting_proc = nil
elsif !termination_keys.include?(k) && (chr.match?(/[[:print:]]/) || k == "\C-h".ord || k == "\C-?".ord || k == "\C-r".ord || k == "\C-s".ord)
search_word, prompt_name, hit_pointer = searcher.call(k) search_word, prompt_name, hit_pointer = searcher.call(k)
Reline.last_incremental_search = search_word Reline.last_incremental_search = search_word
@searching_prompt = "(%s)`%s'" % [prompt_name, search_word] @searching_prompt = "(%s)`%s'" % [prompt_name, search_word]
@searching_prompt += ': ' unless @is_multiline @searching_prompt += ': ' unless @is_multiline
move_history(hit_pointer, line: :end, cursor: :end, save_buffer: false) if hit_pointer move_history(hit_pointer, line: :end, cursor: :end) if hit_pointer
else else
if @history_pointer # terminaton_keys and other keys will terminalte
line = Reline::HISTORY[@history_pointer] move_history(@history_pointer, line: :end, cursor: :start)
else
line = @line_backup_in_history
end
@line_backup_in_history = whole_buffer
@buffer_of_lines = line.split("\n")
@buffer_of_lines = [String.new(encoding: @encoding)] if @buffer_of_lines.empty?
@line_index = @buffer_of_lines.size - 1
@searching_prompt = nil @searching_prompt = nil
@waiting_proc = nil @waiting_proc = nil
@byte_pointer = 0
end
end end
} }
end end
@ -1770,14 +1742,14 @@ class Reline::LineEditor
end end
alias_method :history_search_forward, :ed_search_next_history alias_method :history_search_forward, :ed_search_next_history
private def move_history(history_pointer, line:, cursor:, save_buffer: true) private def move_history(history_pointer, line:, cursor:)
history_pointer ||= Reline::HISTORY.size history_pointer ||= Reline::HISTORY.size
return if history_pointer < 0 || history_pointer > Reline::HISTORY.size return if history_pointer < 0 || history_pointer > Reline::HISTORY.size
old_history_pointer = @history_pointer || Reline::HISTORY.size old_history_pointer = @history_pointer || Reline::HISTORY.size
if old_history_pointer == Reline::HISTORY.size if old_history_pointer == Reline::HISTORY.size
@line_backup_in_history = save_buffer ? whole_buffer : '' @line_backup_in_history = whole_buffer
else else
Reline::HISTORY[old_history_pointer] = whole_buffer if save_buffer Reline::HISTORY[old_history_pointer] = whole_buffer
end end
if history_pointer == Reline::HISTORY.size if history_pointer == Reline::HISTORY.size
buf = @line_backup_in_history buf = @line_backup_in_history

View File

@ -1328,6 +1328,29 @@ class Reline::KeyActor::EmacsTest < Reline::TestCase
assert_line_around_cursor('abd', 'c') assert_line_around_cursor('abd', 'c')
end end
def test_incremental_search_history_saves_and_restores_last_input
Reline::HISTORY.concat(['abc', '123'])
input_keys("abcd")
# \C-j: terminate incremental search
input_keys("\C-r12\C-j")
assert_line_around_cursor('', '123')
input_key_by_symbol(:ed_next_history)
assert_line_around_cursor('abcd', '')
# Most non-printable keys also terminates incremental search
input_keys("\C-r12\C-i")
assert_line_around_cursor('', '123')
input_key_by_symbol(:ed_next_history)
assert_line_around_cursor('abcd', '')
# \C-g: cancel incremental search and restore input, cursor position and history index
input_key_by_symbol(:ed_prev_history)
input_keys("\C-b\C-b")
assert_line_around_cursor('1', '23')
input_keys("\C-rab\C-g")
assert_line_around_cursor('1', '23')
input_key_by_symbol(:ed_next_history)
assert_line_around_cursor('abcd', '')
end
# Unicode emoji test # Unicode emoji test
def test_ed_insert_for_include_zwj_emoji def test_ed_insert_for_include_zwj_emoji
omit "This test is for UTF-8 but the locale is #{Reline.core.encoding}" if Reline.core.encoding != Encoding::UTF_8 omit "This test is for UTF-8 but the locale is #{Reline.core.encoding}" if Reline.core.encoding != Encoding::UTF_8