Browse files

add support for tabbing into previous scope

  • Loading branch information...
1 parent d830ebb commit ef482bb551f692f1ea1260408b4a5881fca26cb0 @jasonLaster jasonLaster committed Aug 3, 2012
Showing with 50 additions and 10 deletions.
  1. +26 −8 lib/pry/completion.rb
  2. +1 −1 lib/pry/default_commands/input_and_output.rb
  3. +1 −1 lib/pry/pry_instance.rb
  4. +22 −0 test/test_completion.rb
View
34 lib/pry/completion.rb
@@ -41,7 +41,7 @@ module InputCompleter
# 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 @@ def self.build_completion_proc(target, commands=[""])
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 @@ def self.build_completion_proc(target, commands=[""])
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
2 lib/pry/default_commands/input_and_output.rb
@@ -33,7 +33,7 @@ module DefaultCommands
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
2 lib/pry/pry_instance.rb
@@ -359,7 +359,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,
+ completion_proc = Pry::InputCompleter.build_completion_proc(target, self,
instance_eval(&custom_completions))
View
22 test/test_completion.rb
@@ -58,5 +58,27 @@ def self.name; :symboly_name; end
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

0 comments on commit ef482bb

Please sign in to comment.