2011-04-07 02:09:03 -04:00
|
|
|
require 'forwardable'
|
|
|
|
|
|
|
|
class Pry
|
|
|
|
class CommandProcessor
|
|
|
|
extend Forwardable
|
2011-04-18 17:31:39 -04:00
|
|
|
|
2011-04-08 00:09:47 -04:00
|
|
|
attr_accessor :pry_instance
|
2011-04-18 17:31:39 -04:00
|
|
|
|
2011-04-07 02:09:03 -04:00
|
|
|
def initialize(pry_instance)
|
|
|
|
@pry_instance = pry_instance
|
|
|
|
end
|
|
|
|
|
|
|
|
def_delegators :@pry_instance, :commands, :nesting, :output
|
2011-04-18 17:31:39 -04:00
|
|
|
|
2011-06-08 10:36:09 -04:00
|
|
|
# Is the string a valid command?
|
2011-04-13 05:16:38 -04:00
|
|
|
# @param [String] val The string passed in from the Pry prompt.
|
2011-06-08 22:41:14 -04:00
|
|
|
# @param [Binding] target The context where the string should be
|
|
|
|
# interpolated in.
|
2011-04-13 05:16:38 -04:00
|
|
|
# @return [Boolean] Whether the string is a valid command.
|
2011-06-08 22:41:14 -04:00
|
|
|
def valid_command?(val, target=binding)
|
|
|
|
!!(command_matched(val, target)[0])
|
2011-05-22 19:57:50 -04:00
|
|
|
end
|
|
|
|
|
2011-06-08 10:36:09 -04:00
|
|
|
# Convert the object to a form that can be interpolated into a
|
|
|
|
# Regexp cleanly.
|
|
|
|
# @return [String] The string to interpolate into a Regexp
|
2011-05-22 19:57:50 -04:00
|
|
|
def convert_to_regex(obj)
|
|
|
|
case obj
|
|
|
|
when String
|
|
|
|
Regexp.escape(obj)
|
|
|
|
else
|
|
|
|
obj
|
|
|
|
end
|
2011-04-07 10:13:16 -04:00
|
|
|
end
|
2011-04-08 05:10:49 -04:00
|
|
|
|
2011-04-13 05:16:38 -04:00
|
|
|
# Revaluate the string (str) and perform interpolation.
|
|
|
|
# @param [String] str The string to reevaluate with interpolation.
|
|
|
|
# @param [Binding] target The context where the string should be
|
2011-06-08 22:41:14 -04:00
|
|
|
# interpolated in.
|
2011-04-13 05:16:38 -04:00
|
|
|
# @return [String] The reevaluated string with interpolations
|
|
|
|
# applied (if any).
|
2011-04-08 05:10:49 -04:00
|
|
|
def interpolate_string(str, target)
|
|
|
|
dumped_str = str.dump
|
2011-04-08 07:00:38 -04:00
|
|
|
dumped_str.gsub!(/\\\#\{/, '#{')
|
2011-04-08 05:10:49 -04:00
|
|
|
target.eval(dumped_str)
|
|
|
|
end
|
2011-04-13 05:16:38 -04:00
|
|
|
|
2011-04-07 02:09:03 -04:00
|
|
|
# Determine whether a Pry command was matched and return command data
|
|
|
|
# and argument string.
|
|
|
|
# This method should not need to be invoked directly.
|
|
|
|
# @param [String] val The line of input.
|
2011-06-08 10:36:09 -04:00
|
|
|
# @param [Binding] target The binding to perform string
|
|
|
|
# interpolation against.
|
2011-04-07 02:09:03 -04:00
|
|
|
# @return [Array] The command data and arg string pair
|
2011-06-08 10:36:09 -04:00
|
|
|
def command_matched(val, target)
|
2011-06-05 18:48:15 -04:00
|
|
|
_, cmd_data = commands.commands.find do |name, data|
|
2011-06-08 10:36:09 -04:00
|
|
|
|
2011-07-26 03:34:54 -04:00
|
|
|
prefix = Regexp.escape(Pry.config.command_prefix)
|
2011-07-26 05:42:44 -04:00
|
|
|
prefix = "(?:#{prefix})?" unless data.options[:use_prefix]
|
2011-07-26 03:34:54 -04:00
|
|
|
|
|
|
|
command_regex = /^#{prefix}#{convert_to_regex(name)}(?!\S)/
|
2011-06-08 10:36:09 -04:00
|
|
|
|
2011-06-10 06:46:46 -04:00
|
|
|
if data.options[:interpolate]
|
|
|
|
# If interpolation fails then the command cannot be matched,
|
|
|
|
# so early exit.
|
|
|
|
begin
|
|
|
|
interp_val = interpolate_string(val, target)
|
2011-06-20 04:28:15 -04:00
|
|
|
rescue Exception
|
2011-06-10 10:12:24 -04:00
|
|
|
next
|
2011-06-10 06:46:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
val.replace interp_val if command_regex =~ interp_val
|
2011-06-08 10:36:09 -04:00
|
|
|
else
|
|
|
|
command_regex =~ val
|
|
|
|
end
|
2011-04-07 02:09:03 -04:00
|
|
|
end
|
|
|
|
|
2011-05-22 19:57:50 -04:00
|
|
|
[cmd_data, (Regexp.last_match ? Regexp.last_match.captures : nil), (Regexp.last_match ? Regexp.last_match.end(0) : nil)]
|
2011-04-07 02:09:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Process Pry commands. Pry commands are not Ruby methods and are evaluated
|
|
|
|
# prior to Ruby expressions.
|
|
|
|
# Commands can be modified/configured by the user: see `Pry::Commands`
|
|
|
|
# This method should not need to be invoked directly - it is called
|
|
|
|
# by `Pry#r`.
|
|
|
|
# @param [String] val The current line of input.
|
|
|
|
# @param [String] eval_string The cumulative lines of input for
|
|
|
|
# multi-line input.
|
|
|
|
# @param [Binding] target The receiver of the commands.
|
|
|
|
def process_commands(val, eval_string, target)
|
|
|
|
|
2011-04-08 05:10:49 -04:00
|
|
|
# no command was matched, so return to caller
|
2011-06-08 10:36:09 -04:00
|
|
|
command, captures, pos = command_matched(val, target)
|
|
|
|
return if !command
|
2011-06-16 09:44:26 -04:00
|
|
|
arg_string = val[pos..-1]
|
|
|
|
|
|
|
|
# remove the one leading space if it exists
|
|
|
|
arg_string.slice!(0) if arg_string.start_with?(" ")
|
2011-06-02 11:28:27 -04:00
|
|
|
|
2011-05-22 19:57:50 -04:00
|
|
|
args = arg_string ? Shellwords.shellwords(arg_string) : []
|
2011-04-18 17:31:39 -04:00
|
|
|
|
2011-04-07 02:09:03 -04:00
|
|
|
options = {
|
|
|
|
:val => val,
|
2011-05-22 19:57:50 -04:00
|
|
|
:arg_string => arg_string,
|
2011-04-07 02:09:03 -04:00
|
|
|
:eval_string => eval_string,
|
|
|
|
:nesting => nesting,
|
2011-05-22 19:57:50 -04:00
|
|
|
:commands => commands.commands,
|
|
|
|
:captures => captures
|
2011-04-07 02:09:03 -04:00
|
|
|
}
|
|
|
|
|
2011-05-27 12:22:07 -04:00
|
|
|
execute_command(target, command.name, options, *(captures + args))
|
2011-04-07 02:09:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Execute a Pry command.
|
|
|
|
# This method should not need to be invoked directly.
|
|
|
|
# @param [Binding] target The target of the Pry session.
|
2011-04-25 07:26:25 -04:00
|
|
|
# @param [String] command The name of the command to be run.
|
2011-04-07 02:09:03 -04:00
|
|
|
# @param [Hash] options The options to set on the Commands object.
|
|
|
|
# @param [Array] args The command arguments.
|
2011-04-25 07:26:25 -04:00
|
|
|
def execute_command(target, command, options, *args)
|
|
|
|
context = CommandContext.new
|
2011-04-07 02:09:03 -04:00
|
|
|
|
|
|
|
# set some useful methods to be used by the action blocks
|
2011-04-25 17:13:36 -04:00
|
|
|
context.opts = options
|
|
|
|
context.target = target
|
|
|
|
context.output = output
|
2011-05-27 12:17:25 -04:00
|
|
|
context.captures = options[:captures]
|
|
|
|
context.eval_string = options[:eval_string]
|
|
|
|
context.arg_string = options[:arg_string]
|
2011-04-25 17:13:36 -04:00
|
|
|
context.command_set = commands
|
2011-04-25 07:26:25 -04:00
|
|
|
|
2011-04-29 12:14:48 -04:00
|
|
|
context.command_processor = self
|
|
|
|
|
2011-04-25 07:26:25 -04:00
|
|
|
ret = commands.run_command(context, command, *args)
|
2011-04-07 02:09:03 -04:00
|
|
|
|
2011-04-13 07:49:45 -04:00
|
|
|
# Tick, tock, im getting rid of this shit soon.
|
2011-05-31 04:45:42 -04:00
|
|
|
options[:val].replace("")
|
2011-04-18 17:31:39 -04:00
|
|
|
|
2011-04-25 07:26:25 -04:00
|
|
|
ret
|
2011-04-07 02:09:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|