[ruby/irb] Omit nesting_level, use indent_level to build prompt
string (https://github.com/ruby/irb/pull/610) https://github.com/ruby/irb/commit/f01ff0811b
This commit is contained in:
parent
a14915ca4b
commit
406799cae8
@ -184,8 +184,8 @@ class RubyLex
|
|||||||
|
|
||||||
def prompt(opens, continue, line_num_offset)
|
def prompt(opens, continue, line_num_offset)
|
||||||
ltype = ltype_from_open_tokens(opens)
|
ltype = ltype_from_open_tokens(opens)
|
||||||
_indent_level, nesting_level = calc_nesting_depth(opens)
|
indent_level = calc_indent_level(opens)
|
||||||
@prompt&.call(ltype, nesting_level, opens.any? || continue, @line_no + line_num_offset)
|
@prompt&.call(ltype, indent_level, opens.any? || continue, @line_no + line_num_offset)
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_code_state(code)
|
def check_code_state(code)
|
||||||
@ -356,10 +356,8 @@ class RubyLex
|
|||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
# Calculates [indent_level, nesting_level]. nesting_level is used in prompt string.
|
def calc_indent_level(opens)
|
||||||
def calc_nesting_depth(opens)
|
|
||||||
indent_level = 0
|
indent_level = 0
|
||||||
nesting_level = 0
|
|
||||||
opens.each_with_index do |t, index|
|
opens.each_with_index do |t, index|
|
||||||
case t.event
|
case t.event
|
||||||
when :on_heredoc_beg
|
when :on_heredoc_beg
|
||||||
@ -377,11 +375,10 @@ class RubyLex
|
|||||||
when :on_embdoc_beg
|
when :on_embdoc_beg
|
||||||
indent_level = 0
|
indent_level = 0
|
||||||
else
|
else
|
||||||
nesting_level += 1
|
|
||||||
indent_level += 1
|
indent_level += 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
[indent_level, nesting_level]
|
indent_level
|
||||||
end
|
end
|
||||||
|
|
||||||
FREE_INDENT_TOKENS = %i[on_tstring_beg on_backtick on_regexp_beg on_symbeg]
|
FREE_INDENT_TOKENS = %i[on_tstring_beg on_backtick on_regexp_beg on_symbeg]
|
||||||
@ -403,8 +400,7 @@ class RubyLex
|
|||||||
|
|
||||||
# To correctly indent line like `end.map do`, we use shortest open tokens on each line for indent calculation.
|
# To correctly indent line like `end.map do`, we use shortest open tokens on each line for indent calculation.
|
||||||
# Shortest open tokens can be calculated by `opens.take(min_depth)`
|
# Shortest open tokens can be calculated by `opens.take(min_depth)`
|
||||||
indent_level, _nesting_level = calc_nesting_depth(prev_opens.take(min_depth))
|
indent = 2 * calc_indent_level(prev_opens.take(min_depth))
|
||||||
indent = 2 * indent_level
|
|
||||||
|
|
||||||
preserve_indent = lines[line_index - (is_newline ? 1 : 0)][/^ */].size
|
preserve_indent = lines[line_index - (is_newline ? 1 : 0)][/^ */].size
|
||||||
|
|
||||||
@ -442,7 +438,7 @@ class RubyLex
|
|||||||
end
|
end
|
||||||
else
|
else
|
||||||
# Heredoc close
|
# Heredoc close
|
||||||
prev_line_indent_level, _prev_line_nesting_level = calc_nesting_depth(prev_opens)
|
prev_line_indent_level = calc_indent_level(prev_opens)
|
||||||
tok.match?(/^<<[~-]/) ? 2 * (prev_line_indent_level - 1) : 0
|
tok.match?(/^<<[~-]/) ? 2 * (prev_line_indent_level - 1) : 0
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
@ -7,7 +7,7 @@ require_relative "helper"
|
|||||||
|
|
||||||
module TestIRB
|
module TestIRB
|
||||||
class RubyLexTest < TestCase
|
class RubyLexTest < TestCase
|
||||||
Row = Struct.new(:content, :current_line_spaces, :new_line_spaces, :nesting_level)
|
Row = Struct.new(:content, :current_line_spaces, :new_line_spaces, :indent_level)
|
||||||
|
|
||||||
class MockIO_AutoIndent
|
class MockIO_AutoIndent
|
||||||
attr_reader :calculated_indent
|
attr_reader :calculated_indent
|
||||||
@ -81,14 +81,14 @@ module TestIRB
|
|||||||
assert_equal(row.new_line_spaces, actual_next_line_spaces, error_message)
|
assert_equal(row.new_line_spaces, actual_next_line_spaces, error_message)
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_nesting_level(lines, expected, local_variables: [])
|
def assert_indent_level(lines, expected, local_variables: [])
|
||||||
nesting_level, _code_block_open = check_state(lines, local_variables: local_variables)
|
indent_level, _code_block_open = check_state(lines, local_variables: local_variables)
|
||||||
error_message = "Calculated the wrong number of nesting level for:\n #{lines.join("\n")}"
|
error_message = "Calculated the wrong number of indent level for:\n #{lines.join("\n")}"
|
||||||
assert_equal(expected, nesting_level, error_message)
|
assert_equal(expected, indent_level, error_message)
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_code_block_open(lines, expected, local_variables: [])
|
def assert_code_block_open(lines, expected, local_variables: [])
|
||||||
_nesting_level, code_block_open = check_state(lines, local_variables: local_variables)
|
_indent_level, code_block_open = check_state(lines, local_variables: local_variables)
|
||||||
error_message = "Wrong result of code_block_open for:\n #{lines.join("\n")}"
|
error_message = "Wrong result of code_block_open for:\n #{lines.join("\n")}"
|
||||||
assert_equal(expected, code_block_open, error_message)
|
assert_equal(expected, code_block_open, error_message)
|
||||||
end
|
end
|
||||||
@ -98,9 +98,9 @@ module TestIRB
|
|||||||
tokens = RubyLex.ripper_lex_without_warning(lines.join("\n"), context: context)
|
tokens = RubyLex.ripper_lex_without_warning(lines.join("\n"), context: context)
|
||||||
opens = IRB::NestingParser.open_tokens(tokens)
|
opens = IRB::NestingParser.open_tokens(tokens)
|
||||||
ruby_lex = RubyLex.new(context)
|
ruby_lex = RubyLex.new(context)
|
||||||
_indent, nesting_level = ruby_lex.calc_nesting_depth(opens)
|
indent_level = ruby_lex.calc_indent_level(opens)
|
||||||
code_block_open = !opens.empty? || ruby_lex.process_continue(tokens)
|
code_block_open = !opens.empty? || ruby_lex.process_continue(tokens)
|
||||||
[nesting_level, code_block_open]
|
[indent_level, code_block_open]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_interpolate_token_with_heredoc_and_unclosed_embexpr
|
def test_interpolate_token_with_heredoc_and_unclosed_embexpr
|
||||||
@ -266,14 +266,14 @@ module TestIRB
|
|||||||
|
|
||||||
def test_heredoc_prompt_with_quotes
|
def test_heredoc_prompt_with_quotes
|
||||||
input_with_prompt = [
|
input_with_prompt = [
|
||||||
PromptRow.new("001:0:':* ", %q(<<~'A')),
|
PromptRow.new("001:1:':* ", %q(<<~'A')),
|
||||||
PromptRow.new("002:0:':* ", %q(#{foobar})),
|
PromptRow.new("002:1:':* ", %q(#{foobar})),
|
||||||
PromptRow.new("003:0: :> ", %q(A)),
|
PromptRow.new("003:0: :> ", %q(A)),
|
||||||
PromptRow.new("004:0:`:* ", %q(<<~`A`)),
|
PromptRow.new("004:1:`:* ", %q(<<~`A`)),
|
||||||
PromptRow.new("005:0:`:* ", %q(whoami)),
|
PromptRow.new("005:1:`:* ", %q(whoami)),
|
||||||
PromptRow.new("006:0: :> ", %q(A)),
|
PromptRow.new("006:0: :> ", %q(A)),
|
||||||
PromptRow.new('007:0:":* ', %q(<<~"A")),
|
PromptRow.new('007:1:":* ', %q(<<~"A")),
|
||||||
PromptRow.new('008:0:":* ', %q(foobar)),
|
PromptRow.new('008:1:":* ', %q(foobar)),
|
||||||
PromptRow.new('009:0: :> ', %q(A)),
|
PromptRow.new('009:0: :> ', %q(A)),
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -411,7 +411,7 @@ module TestIRB
|
|||||||
input_with_correct_indents.each do |row|
|
input_with_correct_indents.each do |row|
|
||||||
lines << row.content
|
lines << row.content
|
||||||
assert_row_indenting(lines, row)
|
assert_row_indenting(lines, row)
|
||||||
assert_nesting_level(lines, row.nesting_level)
|
assert_indent_level(lines, row.indent_level)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -431,7 +431,7 @@ module TestIRB
|
|||||||
input_with_correct_indents.each do |row|
|
input_with_correct_indents.each do |row|
|
||||||
lines << row.content
|
lines << row.content
|
||||||
assert_row_indenting(lines, row)
|
assert_row_indenting(lines, row)
|
||||||
assert_nesting_level(lines, row.nesting_level)
|
assert_indent_level(lines, row.indent_level)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -479,7 +479,7 @@ module TestIRB
|
|||||||
input_with_correct_indents.each do |row|
|
input_with_correct_indents.each do |row|
|
||||||
lines << row.content
|
lines << row.content
|
||||||
assert_row_indenting(lines, row)
|
assert_row_indenting(lines, row)
|
||||||
assert_nesting_level(lines, row.nesting_level)
|
assert_indent_level(lines, row.indent_level)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -494,7 +494,7 @@ module TestIRB
|
|||||||
input_with_correct_indents.each do |row|
|
input_with_correct_indents.each do |row|
|
||||||
lines << row.content
|
lines << row.content
|
||||||
assert_row_indenting(lines, row)
|
assert_row_indenting(lines, row)
|
||||||
assert_nesting_level(lines, row.nesting_level)
|
assert_indent_level(lines, row.indent_level)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -509,7 +509,7 @@ module TestIRB
|
|||||||
input_with_correct_indents.each do |row|
|
input_with_correct_indents.each do |row|
|
||||||
lines << row.content
|
lines << row.content
|
||||||
assert_row_indenting(lines, row)
|
assert_row_indenting(lines, row)
|
||||||
assert_nesting_level(lines, row.nesting_level)
|
assert_indent_level(lines, row.indent_level)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -525,7 +525,7 @@ module TestIRB
|
|||||||
input_with_correct_indents.each do |row|
|
input_with_correct_indents.each do |row|
|
||||||
lines << row.content
|
lines << row.content
|
||||||
assert_row_indenting(lines, row)
|
assert_row_indenting(lines, row)
|
||||||
assert_nesting_level(lines, row.nesting_level)
|
assert_indent_level(lines, row.indent_level)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -540,7 +540,7 @@ module TestIRB
|
|||||||
input_with_correct_indents.each do |row|
|
input_with_correct_indents.each do |row|
|
||||||
lines << row.content
|
lines << row.content
|
||||||
assert_row_indenting(lines, row)
|
assert_row_indenting(lines, row)
|
||||||
assert_nesting_level(lines, row.nesting_level)
|
assert_indent_level(lines, row.indent_level)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -555,7 +555,7 @@ module TestIRB
|
|||||||
input_with_correct_indents.each do |row|
|
input_with_correct_indents.each do |row|
|
||||||
lines << row.content
|
lines << row.content
|
||||||
assert_row_indenting(lines, row)
|
assert_row_indenting(lines, row)
|
||||||
assert_nesting_level(lines, row.nesting_level)
|
assert_indent_level(lines, row.indent_level)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -570,7 +570,7 @@ module TestIRB
|
|||||||
input_with_correct_indents.each do |row|
|
input_with_correct_indents.each do |row|
|
||||||
lines << row.content
|
lines << row.content
|
||||||
assert_row_indenting(lines, row)
|
assert_row_indenting(lines, row)
|
||||||
assert_nesting_level(lines, row.nesting_level)
|
assert_indent_level(lines, row.indent_level)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -585,16 +585,16 @@ module TestIRB
|
|||||||
input_with_correct_indents.each do |row|
|
input_with_correct_indents.each do |row|
|
||||||
lines << row.content
|
lines << row.content
|
||||||
assert_row_indenting(lines, row)
|
assert_row_indenting(lines, row)
|
||||||
assert_nesting_level(lines, row.nesting_level)
|
assert_indent_level(lines, row.indent_level)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_local_variables_dependent_code
|
def test_local_variables_dependent_code
|
||||||
pend if RUBY_ENGINE == 'truffleruby'
|
pend if RUBY_ENGINE == 'truffleruby'
|
||||||
lines = ["a /1#/ do", "2"]
|
lines = ["a /1#/ do", "2"]
|
||||||
assert_nesting_level(lines, 1)
|
assert_indent_level(lines, 1)
|
||||||
assert_code_block_open(lines, true)
|
assert_code_block_open(lines, true)
|
||||||
assert_nesting_level(lines, 0, local_variables: ['a'])
|
assert_indent_level(lines, 0, local_variables: ['a'])
|
||||||
assert_code_block_open(lines, false, local_variables: ['a'])
|
assert_code_block_open(lines, false, local_variables: ['a'])
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -606,9 +606,9 @@ module TestIRB
|
|||||||
Row.new(%q(=end), 0, 0, 0),
|
Row.new(%q(=end), 0, 0, 0),
|
||||||
Row.new(%q(if 1), 0, 2, 1),
|
Row.new(%q(if 1), 0, 2, 1),
|
||||||
Row.new(%q( 2), 2, 2, 1),
|
Row.new(%q( 2), 2, 2, 1),
|
||||||
Row.new(%q(=begin), 0, 0, 1),
|
Row.new(%q(=begin), 0, 0, 0),
|
||||||
Row.new(%q(a), 0, 0, 1),
|
Row.new(%q(a), 0, 0, 0),
|
||||||
Row.new(%q( b), 1, 1, 1),
|
Row.new(%q( b), 1, 1, 0),
|
||||||
Row.new(%q(=end), 0, 2, 1),
|
Row.new(%q(=end), 0, 2, 1),
|
||||||
Row.new(%q( 3), 2, 2, 1),
|
Row.new(%q( 3), 2, 2, 1),
|
||||||
Row.new(%q(end), 0, 0, 0),
|
Row.new(%q(end), 0, 0, 0),
|
||||||
@ -617,7 +617,7 @@ module TestIRB
|
|||||||
input_with_correct_indents.each do |row|
|
input_with_correct_indents.each do |row|
|
||||||
lines << row.content
|
lines << row.content
|
||||||
assert_row_indenting(lines, row)
|
assert_row_indenting(lines, row)
|
||||||
assert_nesting_level(lines, row.nesting_level)
|
assert_indent_level(lines, row.indent_level)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -626,14 +626,14 @@ module TestIRB
|
|||||||
pend 'This test needs Ripper::Lexer#scan to take broken tokens'
|
pend 'This test needs Ripper::Lexer#scan to take broken tokens'
|
||||||
end
|
end
|
||||||
input_with_correct_indents = [
|
input_with_correct_indents = [
|
||||||
Row.new(%q(<<~Q+<<~R), 0, 2, 0),
|
Row.new(%q(<<~Q+<<~R), 0, 2, 1),
|
||||||
Row.new(%q(a), 2, 2, 0),
|
Row.new(%q(a), 2, 2, 1),
|
||||||
Row.new(%q(a), 2, 2, 0),
|
Row.new(%q(a), 2, 2, 1),
|
||||||
Row.new(%q( b), 2, 2, 0),
|
Row.new(%q( b), 2, 2, 1),
|
||||||
Row.new(%q( b), 2, 2, 0),
|
Row.new(%q( b), 2, 2, 1),
|
||||||
Row.new(%q( Q), 0, 2, 0),
|
Row.new(%q( Q), 0, 2, 1),
|
||||||
Row.new(%q( c), 4, 4, 0),
|
Row.new(%q( c), 4, 4, 1),
|
||||||
Row.new(%q( c), 4, 4, 0),
|
Row.new(%q( c), 4, 4, 1),
|
||||||
Row.new(%q( R), 0, 0, 0),
|
Row.new(%q( R), 0, 0, 0),
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -641,7 +641,7 @@ module TestIRB
|
|||||||
input_with_correct_indents.each do |row|
|
input_with_correct_indents.each do |row|
|
||||||
lines << row.content
|
lines << row.content
|
||||||
assert_row_indenting(lines, row)
|
assert_row_indenting(lines, row)
|
||||||
assert_nesting_level(lines, row.nesting_level)
|
assert_indent_level(lines, row.indent_level)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -657,30 +657,30 @@ module TestIRB
|
|||||||
input_with_correct_indents.each do |row|
|
input_with_correct_indents.each do |row|
|
||||||
lines << row.content
|
lines << row.content
|
||||||
assert_row_indenting(lines, row)
|
assert_row_indenting(lines, row)
|
||||||
assert_nesting_level(lines, row.nesting_level)
|
assert_indent_level(lines, row.indent_level)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_broken_heredoc
|
def test_broken_heredoc
|
||||||
input_with_correct_indents = [
|
input_with_correct_indents = [
|
||||||
Row.new(%q(def foo), 0, 2, 1),
|
Row.new(%q(def foo), 0, 2, 1),
|
||||||
Row.new(%q( <<~Q), 2, 4, 1),
|
Row.new(%q( <<~Q), 2, 4, 2),
|
||||||
Row.new(%q( Qend), 4, 4, 1),
|
Row.new(%q( Qend), 4, 4, 2),
|
||||||
]
|
]
|
||||||
lines = []
|
lines = []
|
||||||
input_with_correct_indents.each do |row|
|
input_with_correct_indents.each do |row|
|
||||||
lines << row.content
|
lines << row.content
|
||||||
assert_row_indenting(lines, row)
|
assert_row_indenting(lines, row)
|
||||||
assert_nesting_level(lines, row.nesting_level)
|
assert_indent_level(lines, row.indent_level)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_heredoc_keep_indent_spaces
|
def test_heredoc_keep_indent_spaces
|
||||||
(1..4).each do |indent|
|
(1..4).each do |indent|
|
||||||
row = Row.new(' ' * indent, nil, [4, indent].max, 1)
|
row = Row.new(' ' * indent, nil, [4, indent].max, 2)
|
||||||
lines = ['def foo', ' <<~Q', row.content]
|
lines = ['def foo', ' <<~Q', row.content]
|
||||||
assert_row_indenting(lines, row)
|
assert_row_indenting(lines, row)
|
||||||
assert_nesting_level(lines, row.nesting_level)
|
assert_indent_level(lines, row.indent_level)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -816,7 +816,7 @@ module TestIRB
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_nesting_level_with_heredoc_and_embdoc
|
def test_indent_level_with_heredoc_and_embdoc
|
||||||
reference_code = <<~EOC.chomp
|
reference_code = <<~EOC.chomp
|
||||||
if true
|
if true
|
||||||
hello
|
hello
|
||||||
@ -838,9 +838,9 @@ module TestIRB
|
|||||||
)
|
)
|
||||||
EOC
|
EOC
|
||||||
expected = 1
|
expected = 1
|
||||||
assert_nesting_level(reference_code.lines, expected)
|
assert_indent_level(reference_code.lines, expected)
|
||||||
assert_nesting_level(code_with_heredoc.lines, expected)
|
assert_indent_level(code_with_heredoc.lines, expected)
|
||||||
assert_nesting_level(code_with_embdoc.lines, expected)
|
assert_indent_level(code_with_embdoc.lines, expected)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
Loading…
x
Reference in New Issue
Block a user