[前][次][番号順一覧][スレッド一覧]

ruby-changes:65021

From: aycabta <ko1@a...>
Date: Sun, 24 Jan 2021 14:35:45 +0900 (JST)
Subject: [ruby-changes:65021] 743c44ee21 (master): [ruby/irb] Indent correctly with method calling with receiver

https://git.ruby-lang.org/ruby.git/commit/?id=743c44ee21

From 743c44ee2167e41c828c631ee764e96640f99736 Mon Sep 17 00:00:00 2001
From: aycabta <aycabta@g...>
Date: Sat, 23 Jan 2021 11:39:51 +0900
Subject: [ruby/irb] Indent correctly with method calling with receiver

https://github.com/ruby/irb/commit/e7c68e74a0
---
 lib/irb/ruby-lex.rb       | 21 ++++++++++++++--
 test/irb/test_ruby_lex.rb | 64 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb
index b41126f..7b365a3 100644
--- a/lib/irb/ruby-lex.rb
+++ b/lib/irb/ruby-lex.rb
@@ -424,6 +424,24 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L424
     indent
   end
 
+  def is_method_calling?(tokens, index)
+    tk = tokens[index]
+    if tk[3].anybits?(Ripper::EXPR_CMDARG) and tk[1] == :on_ident
+      # The target method call to pass the block with "do".
+      return true
+    elsif tk[3].anybits?(Ripper::EXPR_ARG) and tk[1] == :on_ident
+      non_sp_index = tokens[0..(index - 1)].rindex{ |t| t[1] != :on_sp }
+      if non_sp_index
+        prev_tk = tokens[non_sp_index]
+        if prev_tk[3].anybits?(Ripper::EXPR_DOT) and prev_tk[1] == :on_period
+          # The target method call with receiver to pass the block with "do".
+          return true
+        end
+      end
+    end
+    false
+  end
+
   def take_corresponding_syntax_to_kw_do(tokens, index)
     syntax_of_do = nil
     # Finding a syntax correnponding to "do".
@@ -437,8 +455,7 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L455
       elsif [:on_ignored_nl, :on_nl, :on_comment].include?(tokens[non_sp_index][1])
         first_in_fomula = true
       end
-      if tk[3].anybits?(Ripper::EXPR_CMDARG) and tk[1] == :on_ident
-        # The target method call to pass the block with "do".
+      if is_method_calling?(tokens, i)
         syntax_of_do = :method_calling
         break if first_in_fomula
       elsif tk[1] == :on_kw && %w{while until for}.include?(tk[2])
diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb
index 7a00111..a45ca66 100644
--- a/test/irb/test_ruby_lex.rb
+++ b/test/irb/test_ruby_lex.rb
@@ -365,6 +365,70 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_ruby_lex.rb#L365
       end
     end
 
+    def test_bracket_corresponding_to_times
+      input_with_correct_indents = [
+        Row.new(%q(3.times { |i|), nil, 2, 1),
+        Row.new(%q(  puts i), nil, 2, 1),
+        Row.new(%q(}), 0, 0, 0),
+      ]
+
+      lines = []
+      input_with_correct_indents.each do |row|
+        lines << row.content
+        assert_indenting(lines, row.current_line_spaces, false)
+        assert_indenting(lines, row.new_line_spaces, true)
+        assert_nesting_level(lines, row.nesting_level)
+      end
+    end
+
+    def test_do_corresponding_to_times
+      input_with_correct_indents = [
+        Row.new(%q(3.times do |i|), nil, 2, 1),
+        #Row.new(%q(  puts i), nil, 2, 1),
+        #Row.new(%q(end), 0, 0, 0),
+      ]
+
+      lines = []
+      input_with_correct_indents.each do |row|
+        lines << row.content
+        assert_indenting(lines, row.current_line_spaces, false)
+        assert_indenting(lines, row.new_line_spaces, true)
+        assert_nesting_level(lines, row.nesting_level)
+      end
+    end
+
+    def test_bracket_corresponding_to_loop
+      input_with_correct_indents = [
+        Row.new(%q(loop {), nil, 2, 1),
+        Row.new(%q(  3), nil, 2, 1),
+        Row.new(%q(}), 0, 0, 0),
+      ]
+
+      lines = []
+      input_with_correct_indents.each do |row|
+        lines << row.content
+        assert_indenting(lines, row.current_line_spaces, false)
+        assert_indenting(lines, row.new_line_spaces, true)
+        assert_nesting_level(lines, row.nesting_level)
+      end
+    end
+
+    def test_do_corresponding_to_loop
+      input_with_correct_indents = [
+        Row.new(%q(loop do), nil, 2, 1),
+        Row.new(%q(  3), nil, 2, 1),
+        Row.new(%q(end), 0, 0, 0),
+      ]
+
+      lines = []
+      input_with_correct_indents.each do |row|
+        lines << row.content
+        assert_indenting(lines, row.current_line_spaces, false)
+        assert_indenting(lines, row.new_line_spaces, true)
+        assert_nesting_level(lines, row.nesting_level)
+      end
+    end
+
     def test_heredoc_with_indent
       input_with_correct_indents = [
         Row.new(%q(<<~Q), nil, 0, 0),
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]