ruby-changes:59755
From: aycabta <ko1@a...>
Date: Mon, 20 Jan 2020 20:16:06 +0900 (JST)
Subject: [ruby-changes:59755] b17797a694 (master): [ruby/reline] Implement vi_to_next_char
https://git.ruby-lang.org/ruby.git/commit/?id=b17797a694 From b17797a6940cb196c9893edc088828b49772554c Mon Sep 17 00:00:00 2001 From: aycabta <aycabta@g...> Date: Fri, 17 Jan 2020 13:44:07 +0900 Subject: [ruby/reline] Implement vi_to_next_char https://github.com/ruby/reline/commit/066ecb0a21 diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index 1387bfa..6890fa5 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -2087,12 +2087,17 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L2087 @waiting_proc = ->(key_for_proc) { search_next_char(key_for_proc, arg) } end - private def search_next_char(key, arg) + private def vi_to_next_char(key, arg: 1) + @waiting_proc = ->(key_for_proc) { search_next_char(key_for_proc, arg, true) } + end + + private def search_next_char(key, arg, need_prev_char = false) if key.instance_of?(String) inputed_char = key else inputed_char = key.chr end + prev_total = nil total = nil found = false @line.byteslice(@byte_pointer..-1).grapheme_clusters.each do |mbchar| @@ -2110,13 +2115,18 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L2115 end end width = Reline::Unicode.get_mbchar_width(mbchar) + prev_total = total total = [total.first + mbchar.bytesize, total.last + width] end end - if found and total + if not need_prev_char and found and total byte_size, width = total @byte_pointer += byte_size @cursor += width + elsif need_prev_char and found and prev_total + byte_size, width = prev_total + @byte_pointer += byte_size + @cursor += width end @waiting_proc = nil end diff --git a/test/reline/test_key_actor_vi.rb b/test/reline/test_key_actor_vi.rb index b8ab160..d23bdb7 100644 --- a/test/reline/test_key_actor_vi.rb +++ b/test/reline/test_key_actor_vi.rb @@ -633,6 +633,24 @@ class Reline::KeyActor::ViInsert::Test < Reline::TestCase https://github.com/ruby/ruby/blob/trunk/test/reline/test_key_actor_vi.rb#L633 assert_cursor_max(6) end + def test_vi_to_next_char + input_keys("abcdef\C-[0") + assert_line('abcdef') + assert_byte_pointer_size('') + assert_cursor(0) + assert_cursor_max(6) + input_keys('tz') + assert_line('abcdef') + assert_byte_pointer_size('') + assert_cursor(0) + assert_cursor_max(6) + input_keys('te') + assert_line('abcdef') + assert_byte_pointer_size('abc') + assert_cursor(3) + assert_cursor_max(6) + end + def test_vi_delete_next_char input_keys("abc\C-[h") assert_byte_pointer_size('a') -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/