mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
added completion (taken from irb). Remember to remove hardcoding expecation of command_info from Pry#re() method, need to turn command addition into DSL
This commit is contained in:
parent
1666945e4b
commit
4ea554fcfd
6 changed files with 270 additions and 40 deletions
|
@ -4,6 +4,7 @@
|
|||
direc = File.dirname(__FILE__)
|
||||
|
||||
require "method_source"
|
||||
require "readline"
|
||||
require "#{direc}/pry/version"
|
||||
require "#{direc}/pry/input"
|
||||
require "#{direc}/pry/output"
|
||||
|
@ -11,6 +12,7 @@ require "#{direc}/pry/hooks"
|
|||
require "#{direc}/pry/print"
|
||||
require "#{direc}/pry/commands"
|
||||
require "#{direc}/pry/prompts"
|
||||
require "#{direc}/pry/completion"
|
||||
require "#{direc}/pry/core_extensions"
|
||||
require "#{direc}/pry/pry_class"
|
||||
require "#{direc}/pry/pry_instance"
|
||||
|
|
|
@ -66,7 +66,7 @@ class Pry
|
|||
"exit_all" => proc do
|
||||
throw(:breakout, 0)
|
||||
end,
|
||||
["exit", "quit", "back", /cd\s*\.\./] => proc do |opts|
|
||||
["exit", "quit", "back", /^cd\s*\.\./] => proc do |opts|
|
||||
throw(:breakout, opts[:nesting].level)
|
||||
end,
|
||||
"ls" => proc do |opts|
|
||||
|
@ -80,6 +80,9 @@ class Pry
|
|||
end,
|
||||
/^cd\s+(.+)/ => proc do |opts|
|
||||
obj = opts[:captures].first
|
||||
|
||||
throw(:breakout, opts[:nesting].level) if obj == ".."
|
||||
|
||||
opts[:target].eval("#{obj}.pry")
|
||||
opts[:val].clear
|
||||
end,
|
||||
|
|
200
lib/pry/completion.rb
Normal file
200
lib/pry/completion.rb
Normal file
|
@ -0,0 +1,200 @@
|
|||
# stolen from irb
|
||||
|
||||
require "readline"
|
||||
|
||||
class Pry
|
||||
module InputCompleter
|
||||
|
||||
ReservedWords = [
|
||||
"BEGIN", "END",
|
||||
"alias", "and",
|
||||
"begin", "break",
|
||||
"case", "class",
|
||||
"def", "defined", "do",
|
||||
"else", "elsif", "end", "ensure",
|
||||
"false", "for",
|
||||
"if", "in",
|
||||
"module",
|
||||
"next", "nil", "not",
|
||||
"or",
|
||||
"redo", "rescue", "retry", "return",
|
||||
"self", "super",
|
||||
"then", "true",
|
||||
"undef", "unless", "until",
|
||||
"when", "while",
|
||||
"yield",
|
||||
]
|
||||
|
||||
Operators = ["%", "&", "*", "**", "+", "-", "/",
|
||||
"<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", ">>",
|
||||
"[]", "[]=", "^", "!", "!=", "!~"]
|
||||
|
||||
def self.build_completion_proc(target, commands=[""])
|
||||
proc { |input|
|
||||
bind = target
|
||||
|
||||
case input
|
||||
when /^(\/[^\/]*\/)\.([^.]*)$/
|
||||
# Regexp
|
||||
receiver = $1
|
||||
message = Regexp.quote($2)
|
||||
|
||||
candidates = Regexp.instance_methods.collect{|m| m.to_s}
|
||||
select_message(receiver, message, candidates)
|
||||
|
||||
when /^([^\]]*\])\.([^.]*)$/
|
||||
# Array
|
||||
receiver = $1
|
||||
message = Regexp.quote($2)
|
||||
|
||||
candidates = Array.instance_methods.collect{|m| m.to_s}
|
||||
select_message(receiver, message, candidates)
|
||||
|
||||
when /^([^\}]*\})\.([^.]*)$/
|
||||
# Proc or Hash
|
||||
receiver = $1
|
||||
message = Regexp.quote($2)
|
||||
|
||||
candidates = Proc.instance_methods.collect{|m| m.to_s}
|
||||
candidates |= Hash.instance_methods.collect{|m| m.to_s}
|
||||
select_message(receiver, message, candidates)
|
||||
|
||||
when /^(:[^:.]*)$/
|
||||
# Symbol
|
||||
if Symbol.respond_to?(:all_symbols)
|
||||
sym = $1
|
||||
candidates = Symbol.all_symbols.collect{|s| ":" + s.id2name}
|
||||
candidates.grep(/^#{sym}/)
|
||||
else
|
||||
[]
|
||||
end
|
||||
|
||||
when /^::([A-Z][^:\.\(]*)$/
|
||||
# Absolute Constant or class methods
|
||||
receiver = $1
|
||||
candidates = Object.constants.collect{|m| m.to_s}
|
||||
candidates.grep(/^#{receiver}/).collect{|e| "::" + e}
|
||||
|
||||
# when /^(((::)?[A-Z][^:.\(]*)+)::?([^:.]*)$/
|
||||
when /^([A-Z].*)::([^:.]*)$/
|
||||
# Constant or class methods
|
||||
receiver = $1
|
||||
message = Regexp.quote($2)
|
||||
begin
|
||||
candidates = eval("#{receiver}.constants.collect{|m| m.to_s}", bind)
|
||||
candidates |= eval("#{receiver}.methods.collect{|m| m.to_s}", bind)
|
||||
rescue Exception
|
||||
candidates = []
|
||||
end
|
||||
candidates.grep(/^#{message}/).collect{|e| receiver + "::" + e}
|
||||
|
||||
when /^(:[^:.]+)\.([^.]*)$/
|
||||
# Symbol
|
||||
receiver = $1
|
||||
message = Regexp.quote($2)
|
||||
|
||||
candidates = Symbol.instance_methods.collect{|m| m.to_s}
|
||||
select_message(receiver, message, candidates)
|
||||
|
||||
when /^(-?(0[dbo])?[0-9_]+(\.[0-9_]+)?([eE]-?[0-9]+)?)\.([^.]*)$/
|
||||
# Numeric
|
||||
receiver = $1
|
||||
message = Regexp.quote($5)
|
||||
|
||||
begin
|
||||
candidates = eval(receiver, bind).methods.collect{|m| m.to_s}
|
||||
rescue Exception
|
||||
candidates = []
|
||||
end
|
||||
select_message(receiver, message, candidates)
|
||||
|
||||
when /^(-?0x[0-9a-fA-F_]+)\.([^.]*)$/
|
||||
# Numeric(0xFFFF)
|
||||
receiver = $1
|
||||
message = Regexp.quote($2)
|
||||
|
||||
begin
|
||||
candidates = eval(receiver, bind).methods.collect{|m| m.to_s}
|
||||
rescue Exception
|
||||
candidates = []
|
||||
end
|
||||
select_message(receiver, message, candidates)
|
||||
|
||||
when /^(\$[^.]*)$/
|
||||
regmessage = Regexp.new(Regexp.quote($1))
|
||||
candidates = global_variables.collect{|m| m.to_s}.grep(regmessage)
|
||||
|
||||
# when /^(\$?(\.?[^.]+)+)\.([^.]*)$/
|
||||
# when /^((\.?[^.]+)+)\.([^.]*)$/
|
||||
when /^([^."].*)\.([^.]*)$/
|
||||
# variable
|
||||
receiver = $1
|
||||
message = Regexp.quote($2)
|
||||
|
||||
gv = eval("global_variables", bind).collect{|m| m.to_s}
|
||||
lv = eval("local_variables", bind).collect{|m| m.to_s}
|
||||
cv = eval("self.class.constants", bind).collect{|m| m.to_s}
|
||||
|
||||
if (gv | lv | cv).include?(receiver) or /^[A-Z]/ =~ receiver && /\./ !~ receiver
|
||||
# foo.func and foo is local var. OR
|
||||
# Foo::Bar.func
|
||||
begin
|
||||
candidates = eval("#{receiver}.methods", bind).collect{|m| m.to_s}
|
||||
rescue Exception
|
||||
candidates = []
|
||||
end
|
||||
else
|
||||
# func1.func2
|
||||
candidates = []
|
||||
ObjectSpace.each_object(Module){|m|
|
||||
begin
|
||||
name = m.name
|
||||
rescue Exception
|
||||
name = ""
|
||||
end
|
||||
next if name != "IRB::Context" and
|
||||
/^(IRB|SLex|RubyLex|RubyToken)/ =~ name
|
||||
candidates.concat m.instance_methods(false).collect{|x| x.to_s}
|
||||
}
|
||||
candidates.sort!
|
||||
candidates.uniq!
|
||||
end
|
||||
select_message(receiver, message, candidates)
|
||||
|
||||
when /^\.([^.]*)$/
|
||||
# unknown(maybe String)
|
||||
|
||||
receiver = ""
|
||||
message = Regexp.quote($1)
|
||||
|
||||
candidates = String.instance_methods(true).collect{|m| m.to_s}
|
||||
select_message(receiver, message, candidates)
|
||||
|
||||
else
|
||||
candidates = eval("methods | private_methods | local_variables | self.class.constants", bind).collect{|m| m.to_s}
|
||||
|
||||
(candidates|ReservedWords|commands).grep(/^#{Regexp.quote(input)}/)
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if Readline.respond_to?("basic_word_break_characters=")
|
||||
Readline.basic_word_break_characters= " \t\n\"\\'`><=;|&{("
|
||||
end
|
||||
Readline.completion_append_character = nil
|
|
@ -2,37 +2,6 @@ require 'readline'
|
|||
|
||||
class Pry
|
||||
|
||||
# read from any IO-alike
|
||||
class IOInput
|
||||
def initialize(io)
|
||||
@io = io
|
||||
end
|
||||
|
||||
def readline
|
||||
@io.readline
|
||||
end
|
||||
|
||||
def close
|
||||
@io.close
|
||||
end
|
||||
end
|
||||
|
||||
# file input class
|
||||
class FileInput
|
||||
def initialize(file, line = 1)
|
||||
@f = File.open(file)
|
||||
(line - 1).times { @f.readline }
|
||||
end
|
||||
|
||||
def readline(prompt)
|
||||
@f.readline
|
||||
end
|
||||
|
||||
def close
|
||||
@f.close
|
||||
end
|
||||
end
|
||||
|
||||
# preset input class
|
||||
class PresetInput
|
||||
def initialize(*actions)
|
||||
|
|
|
@ -110,6 +110,11 @@ class Pry
|
|||
def re(target=TOPLEVEL_BINDING)
|
||||
target = binding_for(target)
|
||||
|
||||
# FIXME!!!!!!!! Should not hardcode command_info in here!
|
||||
if input == Readline
|
||||
Readline.completion_proc = Pry::InputCompleter.build_completion_proc(target, Pry.commands.command_info.keys.flatten)
|
||||
end
|
||||
|
||||
# eval the expression and save to last_result
|
||||
Pry.last_result = target.eval r(target)
|
||||
|
||||
|
|
67
test/test.rb
67
test/test.rb
|
@ -181,16 +181,67 @@ describe Pry do
|
|||
Pry.reset_defaults
|
||||
end
|
||||
|
||||
it 'should set the input default, and the default should be overridable' do
|
||||
Pry.input = InputTester.new("5")
|
||||
describe "input" do
|
||||
|
||||
str_output = StringIO.new
|
||||
Pry.output = str_output
|
||||
Pry.new.rep
|
||||
str_output.string.should =~ /5/
|
||||
after do
|
||||
Pry.reset_defaults
|
||||
end
|
||||
|
||||
it 'should set the input default, and the default should be overridable' do
|
||||
Pry.input = InputTester.new("5")
|
||||
|
||||
Pry.new(:input => InputTester.new("6")).rep
|
||||
str_output.string.should =~ /6/
|
||||
str_output = StringIO.new
|
||||
Pry.output = str_output
|
||||
Pry.new.rep
|
||||
str_output.string.should =~ /5/
|
||||
|
||||
Pry.new(:input => InputTester.new("6")).rep
|
||||
str_output.string.should =~ /6/
|
||||
end
|
||||
|
||||
it 'should pass in the prompt if readline arity is 1' do
|
||||
Pry.prompt = proc { "A" }
|
||||
|
||||
arity_one_input = Class.new do
|
||||
attr_accessor :prompt
|
||||
def readline(prompt)
|
||||
@prompt = prompt
|
||||
"exit"
|
||||
end
|
||||
end.new
|
||||
|
||||
Pry.start(self, :input => arity_one_input, :output => Pry::NullOutput)
|
||||
arity_one_input.prompt.should == Pry.prompt.call
|
||||
end
|
||||
|
||||
it 'should not pass in the prompt if the arity is 0' do
|
||||
Pry.prompt = proc { "A" }
|
||||
|
||||
arity_zero_input = Class.new do
|
||||
def readline
|
||||
"exit"
|
||||
end
|
||||
end.new
|
||||
|
||||
lambda { Pry.start(self, :input => arity_zero_input, :output => Pry::NullOutput) }.should.not.raise Exception
|
||||
end
|
||||
|
||||
it 'should not pass in the prompt if the arity is -1' do
|
||||
Pry.prompt = proc { "A" }
|
||||
|
||||
arity_multi_input = Class.new do
|
||||
attr_accessor :prompt
|
||||
|
||||
def readline(*args)
|
||||
@prompt = args.first
|
||||
"exit"
|
||||
end
|
||||
end.new
|
||||
|
||||
Pry.start(self, :input => arity_multi_input, :output => Pry::NullOutput)
|
||||
arity_multi_input.prompt.should == nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
it 'should set the output default, and the default should be overridable' do
|
||||
|
|
Loading…
Reference in a new issue