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

ruby-changes:62609

From: aycabta <ko1@a...>
Date: Tue, 18 Aug 2020 18:57:02 +0900 (JST)
Subject: [ruby-changes:62609] 5474007d61 (master): [ruby/irb] Support oneline method definition

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

From 5474007d6119a5ac021bc9fc1bbf932aab5f0dbb Mon Sep 17 00:00:00 2001
From: aycabta <aycabta@g...>
Date: Thu, 25 Jun 2020 23:56:03 +0900
Subject: [ruby/irb] Support oneline method definition

https://github.com/ruby/irb/commit/826ae909c9

diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb
index 5d7bf00..3568e66 100644
--- a/lib/irb/ruby-lex.rb
+++ b/lib/irb/ruby-lex.rb
@@ -303,7 +303,31 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L303
 
   def process_nesting_level
     indent = 0
+    in_oneliner_def = nil
     @tokens.each_with_index { |t, index|
+      # detecting one-liner method definition
+      if in_oneliner_def.nil?
+        if t[3].allbits?(Ripper::EXPR_ENDFN)
+          in_oneliner_def = :ENDFN
+        end
+      else
+        if t[3].allbits?(Ripper::EXPR_ENDFN)
+          # continuing
+        elsif t[3].allbits?(Ripper::EXPR_BEG)
+          if t[2] == '='
+            in_oneliner_def = :BODY
+          end
+        elsif t[3].allbits?(Ripper::EXPR_END)
+          if in_oneliner_def == :BODY
+            # one-liner method definition
+            indent -= 1
+          end
+          in_oneliner_def = nil
+        else
+          in_oneliner_def = nil
+        end
+      end
+
       case t[1]
       when :on_lbracket, :on_lbrace, :on_lparen
         indent += 1
@@ -338,7 +362,31 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L362
   def check_newline_depth_difference
     depth_difference = 0
     open_brace_on_line = 0
+    in_oneliner_def = nil
     @tokens.each_with_index do |t, index|
+      # detecting one-liner method definition
+      if in_oneliner_def.nil?
+        if t[3].allbits?(Ripper::EXPR_ENDFN)
+          in_oneliner_def = :ENDFN
+        end
+      else
+        if t[3].allbits?(Ripper::EXPR_ENDFN)
+          # continuing
+        elsif t[3].allbits?(Ripper::EXPR_BEG)
+          if t[2] == '='
+            in_oneliner_def = :BODY
+          end
+        elsif t[3].allbits?(Ripper::EXPR_END)
+          if in_oneliner_def == :BODY
+            # one[-liner method definition
+            depth_difference -= 1
+          end
+          in_oneliner_def = nil
+        else
+          in_oneliner_def = nil
+        end
+      end
+
       case t[1]
       when :on_ignored_nl, :on_nl, :on_comment
         if index != (@tokens.size - 1)
@@ -389,7 +437,36 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L437
     spaces_of_nest = []
     spaces_at_line_head = 0
     open_brace_on_line = 0
+    in_oneliner_def = nil
     @tokens.each_with_index do |t, index|
+      # detecting one-liner method definition
+      if in_oneliner_def.nil?
+        if t[3].allbits?(Ripper::EXPR_ENDFN)
+          in_oneliner_def = :ENDFN
+        end
+      else
+        if t[3].allbits?(Ripper::EXPR_ENDFN)
+          # continuing
+        elsif t[3].allbits?(Ripper::EXPR_BEG)
+          if t[2] == '='
+            in_oneliner_def = :BODY
+          end
+        elsif t[3].allbits?(Ripper::EXPR_END)
+          if in_oneliner_def == :BODY
+            # one-liner method definition
+            if is_first_printable_of_line
+              corresponding_token_depth = spaces_of_nest.pop
+            else
+              spaces_of_nest.pop
+              corresponding_token_depth = nil
+            end
+          end
+          in_oneliner_def = nil
+        else
+          in_oneliner_def = nil
+        end
+      end
+
       case t[1]
       when :on_ignored_nl, :on_nl, :on_comment
         corresponding_token_depth = nil
diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb
index afbad63..db15593 100644
--- a/test/irb/test_ruby_lex.rb
+++ b/test/irb/test_ruby_lex.rb
@@ -205,5 +205,34 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_ruby_lex.rb#L205
         assert_indenting(lines, row.new_line_spaces, true)
       end
     end
+
+    def test_oneliner_method_definition
+      input_with_correct_indents = [
+        Row.new(%q(class A), nil, 2),
+        Row.new(%q(  def foo0), nil, 4),
+        Row.new(%q(    3), nil, 4),
+        Row.new(%q(  end), 2, 2),
+        Row.new(%q(  def foo1()), nil, 4),
+        Row.new(%q(    3), nil, 4),
+        Row.new(%q(  end), 2, 2),
+        Row.new(%q(  def foo2(a, b)), nil, 4),
+        Row.new(%q(    a + b), nil, 4),
+        Row.new(%q(  end), 2, 2),
+        Row.new(%q(  def foo3 a, b), nil, 4),
+        Row.new(%q(    a + b), nil, 4),
+        Row.new(%q(  end), 2, 2),
+        Row.new(%q(  def bar0() = 3), nil, 2),
+        Row.new(%q(  def bar1(a) = a), nil, 2),
+        Row.new(%q(  def bar2(a, b) = a + b), nil, 2),
+        Row.new(%q(end), 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)
+      end
+    end
   end
 end
-- 
cgit v0.10.2


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

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