Skip to content

Commit f01ff08

Browse files
authored
Omit nesting_level, use indent_level to build prompt string (#610)
1 parent 1159c13 commit f01ff08

File tree

2 files changed

+55
-59
lines changed

2 files changed

+55
-59
lines changed

lib/irb/ruby-lex.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ def self.ripper_lex_without_warning(code, context: nil)
184184

185185
def prompt(opens, continue, line_num_offset)
186186
ltype = ltype_from_open_tokens(opens)
187-
_indent_level, nesting_level = calc_nesting_depth(opens)
188-
@prompt&.call(ltype, nesting_level, opens.any? || continue, @line_no + line_num_offset)
187+
indent_level = calc_indent_level(opens)
188+
@prompt&.call(ltype, indent_level, opens.any? || continue, @line_no + line_num_offset)
189189
end
190190

191191
def check_code_state(code)
@@ -356,10 +356,8 @@ def check_code_block(code, tokens)
356356
false
357357
end
358358

359-
# Calculates [indent_level, nesting_level]. nesting_level is used in prompt string.
360-
def calc_nesting_depth(opens)
359+
def calc_indent_level(opens)
361360
indent_level = 0
362-
nesting_level = 0
363361
opens.each_with_index do |t, index|
364362
case t.event
365363
when :on_heredoc_beg
@@ -377,11 +375,10 @@ def calc_nesting_depth(opens)
377375
when :on_embdoc_beg
378376
indent_level = 0
379377
else
380-
nesting_level += 1
381378
indent_level += 1
382379
end
383380
end
384-
[indent_level, nesting_level]
381+
indent_level
385382
end
386383

387384
FREE_INDENT_TOKENS = %i[on_tstring_beg on_backtick on_regexp_beg on_symbeg]
@@ -403,8 +400,7 @@ def process_indent_level(tokens, lines, line_index, is_newline)
403400

404401
# To correctly indent line like `end.map do`, we use shortest open tokens on each line for indent calculation.
405402
# Shortest open tokens can be calculated by `opens.take(min_depth)`
406-
indent_level, _nesting_level = calc_nesting_depth(prev_opens.take(min_depth))
407-
indent = 2 * indent_level
403+
indent = 2 * calc_indent_level(prev_opens.take(min_depth))
408404

409405
preserve_indent = lines[line_index - (is_newline ? 1 : 0)][/^ */].size
410406

@@ -442,7 +438,7 @@ def process_indent_level(tokens, lines, line_index, is_newline)
442438
end
443439
else
444440
# Heredoc close
445-
prev_line_indent_level, _prev_line_nesting_level = calc_nesting_depth(prev_opens)
441+
prev_line_indent_level = calc_indent_level(prev_opens)
446442
tok.match?(/^<<[~-]/) ? 2 * (prev_line_indent_level - 1) : 0
447443
end
448444
else

test/irb/test_ruby_lex.rb

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
module TestIRB
99
class RubyLexTest < TestCase
10-
Row = Struct.new(:content, :current_line_spaces, :new_line_spaces, :nesting_level)
10+
Row = Struct.new(:content, :current_line_spaces, :new_line_spaces, :indent_level)
1111

1212
class MockIO_AutoIndent
1313
attr_reader :calculated_indent
@@ -81,14 +81,14 @@ def assert_row_indenting(lines, row)
8181
assert_equal(row.new_line_spaces, actual_next_line_spaces, error_message)
8282
end
8383

84-
def assert_nesting_level(lines, expected, local_variables: [])
85-
nesting_level, _code_block_open = check_state(lines, local_variables: local_variables)
86-
error_message = "Calculated the wrong number of nesting level for:\n #{lines.join("\n")}"
87-
assert_equal(expected, nesting_level, error_message)
84+
def assert_indent_level(lines, expected, local_variables: [])
85+
indent_level, _code_block_open = check_state(lines, local_variables: local_variables)
86+
error_message = "Calculated the wrong number of indent level for:\n #{lines.join("\n")}"
87+
assert_equal(expected, indent_level, error_message)
8888
end
8989

9090
def assert_code_block_open(lines, expected, local_variables: [])
91-
_nesting_level, code_block_open = check_state(lines, local_variables: local_variables)
91+
_indent_level, code_block_open = check_state(lines, local_variables: local_variables)
9292
error_message = "Wrong result of code_block_open for:\n #{lines.join("\n")}"
9393
assert_equal(expected, code_block_open, error_message)
9494
end
@@ -98,9 +98,9 @@ def check_state(lines, local_variables: [])
9898
tokens = RubyLex.ripper_lex_without_warning(lines.join("\n"), context: context)
9999
opens = IRB::NestingParser.open_tokens(tokens)
100100
ruby_lex = RubyLex.new(context)
101-
_indent, nesting_level = ruby_lex.calc_nesting_depth(opens)
101+
indent_level = ruby_lex.calc_indent_level(opens)
102102
code_block_open = !opens.empty? || ruby_lex.process_continue(tokens)
103-
[nesting_level, code_block_open]
103+
[indent_level, code_block_open]
104104
end
105105

106106
def test_interpolate_token_with_heredoc_and_unclosed_embexpr
@@ -266,14 +266,14 @@ def test_heredoc_with_embexpr
266266

267267
def test_heredoc_prompt_with_quotes
268268
input_with_prompt = [
269-
PromptRow.new("001:0:':* ", %q(<<~'A')),
270-
PromptRow.new("002:0:':* ", %q(#{foobar})),
269+
PromptRow.new("001:1:':* ", %q(<<~'A')),
270+
PromptRow.new("002:1:':* ", %q(#{foobar})),
271271
PromptRow.new("003:0: :> ", %q(A)),
272-
PromptRow.new("004:0:`:* ", %q(<<~`A`)),
273-
PromptRow.new("005:0:`:* ", %q(whoami)),
272+
PromptRow.new("004:1:`:* ", %q(<<~`A`)),
273+
PromptRow.new("005:1:`:* ", %q(whoami)),
274274
PromptRow.new("006:0: :> ", %q(A)),
275-
PromptRow.new('007:0:":* ', %q(<<~"A")),
276-
PromptRow.new('008:0:":* ', %q(foobar)),
275+
PromptRow.new('007:1:":* ', %q(<<~"A")),
276+
PromptRow.new('008:1:":* ', %q(foobar)),
277277
PromptRow.new('009:0: :> ', %q(A)),
278278
]
279279

@@ -411,7 +411,7 @@ def test_tlambda
411411
input_with_correct_indents.each do |row|
412412
lines << row.content
413413
assert_row_indenting(lines, row)
414-
assert_nesting_level(lines, row.nesting_level)
414+
assert_indent_level(lines, row.indent_level)
415415
end
416416
end
417417

@@ -431,7 +431,7 @@ def test_corresponding_syntax_to_keyword_do_in_class
431431
input_with_correct_indents.each do |row|
432432
lines << row.content
433433
assert_row_indenting(lines, row)
434-
assert_nesting_level(lines, row.nesting_level)
434+
assert_indent_level(lines, row.indent_level)
435435
end
436436
end
437437

@@ -479,7 +479,7 @@ def test_corresponding_syntax_to_keyword_do
479479
input_with_correct_indents.each do |row|
480480
lines << row.content
481481
assert_row_indenting(lines, row)
482-
assert_nesting_level(lines, row.nesting_level)
482+
assert_indent_level(lines, row.indent_level)
483483
end
484484
end
485485

@@ -494,7 +494,7 @@ def test_corresponding_syntax_to_keyword_for
494494
input_with_correct_indents.each do |row|
495495
lines << row.content
496496
assert_row_indenting(lines, row)
497-
assert_nesting_level(lines, row.nesting_level)
497+
assert_indent_level(lines, row.indent_level)
498498
end
499499
end
500500

@@ -509,7 +509,7 @@ def test_corresponding_syntax_to_keyword_for_with_do
509509
input_with_correct_indents.each do |row|
510510
lines << row.content
511511
assert_row_indenting(lines, row)
512-
assert_nesting_level(lines, row.nesting_level)
512+
assert_indent_level(lines, row.indent_level)
513513
end
514514
end
515515

@@ -525,7 +525,7 @@ def test_typing_incomplete_include_interpreted_as_keyword_in
525525
input_with_correct_indents.each do |row|
526526
lines << row.content
527527
assert_row_indenting(lines, row)
528-
assert_nesting_level(lines, row.nesting_level)
528+
assert_indent_level(lines, row.indent_level)
529529
end
530530
end
531531

@@ -540,7 +540,7 @@ def test_bracket_corresponding_to_times
540540
input_with_correct_indents.each do |row|
541541
lines << row.content
542542
assert_row_indenting(lines, row)
543-
assert_nesting_level(lines, row.nesting_level)
543+
assert_indent_level(lines, row.indent_level)
544544
end
545545
end
546546

@@ -555,7 +555,7 @@ def test_do_corresponding_to_times
555555
input_with_correct_indents.each do |row|
556556
lines << row.content
557557
assert_row_indenting(lines, row)
558-
assert_nesting_level(lines, row.nesting_level)
558+
assert_indent_level(lines, row.indent_level)
559559
end
560560
end
561561

@@ -570,7 +570,7 @@ def test_bracket_corresponding_to_loop
570570
input_with_correct_indents.each do |row|
571571
lines << row.content
572572
assert_row_indenting(lines, row)
573-
assert_nesting_level(lines, row.nesting_level)
573+
assert_indent_level(lines, row.indent_level)
574574
end
575575
end
576576

@@ -585,16 +585,16 @@ def test_do_corresponding_to_loop
585585
input_with_correct_indents.each do |row|
586586
lines << row.content
587587
assert_row_indenting(lines, row)
588-
assert_nesting_level(lines, row.nesting_level)
588+
assert_indent_level(lines, row.indent_level)
589589
end
590590
end
591591

592592
def test_local_variables_dependent_code
593593
pend if RUBY_ENGINE == 'truffleruby'
594594
lines = ["a /1#/ do", "2"]
595-
assert_nesting_level(lines, 1)
595+
assert_indent_level(lines, 1)
596596
assert_code_block_open(lines, true)
597-
assert_nesting_level(lines, 0, local_variables: ['a'])
597+
assert_indent_level(lines, 0, local_variables: ['a'])
598598
assert_code_block_open(lines, false, local_variables: ['a'])
599599
end
600600

@@ -606,9 +606,9 @@ def test_embdoc_indent
606606
Row.new(%q(=end), 0, 0, 0),
607607
Row.new(%q(if 1), 0, 2, 1),
608608
Row.new(%q( 2), 2, 2, 1),
609-
Row.new(%q(=begin), 0, 0, 1),
610-
Row.new(%q(a), 0, 0, 1),
611-
Row.new(%q( b), 1, 1, 1),
609+
Row.new(%q(=begin), 0, 0, 0),
610+
Row.new(%q(a), 0, 0, 0),
611+
Row.new(%q( b), 1, 1, 0),
612612
Row.new(%q(=end), 0, 2, 1),
613613
Row.new(%q( 3), 2, 2, 1),
614614
Row.new(%q(end), 0, 0, 0),
@@ -617,7 +617,7 @@ def test_embdoc_indent
617617
input_with_correct_indents.each do |row|
618618
lines << row.content
619619
assert_row_indenting(lines, row)
620-
assert_nesting_level(lines, row.nesting_level)
620+
assert_indent_level(lines, row.indent_level)
621621
end
622622
end
623623

@@ -626,22 +626,22 @@ def test_heredoc_with_indent
626626
pend 'This test needs Ripper::Lexer#scan to take broken tokens'
627627
end
628628
input_with_correct_indents = [
629-
Row.new(%q(<<~Q+<<~R), 0, 2, 0),
630-
Row.new(%q(a), 2, 2, 0),
631-
Row.new(%q(a), 2, 2, 0),
632-
Row.new(%q( b), 2, 2, 0),
633-
Row.new(%q( b), 2, 2, 0),
634-
Row.new(%q( Q), 0, 2, 0),
635-
Row.new(%q( c), 4, 4, 0),
636-
Row.new(%q( c), 4, 4, 0),
629+
Row.new(%q(<<~Q+<<~R), 0, 2, 1),
630+
Row.new(%q(a), 2, 2, 1),
631+
Row.new(%q(a), 2, 2, 1),
632+
Row.new(%q( b), 2, 2, 1),
633+
Row.new(%q( b), 2, 2, 1),
634+
Row.new(%q( Q), 0, 2, 1),
635+
Row.new(%q( c), 4, 4, 1),
636+
Row.new(%q( c), 4, 4, 1),
637637
Row.new(%q( R), 0, 0, 0),
638638
]
639639

640640
lines = []
641641
input_with_correct_indents.each do |row|
642642
lines << row.content
643643
assert_row_indenting(lines, row)
644-
assert_nesting_level(lines, row.nesting_level)
644+
assert_indent_level(lines, row.indent_level)
645645
end
646646
end
647647

@@ -657,30 +657,30 @@ def test_oneliner_def_in_multiple_lines
657657
input_with_correct_indents.each do |row|
658658
lines << row.content
659659
assert_row_indenting(lines, row)
660-
assert_nesting_level(lines, row.nesting_level)
660+
assert_indent_level(lines, row.indent_level)
661661
end
662662
end
663663

664664
def test_broken_heredoc
665665
input_with_correct_indents = [
666666
Row.new(%q(def foo), 0, 2, 1),
667-
Row.new(%q( <<~Q), 2, 4, 1),
668-
Row.new(%q( Qend), 4, 4, 1),
667+
Row.new(%q( <<~Q), 2, 4, 2),
668+
Row.new(%q( Qend), 4, 4, 2),
669669
]
670670
lines = []
671671
input_with_correct_indents.each do |row|
672672
lines << row.content
673673
assert_row_indenting(lines, row)
674-
assert_nesting_level(lines, row.nesting_level)
674+
assert_indent_level(lines, row.indent_level)
675675
end
676676
end
677677

678678
def test_heredoc_keep_indent_spaces
679679
(1..4).each do |indent|
680-
row = Row.new(' ' * indent, nil, [4, indent].max, 1)
680+
row = Row.new(' ' * indent, nil, [4, indent].max, 2)
681681
lines = ['def foo', ' <<~Q', row.content]
682682
assert_row_indenting(lines, row)
683-
assert_nesting_level(lines, row.nesting_level)
683+
assert_indent_level(lines, row.indent_level)
684684
end
685685
end
686686

@@ -816,7 +816,7 @@ def test_unterminated_heredoc_string_literal
816816
end
817817
end
818818

819-
def test_nesting_level_with_heredoc_and_embdoc
819+
def test_indent_level_with_heredoc_and_embdoc
820820
reference_code = <<~EOC.chomp
821821
if true
822822
hello
@@ -838,9 +838,9 @@ def test_nesting_level_with_heredoc_and_embdoc
838838
)
839839
EOC
840840
expected = 1
841-
assert_nesting_level(reference_code.lines, expected)
842-
assert_nesting_level(code_with_heredoc.lines, expected)
843-
assert_nesting_level(code_with_embdoc.lines, expected)
841+
assert_indent_level(reference_code.lines, expected)
842+
assert_indent_level(code_with_heredoc.lines, expected)
843+
assert_indent_level(code_with_embdoc.lines, expected)
844844
end
845845

846846
private

0 commit comments

Comments
 (0)