1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Refactor completion API (git diff -w)

This commit is contained in:
Conrad Irwin 2012-12-28 15:19:21 -08:00
parent 2c95c99735
commit fdb703a8de
6 changed files with 218 additions and 216 deletions

View file

@ -12,8 +12,6 @@ class Pry
else
_pry_.push_prompt Pry::SHELL_PROMPT
_pry_.custom_completions = Pry::FILE_COMPLETIONS
Readline.completion_proc = Pry::InputCompleter.build_completion_proc target,
_pry_.instance_eval(&Pry::FILE_COMPLETIONS)
end
end
end

View file

@ -3,37 +3,38 @@
class Pry
module BondCompleter
def self.build_completion_proc(target, pry=nil, commands=[""])
if !@started
@started = true
start
end
Pry.th[:pry] = pry
proc{ |*a| Bond.agent.call(*a) }
def self.call(input, options)
Pry.th[:pry] = options[:pry]
Bond.agent.call(input)
end
def self.start
Bond.start(:eval_binding => lambda{ Pry.th[:pry].current_context })
Bond.start(:eval_binding => lambda{ Pry.th[:pry] && Pry.th[:pry].current_context || TOPLEVEL_BINDING })
Bond.complete(:on => /\A/) do |input|
Pry.commands.complete(input.line,
:pry_instance => Pry.th[:pry],
:target => Pry.th[:pry].current_context,
:command_set => Pry.th[:pry].commands)
end
self
end
end
# Implements tab completion for Readline in Pry
module InputCompleter
def self.call(input, options)
build_completion_proc(options[:target], options[:pry], options[:custom_completions]).call input
end
def self.start
if Readline.respond_to?("basic_word_break_characters=")
Readline.basic_word_break_characters = " \t\n\"\\'`><=;|&{("
end
Readline.completion_append_character = nil
self
end
ReservedWords = [
"BEGIN", "END",
@ -63,16 +64,18 @@ class Pry
# Return a new completion proc for use by Readline.
# @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=[""])
def self.call(input, options)
proc do |input|
custom_completions = options[:custom_completions] || []
# if there are multiple contexts e.g. cd 1/2/3
# get new target for 1/2 and find candidates for 3
path, input = build_path(input)
unless path.call.empty?
target, _ = Pry::Helpers::BaseHelpers.context_from_object_path(path.call, pry)
if path.call.empty?
target = options[:target]
else
target, _ = Pry::Helpers::BaseHelpers.context_from_object_path(path.call, options[:pry])
target = target.last
end
@ -250,14 +253,13 @@ class Pry
if eval("respond_to?(:class_variables)", bind)
candidates += eval("class_variables", bind).collect(&:to_s)
end
candidates = (candidates|ReservedWords|commands).grep(/^#{Regexp.quote(input)}/)
candidates = (candidates|ReservedWords|custom_completions).grep(/^#{Regexp.quote(input)}/)
candidates.collect(&path)
end
rescue RescuableException
[]
end
end
end
def self.select_message(path, receiver, message, candidates)
candidates.grep(/^#{message}/).collect do |e|

View file

@ -270,9 +270,9 @@ class Pry
config.output_prefix = "=> "
if defined?(Bond) && Readline::VERSION !~ /editline/i
config.completer = Pry::BondCompleter
config.completer = Pry::BondCompleter.start
else
config.completer = Pry::InputCompleter
config.completer = Pry::InputCompleter.start
end
config.gist ||= OpenStruct.new

View file

@ -171,6 +171,18 @@ class Pry
end
end
# Generate completions.
#
# @param [String] what the user has typed so far
# @return [Array<String>] possible completions
def complete(input)
Pry.critical_section do
Pry.config.completer.call(input, :target => current_binding,
:pry => self,
:custom_completions => instance_eval(&custom_completions))
end
end
# Injects a local variable into the provided binding.
# @param [String] name The name of the local to inject.
# @param [Object] value The value to set the local to.

View file

@ -70,15 +70,11 @@ class Pry
@indent.reset if pry.eval_string.empty?
current_prompt = pry.select_prompt
completion_proc = Pry.config.completer.build_completion_proc(pry.current_binding, pry,
pry.instance_eval(&pry.custom_completions))
safe_completion_proc = proc{ |*a| Pry.critical_section{ completion_proc.call(*a) } }
indentation = Pry.config.auto_indent ? @indent.current_prefix : ''
begin
val = read_line("#{current_prompt}#{indentation}", safe_completion_proc)
val = read_line("#{current_prompt}#{indentation}")
# Handle <Ctrl+C> like Bash, empty the current input buffer but do not quit.
# This is only for ruby-1.9; other versions of ruby do not let you send Interrupt
@ -148,15 +144,17 @@ class Pry
# Returns the next line of input to be used by the pry instance.
# @param [String] current_prompt The prompt to use for input.
# @return [String] The next line of input.
def read_line(current_prompt="> ", completion_proc=nil)
def read_line(current_prompt)
handle_read_errors do
if defined? Coolline and input.is_a? Coolline
input.completion_proc = proc do |cool|
completions = completion_proc.call cool.completed_word
completions = @pry.complete cool.completed_word
completions.compact
end
elsif input.respond_to? :completion_proc=
input.completion_proc = completion_proc
input.completion_proc = proc do |input|
@pry.complete input
end
end
if input == Readline

View file

@ -1,12 +1,8 @@
require 'helper'
def new_completer(bind, pry=nil)
Pry::InputCompleter.build_completion_proc(Pry.binding_for(bind), pry)
end
def completer_test(bind, pry=nil, assert_flag=true)
completer = new_completer(bind, pry)
test = proc {|symbol| completer.call(symbol[0..-2]).include?(symbol).should == assert_flag}
test = proc {|symbol|
Pry::InputCompleter.call(symbol[0..-2], :target => Pry.binding_for(bind), :pry => pry).include?(symbol).should == assert_flag}
return proc {|*symbols| symbols.each(&test) }
end
@ -40,16 +36,12 @@ describe Pry::InputCompleter do
# another jruby hack :((
if !Pry::Helpers::BaseHelpers.jruby?
it "should not crash if there's a Module that has a symbolic name." do
completer = Pry::InputCompleter.build_completion_proc(Pry.binding_for(Object.new))
lambda{ completer.call "a.to_s." }.should.not.raise Exception
lambda{ Pry::InputCompleter.call "a.to_s.", :target => Pry.binding_for(Object.new) }.should.not.raise Exception
end
end
it 'should take parenthesis and other characters into account for symbols' do
b = Pry.binding_for(Object.new)
completer = Pry::InputCompleter.build_completion_proc(b)
lambda { completer.call(":class)") }.should.not.raise(RegexpError)
lambda { Pry::InputCompleter.call(":class)", :target => Pry.binding_for(Object.new)) }.should.not.raise(RegexpError)
end
it 'should complete instance variables' do
@ -128,7 +120,7 @@ describe Pry::InputCompleter do
completer_test(binding).call('o.foo')
# trailing slash
new_completer(Mod).call('Mod2/').include?('Mod2/').should == true
Pry::InputCompleter.call('Mod2/', :target => Pry.binding_for(Mod)).include?('Mod2/').should == true
end
it 'should complete for arbitrary scopes' do
@ -199,7 +191,7 @@ describe Pry::InputCompleter do
completer_test(binding).call('o.foo')
# trailing slash
new_completer(Mod).call('Mod2/').include?('Mod2/').should == true
Pry::InputCompleter.call('Mod2/', :target => Pry.binding_for(Mod)).include?('Mod2/').should == true
end