Skip to content

Commit

Permalink
Merge branch 'rking/bond' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradIrwin committed Aug 21, 2012
2 parents 64ca5f0 + 246b88c commit 84c154e
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 31 deletions.
2 changes: 2 additions & 0 deletions Rakefile
Expand Up @@ -25,6 +25,8 @@ def apply_spec_defaults(s)
s.add_development_dependency('bacon', '~> 1.1')
s.add_development_dependency('open4', '~> 1.3')
s.add_development_dependency('rake', '~> 0.9')
# TODO: make this a plain dependency:
s.add_development_dependency('bond', '~> 0.4.2')
end

def check_dependencies
Expand Down
60 changes: 34 additions & 26 deletions lib/pry.rb
Expand Up @@ -166,13 +166,13 @@ module ExtendCommandBundle
end
end

require "method_source"
require 'method_source'
require 'shellwords'
require "stringio"
require "coderay"
require "optparse"
require "slop"
require "rbconfig"
require 'stringio'
require 'coderay'
require 'optparse'
require 'slop'
require 'rbconfig'
require 'tempfile'

begin
Expand Down Expand Up @@ -202,23 +202,31 @@ module ExtendCommandBundle
end
end

require "pry/version"
require "pry/rbx_method"
require "pry/rbx_path"
require "pry/code"
require "pry/method"
require "pry/wrapped_module"
require "pry/history_array"
require "pry/helpers"
require "pry/history"
require "pry/command"
require "pry/command_set"
require "pry/commands"
require "pry/custom_completions"
require "pry/completion"
require "pry/plugins"
require "pry/core_extensions"
require "pry/pry_class"
require "pry/pry_instance"
require "pry/cli"
require "pry/pager"
require 'pry/version'
require 'pry/rbx_method'
require 'pry/rbx_path'
require 'pry/code'
require 'pry/method'
require 'pry/wrapped_module'
require 'pry/history_array'
require 'pry/helpers'
require 'pry/history'
require 'pry/command'
require 'pry/command_set'
require 'pry/commands'
require 'pry/custom_completions'
require 'pry/completion'
require 'pry/plugins'
require 'pry/core_extensions'
require 'pry/pry_class'
require 'pry/pry_instance'
require 'pry/cli'
require 'pry/pager'

begin
require 'bond'
rescue LoadError
Pry.config.completer = Pry::InputCompleter
else
Pry.config.completer = Pry::BondCompleter
end
15 changes: 15 additions & 0 deletions lib/pry/command.rb
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions lib/pry/command_set.rb
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions lib/pry/commands/edit.rb
Expand Up @@ -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."
Expand Down
18 changes: 16 additions & 2 deletions lib/pry/completion.rb
@@ -1,9 +1,22 @@
# taken from irb

require "readline"

class Pry

module BondCompleter

def self.build_completion_proc(target, pry=nil, commands=[""])
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

# Implements tab completion for Readline in Pry
module InputCompleter

Expand Down Expand Up @@ -42,6 +55,7 @@ module InputCompleter
# @param [Binding] target The current binding context.
# @param [Array<String>] commands The array of Pry commands.
def self.build_completion_proc(target, pry=nil, commands=[""])

proc do |input|

# if there are multiple contexts e.g. cd 1/2/3
Expand Down
3 changes: 3 additions & 0 deletions lib/pry/config.rb
Expand Up @@ -228,6 +228,9 @@ def hooks=(v)
# Pry.config.extra_sticky_locals = { :random_number => proc {
# rand(10) } }
attr_accessor :extra_sticky_locals

# @return [#build_completion_proc] A completer to use.
attr_accessor :completer
end
end

2 changes: 1 addition & 1 deletion lib/pry/pry_instance.rb
Expand Up @@ -360,7 +360,7 @@ def retrieve_line(eval_string, target)
@indent.reset if eval_string.empty?

current_prompt = select_prompt(eval_string, target)
completion_proc = Pry::InputCompleter.build_completion_proc(target, self,
completion_proc = Pry.config.completer.build_completion_proc(target, self,
instance_eval(&custom_completions))


Expand Down
5 changes: 4 additions & 1 deletion pry.gemspec
Expand Up @@ -6,7 +6,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["John Mair (banisterfiend)", "Conrad Irwin"]
s.date = "2012-08-13"
s.date = "2012-08-18"
s.description = "An IRB alternative and runtime developer console"
s.email = ["jrmair@gmail.com", "conrad.irwin@gmail.com"]
s.executables = ["pry"]
Expand All @@ -27,13 +27,15 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<bacon>, ["~> 1.1"])
s.add_development_dependency(%q<open4>, ["~> 1.3"])
s.add_development_dependency(%q<rake>, ["~> 0.9"])
s.add_development_dependency(%q<bond>, ["~> 0.4.2"])
else
s.add_dependency(%q<coderay>, ["~> 1.0.5"])
s.add_dependency(%q<slop>, ["~> 3.3.1"])
s.add_dependency(%q<method_source>, ["~> 0.8"])
s.add_dependency(%q<bacon>, ["~> 1.1"])
s.add_dependency(%q<open4>, ["~> 1.3"])
s.add_dependency(%q<rake>, ["~> 0.9"])
s.add_dependency(%q<bond>, ["~> 0.4.2"])
end
else
s.add_dependency(%q<coderay>, ["~> 1.0.5"])
Expand All @@ -42,5 +44,6 @@ Gem::Specification.new do |s|
s.add_dependency(%q<bacon>, ["~> 1.1"])
s.add_dependency(%q<open4>, ["~> 1.3"])
s.add_dependency(%q<rake>, ["~> 0.9"])
s.add_dependency(%q<bond>, ["~> 0.4.2"])
end
end
14 changes: 14 additions & 0 deletions test/test_command.rb
Expand Up @@ -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(
Expand Down
12 changes: 12 additions & 0 deletions test/test_command_set.rb
Expand Up @@ -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
10 changes: 9 additions & 1 deletion test/test_completion.rb
Expand Up @@ -10,6 +10,11 @@ def completer_test(bind, pry=nil, assert_flag=true)
return proc {|*symbols| symbols.each(&test) }
end

describe 'bond-based completion' do
it 'should pull in Bond by default' do
Pry.config.completer.should == Pry::BondCompleter
end
end

describe Pry::InputCompleter do

Expand All @@ -20,9 +25,13 @@ def completer_test(bind, pry=nil, assert_flag=true)
module SymbolyName
def self.name; :symboly_name; end
end

$default_completer = Pry.config.completer
Pry.config.completer = Pry::InputCompleter
end

after do
Pry.config.completer = $default_completer
Object.remove_const :SymbolyName
end

Expand Down Expand Up @@ -216,4 +225,3 @@ module Baz
end

end

0 comments on commit 84c154e

Please sign in to comment.