Skip to content

Commit 6de1341

Browse files
no6vaycabta
authored andcommitted
handle rescue modifier properly
1 parent f87eec2 commit 6de1341

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

lib/irb/ruby-lex.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def process_nesting_level
324324
when 'def', 'case', 'for', 'begin', 'class', 'module'
325325
indent += 1
326326
when 'if', 'unless', 'while', 'until'
327-
# postfix if/unless/while/until/rescue must be Ripper::EXPR_LABEL
327+
# postfix if/unless/while/until must be Ripper::EXPR_LABEL
328328
indent += 1 unless t[3].allbits?(Ripper::EXPR_LABEL)
329329
when 'end'
330330
indent -= 1
@@ -369,12 +369,12 @@ def check_newline_depth_difference
369369
end
370370
when 'def', 'case', 'for', 'begin', 'class', 'module'
371371
depth_difference += 1
372-
when 'if', 'unless', 'while', 'until'
372+
when 'if', 'unless', 'while', 'until', 'rescue'
373373
# postfix if/unless/while/until/rescue must be Ripper::EXPR_LABEL
374374
unless t[3].allbits?(Ripper::EXPR_LABEL)
375375
depth_difference += 1
376376
end
377-
when 'else', 'elsif', 'rescue', 'ensure', 'when', 'in'
377+
when 'else', 'elsif', 'ensure', 'when', 'in'
378378
depth_difference += 1
379379
end
380380
end
@@ -420,12 +420,16 @@ def check_corresponding_token_depth
420420
case t[2]
421421
when 'def', 'do', 'case', 'for', 'begin', 'class', 'module'
422422
spaces_of_nest.push(spaces_at_line_head)
423+
when 'rescue'
424+
unless t[3].allbits?(Ripper::EXPR_LABEL)
425+
corresponding_token_depth = spaces_of_nest.last
426+
end
423427
when 'if', 'unless', 'while', 'until'
424-
# postfix if/unless/while/until/rescue must be Ripper::EXPR_LABEL
428+
# postfix if/unless/while/until must be Ripper::EXPR_LABEL
425429
unless t[3].allbits?(Ripper::EXPR_LABEL)
426430
spaces_of_nest.push(spaces_at_line_head)
427431
end
428-
when 'else', 'elsif', 'rescue', 'ensure', 'when', 'in'
432+
when 'else', 'elsif', 'ensure', 'when', 'in'
429433
corresponding_token_depth = spaces_of_nest.last
430434
when 'end'
431435
if is_first_printable_of_line

test/irb/test_ruby_lex.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,32 @@ def test_incomplete_vim_coding_magic_comment
178178
assert_indenting(lines, row.new_line_spaces, true)
179179
end
180180
end
181+
182+
def test_mixed_rescue
183+
input_with_correct_indents = [
184+
Row.new(%q(def m), nil, 2),
185+
Row.new(%q( begin), nil, 4),
186+
Row.new(%q( begin), nil, 6),
187+
Row.new(%q( x = a rescue 4), nil, 6),
188+
Row.new(%q( y = [(a rescue 5)]), nil, 6),
189+
Row.new(%q( [x, y]), nil, 6),
190+
Row.new(%q( rescue => e), 4, 6),
191+
Row.new(%q( raise e rescue 8), nil, 6),
192+
Row.new(%q( end), 4, 4),
193+
Row.new(%q( rescue), 2, 4),
194+
Row.new(%q( raise rescue 11), nil, 4),
195+
Row.new(%q( end), 2, 2),
196+
Row.new(%q(rescue => e), 0, 2),
197+
Row.new(%q( raise e rescue 14), nil, 2),
198+
Row.new(%q(end), 0, 0),
199+
]
200+
201+
lines = []
202+
input_with_correct_indents.each do |row|
203+
lines << row.content
204+
assert_indenting(lines, row.current_line_spaces, false)
205+
assert_indenting(lines, row.new_line_spaces, true)
206+
end
207+
end
181208
end
182209
end

0 commit comments

Comments
 (0)