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

ruby-changes:59700

From: Ben <ko1@a...>
Date: Tue, 14 Jan 2020 15:45:12 +0900 (JST)
Subject: [ruby-changes:59700] 9994eb8a5e (master): [ruby/irb] Fix newline depth with multiple braces

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

From 9994eb8a5e72ff68ee2a13ddeff8d9307ba7cd84 Mon Sep 17 00:00:00 2001
From: Ben <kanobt61@g...>
Date: Mon, 30 Dec 2019 11:18:05 -0500
Subject: [ruby/irb] Fix newline depth with multiple braces

This commit fixes the check_newline_depth_difference method to multiple
open braces on one line into account. Before this change we were
subtracting from the depth in check_newline_depth_difference on
every open brace. This is the right thing to do if the opening and
closing brace are on the same line. For example in a method definition we
have an opening and closing parentheses we want to add 1 to our depth,
and then remove it.

```
def foo()
end
```

However this isn't the correct behavior when the brace spans multiple
lines. If a brace spans multiple lines we don't want to subtract from
check_newline_depth_difference and we want to treat the braces the same
way as we do `end` and allow check_corresponding_token_depth to pop the
correct depth.

Example of bad behavior:

```
def foo()
  [
  ]
puts 'bar'
end
```

Example of desired behavior:

```
def foo()
  [
  ]
  puts 'bar'
end
```

https://github.com/ruby/irb/commit/7dc8af01e0

diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb
index b4c31c1..0a5e2c4 100644
--- a/lib/irb/ruby-lex.rb
+++ b/lib/irb/ruby-lex.rb
@@ -317,11 +317,13 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L317
 
   def check_newline_depth_difference
     depth_difference = 0
+    open_brace_on_line = 0
     @tokens.each_with_index do |t, index|
       case t[1]
       when :on_ignored_nl, :on_nl, :on_comment
         if index != (@tokens.size - 1)
           depth_difference = 0
+          open_brace_on_line = 0
         end
         next
       when :on_sp
@@ -330,8 +332,9 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L332
       case t[1]
       when :on_lbracket, :on_lbrace, :on_lparen
         depth_difference += 1
+        open_brace_on_line += 1
       when :on_rbracket, :on_rbrace, :on_rparen
-        depth_difference -= 1
+        depth_difference -= 1 if open_brace_on_line > 0
       when :on_kw
         next if index > 0 and @tokens[index - 1][3].allbits?(Ripper::EXPR_FNAME)
         case t[2]
diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb
index f3a9b30..ae25b1d 100644
--- a/test/irb/test_ruby_lex.rb
+++ b/test/irb/test_ruby_lex.rb
@@ -73,7 +73,22 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_ruby_lex.rb#L73
       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
 
+    def test_braces_on_thier_own_line
+      input_with_correct_indents = [
+        Row.new(%q(if true), nil, 2),
+        Row.new(%q(  [), nil, 4),
+        Row.new(%q(  ]), 2, 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
-- 
cgit v0.10.2


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

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