summaryrefslogtreecommitdiff
path: root/lib/irb
diff options
context:
space:
mode:
authoraycabta <aycabta@gmail.com>2021-01-22 11:51:54 +0900
committergit <svn-admin@ruby-lang.org>2021-01-24 14:35:26 +0900
commitfc54af8aa136888d8c5a8bf7d68594f979a43946 (patch)
tree1e273220464ef90377c0fe064beba42aa419a9a1 /lib/irb
parent8cb999dd840de61ea64c87e8dc54b4320ed0fd5a (diff)
downloadruby-fc54af8aa136888d8c5a8bf7d68594f979a43946.tar.gz
[ruby/irb] Indent correctly with keyword "for" and "in"
https://github.com/ruby/irb/commit/47c83ea724
Diffstat (limited to 'lib/irb')
-rw-r--r--lib/irb/ruby-lex.rb34
1 files changed, 33 insertions, 1 deletions
diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb
index 006fc191ba..b41126f985 100644
--- a/lib/irb/ruby-lex.rb
+++ b/lib/irb/ruby-lex.rb
@@ -456,6 +456,34 @@ class RubyLex
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
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