1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/reline] Add ed_search_next_history

https://github.com/ruby/reline/commit/ca750b676b
This commit is contained in:
aycabta 2020-04-17 04:38:47 +09:00
parent bea3e31e5f
commit db0d850d4e
2 changed files with 126 additions and 0 deletions

View file

@ -1468,6 +1468,58 @@ class Reline::LineEditor
end end
alias_method :history_search_backward, :ed_search_prev_history alias_method :history_search_backward, :ed_search_prev_history
private def ed_search_next_history(key, arg: 1)
substr = @line.slice(0, @byte_pointer)
if @history_pointer.nil?
return
elsif @history_pointer == (Reline::HISTORY.size - 1) and not substr.empty?
return
end
history = Reline::HISTORY.slice((@history_pointer + 1)..-1)
h_pointer = nil
line_no = nil
if @is_multiline
h_pointer = history.index { |h|
h.split("\n").each_with_index { |l, i|
if l.start_with?(substr)
line_no = i
break
end
}
not line_no.nil?
}
else
h_pointer = history.index { |l|
l.start_with?(substr)
}
end
h_pointer += @history_pointer + 1 if h_pointer and @history_pointer
return if h_pointer.nil? and not substr.empty?
@history_pointer = h_pointer
if @is_multiline
if @history_pointer.nil? and substr.empty?
@buffer_of_lines = []
@line_index = 0
else
@buffer_of_lines = Reline::HISTORY[@history_pointer].split("\n")
@line_index = line_no
end
@buffer_of_lines = [String.new(encoding: @encoding)] if @buffer_of_lines.empty?
@line = @buffer_of_lines.last
@rerender_all = true
else
if @history_pointer.nil? and substr.empty?
@line = ''
else
@line = Reline::HISTORY[@history_pointer]
end
end
@cursor_max = calculate_width(@line)
arg -= 1
ed_search_next_history(key, arg: arg) if arg > 0
end
alias_method :history_search_forward, :ed_search_next_history
private def ed_prev_history(key, arg: 1) private def ed_prev_history(key, arg: 1)
if @is_multiline and @line_index > 0 if @is_multiline and @line_index > 0
@previous_line_index = @line_index @previous_line_index = @line_index

View file

@ -1981,6 +1981,80 @@ class Reline::KeyActor::Emacs::Test < Reline::TestCase
assert_line('ABC') assert_line('ABC')
end end
def test_ed_search_next_history
Reline::HISTORY.concat([
'12356', # old
'12aaa',
'12345' # new
])
input_keys('123')
# The ed_search_prev_history and ed_search_next_history doesn't have default binding
@line_editor.__send__(:ed_search_prev_history, "\C-p".ord)
assert_byte_pointer_size('123')
assert_cursor(3)
assert_cursor_max(5)
assert_line('12345')
@line_editor.__send__(:ed_search_prev_history, "\C-p".ord)
assert_byte_pointer_size('123')
assert_cursor(3)
assert_cursor_max(5)
assert_line('12356')
@line_editor.__send__(:ed_search_prev_history, "\C-p".ord)
assert_byte_pointer_size('123')
assert_cursor(3)
assert_cursor_max(5)
assert_line('12356')
@line_editor.__send__(:ed_search_next_history, "\C-n".ord)
assert_byte_pointer_size('123')
assert_cursor(3)
assert_cursor_max(5)
assert_line('12345')
@line_editor.__send__(:ed_search_next_history, "\C-n".ord)
assert_byte_pointer_size('123')
assert_cursor(3)
assert_cursor_max(5)
assert_line('12345')
end
def test_ed_search_next_history_with_empty
Reline::HISTORY.concat([
'12356', # old
'12aaa',
'12345' # new
])
# The ed_search_prev_history and ed_search_next_history doesn't have default binding
@line_editor.__send__(:ed_search_prev_history, "\C-p".ord)
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(5)
assert_line('12345')
@line_editor.__send__(:ed_search_prev_history, "\C-p".ord)
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(5)
assert_line('12aaa')
@line_editor.__send__(:ed_search_prev_history, "\C-p".ord)
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(5)
assert_line('12356')
@line_editor.__send__(:ed_search_next_history, "\C-n".ord)
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(5)
assert_line('12aaa')
@line_editor.__send__(:ed_search_next_history, "\C-n".ord)
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(5)
assert_line('12345')
@line_editor.__send__(:ed_search_next_history, "\C-n".ord)
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(0)
assert_line('')
end
=begin # TODO: move KeyStroke instance from Reline to LineEditor =begin # TODO: move KeyStroke instance from Reline to LineEditor
def test_key_delete def test_key_delete
input_keys('ab') input_keys('ab')