Skip to content

Commit 1d0d30f

Browse files
committed
Remove instance variable prompt and line_no from RubyLex
1 parent a061744 commit 1d0d30f

File tree

2 files changed

+43
-62
lines changed

2 files changed

+43
-62
lines changed

lib/irb.rb

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ def initialize(workspace = nil, input_method = nil)
437437
@context.workspace.load_commands_to_main
438438
@signal_status = :IN_IRB
439439
@scanner = RubyLex.new(@context)
440+
@line_no = 1
440441
end
441442

442443
# A hook point for `debug` command's breakpoint after :IRB_EXIT as well as its clean-up
@@ -454,7 +455,7 @@ def debug_readline(binding)
454455
workspace = IRB::WorkSpace.new(binding)
455456
context.workspace = workspace
456457
context.workspace.load_commands_to_main
457-
scanner.increase_line_no(1)
458+
@line_no += 1
458459

459460
# When users run:
460461
# 1. Debugging commands, like `step 2`
@@ -476,7 +477,7 @@ def debug_readline(binding)
476477
end
477478

478479
if input&.include?("\n")
479-
scanner.increase_line_no(input.count("\n") - 1)
480+
@line_no += input.count("\n") - 1
480481
end
481482

482483
input
@@ -513,34 +514,38 @@ def run(conf = IRB.conf)
513514
# The lexer used by this irb session
514515
attr_accessor :scanner
515516

516-
# Evaluates input for this session.
517-
def eval_input
518-
@scanner.set_prompt do
519-
|ltype, indent, continue, line_no|
520-
if ltype
521-
f = @context.prompt_s
522-
elsif continue
523-
f = @context.prompt_c
524-
else
525-
f = @context.prompt_i
526-
end
527-
f = "" unless f
528-
if @context.prompting?
529-
@context.io.prompt = p = prompt(f, ltype, indent, line_no)
530-
else
531-
@context.io.prompt = p = ""
532-
end
533-
if @context.auto_indent_mode and !@context.io.respond_to?(:auto_indent)
534-
unless ltype
535-
prompt_i = @context.prompt_i.nil? ? "" : @context.prompt_i
536-
ind = prompt(prompt_i, ltype, indent, line_no)[/.*\z/].size +
537-
indent * 2 - p.size
538-
@context.io.prompt = p + " " * ind if ind > 0
539-
end
517+
def prompt(opens, continue, line_offset)
518+
ltype = @scanner.ltype_from_open_tokens(opens)
519+
indent = @scanner.calc_indent_level(opens)
520+
continue = opens.any? || continue
521+
line_no = @line_no + line_offset
522+
523+
if ltype
524+
f = @context.prompt_s
525+
elsif continue
526+
f = @context.prompt_c
527+
else
528+
f = @context.prompt_i
529+
end
530+
f = "" unless f
531+
if @context.prompting?
532+
p = format_prompt(f, ltype, indent, line_no)
533+
else
534+
p = ""
535+
end
536+
if @context.auto_indent_mode and !@context.io.respond_to?(:auto_indent)
537+
unless ltype
538+
prompt_i = @context.prompt_i.nil? ? "" : @context.prompt_i
539+
ind = format_prompt(prompt_i, ltype, indent, line_no)[/.*\z/].size +
540+
indent * 2 - p.size
541+
p += " " * ind if ind > 0
540542
end
541-
@context.io.prompt
542543
end
544+
p
545+
end
543546

547+
# Evaluates input for this session.
548+
def eval_input
544549
configure_io
545550

546551
each_top_level_statement do |statement, line_no|
@@ -572,8 +577,9 @@ def eval_input
572577
end
573578
end
574579

575-
def read_input
580+
def read_input(initial_prompt)
576581
signal_status(:IN_INPUT) do
582+
@context.io.prompt = initial_prompt
577583
if l = @context.io.gets
578584
print l if @context.verbose?
579585
else
@@ -591,16 +597,16 @@ def read_input
591597
end
592598

593599
def readmultiline
594-
@scanner.save_prompt_to_context_io([], false, 0)
600+
prompt_string = prompt([], false, 0)
595601

