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

ruby-changes:65020

From: aycabta <ko1@a...>
Date: Sun, 24 Jan 2021 14:35:42 +0900 (JST)
Subject: [ruby-changes:65020] fc54af8aa1 (master): [ruby/irb] Indent correctly with keyword "for" and "in"

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

From fc54af8aa136888d8c5a8bf7d68594f979a43946 Mon Sep 17 00:00:00 2001
From: aycabta <aycabta@g...>
Date: Fri, 22 Jan 2021 11:51:54 +0900
Subject: [ruby/irb] Indent correctly with keyword "for" and "in"

https://github.com/ruby/irb/commit/47c83ea724
---
 lib/irb/ruby-lex.rb       | 34 +++++++++++++++++++++++++++++++++-
 test/irb/test_ruby_lex.rb | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 1 deletion(-)

diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb
index 006fc19..b41126f 100644
--- a/lib/irb/ruby-lex.rb
+++ b/lib/irb/ruby-lex.rb
@@ -456,6 +456,34 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L456
     syntax_of_do
   end
 
+  def is_the_in_correspond_to_a_for(tokens, index)
+    syntax_of_in = nil
+    # Finding a syntax correnponding to "do".
+    index.downto(0) do |i|
+      tk = tokens[i]
+      # In "continue", the token isn't the corresponding syntax to "do".
+      non_sp_index = tokens[0..(i - 1)].rindex{ |t| t[1] != :on_sp }
+      first_in_fomula = false
+      if non_sp_index.nil?
+        first_in_fomula = true
+      elsif [:on_ignored_nl, :on_nl, :on_comment].include?(tokens[non_sp_index][1])
+        first_in_fomula = true
+      end
+      if tk[1] == :on_kw && tk[2] == 'for'
+        # A loop syntax in front of "do" found.
+        #
+        #   while cond do # also "until" or "for"
+        #   end
+        #
+        # This "do" doesn't increment indent because the loop syntax already
+        # incremented.
+        syntax_of_in = :for
+      end
+      break if first_in_fomula
+    end
+    syntax_of_in
+  end
+
   def check_newline_depth_difference
     depth_difference = 0
     open_brace_on_line = 0
@@ -511,8 +539,12 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L539
           unless t[3].allbits?(Ripper::EXPR_LABEL)
             depth_difference += 1
           end
-        when 'else', 'elsif', 'ensure', 'when', 'in'
+        when 'else', 'elsif', 'ensure', 'when'
           depth_difference += 1
+        when 'in'
+          unless is_the_in_correspond_to_a_for(@tokens, index)
+            depth_difference += 1
+          end
         when 'end'
           depth_difference -= 1
         end
diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb
index ed4944a..7a00111 100644
--- a/test/irb/test_ruby_lex.rb
+++ b/test/irb/test_ruby_lex.rb
@@ -333,6 +333,38 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_ruby_lex.rb#L333
       end
     end
 
+    def test_corresponding_syntax_to_keyword_for
+      input_with_correct_indents = [
+        Row.new(%q(for i in [1]), 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_corresponding_syntax_to_keyword_for_with_do
+      input_with_correct_indents = [
+        Row.new(%q(for i in [1] do), 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_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/

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