diff options
author | aycabta <aycabta@gmail.com> | 2021-01-02 05:20:48 +0900 |
---|---|---|
committer | aycabta <aycabta@gmail.com> | 2021-01-05 18:06:26 +0900 |
commit | 505e01fe12007382aa2cd4b7231698597f563b42 (patch) | |
tree | 42c35cbbe3f4c24cb1ea7a0a6aefd6cb265cccfa /lib/irb/ruby-lex.rb | |
parent | 5012512398daed11e65113310c212d50ad51fd16 (diff) | |
download | ruby-505e01fe12007382aa2cd4b7231698597f563b42.tar.gz |
[ruby/irb] Heredoc may contain multiple newlines in a single token
Use the start token as the indentation criteria so that it works properly in
heredoc.
ref. https://github.com/ruby/reline/pull/242
https://github.com/ruby/irb/commit/9704808dfd
Diffstat (limited to 'lib/irb/ruby-lex.rb')
-rw-r--r-- | lib/irb/ruby-lex.rb | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index 9ae9f16cb7..5c07ef4341 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -116,13 +116,32 @@ class RubyLex tokens end + def find_prev_spaces(line_index) + return 0 if @tokens.size == 0 + md = @tokens[0][2].match(/(\A +)/) + prev_spaces = md.nil? ? 0 : md[1].count(' ') + line_count = 0 + @tokens.each_with_index do |t, i| + if t[2].include?("\n") + line_count += t[2].count("\n") + if line_count >= line_index + return prev_spaces + end + if (@tokens.size - 1) > i + md = @tokens[i + 1][2].match(/(\A +)/) + prev_spaces = md.nil? ? 0 : md[1].count(' ') + end + end + end + prev_spaces + end + def set_auto_indent(context) if @io.respond_to?(:auto_indent) and context.auto_indent_mode @io.auto_indent do |lines, line_index, byte_pointer, is_newline| if is_newline - md = lines[line_index - 1].match(/(\A +)/) - prev_spaces = md.nil? ? 0 : md[1].count(' ') @tokens = ripper_lex_without_warning(lines[0..line_index].join("\n")) + prev_spaces = find_prev_spaces(line_index) depth_difference = check_newline_depth_difference depth_difference = 0 if depth_difference < 0 prev_spaces + depth_difference * 2 |