Skip to content

Commit 9646434

Browse files
committed
Handle indentations related to keyword "do" correctly
This fixes #158.
1 parent 4cdfc60 commit 9646434

File tree

2 files changed

+117
-17
lines changed

2 files changed

+117
-17
lines changed

lib/irb/ruby-lex.rb

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def set_auto_indent(context)
124124
prev_spaces = md.nil? ? 0 : md[1].count(' ')
125125
@tokens = ripper_lex_without_warning(lines[0..line_index].join("\n"))
126126
depth_difference = check_newline_depth_difference
127+
depth_difference = 0 if depth_difference < 0
127128
prev_spaces + depth_difference * 2
128129
else
129130
code = line_index.zero? ? '' : lines[0..(line_index - 1)].map{ |l| l + "\n" }.join
@@ -360,14 +361,8 @@ def process_nesting_level(tokens = @tokens)
360361
next if index > 0 and tokens[index - 1][3].allbits?(Ripper::EXPR_FNAME)
361362
case t[2]
362363
when 'do'
363-
if index > 0 and tokens[index - 1][3].anybits?(Ripper::EXPR_CMDARG | Ripper::EXPR_ENDFN | Ripper::EXPR_ARG)
364-
# method_with_block do; end
365-
indent += 1
366-
else
367-
# while cond do; end # also "until" or "for"
368-
# This "do" doesn't increment indent because "while" already
369-
# incremented.
370-
end
364+
syntax_of_do = take_corresponding_syntax_to_kw_do(tokens, index)
365+
indent += 1 if syntax_of_do == :method_calling
371366
when 'def', 'case', 'for', 'begin', 'class', 'module'
372367
indent += 1
373368
when 'if', 'unless', 'while', 'until'
@@ -382,6 +377,40 @@ def process_nesting_level(tokens = @tokens)
382377
indent
383378
end
384379

380+
def take_corresponding_syntax_to_kw_do(tokens, index)
381+
syntax_of_do = nil
382+
# Finding a syntax correnponding to "do".
383+
index.downto(0) do |i|
384+
tk = tokens[i]
385+
# In "continue", the token isn't the corresponding syntax to "do".
386+
#is_continue = process_continue(@tokens[0..(i - 1)])
387+
# continue ではなく、直前に (:on_ignored_nl|:on_nl|:on_comment):on_sp* みたいなのがあるかどうかを調べる
388+
non_sp_index = tokens[0..(i - 1)].rindex{ |t| t[1] != :on_sp }
389+
first_in_fomula = false
390+
if non_sp_index.nil?
391+
first_in_fomula = true
392+
elsif [:on_ignored_nl, :on_nl, :on_comment].include?(tokens[non_sp_index][1])
393+
first_in_fomula = true
394+
end
395+
if tk[3].anybits?(Ripper::EXPR_CMDARG) and tk[1] == :on_ident
396+
# The target method call to pass the block with "do".
397+
syntax_of_do = :method_calling
398+
break if first_in_fomula
399+
elsif tk[1] == :on_kw && %w{while until for}.include?(tk[2])
400+
# A loop syntax in front of "do" found.
401+
#
402+
# while cond do # also "until" or "for"
403+
# end
404+
#
405+
# This "do" doesn't increment indent because the loop syntax already
406+
# incremented.
407+
syntax_of_do = :loop_syntax
408+
break if first_in_fomula
409+
end
410+
end
411+
syntax_of_do
412+
end
413+
385414
def check_newline_depth_difference
386415
depth_difference = 0
387416
open_brace_on_line = 0
@@ -428,14 +457,8 @@ def check_newline_depth_difference
428457
next if index > 0 and @tokens[index - 1][3].allbits?(Ripper::EXPR_FNAME)
429458
case t[2]
430459
when 'do'
431-
if index > 0 and @tokens[index - 1][3].anybits?(Ripper::EXPR_CMDARG | Ripper::EXPR_ENDFN | Ripper::EXPR_ARG)
432-
# method_with_block do; end
433-
depth_difference += 1
434-
else
435-
# while cond do; end # also "until" or "for"
436-
# This "do" doesn't increment indent because "while" already
437-
# incremented.
438-
end
460+
syntax_of_do = take_corresponding_syntax_to_kw_do(@tokens, index)
461+
depth_difference += 1 if syntax_of_do == :method_calling
439462
when 'def', 'case', 'for', 'begin', 'class', 'module'
440463
depth_difference += 1
441464
when 'if', 'unless', 'while', 'until', 'rescue'
@@ -445,6 +468,8 @@ def check_newline_depth_difference
445468
end
446469
when 'else', 'elsif', 'ensure', 'when', 'in'
447470
depth_difference += 1
471+
when 'end'
472+
depth_difference -= 1
448473
end
449474
end
450475
end
@@ -516,7 +541,12 @@ def check_corresponding_token_depth
516541
when :on_kw
517542
next if index > 0 and @tokens[index - 1][3].allbits?(Ripper::EXPR_FNAME)
518543
case t[2]
519-
when 'def', 'do', 'case', 'for', 'begin', 'class', 'module'
544+
when 'do'
545+
syntax_of_do = take_corresponding_syntax_to_kw_do(@tokens, index)
546+
if syntax_of_do == :method_calling
547+
spaces_of_nest.push(spaces_at_line_head)
548+
end
549+
when 'def', 'case', 'for', 'begin', 'class', 'module'
520550
spaces_of_nest.push(spaces_at_line_head)
521551
when 'rescue'
522552
unless t[3].allbits?(Ripper::EXPR_LABEL)

