From ef482bb551f692f1ea1260408b4a5881fca26cb0 Mon Sep 17 00:00:00 2001 From: Jason Laster Date: Fri, 3 Aug 2012 21:20:20 -0400 Subject: [PATCH] add support for tabbing into previous scope --- lib/pry/completion.rb | 34 +++++++++++++++----- lib/pry/default_commands/input_and_output.rb | 2 +- lib/pry/pry_instance.rb | 2 +- test/test_completion.rb | 22 +++++++++++++ 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/lib/pry/completion.rb b/lib/pry/completion.rb index 9d7c913e..e64e1463 100644 --- a/lib/pry/completion.rb +++ b/lib/pry/completion.rb @@ -41,7 +41,7 @@ class Pry # Return a new completion proc for use by Readline. # @param [Binding] target The current binding context. # @param [Array] 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 diff --git a/lib/pry/default_commands/input_and_output.rb b/lib/pry/default_commands/input_and_output.rb index 32c59bd5..eb50ea70 100644 --- a/lib/pry/default_commands/input_and_output.rb +++ b/lib/pry/default_commands/input_and_output.rb @@ -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 diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index 8c905517..532d19b6 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -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)) diff --git a/test/test_completion.rb b/test/test_completion.rb index 8b0b3d4c..50b91ba7 100644 --- a/test/test_completion.rb +++ b/test/test_completion.rb @@ -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