Skip to content
Browse files

Bond completion includes commands

  • Loading branch information...
1 parent 4fca664 commit 246b88cf16d92b4edf9273ab78ae1218d7d3f37f @ConradIrwin ConradIrwin committed Aug 20, 2012
Showing with 67 additions and 3 deletions.
  1. +15 −0 lib/pry/command.rb
  2. +14 −0 lib/pry/command_set.rb
  3. +4 −0 lib/pry/commands/edit.rb
  4. +8 −3 lib/pry/completion.rb
  5. +14 −0 test/test_command.rb
  6. +12 −0 test/test_command_set.rb
View
15 lib/pry/command.rb
@@ -392,6 +392,12 @@ def dependencies_met?
@dependencies_met ||= command_dependencies_met?(command_options)
end
+ # Generate completions for this command
+ #
+ # @param [String] search The line typed so far
+ # @return [Array<String>] Completion words
+ def complete(search); Bond::DefaultMission.completions; end
+
private
# Run the `#call` method and all the registered hooks.
@@ -499,6 +505,15 @@ def slop
end
end
+ # Generate shell completions
+ # @param [String] search The line typed so far
+ # @return [Array<String>] the words to complete
+ def complete(search)
+ slop.map do |opt|
+ [opt.long && "--#{opt.long}" || opt.short && "-#{opt.short}"]
+ end.flatten(1).compact + super
+ end
+
# A function called just before `options(opt)` as part of `call`.
#
# This function can be used to set up any context your command needs to run, for example
View
14 lib/pry/command_set.rb
@@ -348,6 +348,20 @@ def run_command(context, match, *args)
command.new(context).call_safely(*args)
end
+ # Generate completions for the user's search.
+ # @param [String] search The line to search for
+ # @param [Hash] context The context to create the command with
+ # @return [Array<String>]
+ def complete(search, context={})
+ if command = find_command(search)
+ command.new(context).complete(search)
+ else
+ commands.keys.select do |x|
+ String === x && x.start_with?(search)
+ end + Bond::DefaultMission.completions
+ end
+ end
+
private
def default_options(match)
View
4 lib/pry/commands/edit.rb
@@ -26,6 +26,10 @@ def options(opt)
opt.on :r, :reload, "Reload the edited code immediately (default for ruby files)"
end
+ def complete(search)
+ super + Bond::Rc.files(search.split(" ").last || '')
+ end
+
def process
if [opts.present?(:ex), opts.present?(:temp), opts.present?(:in), !args.empty?].count(true) > 1
raise CommandError, "Only one of --ex, --temp, --in and FILE may be specified."
View
11 lib/pry/completion.rb
@@ -3,11 +3,16 @@
class Pry
module BondCompleter
+
def self.build_completion_proc(target, pry=nil, commands=[""])
- Bond.start
- Bond.complete(:on => %r/^edit\s/) do |input|
- ::Readline::FILENAME_COMPLETION_PROC.call(input) || []
+ Bond.restart(:eval_binding => lambda{ pry.current_context })
+ Bond.complete(:on => /\A/) do |input|
+ Pry.commands.complete(input.line,
+ :pry_instance => pry,
+ :target => pry.current_context,
+ :command_set => pry.commands)
end
+
proc{ |*a| Bond.agent.call(*a) }
end
end
View
14 test/test_command.rb
@@ -696,6 +696,20 @@ def process
end
end
+ describe 'complete' do
+ it 'should return the arguments that are defined' do
+ @set.create_command "torrid" do
+ def options(opt)
+ opt.on :test
+ opt.on :lest
+ opt.on :pests
+ end
+ end
+
+ @set.complete('torrid ').should.include('--test')
+ end
+ end
+
describe 'group' do
before do
@set.import(
View
12 test/test_command_set.rb
@@ -610,4 +610,16 @@ def process; 42; end
@set.process_line('nannnnnny oggggg')
end
end
+
+ describe '.complete' do
+ it "should list all command names" do
+ @set.create_command('susan'){ }
+ @set.complete('sus').should.include 'susan'
+ end
+
+ it "should delegate to commands" do
+ @set.create_command('susan'){ def complete(search); ['--foo']; end }
+ @set.complete('susan ').should == ['--foo']
+ end
+ end
end

0 comments on commit 246b88c

Please sign in to comment.
Something went wrong with that request. Please try again.