Skip to content

Commit

Permalink
Remove instance variable prompt and line_no from RubyLex
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Aug 30, 2023
1 parent a061744 commit 1d0d30f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 62 deletions.
82 changes: 43 additions & 39 deletions lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ def initialize(workspace = nil, input_method = nil)
@context.workspace.load_commands_to_main
@signal_status = :IN_IRB
@scanner = RubyLex.new(@context)
@line_no = 1
end

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

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

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

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

# Evaluates input for this session.
def eval_input
@scanner.set_prompt do
|ltype, indent, continue, line_no|
if ltype
f = @context.prompt_s
elsif continue
f = @context.prompt_c
else
f = @context.prompt_i
end
f = "" unless f
if @context.prompting?
@context.io.prompt = p = prompt(f, ltype, indent, line_no)
else
@context.io.prompt = p = ""
end
if @context.auto_indent_mode and !@context.io.respond_to?(:auto_indent)
unless ltype
prompt_i = @context.prompt_i.nil? ? "" : @context.prompt_i
ind = prompt(prompt_i, ltype, indent, line_no)[/.*\z/].size +
indent * 2 - p.size
@context.io.prompt = p + " " * ind if ind > 0
end
def prompt(opens, continue, line_offset)
ltype = @scanner.ltype_from_open_tokens(opens)
indent = @scanner.calc_indent_level(opens)
continue = opens.any? || continue
line_no = @line_no + line_offset

if ltype
f = @context.prompt_s
elsif continue
f = @context.prompt_c
else
f = @context.prompt_i
end
f = "" unless f
if @context.prompting?
p = format_prompt(f, ltype, indent, line_no)
else
p = ""
end
if @context.auto_indent_mode and !@context.io.respond_to?(:auto_indent)
unless ltype
prompt_i = @context.prompt_i.nil? ? "" : @context.prompt_i
ind = format_prompt(prompt_i, ltype, indent, line_no)[/.*\z/].size +
indent * 2 - p.size
p += " " * ind if ind > 0
end
@context.io.prompt
end
p
end

# Evaluates input for this session.
def eval_input
configure_io

each_top_level_statement do |statement, line_no|
Expand Down Expand Up @@ -572,8 +577,9 @@ def eval_input
end
end

def read_input
def read_input(initial_prompt)
signal_status(:IN_INPUT) do
@context.io.prompt = initial_prompt
if l = @context.io.gets
print l if @context.verbose?
else
Expand All @@ -591,16 +597,16 @@ def read_input
end

def readmultiline
@scanner.save_prompt_to_context_io([], false, 0)
prompt_string = prompt([], false, 0)

# multiline
return read_input if @context.io.respond_to?(:check_termination)
return read_input(prompt_string) if @context.io.respond_to?(:check_termination)

# nomultiline
code = ''
line_offset = 0
loop do
line = read_input
line = read_input(prompt_string)
unless line
return code.empty? ? nil : code
end
Expand All @@ -615,7 +621,7 @@ def readmultiline

line_offset += 1
continue = @scanner.should_continue?(tokens)
@scanner.save_prompt_to_context_io(opens, continue, line_offset)
prompt_string = prompt(opens, continue, line_offset)
end
end

Expand All @@ -625,9 +631,9 @@ def each_top_level_statement
break unless code

if code != "\n"
yield build_statement(code), @scanner.line_no
yield build_statement(code), @line_no
end
@scanner.increase_line_no(code.count("\n"))
@line_no += code.count("\n")
rescue RubyLex::TerminateLineInput
end
end
Expand Down Expand Up @@ -687,7 +693,7 @@ def configure_io
tokens_until_line << token if token != tokens_until_line.last
end
continue = @scanner.should_continue?(tokens_until_line)
@scanner.prompt(next_opens, continue, line_num_offset)
prompt(next_opens, continue, line_num_offset)
end
end
end
Expand Down Expand Up @@ -882,9 +888,8 @@ def truncate_prompt_main(str) # :nodoc:
end
end

def prompt(prompt, ltype, indent, line_no) # :nodoc:
p = prompt.dup
p.gsub!(/%([0-9]+)?([a-zA-Z])/) do
def format_prompt(format, ltype, indent, line_no) # :nodoc:
format.gsub(/%([0-9]+)?([a-zA-Z])/) do
case $2
when "N"
@context.irb_name
Expand Down Expand Up @@ -918,7 +923,6 @@ def prompt(prompt, ltype, indent, line_no) # :nodoc:
"%"
end
end
p
end

def output_value(omit = false) # :nodoc:
Expand Down
23 changes: 0 additions & 23 deletions lib/irb/ruby-lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,8 @@ def initialize
end
end

attr_reader :line_no

def initialize(context)
@context = context
@line_no = 1
@prompt = nil
end

def self.compile_with_errors_suppressed(code, line_no: 1)
Expand All @@ -67,10 +63,6 @@ def self.compile_with_errors_suppressed(code, line_no: 1)
result
end

def set_prompt(&block)
@prompt = block
end

ERROR_TOKENS = [
:on_parse_error,
:compile_error,
Expand Down Expand Up @@ -146,12 +138,6 @@ def self.ripper_lex_without_warning(code, context: nil)
$VERBOSE = verbose
end

def prompt(opens, continue, line_num_offset)
ltype = ltype_from_open_tokens(opens)
indent_level = calc_indent_level(opens)
@prompt&.call(ltype, indent_level, opens.any? || continue, @line_no + line_num_offset)
end

def check_code_state(code)
tokens = self.class.ripper_lex_without_warning(code, context: @context)
opens = NestingParser.open_tokens(tokens)
Expand All @@ -171,15 +157,6 @@ def code_terminated?(code, tokens, opens)
end
end

def save_prompt_to_context_io(opens, continue, line_num_offset)
# Implicitly saves prompt string to `@context.io.prompt`. This will be used in the next `@input.call`.
prompt(opens, continue, line_num_offset)
end

def increase_line_no(addition)
@line_no += addition
end

def assignment_expression?(code)
# Try to parse the code and check if the last of possibly multiple
# expressions is an assignment type.
Expand Down

0 comments on commit 1d0d30f

Please sign in to comment.