596602
# multiline
597-
return read_input if @context.io.respond_to?(:check_termination)
603+
return read_input(prompt_string) if @context.io.respond_to?(:check_termination)
598604

599605
# nomultiline
600606
code = ''
601607
line_offset = 0
602608
loop do
603-
line = read_input
609+
line = read_input(prompt_string)
604610
unless line
605611
return code.empty? ? nil : code
606612
end
@@ -615,7 +621,7 @@ def readmultiline
615621

616622
line_offset += 1
617623
continue = @scanner.should_continue?(tokens)
618-
@scanner.save_prompt_to_context_io(opens, continue, line_offset)
624+
prompt_string = prompt(opens, continue, line_offset)
619625
end
620626
end
621627

@@ -625,9 +631,9 @@ def each_top_level_statement
625631
break unless code
626632

627633
if code != "\n"
628-
yield build_statement(code), @scanner.line_no
634+
yield build_statement(code), @line_no
629635
end
630-
@scanner.increase_line_no(code.count("\n"))
636+
@line_no += code.count("\n")
631637
rescue RubyLex::TerminateLineInput
632638
end
633639
end
@@ -687,7 +693,7 @@ def configure_io
687693
tokens_until_line << token if token != tokens_until_line.last
688694
end
689695
continue = @scanner.should_continue?(tokens_until_line)
690-
@scanner.prompt(next_opens, continue, line_num_offset)
696+
prompt(next_opens, continue, line_num_offset)
691697
end
692698
end
693699
end
@@ -882,9 +888,8 @@ def truncate_prompt_main(str) # :nodoc:
882888
end
883889
end
884890

885-
def prompt(prompt, ltype, indent, line_no) # :nodoc:
886-
p = prompt.dup
887-
p.gsub!(/%([0-9]+)?([a-zA-Z])/) do
891+
def format_prompt(format, ltype, indent, line_no) # :nodoc:
892+
format.gsub(/%([0-9]+)?([a-zA-Z])/) do
888893
case $2
889894
when "N"
890895
@context.irb_name
@@ -918,7 +923,6 @@ def prompt(prompt, ltype, indent, line_no) # :nodoc:
918923
"%"
919924
end
920925
end
921-
p
922926
end
923927

924928
def output_value(omit = false) # :nodoc:

lib/irb/ruby-lex.rb

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,8 @@ def initialize
4242
end
4343
end
4444

45-
attr_reader :line_no
46-
4745
def initialize(context)
4846
@context = context
49-
@line_no = 1
50-
@prompt = nil
5147
end
5248

5349
def self.compile_with_errors_suppressed(code, line_no: 1)
@@ -67,10 +63,6 @@ def self.compile_with_errors_suppressed(code, line_no: 1)
6763
result
6864
end
6965

70-
def set_prompt(&block)
71-
@prompt = block
72-
end
73-
7466
ERROR_TOKENS = [
7567
:on_parse_error,
7668
:compile_error,
@@ -146,12 +138,6 @@ def self.ripper_lex_without_warning(code, context: nil)
146138
$VERBOSE = verbose
147139
end
148140

149-
def prompt(opens, continue, line_num_offset)
150-
ltype = ltype_from_open_tokens(opens)
151-
indent_level = calc_indent_level(opens)
152-
@prompt&.call(ltype, indent_level, opens.any? || continue, @line_no + line_num_offset)
153-
end
154-
155141
def check_code_state(code)
156142
tokens = self.class.ripper_lex_without_warning(code, context: @context)
157143
opens = NestingParser.open_tokens(tokens)
@@ -171,15 +157,6 @@ def code_terminated?(code, tokens, opens)
171157
end
172158
end
173159

174-
def save_prompt_to_context_io(opens, continue, line_num_offset)
175-
# Implicitly saves prompt string to `@context.io.prompt`. This will be used in the next `@input.call`.
176-
prompt(opens, continue, line_num_offset)
177-
end
178-
179-
def increase_line_no(addition)
180-
@line_no += addition
181-
end
182-
183160
def assignment_expression?(code)
184161
# Try to parse the code and check if the last of possibly multiple
185162
# expressions is an assignment type.

0 commit comments

Comments
 (0)