From 3fd66b6403f7164e9566641fe4b754614e04f242 Mon Sep 17 00:00:00 2001 From: robert Date: Thu, 3 Apr 2014 02:16:24 +0200 Subject: [PATCH] `input` becomes primary argument in #initialize. with `_pry_` as an optional second. in the repl the second argument will always be met but to avoid needing a pry instance around to use Pry::InputCompleter, it uses the first argument(`input`) when implementing auto completion, and 'pry' can be used if its around/available. --- lib/pry/input_completer.rb | 11 +++-------- lib/pry/pry_instance.rb | 3 ++- spec/completion_spec.rb | 12 ++++++------ 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/lib/pry/input_completer.rb b/lib/pry/input_completer.rb index 7c013226..5331eedd 100644 --- a/lib/pry/input_completer.rb +++ b/lib/pry/input_completer.rb @@ -1,7 +1,6 @@ # taken from irb # Implements tab completion for Readline in Pry class Pry::InputCompleter - NUMERIC_REGEXP = /^(-?(0[dbo])?[0-9_]+(\.[0-9_]+)?([eE]-?[0-9]+)?)\.([^.]*)$/ ARRAY_REGEXP = /^([^\]]*\])\.([^.]*)$/ SYMBOL_REGEXP = /^(:[^:.]*)$/ @@ -42,13 +41,9 @@ class Pry::InputCompleter WORD_ESCAPE_STR = " \t\n\"\\'`><=;|&{(" - def initialize(pry = nil) - if pry - @pry = pry - @input = pry.input - else - @input = Readline - end + def initialize(input, pry = nil) + @pry = pry if pry + @input = input @input.basic_word_break_characters = WORD_ESCAPE_STR if @input.respond_to?(:basic_word_break_characters=) @input.completion_append_character = nil if @input.respond_to?(:completion_append_character=) end diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index bf510b13..28698479 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -127,7 +127,8 @@ class Pry def complete(input) return EMPTY_COMPLETIONS unless config.completer Pry.critical_section do - completer = config.completer.new(self) + completer = config.completer.new(input, self) + # todo: `input` argument isn't needed. completer.call input, target: current_binding, custom_completions: custom_completions.call.push(*sticky_locals.keys) end end diff --git a/spec/completion_spec.rb b/spec/completion_spec.rb index 7cb742d9..1207f253 100644 --- a/spec/completion_spec.rb +++ b/spec/completion_spec.rb @@ -4,7 +4,7 @@ require "pry/input_completer" def completer_test(bind, pry=nil, assert_flag=true) test = proc {|symbol| - Pry::InputCompleter.new(pry).call(symbol[0..-2], :target => Pry.binding_for(bind)).include?(symbol).should == assert_flag} + Pry::InputCompleter.new(pry || Readline, pry).call(symbol[0..-2], :target => Pry.binding_for(bind)).include?(symbol).should == assert_flag} return proc {|*symbols| symbols.each(&test) } end @@ -30,12 +30,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 - lambda{ Pry::InputCompleter.new.call "a.to_s.", :target => Pry.binding_for(Object.new) }.should.not.raise Exception + lambda{ Pry::InputCompleter.new(Readline).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 - lambda { Pry::InputCompleter.new.call(":class)", :target => Pry.binding_for(Object.new)) }.should.not.raise(RegexpError) + lambda { Pry::InputCompleter.new(Readline).call(":class)", :target => Pry.binding_for(Object.new)) }.should.not.raise(RegexpError) end it 'should complete instance variables' do @@ -114,7 +114,7 @@ describe Pry::InputCompleter do completer_test(binding).call('o.foo') # trailing slash - Pry::InputCompleter.new.call('Mod2/', :target => Pry.binding_for(Mod)).include?('Mod2/').should == true + Pry::InputCompleter.new(Readline).call('Mod2/', :target => Pry.binding_for(Mod)).include?('Mod2/').should == true end it 'should complete for arbitrary scopes' do @@ -185,7 +185,7 @@ describe Pry::InputCompleter do completer_test(binding).call('o.foo') # trailing slash - Pry::InputCompleter.new.call('Mod2/', :target => Pry.binding_for(Mod)).include?('Mod2/').should == true + Pry::InputCompleter.new(Readline).call('Mod2/', :target => Pry.binding_for(Mod)).include?('Mod2/').should == true end it 'should complete for arbitrary scopes' do @@ -209,6 +209,6 @@ describe Pry::InputCompleter do it 'should not return nil in its output' do pry = Pry.new - Pry::InputCompleter.new(pry).call("pry.", :target => binding).should.not.include nil + Pry::InputCompleter.new(Readline, pry).call("pry.", :target => binding).should.not.include nil end end