add support for tabbing into previous scope

This commit is contained in:
Jason Laster 2012-08-03 21:20:20 -04:00
parent d830ebbacc
commit ef482bb551
4 changed files with 50 additions and 10 deletions

View File

@ -41,7 +41,7 @@ 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, commands=[""])
def self.build_completion_proc(target, pry=nil, commands=[""])
proc do |input|
begin
bind = target
@ -177,6 +177,20 @@ class Pry
end
select_message(receiver, message, candidates)
when /^(\.{2}\/)(.*)$/
# previous scope (../) *
stack = pry.binding_stack.dup
stack.pop
if stack.length >= 1
receiver = $1
message = $2
scope = stack[-1]
ivars = eval("instance_variables", scope)
locals = eval("local_variables", scope)
select_message(receiver, message, ivars + locals)
end
when /^\.([^.]*)$/
# unknown(maybe String)
@ -207,13 +221,17 @@ class Pry
def self.select_message(receiver, message, candidates)
candidates.grep(/^#{message}/).collect do |e|
case e
when /^[a-zA-Z_]/
receiver + "." + e
when /^[0-9]/
when *Operators
#receiver + " " + e
end
if receiver.match(/^\.{2}\//)
receiver + e.to_s
else
case e
when /^[a-zA-Z_]/
receiver + "." + e
when /^[0-9]/
when *Operators
#receiver + " " + e
end
end
end
end
end

View File

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

View File

@ -359,7 +359,7 @@ class Pry
@indent.reset if eval_string.empty?
current_prompt = select_prompt(eval_string, target)
completion_proc = Pry::InputCompleter.build_completion_proc(target,
completion_proc = Pry::InputCompleter.build_completion_proc(target, self,
instance_eval(&custom_completions))

View File

@ -58,5 +58,27 @@ describe Pry::InputCompleter do
completer.call('@@nu').include?('@@number').should == true
completer.call('@@number.cl').include?('@@number.class').should == true
end
it 'should complete previous scope' do
module Bar
@barvar = :bar
end
module Baz
@bar = Bar
@bazvar = :baz
end
pry = Pry.new()
stack = pry.binding_stack
stack.push(Pry.binding_for(Baz))
stack.push(Pry.binding_for(Bar))
completer = Pry::InputCompleter.build_completion_proc(
Pry.binding_for(Bar), pry
)
completer.call('../@').include?("../@bazvar").should == true
end
end