summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert <r-obert@users.noreply.github.com>2017-08-13 21:02:10 +0100
committerGitHub <noreply@github.com>2017-08-13 21:02:10 +0100
commit9832fae3424d60a18501ef4ccfcc613ff66ec86c (patch)
tree3eecacab06629f2ed2282ce604a9959413516c51
parentbeaad80d0c1c1bf81ee464cdf9ae9a9e9b38c1df (diff)
parente1eeed547b1cebcc323bd1b78b94aec4f0dc3c12 (diff)
downloadpry-9832fae3424d60a18501ef4ccfcc613ff66ec86c.tar.gz
Merge pull request #1590 from dgutov/string_literal_methods_completion
Fix string literal methods completion
-rw-r--r--lib/pry/basic_object.rb2
-rw-r--r--lib/pry/config.rb15
-rw-r--r--lib/pry/config/behavior.rb4
-rw-r--r--lib/pry/input_completer.rb15
-rw-r--r--spec/completion_spec.rb60
-rw-r--r--spec/config_spec.rb14
6 files changed, 47 insertions, 63 deletions
diff --git a/lib/pry/basic_object.rb b/lib/pry/basic_object.rb
index fa2dccfb..8a9de3ef 100644
--- a/lib/pry/basic_object.rb
+++ b/lib/pry/basic_object.rb
@@ -1,5 +1,5 @@
class Pry::BasicObject < BasicObject
- [:Kernel, :Pry].each do |constant|
+ [:Kernel, :Pry, :ArgumentError].each do |constant|
const_set constant, ::Object.const_get(constant)
end
include Kernel
diff --git a/lib/pry/config.rb b/lib/pry/config.rb
index 4f702863..ba2076c9 100644
--- a/lib/pry/config.rb
+++ b/lib/pry/config.rb
@@ -8,4 +8,19 @@ class Pry::Config < Pry::BasicObject
def self.shortcuts
Convenience::SHORTCUTS
end
+
+ READLINE_WORD_ESCAPE_STR = " \t\n`><=;|&{("
+
+ def input=(input)
+ @lookup['input'] = input
+
+ if input.respond_to?(:completer_word_break_characters=)
+ begin
+ input.completer_word_break_characters = READLINE_WORD_ESCAPE_STR
+ rescue ArgumentError
+ # Hi JRuby
+ input.basic_word_break_characters = READLINE_WORD_ESCAPE_STR
+ end
+ end
+ end
end
diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb
index f487637d..ee67ac3f 100644
--- a/lib/pry/config/behavior.rb
+++ b/lib/pry/config/behavior.rb
@@ -219,9 +219,11 @@ private
def __push(key,value)
unless singleton_class.method_defined? key
define_singleton_method(key) { self[key] }
+ end
+ unless singleton_class.method_defined? "#{key}="
define_singleton_method("#{key}=") { |val| @lookup[key] = val }
end
- @lookup[key] = value
+ send("#{key}=", value)
end
def __remove(key)
diff --git a/lib/pry/input_completer.rb b/lib/pry/input_completer.rb
index 859cb667..b33668c1 100644
--- a/lib/pry/input_completer.rb
+++ b/lib/pry/input_completer.rb
@@ -12,7 +12,8 @@ class Pry::InputCompleter
CONSTANT_OR_METHOD_REGEXP = /^([A-Z].*)::([^:.]*)$/
HEX_REGEXP = /^(-?0x[0-9a-fA-F_]+)\.([^.]*)$/
GLOBALVARIABLE_REGEXP = /^(\$[^.]*)$/
- VARIABLE_REGEXP = /^([^."].*)\.([^.]*)$/
+ VARIABLE_REGEXP = /^(.*[^.'"])\.([^.]*)$/
+ STRING_REGEXP = /^(.*["'])\.([^.]*)$/
ReservedWords = [
"BEGIN", "END",
@@ -39,12 +40,9 @@ class Pry::InputCompleter
"[]", "[]=", "^", "!", "!=", "!~"
]
- WORD_ESCAPE_STR = " \t\n\"\\'`><=;|&{("
-
def initialize(input, pry = nil)
@pry = 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
@@ -179,12 +177,13 @@ class Pry::InputCompleter
}
end
select_message(path, receiver, message, candidates.sort)
- when /^\.([^.]*)$/
- # Unknown(maybe String)
- receiver = ""
- message = Regexp.quote($1)
+ when STRING_REGEXP
+ receiver = $1
+ message = Regexp.quote($2)
candidates = String.instance_methods(true).collect(&:to_s)
select_message(path, receiver, message, candidates)
+ # TODO: Handle the target-less calls (e.g. with a space before the dot).
+ # when /^\.([^.]*)$/
else
candidates = eval(
"methods | private_methods | local_variables | " \
diff --git a/spec/completion_spec.rb b/spec/completion_spec.rb
index 58a193c9..40c73e5e 100644
--- a/spec/completion_spec.rb
+++ b/spec/completion_spec.rb
@@ -85,6 +85,13 @@ describe Pry::InputCompleter do
# Absolute Constant
completer_test(o).call('::IndexError')
+
+ # String
+ completer_test(Object.new).call('"".size')
+ completer_test(Object.new).call('abcd".size')
+
+ # Chain of calls starting with a string
+ completer_test(Object.new).call('abcd".size.times')
end
it 'should complete for target symbols' do
@@ -138,59 +145,6 @@ describe Pry::InputCompleter do
completer_test(b, pry).call('/Con')
end
- it 'should complete for stdlib symbols' do
-
- o = Object.new
- # Regexp
- completer_test(o).call('/foo/.extend')
-
- # Array
- completer_test(o).call('[1].push')
-
- # Hash
- completer_test(o).call('{"a" => "b"}.keys')
-
- # Proc
- completer_test(o).call('{2}.call')
-
- # Symbol
- completer_test(o).call(':symbol.to_s')
-
- # Absolute Constant
- completer_test(o).call('::IndexError')
- end
-
- it 'should complete for target symbols' do
- o = Object.new
-
- # Constant
- module Mod
- remove_const :Con if defined? Con
- Con = 'Constant'
- module Mod2
- end
- end
-
- completer_test(Mod).call('Con')
-
- # Constants or Class Methods
- completer_test(o).call('Mod::Con')
-
- # Symbol
- _foo = :symbol
- completer_test(o).call(':symbol')
-
- # Variables
- class << o
- attr_accessor :foo
- end
- o.foo = 'bar'
- completer_test(binding).call('o.foo')
-
- # trailing slash
- expect(Pry::InputCompleter.new(Readline).call('Mod2/', :target => Pry.binding_for(Mod)).include?('Mod2/')).to eq(true)
- end
-
it 'should complete for arbitrary scopes' do
module Bar
@barvar = :bar
diff --git a/spec/config_spec.rb b/spec/config_spec.rb
index d6961c80..9d5c3956 100644
--- a/spec/config_spec.rb
+++ b/spec/config_spec.rb
@@ -239,4 +239,18 @@ describe Pry::Config do
expect(local['output']).to eq(nil)
end
end
+
+ describe "#input=" do
+ it 'assigns a value on the key' do
+ local = Pry::Config.from_hash({})
+ local.input = :foo
+ expect(local.input).to eq(:foo)
+ end
+
+ it 'modifies input itself' do
+ input = OpenStruct.new(completer_word_break_characters: 'asdf')
+ local = Pry::Config.from_hash(input: input)
+ expect(input.completer_word_break_characters).to eq(Pry::Config::READLINE_WORD_ESCAPE_STR)
+ end
+ end
end