Skip to content

Commit

Permalink
Stop evaling command when it detects that the code will never be valid
Browse files Browse the repository at this point in the history
  • Loading branch information
andrehjr committed Mar 29, 2024
1 parent 08753a9 commit 48789fb
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/pry/repl.rb
Expand Up @@ -220,7 +220,7 @@ def input_reline(*args)
Pry::InputLock.for(:all).interruptible_region do
input.readmultiline(*args) do |multiline_input|
Pry.commands.find_command(multiline_input) ||
(Prism.parse_success?(multiline_input) && !Reline::IOGate.in_pasting?)
(complete_expression?(multiline_input) && !Reline::IOGate.in_pasting?)
end
end
end
Expand Down Expand Up @@ -264,6 +264,17 @@ def set_readline_output
@readline_output = (Readline.output = Pry.config.output) if piping?
end

UNEXPECTED_TOKENS = %i[unexpected_token_ignore unexpected_token_close_context].freeze

def complete_expression?(multiline_input)
lex = Prism.lex(multiline_input)

errors = lex.errors
return true if errors.empty?

errors.any? { |error| UNEXPECTED_TOKENS.include?(error.type) }
end

# Calculates correct overhang for current line. Supports vi Readline
# mode and its indicators such as "(ins)" or "(cmd)".
#
Expand Down

0 comments on commit 48789fb

Please sign in to comment.