From 34f8ca1714a91e4e051ef80f22df2d0e5e0acfae Mon Sep 17 00:00:00 2001 From: tomoya ishida Date: Tue, 3 Jan 2023 04:25:07 +0900 Subject: [ruby/irb] Fix prompt and code mismatch (https://github.com/ruby/irb/pull/386) * fix prompt and code mismatch * Add test for prompt and code mismatch bug https://github.com/ruby/irb/commit/a5765d8177 Co-authored-by: Stan Lo --- lib/irb/ruby-lex.rb | 3 ++- test/irb/test_ruby_lex.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index 04d9bceb07..c3171f486c 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -96,7 +96,8 @@ class RubyLex if t.tok.include?("\n") t_str = t.tok t_str.each_line("\n") do |s| - code << s << "\n" + code << s + next unless s.include?("\n") ltype, indent, continue, code_block_open = check_state(code, partial_tokens, context: context) result << @prompt.call(ltype, indent, continue || code_block_open, @line_no + line_num_offset) line_num_offset += 1 diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb index 3caa036ce5..1c1ce92e6a 100644 --- a/test/irb/test_ruby_lex.rb +++ b/test/irb/test_ruby_lex.rb @@ -661,6 +661,38 @@ module TestIRB assert_dynamic_prompt(lines, expected_prompt_list) end + def test_dyanmic_prompt_with_double_newline_braking_code + input_with_prompt = [ + PromptRow.new('001:1: :* ', %q(if true)), + PromptRow.new('002:1: :* ', %q(%)), + PromptRow.new('003:1: :* ', %q(;end)), + PromptRow.new('004:1: :* ', %q(;hello)), + PromptRow.new('005:0: :> ', %q(end)), + ] + + lines = input_with_prompt.map(&:content) + expected_prompt_list = input_with_prompt.map(&:prompt) + assert_dynamic_prompt(lines, expected_prompt_list) + end + + def test_dyanmic_prompt_with_multiline_literal + input_with_prompt = [ + PromptRow.new('001:1: :* ', %q(if true)), + PromptRow.new('002:1:]:* ', %q( %w[)), + PromptRow.new('003:1:]:* ', %q( a)), + PromptRow.new('004:1: :* ', %q( ])), + PromptRow.new('005:1: :* ', %q( b)), + PromptRow.new('006:1:]:* ', %q( %w[)), + PromptRow.new('007:1:]:* ', %q( c)), + PromptRow.new('008:1: :* ', %q( ])), + PromptRow.new('009:0: :> ', %q(end)), + ] + + lines = input_with_prompt.map(&:content) + expected_prompt_list = input_with_prompt.map(&:prompt) + assert_dynamic_prompt(lines, expected_prompt_list) + end + def test_dyanmic_prompt_with_blank_line input_with_prompt = [ PromptRow.new('001:0:]:* ', %q(%w[)), -- cgit v1.2.3