Display a warning when command name collides with a local/method.

Warnings can be turned on/off by setting Pry.config.collision_warning = true/false. Defaults to true
This commit is contained in:
John Mair 2011-11-17 16:00:46 +13:00
parent ef49844825
commit 6e92cff1f7
3 changed files with 18 additions and 0 deletions

View File

@ -101,6 +101,17 @@ class Pry
[cmd_data, (Regexp.last_match ? Regexp.last_match.captures : nil), (Regexp.last_match ? Regexp.last_match.end(0) : nil)]
end
# Display a warning if a command collides with a local/method in
# the current scope.
# @param [String] command_name_match The name of the colliding command.
# @param [Binding] target The current binding context.
def check_for_command_name_collision(command_name_match, target)
if collision_type = target.eval("defined?(#{command_name_match})")
pry_instance.output.puts "#{Pry::Helpers::Text.bold('WARNING:')} Command name collision with a #{collision_type}: '#{command_name_match}'\n\n"
end
rescue Pry::RescuableException
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`
@ -123,6 +134,8 @@ class Pry
arg_string = val[pos..-1]
check_for_command_name_collision(val[0..pos].rstrip, target) if Pry.config.collision_warning
# remove the one leading space if it exists
arg_string.slice!(0) if arg_string.start_with?(" ")

View File

@ -138,6 +138,10 @@ class Pry
# @return [Boolean] Whether or not indentation should be corrected
# after hitting enter. This feature is not supported by all terminals.
attr_accessor :correct_indent
# @return [Boolean] Whether or not a warning will be displayed when
# a command name collides with a method/local in the current context.
attr_accessor :collision_warning
end
end

View File

@ -206,6 +206,7 @@ class Pry
config.command_prefix = ""
config.auto_indent = true
config.correct_indent = true
config.collision_warning = true
config.plugins ||= OpenStruct.new
config.plugins.enabled = true