Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial support for Reline on Ruby 3.3 #2298

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/pry/indent.rb
Expand Up @@ -17,6 +17,9 @@ class UnparseableNestingError < StandardError; end
# @return [String] String containing the spaces to be inserted before the next line.
attr_reader :indent_level

# @return [String] String containing the spaces for the current line.
attr_reader :last_indent_level

# @return [Array<String>] The stack of open tokens.
attr_reader :stack

Expand Down Expand Up @@ -110,6 +113,7 @@ def initialize(pry_instance = Pry.new)
def reset
@stack = []
@indent_level = String.new # rubocop:disable Style/EmptyLiteral
@last_indent_level = @indent_level
@heredoc_queue = []
@close_heredocs = {}
@string_start = nil
Expand Down Expand Up @@ -164,11 +168,11 @@ def indent(input)

output += line

@last_indent_level = prefix
prefix = new_prefix
end

@indent_level = prefix

output
end

Expand Down
37 changes: 35 additions & 2 deletions lib/pry/repl.rb
Expand Up @@ -100,7 +100,7 @@ def read
# Return nil for EOF, :no_more_input for error, or :control_c for <Ctrl-C>
return val unless val.is_a?(String)

if pry.config.auto_indent
if pry.config.auto_indent && !reline_available?
original_val = "#{indentation}#{val}"
indented_val = @indent.indent(val)

Expand Down Expand Up @@ -179,7 +179,9 @@ def read_line(current_prompt)
end
end

if readline_available?
if reline_available?
input_reline(current_prompt)
elsif readline_available?
set_readline_output
input_readline(current_prompt, false) # false since we'll add it manually
elsif coolline_available?
Expand All @@ -192,12 +194,43 @@ def read_line(current_prompt)
end
end

def input_reline(*args)
require 'prism'

Reline.output_modifier_proc = lambda do |text, _|
if pry.color
SyntaxHighlighter.highlight(text)
else
text
end
end

if pry.config.auto_indent
Reline.auto_indent_proc = lambda do |lines, _line_index, _byte_pointer, _newline|
pry_indentation = Pry::Indent.new
pry_indentation.indent(lines.join("\n"))
pry_indentation.last_indent_level.length
end
end

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?)
end
end
end

def input_readline(*args)
Pry::InputLock.for(:all).interruptible_region do
input.readline(*args)
end
end

def reline_available?
defined?(Reline) && input == Reline
end

def readline_available?
defined?(Readline) && input == Readline
end
Expand Down