Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions lib/irb/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace
receiver = $1
message = $3

candidates = String.instance_methods.collect{|m| m.to_s}
if doc_namespace
"String.#{message}"
else
candidates = String.instance_methods.collect{|m| m.to_s}
select_message(receiver, message, candidates)
end

Expand All @@ -183,10 +183,10 @@ def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace
receiver = $1
message = $2

candidates = Regexp.instance_methods.collect{|m| m.to_s}
if doc_namespace
"Regexp.#{message}"
else
candidates = Regexp.instance_methods.collect{|m| m.to_s}
select_message(receiver, message, candidates)
end

Expand All @@ -195,10 +195,10 @@ def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace
receiver = $1
message = $2

candidates = Array.instance_methods.collect{|m| m.to_s}
if doc_namespace
"Array.#{message}"
else
candidates = Array.instance_methods.collect{|m| m.to_s}
select_message(receiver, message, candidates)
end

Expand All @@ -207,29 +207,33 @@ def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace
receiver = $1
message = $2

proc_candidates = Proc.instance_methods.collect{|m| m.to_s}
hash_candidates = Hash.instance_methods.collect{|m| m.to_s}
if doc_namespace
["Proc.#{message}", "Hash.#{message}"]
else
proc_candidates = Proc.instance_methods.collect{|m| m.to_s}
hash_candidates = Hash.instance_methods.collect{|m| m.to_s}
select_message(receiver, message, proc_candidates | hash_candidates)
end

when /^(:[^:.]*)$/
# Symbol
return nil if doc_namespace
Comment thread
peterzhu2118 marked this conversation as resolved.
sym = $1
candidates = Symbol.all_symbols.collect do |s|
":" + s.id2name.encode(Encoding.default_external)
rescue EncodingError
# ignore
if doc_namespace
nil
else
sym = $1
candidates = Symbol.all_symbols.collect do |s|
":" + s.id2name.encode(Encoding.default_external)
rescue EncodingError
# ignore
end
candidates.grep(/^#{Regexp.quote(sym)}/)
end
candidates.grep(/^#{Regexp.quote(sym)}/)

when /^::([A-Z][^:\.\(\)]*)$/
# Absolute Constant or class methods
receiver = $1

candidates = Object.constants.collect{|m| m.to_s}

if doc_namespace
candidates.find { |i| i == receiver }
else
Expand All @@ -240,16 +244,18 @@ def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace
# Constant or class methods
receiver = $1
message = $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

if doc_namespace
"#{receiver}::#{message}"
else
select_message(receiver, message, candidates, "::")
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

select_message(receiver, message, candidates.sort, "::")
end

when /^(:[^:.]+)(\.|::)([^.]*)$/
Expand All @@ -258,10 +264,10 @@ def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace
sep = $2
message = $3

candidates = Symbol.instance_methods.collect{|m| m.to_s}
if doc_namespace
"Symbol.#{message}"
else
candidates = Symbol.instance_methods.collect{|m| m.to_s}
select_message(receiver, message, candidates, sep)
end

Expand All @@ -273,6 +279,7 @@ def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace

begin
instance = eval(receiver, bind)

if doc_namespace
"#{instance.class.name}.#{message}"
else
Expand All @@ -283,7 +290,7 @@ def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace
if doc_namespace
nil
else
candidates = []
[]
end
end

Expand All @@ -305,14 +312,15 @@ def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace
if doc_namespace
nil
else
candidates = []
[]
end
end

when /^(\$[^.]*)$/
# global var
gvar = $1
all_gvars = global_variables.collect{|m| m.to_s}

if doc_namespace
all_gvars.find{ |i| i == gvar }
else
Expand Down Expand Up @@ -356,6 +364,7 @@ def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace
candidates.sort!
candidates.uniq!
end

if doc_namespace
rec_class = rec.is_a?(Module) ? rec : rec.class
"#{rec_class.name}#{sep}#{candidates.find{ |i| i == message }}"
Expand All @@ -370,6 +379,7 @@ def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace
message = $1

candidates = String.instance_methods(true).collect{|m| m.to_s}

if doc_namespace
"String.#{candidates.find{ |i| i == message }}"
else
Expand Down
Loading