From efcc3f1030010e72d0eed42a959ca4c66eaa80a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Luis=20Leal=20Cardoso=20Junior?= Date: Fri, 29 Mar 2024 10:55:45 -0300 Subject: [PATCH] Stop evaling command when it detects that the code will never be valid --- lib/pry/repl.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/pry/repl.rb b/lib/pry/repl.rb index 3e5129be6..0bbf0ef44 100644 --- a/lib/pry/repl.rb +++ b/lib/pry/repl.rb @@ -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 @@ -264,6 +264,17 @@ def set_readline_output @readline_output = (Readline.output = Pry.config.output) if piping? end + UNEXPECTED_TOKENS = %i[unexpected_token_ignore].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)". #