diff options
| author | tomoya ishida <tomoyapenguin@gmail.com> | 2024-11-11 04:11:54 +0900 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2024-11-10 19:11:58 +0000 |
| commit | 5eaa4c76c64c5d14e826fbd0eeef014643f9fb27 (patch) | |
| tree | 484008be8ad6d73e3c424d000850f9ecc75ac103 /lib | |
| parent | bce1bd1dc168f41e6d41c19e5a4cefb618fb9e29 (diff) | |
[ruby/reline] Remove unused things from reline/unicode.rb
(https://github.com/ruby/reline/pull/759)
* Remove garbage(nil) from Reline::Unicode.split_by_width result
* Remove unused width from Reline::Unicode vi_ ed_ em_ method return value
* Remove unused height from Unicode.split_by_width return value
* Rename split_by_width to split_line_by_width and add legacy split_by_width for IRB
https://github.com/ruby/reline/commit/f32446ebc4
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/reline/line_editor.rb | 42 | ||||
| -rw-r--r-- | lib/reline/unicode.rb | 87 |
2 files changed, 45 insertions, 84 deletions
diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index 709246980d..c0c8f499d3 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -300,8 +300,8 @@ class Reline::LineEditor end end - private def split_by_width(str, max_width, offset: 0) - Reline::Unicode.split_by_width(str, max_width, encoding, offset: offset) + private def split_line_by_width(str, max_width, offset: 0) + Reline::Unicode.split_line_by_width(str, max_width, encoding, offset: offset) end def current_byte_pointer_cursor @@ -391,8 +391,8 @@ class Reline::LineEditor if (cached = cached_wraps[[prompt, line]]) next cached end - *wrapped_prompts, code_line_prompt = split_by_width(prompt, width).first.compact - wrapped_lines = split_by_width(line, width, offset: calculate_width(code_line_prompt, true)).first.compact + *wrapped_prompts, code_line_prompt = split_line_by_width(prompt, width) + wrapped_lines = split_line_by_width(line, width, offset: calculate_width(code_line_prompt, true)) wrapped_prompts.map { |p| [p, ''] } + [[code_line_prompt, wrapped_lines.first]] + wrapped_lines.drop(1).map { |c| ['', c] } end end @@ -440,7 +440,7 @@ class Reline::LineEditor def wrapped_cursor_position prompt_width = calculate_width(prompt_list[@line_index], true) line_before_cursor = whole_lines[@line_index].byteslice(0, @byte_pointer) - wrapped_line_before_cursor = split_by_width(' ' * prompt_width + line_before_cursor, screen_width).first.compact + wrapped_line_before_cursor = split_line_by_width(' ' * prompt_width + line_before_cursor, screen_width) wrapped_cursor_y = wrapped_prompt_and_input_lines[0...@line_index].sum(&:size) + wrapped_line_before_cursor.size - 1 wrapped_cursor_x = calculate_width(wrapped_line_before_cursor.last) [wrapped_cursor_x, wrapped_cursor_y] @@ -465,7 +465,7 @@ class Reline::LineEditor render_differential([], 0, 0) lines = @buffer_of_lines.size.times.map do |i| line = Reline::Unicode.strip_non_printing_start_end(prompt_list[i]) + modified_lines[i] - wrapped_lines, = split_by_width(line, screen_width) + wrapped_lines = split_line_by_width(line, screen_width) wrapped_lines.last.empty? ? "#{line} " : line end @output.puts lines.map { |l| "#{l}\r\n" }.join @@ -1582,7 +1582,7 @@ class Reline::LineEditor alias_method :backward_char, :ed_prev_char private def vi_first_print(key) - @byte_pointer, = Reline::Unicode.vi_first_print(current_line) + @byte_pointer = Reline::Unicode.vi_first_print(current_line) end private def ed_move_to_beg(key) @@ -1961,7 +1961,7 @@ class Reline::LineEditor private def em_next_word(key) if current_line.bytesize > @byte_pointer - byte_size, _ = Reline::Unicode.em_forward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.em_forward_word(current_line, @byte_pointer) @byte_pointer += byte_size end end @@ -1969,7 +1969,7 @@ class Reline::LineEditor private def ed_prev_word(key) if @byte_pointer > 0 - byte_size, _ = Reline::Unicode.em_backward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.em_backward_word(current_line, @byte_pointer) @byte_pointer -= byte_size end end @@ -1977,7 +1977,7 @@ class Reline::LineEditor private def em_delete_next_word(key) if current_line.bytesize > @byte_pointer - byte_size, _ = Reline::Unicode.em_forward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.em_forward_word(current_line, @byte_pointer) line, word = byteslice!(current_line, @byte_pointer, byte_size) set_current_line(line) @kill_ring.append(word) @@ -1987,7 +1987,7 @@ class Reline::LineEditor private def ed_delete_prev_word(key) if @byte_pointer > 0 - byte_size, _ = Reline::Unicode.em_backward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.em_backward_word(current_line, @byte_pointer) line, word = byteslice!(current_line, @byte_pointer - byte_size, byte_size) set_current_line(line, @byte_pointer - byte_size) @kill_ring.append(word, true) @@ -2027,7 +2027,7 @@ class Reline::LineEditor private def em_capitol_case(key) if current_line.bytesize > @byte_pointer - byte_size, _, new_str = Reline::Unicode.em_forward_word_with_capitalization(current_line, @byte_pointer) + byte_size, new_str = Reline::Unicode.em_forward_word_with_capitalization(current_line, @byte_pointer) before = current_line.byteslice(0, @byte_pointer) after = current_line.byteslice((@byte_pointer + byte_size)..-1) set_current_line(before + new_str + after, @byte_pointer + new_str.bytesize) @@ -2037,7 +2037,7 @@ class Reline::LineEditor private def em_lower_case(key) if current_line.bytesize > @byte_pointer - byte_size, = Reline::Unicode.em_forward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.em_forward_word(current_line, @byte_pointer) part = current_line.byteslice(@byte_pointer, byte_size).grapheme_clusters.map { |mbchar| mbchar =~ /[A-Z]/ ? mbchar.downcase : mbchar }.join @@ -2050,7 +2050,7 @@ class Reline::LineEditor private def em_upper_case(key) if current_line.bytesize > @byte_pointer - byte_size, = Reline::Unicode.em_forward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.em_forward_word(current_line, @byte_pointer) part = current_line.byteslice(@byte_pointer, byte_size).grapheme_clusters.map { |mbchar| mbchar =~ /[a-z]/ ? mbchar.upcase : mbchar }.join @@ -2063,7 +2063,7 @@ class Reline::LineEditor private def em_kill_region(key) if @byte_pointer > 0 - byte_size, _ = Reline::Unicode.em_big_backward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.em_big_backward_word(current_line, @byte_pointer) line, deleted = byteslice!(current_line, @byte_pointer - byte_size, byte_size) set_current_line(line, @byte_pointer - byte_size) @kill_ring.append(deleted, true) @@ -2094,7 +2094,7 @@ class Reline::LineEditor private def vi_next_word(key, arg: 1) if current_line.bytesize > @byte_pointer - byte_size, _ = Reline::Unicode.vi_forward_word(current_line, @byte_pointer, @drop_terminate_spaces) + byte_size = Reline::Unicode.vi_forward_word(current_line, @byte_pointer, @drop_terminate_spaces) @byte_pointer += byte_size end arg -= 1 @@ -2103,7 +2103,7 @@ class Reline::LineEditor private def vi_prev_word(key, arg: 1) if @byte_pointer > 0 - byte_size, _ = Reline::Unicode.vi_backward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.vi_backward_word(current_line, @byte_pointer) @byte_pointer -= byte_size end arg -= 1 @@ -2112,7 +2112,7 @@ class Reline::LineEditor private def vi_end_word(key, arg: 1, inclusive: false) if current_line.bytesize > @byte_pointer - byte_size, _ = Reline::Unicode.vi_forward_end_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.vi_forward_end_word(current_line, @byte_pointer) @byte_pointer += byte_size end arg -= 1 @@ -2127,7 +2127,7 @@ class Reline::LineEditor private def vi_next_big_word(key, arg: 1) if current_line.bytesize > @byte_pointer - byte_size, _ = Reline::Unicode.vi_big_forward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.vi_big_forward_word(current_line, @byte_pointer) @byte_pointer += byte_size end arg -= 1 @@ -2136,7 +2136,7 @@ class Reline::LineEditor private def vi_prev_big_word(key, arg: 1) if @byte_pointer > 0 - byte_size, _ = Reline::Unicode.vi_big_backward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.vi_big_backward_word(current_line, @byte_pointer) @byte_pointer -= byte_size end arg -= 1 @@ -2145,7 +2145,7 @@ class Reline::LineEditor private def vi_end_big_word(key, arg: 1, inclusive: false) if current_line.bytesize > @byte_pointer - byte_size, _ = Reline::Unicode.vi_big_forward_end_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.vi_big_forward_end_word(current_line, @byte_pointer) @byte_pointer += byte_size end arg -= 1 diff --git a/lib/reline/unicode.rb b/lib/reline/unicode.rb index 642f4d7e36..7bca22aeed 100644 --- a/lib/reline/unicode.rb +++ b/lib/reline/unicode.rb @@ -121,9 +121,14 @@ class Reline::Unicode end end - def self.split_by_width(str, max_width, encoding = str.encoding, offset: 0) + # This method is used by IRB + def self.split_by_width(str, max_width) + lines = split_line_by_width(str, max_width) + [lines, lines.size] + end + + def self.split_line_by_width(str, max_width, encoding = str.encoding, offset: 0) lines = [String.new(encoding: encoding)] - height = 1 width = offset rest = str.encode(Encoding::UTF_8) in_zero_width = false @@ -151,9 +156,7 @@ class Reline::Unicode mbchar_width = get_mbchar_width(gc) if (width += mbchar_width) > max_width width = mbchar_width - lines << nil lines << seq.dup - height += 1 end end lines.last << gc @@ -161,11 +164,9 @@ class Reline::Unicode end # The cursor moves to next line in first if width == max_width - lines << nil lines << String.new(encoding: encoding) - height += 1 end - [lines, height] + lines end def self.strip_non_printing_start_end(prompt) @@ -261,27 +262,23 @@ class Reline::Unicode end def self.em_forward_word(line, byte_pointer) - width = 0 byte_size = 0 while line.bytesize > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) mbchar = line.byteslice(byte_pointer + byte_size, size) break if mbchar.encode(Encoding::UTF_8) =~ /\p{Word}/ - width += get_mbchar_width(mbchar) byte_size += size end while line.bytesize > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) mbchar = line.byteslice(byte_pointer + byte_size, size) break if mbchar.encode(Encoding::UTF_8) =~ /\P{Word}/ - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width] + byte_size end def self.em_forward_word_with_capitalization(line, byte_pointer) - width = 0 byte_size = 0 new_str = String.new while line.bytesize > (byte_pointer + byte_size) @@ -289,7 +286,6 @@ class Reline::Unicode mbchar = line.byteslice(byte_pointer + byte_size, size) break if mbchar.encode(Encoding::UTF_8) =~ /\p{Word}/ new_str += mbchar - width += get_mbchar_width(mbchar) byte_size += size end first = true @@ -303,50 +299,43 @@ class Reline::Unicode else new_str += mbchar.downcase end - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width, new_str] + [byte_size, new_str] end def self.em_backward_word(line, byte_pointer) - width = 0 byte_size = 0 while 0 < (byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size) mbchar = line.byteslice(byte_pointer - byte_size - size, size) break if mbchar.encode(Encoding::UTF_8) =~ /\p{Word}/ - width += get_mbchar_width(mbchar) byte_size += size end while 0 < (byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size) mbchar = line.byteslice(byte_pointer - byte_size - size, size) break if mbchar.encode(Encoding::UTF_8) =~ /\P{Word}/ - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width] + byte_size end def self.em_big_backward_word(line, byte_pointer) - width = 0 byte_size = 0 while 0 < (byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size) mbchar = line.byteslice(byte_pointer - byte_size - size, size) break if mbchar =~ /\S/ - width += get_mbchar_width(mbchar) byte_size += size end while 0 < (byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size) mbchar = line.byteslice(byte_pointer - byte_size - size, size) break if mbchar =~ /\s/ - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width] + byte_size end def self.ed_transpose_words(line, byte_pointer) @@ -451,73 +440,61 @@ class Reline::Unicode end def self.vi_big_forward_word(line, byte_pointer) - width = 0 byte_size = 0 while (line.bytesize - 1) > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) mbchar = line.byteslice(byte_pointer + byte_size, size) break if mbchar =~ /\s/ - width += get_mbchar_width(mbchar) byte_size += size end while (line.bytesize - 1) > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) mbchar = line.byteslice(byte_pointer + byte_size, size) break if mbchar =~ /\S/ - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width] + byte_size end def self.vi_big_forward_end_word(line, byte_pointer) if (line.bytesize - 1) > byte_pointer size = get_next_mbchar_size(line, byte_pointer) - mbchar = line.byteslice(byte_pointer, size) - width = get_mbchar_width(mbchar) byte_size = size else - return [0, 0] + return 0 end while (line.bytesize - 1) > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) mbchar = line.byteslice(byte_pointer + byte_size, size) break if mbchar =~ /\S/ - width += get_mbchar_width(mbchar) byte_size += size end - prev_width = width prev_byte_size = byte_size while line.bytesize > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) mbchar = line.byteslice(byte_pointer + byte_size, size) break if mbchar =~ /\s/ - prev_width = width prev_byte_size = byte_size - width += get_mbchar_width(mbchar) byte_size += size end - [prev_byte_size, prev_width] + prev_byte_size end def self.vi_big_backward_word(line, byte_pointer) - width = 0 byte_size = 0 while 0 < (byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size) mbchar = line.byteslice(byte_pointer - byte_size - size, size) break if mbchar =~ /\S/ - width += get_mbchar_width(mbchar) byte_size += size end while 0 < (byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size) mbchar = line.byteslice(byte_pointer - byte_size - size, size) break if mbchar =~ /\s/ - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width] + byte_size end def self.vi_forward_word(line, byte_pointer, drop_terminate_spaces = false) @@ -531,10 +508,9 @@ class Reline::Unicode else started_by = :non_word_printable end - width = get_mbchar_width(mbchar) byte_size = size else - return [0, 0] + return 0 end while line.bytesize > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) @@ -547,18 +523,16 @@ class Reline::Unicode when :non_word_printable break if mbchar =~ /\w|\s/ end - width += get_mbchar_width(mbchar) byte_size += size end - return [byte_size, width] if drop_terminate_spaces + return byte_size if drop_terminate_spaces while line.bytesize > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) mbchar = line.byteslice(byte_pointer + byte_size, size) break if mbchar =~ /\S/ - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width] + byte_size end def self.vi_forward_end_word(line, byte_pointer) @@ -572,10 +546,9 @@ class Reline::Unicode else started_by = :non_word_printable end - width = get_mbchar_width(mbchar) byte_size = size else - return [0, 0] + return 0 end if (line.bytesize - 1) > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) @@ -587,13 +560,11 @@ class Reline::Unicode else second = :non_word_printable end - second_width = get_mbchar_width(mbchar) second_byte_size = size else - return [byte_size, width] + return byte_size end if second == :space - width += second_width byte_size += second_byte_size while (line.bytesize - 1) > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) @@ -606,7 +577,6 @@ class Reline::Unicode end break end - width += get_mbchar_width(mbchar) byte_size += size end else @@ -614,12 +584,10 @@ class Reline::Unicode when [:word, :non_word_printable], [:non_word_printable, :word] started_by = second else - width += second_width byte_size += second_byte_size started_by = second end end - prev_width = width prev_byte_size = byte_size while line.bytesize > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) @@ -630,16 +598,13 @@ class Reline::Unicode when :non_word_printable break if mbchar =~ /[\w\s]/ end - prev_width = width prev_byte_size = byte_size - width += get_mbchar_width(mbchar) byte_size += size end - [prev_byte_size, prev_width] + prev_byte_size end def self.vi_backward_word(line, byte_pointer) - width = 0 byte_size = 0 while 0 < (byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size) @@ -652,7 +617,6 @@ class Reline::Unicode end break end - width += get_mbchar_width(mbchar) byte_size += size end while 0 < (byte_pointer - byte_size) @@ -664,14 +628,12 @@ class Reline::Unicode when :non_word_printable break if mbchar =~ /[\w\s]/ end - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width] + byte_size end def self.vi_first_print(line) - width = 0 byte_size = 0 while (line.bytesize - 1) > byte_size size = get_next_mbchar_size(line, byte_size) @@ -679,9 +641,8 @@ class Reline::Unicode if mbchar =~ /\S/ break end - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width] + byte_size end end |
