Skip to content

Commit 849087b

Browse files
authored
Refactor RubyLex's input/io methods (#583)
1. Make `RubyLex#set_input` simply assign the input block. This matches the behavior of `RubyLex#set_prompt`. 2. Merge `RubyLex#set_input`'s IO configuration logic with `#set_auto_indent` into `#configure_io`.
1 parent 88ccad0 commit 849087b

3 files changed

Lines changed: 27 additions & 31 deletions

File tree

lib/irb.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ def eval_input
537537
@context.io.prompt
538538
end
539539

540-
@scanner.set_input(@context.io) do
540+
@scanner.set_input do
541541
signal_status(:IN_INPUT) do
542542
if l = @context.io.gets
543543
print l if @context.verbose?
@@ -555,7 +555,7 @@ def eval_input
555555
end
556556
end
557557

558-
@scanner.set_auto_indent
558+
@scanner.configure_io(@context.io)
559559

560560
@scanner.each_top_level_statement do |line, line_no|
561561
signal_status(:IN_EVAL) do

lib/irb/ruby-lex.rb

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ def self.compile_with_errors_suppressed(code, line_no: 1)
4343
end
4444

4545
# io functions
46-
def set_input(io, &block)
46+
def set_input(&block)
47+
@input = block
48+
end
49+
50+
def configure_io(io)
4751
@io = io
4852
if @io.respond_to?(:check_termination)
4953
@io.check_termination do |code|
@@ -112,10 +116,22 @@ def set_input(io, &block)
112116
end
113117
end
114118

115-
if block_given?
116-
@input = block
117-
else
118-
@input = Proc.new{@io.gets}
119+
if @io.respond_to?(:auto_indent) and @context.auto_indent_mode
120+
@io.auto_indent do |lines, line_index, byte_pointer, is_newline|
121+
if is_newline
122+
@tokens = self.class.ripper_lex_without_warning(lines[0..line_index].join("\n"), context: @context)
123+
prev_spaces = find_prev_spaces(line_index)
124+
depth_difference = check_newline_depth_difference
125+
depth_difference = 0 if depth_difference < 0
126+
prev_spaces + depth_difference * 2
127+
else
128+
code = line_index.zero? ? '' : lines[0..(line_index - 1)].map{ |l| l + "\n" }.join
129+
last_line = lines[line_index]&.byteslice(0, byte_pointer)
130+
code += last_line if last_line
131+
@tokens = self.class.ripper_lex_without_warning(code, context: @context)
132+
check_corresponding_token_depth(lines, line_index)
133+
end
134+
end
119135
end
120136
end
121137

@@ -184,26 +200,6 @@ def find_prev_spaces(line_index)
184200
prev_spaces
185201
end
186202

187-
def set_auto_indent
188-
if @io.respond_to?(:auto_indent) and @context.auto_indent_mode
189-
@io.auto_indent do |lines, line_index, byte_pointer, is_newline|
190-
if is_newline
191-
@tokens = self.class.ripper_lex_without_warning(lines[0..line_index].join("\n"), context: @context)
192-
prev_spaces = find_prev_spaces(line_index)
193-
depth_difference = check_newline_depth_difference
194-
depth_difference = 0 if depth_difference < 0
195-
prev_spaces + depth_difference * 2
196-
else
197-
code = line_index.zero? ? '' : lines[0..(line_index - 1)].map{ |l| l + "\n" }.join
198-
last_line = lines[line_index]&.byteslice(0, byte_pointer)
199-
code += last_line if last_line
200-
@tokens = self.class.ripper_lex_without_warning(code, context: @context)
201-
check_corresponding_token_depth(lines, line_index)
202-
end
203-
end
204-
end
205-
end
206-
207203
def check_state(code, tokens)
208204
ltype = process_literal_type(tokens)
209205
indent = process_nesting_level(tokens)

test/irb/test_ruby_lex.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def calculate_indenting(lines, add_new_line)
4141
ruby_lex = RubyLex.new(context)
4242
mock_io = MockIO_AutoIndent.new(lines, last_line_index, byte_pointer, add_new_line)
4343

44-
ruby_lex.set_input(mock_io)
45-
ruby_lex.set_auto_indent
44+
ruby_lex.set_input { @io.gets }
45+
ruby_lex.configure_io(mock_io)
4646
mock_io.calculated_indent
4747
end
4848

@@ -99,7 +99,7 @@ def ruby_lex_for_lines(lines, local_variables: [])
9999
ruby_lex = RubyLex.new(context)
100100

101101
io = proc{ lines.join("\n") }
102-
ruby_lex.set_input(io) do
102+
ruby_lex.set_input do
103103
lines.join("\n")
104104
end
105105
ruby_lex.lex
@@ -658,7 +658,7 @@ def assert_dynamic_prompt(lines, expected_prompt_list)
658658
ruby_lex.set_prompt do |ltype, indent, continue, line_no|
659659
'%03d:%01d:%1s:%s ' % [line_no, indent, ltype, continue ? '*' : '>']
660660
end
661-
ruby_lex.set_input(io)
661+
ruby_lex.set_input { @io.gets }
662662
end
663663

664664
def test_dyanmic_prompt

0 commit comments

Comments
 (0)