From 38361791817460a0d3853dfad884d5415ef411de Mon Sep 17 00:00:00 2001 From: Reginald Tan Date: Mon, 13 Aug 2012 23:54:07 -0400 Subject: [PATCH] Depracated show-command and moved its functionality to show-source Also, show-doc on commands now displays their banner --- lib/pry/command.rb | 13 +++++ lib/pry/commands/show_command.rb | 29 +--------- lib/pry/commands/show_doc.rb | 18 +++++++ lib/pry/commands/show_source.rb | 17 ++++++ .../helpers/module_introspection_helpers.rb | 14 ++++- test/test_command.rb | 6 ++- .../test_documentation.rb | 30 +++++++++++ .../test_introspection.rb | 54 ------------------- .../test_default_commands/test_show_source.rb | 45 ++++++++++++++++ 9 files changed, 141 insertions(+), 85 deletions(-) diff --git a/lib/pry/command.rb b/lib/pry/command.rb index 05e5159c..30890d00 100644 --- a/lib/pry/command.rb +++ b/lib/pry/command.rb @@ -1,9 +1,12 @@ +require 'pry/helpers/documentation_helpers' + class Pry # The super-class of all commands, new commands should be created by calling # {Pry::CommandSet#command} which creates a BlockCommand or {Pry::CommandSet#create_command} # which creates a ClassCommand. Please don't use this class directly. class Command + extend Helpers::DocumentationHelpers # represents a void return value for a command VOID_VALUE = Object.new @@ -49,6 +52,14 @@ class Pry def block @block || instance_method(:process) && instance_method(:process) end + + def source + strip_leading_whitespace(block.source) + end + + def source_location + block.source_location + end end # Make those properties accessible to instances @@ -58,6 +69,8 @@ class Pry def block; self.class.block; end def command_options; self.class.options; end def command_name; command_options[:listing]; end + def source; self.class.source; end + def source_location; self.class.source_location; end class << self def name diff --git a/lib/pry/commands/show_command.rb b/lib/pry/commands/show_command.rb index b65e371a..c701bc4a 100644 --- a/lib/pry/commands/show_command.rb +++ b/lib/pry/commands/show_command.rb @@ -8,40 +8,15 @@ class Pry opts = Slop.parse!(args) do |opt| opt.banner unindent <<-USAGE - Usage: show-command [OPTIONS] [CMD] - Show the source for command CMD. - e.g: show-command show-method + NOTE: show-command is DEPRACTED. Use show-source [command_name] instead. USAGE - opt.on :l, "line-numbers", "Show line numbers." - opt.on :f, :flood, "Do not use a pager to view text longer than one screen." opt.on :h, :help, "This message." do output.puts opt.help end end - return if opts.present?(:help) - - command_name = args.shift - if !command_name - raise CommandError, "You must provide a command name." - end - - if find_command(command_name) - block = Pry::Method.new(find_command(command_name).block) - - return unless block.source - set_file_and_dir_locals(block.source_file) - - output.puts make_header(block) - output.puts - - code = Pry::Code.from_method(block).with_line_numbers(opts.present?(:'line-numbers')).to_s - - render_output(code, opts) - else - raise CommandError, "No such command: #{command_name}." - end + render_output opts.banner end end end diff --git a/lib/pry/commands/show_doc.rb b/lib/pry/commands/show_doc.rb index 73b4c05d..730e91af 100644 --- a/lib/pry/commands/show_doc.rb +++ b/lib/pry/commands/show_doc.rb @@ -111,6 +111,24 @@ class Pry doc end + def process_command + name = args.join(" ").gsub(/\"/,"") + command = find_command(name) + + doc = command.new.help + doc = strip_leading_whitespace(doc) + + file_name, line = command.source_location + set_file_and_dir_locals(file_name) + + + result = "" + result << "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} @ line #{line}:\n" + result << "#{Pry::Helpers::Text.bold('Number of lines:')} #{doc.lines.count}\n\n" + result << doc + result << "\n" + end + def module_start_line(mod, candidate=0) if opts.present?(:'base-one') 1 diff --git a/lib/pry/commands/show_source.rb b/lib/pry/commands/show_source.rb index 11dd60b3..3e53ed64 100644 --- a/lib/pry/commands/show_source.rb +++ b/lib/pry/commands/show_source.rb @@ -111,6 +111,23 @@ class Pry result end + def process_command + name = args.join(" ").gsub(/\"/,"") + command = find_command(name) + + file_name, line = command.source_location + set_file_and_dir_locals(file_name) + + code = Pry::Code.new(command.source, line).with_line_numbers(use_line_numbers?).to_s + + result = "" + result << "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} @ line #{line}:\n" + result << "#{Pry::Helpers::Text.bold('Number of lines:')} #{code.lines.count}\n\n" + result << "\n" + + result << code + end + def use_line_numbers? opts.present?(:b) || opts.present?(:l) end diff --git a/lib/pry/helpers/module_introspection_helpers.rb b/lib/pry/helpers/module_introspection_helpers.rb index 5ae7d49b..96c374ba 100644 --- a/lib/pry/helpers/module_introspection_helpers.rb +++ b/lib/pry/helpers/module_introspection_helpers.rb @@ -33,13 +33,21 @@ class Pry :module elsif target.eval("defined? #{input} ") =~ /variable|constant/ :variable_or_constant + elsif find_command(input) + :command + else + :unknown + end + rescue SyntaxError + if find_command(input) + :command else :unknown end end def process(name) - input = args.join(" ") + input = args.join(" ").gsub(/\"/,"") type = input_type(input, target) code_or_doc = case type @@ -53,8 +61,10 @@ class Pry process_module when :variable_or_constant process_variable_or_constant + when :command + process_command else - command_error("method or module for '#{input}' could not be found or derived", false) + command_error("method/module/command for '#{input}' could not be found or derived", false) end render_output(code_or_doc, opts) diff --git a/test/test_command.rb b/test/test_command.rb index 9ecc053b..66dee88b 100644 --- a/test/test_command.rb +++ b/test/test_command.rb @@ -619,8 +619,10 @@ describe "Pry::Command" do mock_pry("my---test").should =~ /my-testmy-test/ end - it "should show the source of the process method" do - mock_pry("show-command my-test").should =~ /output.puts command_name/ + if !mri18_and_no_real_source_location? + it "should show the source of the process method" do + mock_pry("show-source my-test").should =~ /output.puts command_name/ + end end end diff --git a/test/test_default_commands/test_documentation.rb b/test/test_default_commands/test_documentation.rb index 75016f65..5debada5 100644 --- a/test/test_default_commands/test_documentation.rb +++ b/test/test_default_commands/test_documentation.rb @@ -319,5 +319,35 @@ if !mri18_and_no_real_source_location? end end + describe "on commands" do + + # mostly copied & modified from test_help.rb + before do + @oldset = Pry.config.commands + @set = Pry.config.commands = Pry::CommandSet.new do + import Pry::Commands + end + end + + after do + Pry.config.commands = @oldset + end + + it 'should display help for a specific command' do + mock_pry('show-doc ls').should =~ /Usage: ls/ + end + + it 'should display help for a regex command with a "listing"' do + @set.command /bar(.*)/, "Test listing", :listing => "foo" do; end + mock_pry('show-doc foo').should =~ /Test listing/ + end + + it 'should display help for a command with a spaces in its name' do + @set.command "command with spaces", "description of a command with spaces" do; end + mock_pry('show-doc "command with spaces"').should =~ /description of a command with spaces/ + end + + end + end end diff --git a/test/test_default_commands/test_introspection.rb b/test/test_default_commands/test_introspection.rb index 3b3d413d..24631f55 100644 --- a/test/test_default_commands/test_introspection.rb +++ b/test/test_default_commands/test_introspection.rb @@ -470,58 +470,4 @@ describe "Pry::DefaultCommands::Introspection" do end end - - # show-command only works in implementations that support Proc#source_location - if Proc.method_defined?(:source_location) - describe "show-command" do - before do - @str_output = StringIO.new - end - - it 'should show source for an ordinary command' do - set = Pry::CommandSet.new do - import_from Pry::Commands, "show-command" - command "foo" do - :body_of_foo - end - end - - redirect_pry_io(InputTester.new("show-command foo"), @str_output) do - Pry.new(:commands => set).rep - end - - @str_output.string.should =~ /:body_of_foo/ - end - - it 'should show source for a command with spaces in its name' do - set = Pry::CommandSet.new do - import_from Pry::Commands, "show-command" - command "foo bar" do - :body_of_foo_bar - end - end - - redirect_pry_io(InputTester.new("show-command \"foo bar\""), @str_output) do - Pry.new(:commands => set).rep - end - - @str_output.string.should =~ /:body_of_foo_bar/ - end - - it 'should show source for a command by listing name' do - set = Pry::CommandSet.new do - import_from Pry::Commands, "show-command" - command /foo(.*)/, "", :listing => "bar" do - :body_of_foo_regex - end - end - - redirect_pry_io(InputTester.new("show-command bar"), @str_output) do - Pry.new(:commands => set).rep - end - - @str_output.string.should =~ /:body_of_foo_regex/ - end - end - end end diff --git a/test/test_default_commands/test_show_source.rb b/test/test_default_commands/test_show_source.rb index b111e733..37daeab3 100644 --- a/test/test_default_commands/test_show_source.rb +++ b/test/test_default_commands/test_show_source.rb @@ -553,6 +553,51 @@ if !mri18_and_no_real_source_location? end end end + + describe "on commands" do + before do + @oldset = Pry.config.commands + @set = Pry.config.commands = Pry::CommandSet.new do + import Pry::Commands + end + end + + after do + Pry.config.commands = @oldset + end + + it 'should show source for an ordinary command' do + @set.command "foo", :body_of_foo do; end + + + string = mock_pry("show-source foo") + string.should =~ /:body_of_foo/ + end + + it "should output source of commands using special characters" do + @set.command "!", "Clear the input buffer" do; end + + + string = mock_pry("show-source !") + string.should =~ /Clear the input buffer/ + end + + it 'should show source for a command with spaces in its name' do + @set.command "foo bar", :body_of_foo_bar do; end + + + string = mock_pry("show-source \"foo bar\"") + string.should =~ /:body_of_foo_bar/ + end + + it 'should show source for a command by listing name' do + @set.command /foo(.*)/, :body_of_foo_bar_regex, :listing => "bar" do; end + + string = mock_pry("show-source bar") + string.should =~ /:body_of_foo_bar_regex/ + end + end + end end