mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[ruby/irb] Use typed spaces when the line is inside the here documents
Use first method instead of square brackets to support 2.5 and 2.6 versions
Use tokens
Clear check_newline_depth_difference
6fec2a5d46
This commit is contained in:
parent
7c0230b05d
commit
782d1d876b
2 changed files with 22 additions and 10 deletions
|
@ -207,7 +207,7 @@ class RubyLex
|
||||||
last_line = lines[line_index]&.byteslice(0, byte_pointer)
|
last_line = lines[line_index]&.byteslice(0, byte_pointer)
|
||||||
code += last_line if last_line
|
code += last_line if last_line
|
||||||
@tokens = self.class.ripper_lex_without_warning(code, context: context)
|
@tokens = self.class.ripper_lex_without_warning(code, context: context)
|
||||||
corresponding_token_depth = check_corresponding_token_depth
|
corresponding_token_depth = check_corresponding_token_depth(lines, line_index)
|
||||||
if corresponding_token_depth
|
if corresponding_token_depth
|
||||||
corresponding_token_depth
|
corresponding_token_depth
|
||||||
else
|
else
|
||||||
|
@ -603,7 +603,7 @@ class RubyLex
|
||||||
depth_difference
|
depth_difference
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_corresponding_token_depth
|
def check_corresponding_token_depth(lines, line_index)
|
||||||
corresponding_token_depth = nil
|
corresponding_token_depth = nil
|
||||||
is_first_spaces_of_line = true
|
is_first_spaces_of_line = true
|
||||||
is_first_printable_of_line = true
|
is_first_printable_of_line = true
|
||||||
|
@ -611,6 +611,11 @@ class RubyLex
|
||||||
spaces_at_line_head = 0
|
spaces_at_line_head = 0
|
||||||
open_brace_on_line = 0
|
open_brace_on_line = 0
|
||||||
in_oneliner_def = nil
|
in_oneliner_def = nil
|
||||||
|
|
||||||
|
if heredoc_scope?
|
||||||
|
return lines[line_index][/^ */].length
|
||||||
|
end
|
||||||
|
|
||||||
@tokens.each_with_index do |t, index|
|
@tokens.each_with_index do |t, index|
|
||||||
# detecting one-liner method definition
|
# detecting one-liner method definition
|
||||||
if in_oneliner_def.nil?
|
if in_oneliner_def.nil?
|
||||||
|
@ -817,5 +822,12 @@ class RubyLex
|
||||||
end
|
end
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def heredoc_scope?
|
||||||
|
heredoc_tokens = @tokens.select { |t| [:on_heredoc_beg, :on_heredoc_end].include?(t.event) }
|
||||||
|
heredoc_tokens[-1]&.event == :on_heredoc_beg
|
||||||
|
end
|
||||||
end
|
end
|
||||||
# :startdoc:
|
# :startdoc:
|
||||||
|
|
|
@ -109,8 +109,8 @@ module TestIRB
|
||||||
Row.new(%q( ]), 4, 4),
|
Row.new(%q( ]), 4, 4),
|
||||||
Row.new(%q( ]), 2, 2),
|
Row.new(%q( ]), 2, 2),
|
||||||
Row.new(%q(]), 0, 0),
|
Row.new(%q(]), 0, 0),
|
||||||
Row.new(%q([<<FOO]), nil, 0),
|
Row.new(%q([<<FOO]), 0, 0),
|
||||||
Row.new(%q(hello), nil, 0),
|
Row.new(%q(hello), 0, 0),
|
||||||
Row.new(%q(FOO), nil, 0),
|
Row.new(%q(FOO), nil, 0),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -465,10 +465,10 @@ module TestIRB
|
||||||
|
|
||||||
def test_heredoc_with_indent
|
def test_heredoc_with_indent
|
||||||
input_with_correct_indents = [
|
input_with_correct_indents = [
|
||||||
Row.new(%q(<<~Q), nil, 0, 0),
|
Row.new(%q(<<~Q), 0, 0, 0),
|
||||||
Row.new(%q({), nil, 0, 0),
|
Row.new(%q({), 0, 0, 0),
|
||||||
Row.new(%q( #), nil, 0, 0),
|
Row.new(%q( #), 2, 0, 0),
|
||||||
Row.new(%q(}), nil, 0, 0),
|
Row.new(%q(}), 0, 0, 0)
|
||||||
]
|
]
|
||||||
|
|
||||||
lines = []
|
lines = []
|
||||||
|
@ -503,8 +503,8 @@ module TestIRB
|
||||||
end
|
end
|
||||||
input_with_correct_indents = [
|
input_with_correct_indents = [
|
||||||
Row.new(%q(def foo), nil, 2, 1),
|
Row.new(%q(def foo), nil, 2, 1),
|
||||||
Row.new(%q( <<~Q), nil, 2, 1),
|
Row.new(%q( <<~Q), 2, 2, 1),
|
||||||
Row.new(%q( Qend), nil, 2, 1),
|
Row.new(%q( Qend), 2, 2, 1),
|
||||||
]
|
]
|
||||||
|
|
||||||
lines = []
|
lines = []
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue