|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RubyJard |
| 4 | + # Proxy for Pry::REPL. Implement support for ::Readline only |
| 5 | + class PryReplProxy < Pry::REPL |
| 6 | + def read_line(current_prompt) |
| 7 | + handle_read_errors do |
| 8 | + Pry::InputLock.for(:all).interruptible_region do |
| 9 | + input.readline(current_prompt, false) |
| 10 | + end |
| 11 | + end |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + # Proxy for Pry instance. Safely overidding some attributes |
| 16 | + class PryProxy < ::Pry |
| 17 | + # Some commands overlaps with Jard, Ruby, and even cause confusion for |
| 18 | + # users. It's better ignore or re-implement those commands. |
| 19 | + PRY_EXCLUDED_COMMANDS = [ |
| 20 | + 'pry-backtrace', # Redundant method for normal user |
| 21 | + 'watch', # Conflict with byebug and jard watch |
| 22 | + 'edit', # Sorry, but a file should not be editted while debugging, as it made breakpoints shifted |
| 23 | + 'play', # What if the played files or methods include jard again? |
| 24 | + 'stat', # Included in jard UI |
| 25 | + 'backtrace', # Re-implemented later |
| 26 | + 'break', # Re-implemented later |
| 27 | + 'exit-all', # Conflicted with continue |
| 28 | + 'exit-program', # We already have `exit` native command |
| 29 | + '!pry', # No need to complicate things |
| 30 | + 'jump-to', # No need to complicate things |
| 31 | + 'nesting', # No need to complicate things |
| 32 | + 'switch-to', # No need to complicate things |
| 33 | + 'disable-pry' # No need to complicate things |
| 34 | + ].freeze |
| 35 | + |
| 36 | + Commands = Pry::CommandSet.new |
| 37 | + |
| 38 | + attr_reader :console |
| 39 | + |
| 40 | + def initialize(options = {}) |
| 41 | + @redirected_input = options[:redirected_input] |
| 42 | + @redirected_output = options[:redirected_output] |
| 43 | + @original_input = options[:original_input] |
| 44 | + @original_output = options[:original_output] |
| 45 | + @state_hooks = options[:state_hooks] || {} |
| 46 | + options = options.merge( |
| 47 | + input: ::Readline, |
| 48 | + output: @redirected_output, |
| 49 | + prompt: pry_jard_prompt, |
| 50 | + commands: pry_command_set, |
| 51 | + quiet: true, |
| 52 | + hooks: pry_hooks |
| 53 | + ) |
| 54 | + super(options) |
| 55 | + end |
| 56 | + |
| 57 | + def handle_line(line, *args) |
| 58 | + command = RubyJard::ReplSequence.detect(line) |
| 59 | + if command.nil? |
| 60 | + super(line, *args) |
| 61 | + else |
| 62 | + super(command, *args) |
| 63 | + end |
| 64 | + ensure |
| 65 | + exec_hook :after_handle_line, *args, self |
| 66 | + end |
| 67 | + |
| 68 | + def start(target = nil) |
| 69 | + ::Readline.input = @redirected_input |
| 70 | + ::Readline.output = @redirected_output |
| 71 | + PryReplProxy.new(self, target: target).start |
| 72 | + end |
| 73 | + |
| 74 | + def stop |
| 75 | + ::Readline.input = @original_input |
| 76 | + ::Readline.output = @original_output |
| 77 | + end |
| 78 | + |
| 79 | + def pager |
| 80 | + RubyJard::Pager.new(self) |
| 81 | + end |
| 82 | + |
| 83 | + def line_buffer |
| 84 | + Readline.line_buffer |
| 85 | + end |
| 86 | + |
| 87 | + private |
| 88 | + |
| 89 | + def pry_jard_prompt |
| 90 | + Pry::Prompt.new( |
| 91 | + :jard, |
| 92 | + 'Custom pry promt for Jard', [ |
| 93 | + proc do |_context, _nesting, _pry_instance| |
| 94 | + 'jard >> ' |
| 95 | + end, |
| 96 | + proc do |_context, _nesting, _pry_instance| |
| 97 | + 'jard *> ' |
| 98 | + end |
| 99 | + ] |
| 100 | + ) |
| 101 | + end |
| 102 | + |
| 103 | + def pry_command_set |
| 104 | + set = Pry::CommandSet.new |
| 105 | + set.import_from( |
| 106 | + Pry::Commands, |
| 107 | + *(Pry::Commands.list_commands - PRY_EXCLUDED_COMMANDS) |
| 108 | + ) |
| 109 | + set.import_from( |
| 110 | + PryProxy::Commands, |
| 111 | + *PryProxy::Commands.list_commands |
| 112 | + ) |
| 113 | + set |
| 114 | + end |
| 115 | + |
| 116 | + def pry_hooks |
| 117 | + hooks = Pry::Hooks.default |
| 118 | + hooks.add_hook(:after_read, :jard_proxy_acquire_lock) do |_read_string, _pry| |
| 119 | + @state_hooks[:after_read]&.call |
| 120 | + end |
| 121 | + hooks.add_hook(:after_handle_line, :jard_proxy_release_lock) do |
| 122 | + @state_hooks[:after_handle_line]&.call |
| 123 | + end |
| 124 | + hooks.add_hook(:before_pager, :jard_proxy_before_pager) do |
| 125 | + @state_hooks[:before_pager]&.call |
| 126 | + end |
| 127 | + hooks.add_hook(:after_pager, :jard_proxy_after_pager) do |
| 128 | + @state_hooks[:after_pager]&.call |
| 129 | + end |
| 130 | + end |
| 131 | + end |
| 132 | +end |
| 133 | + |
| 134 | +RubyJard::PryProxy.init |
| 135 | + |
| 136 | +require 'ruby_jard/commands/base_command' |
| 137 | +require 'ruby_jard/commands/validation_helpers' |
| 138 | +require 'ruby_jard/commands/color_helpers' |
| 139 | +require 'ruby_jard/commands/continue_command' |
| 140 | +require 'ruby_jard/commands/exit_command' |
| 141 | +require 'ruby_jard/commands/up_command' |
| 142 | +require 'ruby_jard/commands/down_command' |
| 143 | +require 'ruby_jard/commands/next_command' |
| 144 | +require 'ruby_jard/commands/step_command' |
| 145 | +require 'ruby_jard/commands/step_out_command' |
| 146 | +require 'ruby_jard/commands/frame_command' |
| 147 | +require 'ruby_jard/commands/list_command' |
| 148 | +require 'ruby_jard/commands/skip_command' |
| 149 | +require 'ruby_jard/commands/jard_command' |
| 150 | +require 'ruby_jard/commands/help_command' |
0 commit comments