ruby-changes:68078
From: Ka=C3=ADque <ko1@a...>
Date: Wed, 22 Sep 2021 23:27:00 +0900 (JST)
Subject: [ruby-changes:68078] 782d1d876b (master): [ruby/irb] Use typed spaces when the line is inside the here documents
https://git.ruby-lang.org/ruby.git/commit/?id=782d1d876b From 782d1d876bb119284f52a7eb59e63f3571c4d55e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ka=C3=ADque=20Kandy=20Koga?= <kaiquekandykoga@g...> Date: Sun, 19 Sep 2021 16:44:11 -0300 Subject: [ruby/irb] Use typed spaces when the line is inside the here documents Use first method instead of square brackets to support 2.5 and 2.6 versions Use tokens Clear check_newline_depth_difference https://github.com/ruby/irb/commit/6fec2a5d46 --- lib/irb/ruby-lex.rb | 16 ++++++++++++++-- test/irb/test_ruby_lex.rb | 16 ++++++++-------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index e281169..f0c056e 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -207,7 +207,7 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L207 last_line = lines[line_index]&.byteslice(0, byte_pointer) code += last_line if last_line @tokens = self.class.ripper_lex_without_warning(code, context: context) - corresponding_token_depth = check_corresponding_token_depth + corresponding_token_depth = check_corresponding_token_depth(lines, line_index) if corresponding_token_depth corresponding_token_depth else @@ -603,7 +603,7 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L603 depth_difference end - def check_corresponding_token_depth + def check_corresponding_token_depth(lines, line_index) corresponding_token_depth = nil is_first_spaces_of_line = true is_first_printable_of_line = true @@ -611,6 +611,11 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L611 spaces_at_line_head = 0 open_brace_on_line = 0 in_oneliner_def = nil + + if heredoc_scope? + return lines[line_index][/^ */].length + end + @tokens.each_with_index do |t, index| # detecting one-liner method definition if in_oneliner_def.nil? @@ -817,5 +822,12 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L822 end false end + + private + + def heredoc_scope? + heredoc_tokens = @tokens.select { |t| [:on_heredoc_beg, :on_heredoc_end].include?(t.event) } + heredoc_tokens[-1]&.event == :on_heredoc_beg + end end # :startdoc: diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb index e255720..e02370d 100644 --- a/test/irb/test_ruby_lex.rb +++ b/test/irb/test_ruby_lex.rb @@ -109,8 +109,8 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_ruby_lex.rb#L109 Row.new(%q( ]), 4, 4), Row.new(%q( ]), 2, 2), Row.new(%q(]), 0, 0), - Row.new(%q([<<FOO]), nil, 0), - Row.new(%q(hello), nil, 0), + Row.new(%q([<<FOO]), 0, 0), + Row.new(%q(hello), 0, 0), Row.new(%q(FOO), nil, 0), ] @@ -465,10 +465,10 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_ruby_lex.rb#L465 def test_heredoc_with_indent input_with_correct_indents = [ - Row.new(%q(<<~Q), nil, 0, 0), - Row.new(%q({), nil, 0, 0), - Row.new(%q( #), nil, 0, 0), - Row.new(%q(}), nil, 0, 0), + Row.new(%q(<<~Q), 0, 0, 0), + Row.new(%q({), 0, 0, 0), + Row.new(%q( #), 2, 0, 0), + Row.new(%q(}), 0, 0, 0) ] lines = [] @@ -503,8 +503,8 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_ruby_lex.rb#L503 end input_with_correct_indents = [ Row.new(%q(def foo), nil, 2, 1), - Row.new(%q( <<~Q), nil, 2, 1), - Row.new(%q( Qend), nil, 2, 1), + Row.new(%q( <<~Q), 2, 2, 1), + Row.new(%q( Qend), 2, 2, 1), ] lines = [] -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/