test/irb/test_ruby_lex.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,76 @@ def test_tlambda
263263
end
264264
end
265265

266+
def test_corresponding_syntax_to_keyword_do_in_class
267+
input_with_correct_indents = [
268+
Row.new(%q(class C), nil, 2, 1),
269+
Row.new(%q( while method_name do), nil, 4, 2),
270+
Row.new(%q( 3), nil, 4, 2),
271+
Row.new(%q( end), 2, 2, 1),
272+
Row.new(%q( foo do), nil, 4, 2),
273+
Row.new(%q( 3), nil, 4, 2),
274+
Row.new(%q( end), 2, 2, 1),
275+
Row.new(%q(end), 0, 0, 0),
276+
]
277+
278+
lines = []
279+
input_with_correct_indents.each do |row|
280+
lines << row.content
281+
assert_indenting(lines, row.current_line_spaces, false)
282+
assert_indenting(lines, row.new_line_spaces, true)
283+
assert_nesting_level(lines, row.nesting_level)
284+
end
285+
end
286+
287+
def test_corresponding_syntax_to_keyword_do
288+
input_with_correct_indents = [
289+
Row.new(%q(while i > 0), nil, 2, 1),
290+
Row.new(%q( 3), nil, 2, 1),
291+
Row.new(%q(end), 0, 0, 0),
292+
Row.new(%q(while true), nil, 2, 1),
293+
Row.new(%q( 3), nil, 2, 1),
294+
Row.new(%q(end), 0, 0, 0),
295+
Row.new(%q(while ->{i > 0}.call), nil, 2, 1),
296+
Row.new(%q( 3), nil, 2, 1),
297+
Row.new(%q(end), 0, 0, 0),
298+
Row.new(%q(while ->{true}.call), nil, 2, 1),
299+
Row.new(%q( 3), nil, 2, 1),
300+
Row.new(%q(end), 0, 0, 0),
301+
Row.new(%q(while i > 0 do), nil, 2, 1),
302+
Row.new(%q( 3), nil, 2, 1),
303+
Row.new(%q(end), 0, 0, 0),
304+
Row.new(%q(while true do), nil, 2, 1),
305+
Row.new(%q( 3), nil, 2, 1),
306+
Row.new(%q(end), 0, 0, 0),
307+
Row.new(%q(while ->{i > 0}.call do), nil, 2, 1),
308+
Row.new(%q( 3), nil, 2, 1),
309+
Row.new(%q(end), 0, 0, 0),
310+
Row.new(%q(while ->{true}.call do), nil, 2, 1),
311+
Row.new(%q( 3), nil, 2, 1),
312+
Row.new(%q(end), 0, 0, 0),
313+
Row.new(%q(foo do), nil, 2, 1),
314+
Row.new(%q( 3), nil, 2, 1),
315+
Row.new(%q(end), 0, 0, 0),
316+
Row.new(%q(foo true do), nil, 2, 1),
317+
Row.new(%q( 3), nil, 2, 1),
318+
Row.new(%q(end), 0, 0, 0),
319+
Row.new(%q(foo ->{true} do), nil, 2, 1),
320+
Row.new(%q( 3), nil, 2, 1),
321+
Row.new(%q(end), 0, 0, 0),
322+
Row.new(%q(foo ->{i > 0} do), nil, 2, 1),
323+
Row.new(%q( 3), nil, 2, 1),
324+
Row.new(%q(end), 0, 0, 0),
325+
]
326+
327+
lines = []
328+
input_with_correct_indents.each do |row|
329+
lines << row.content
330+
assert_indenting(lines, row.current_line_spaces, false)
331+
assert_indenting(lines, row.new_line_spaces, true)
332+
assert_nesting_level(lines, row.nesting_level)
333+
end
334+
end
335+
266336
def test_oneliner_def_in_multiple_lines
267337
input_with_correct_indents = [
268338
Row.new(%q(def a()=[), nil, 4, 2),

0 commit comments

Comments
 (0)