From 7cafe09aec76924eafe3ca46b0265de44930a67e Mon Sep 17 00:00:00 2001 From: st0012 Date: Thu, 6 Oct 2022 11:54:11 +0100 Subject: [PATCH] [ruby/irb] Lazily evaluate candidates locals https://github.com/ruby/irb/commit/19a2fcbd87 --- lib/irb/completion.rb | 54 +++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/lib/irb/completion.rb b/lib/irb/completion.rb index 35767feeb7..9d386bbeca 100644 --- a/lib/irb/completion.rb +++ b/lib/irb/completion.rb @@ -171,10 +171,10 @@ module IRB 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 @@ -183,10 +183,10 @@ module IRB 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 @@ -195,10 +195,10 @@ module IRB 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 @@ -207,29 +207,33 @@ module IRB 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 - 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 @@ -240,15 +244,17 @@ module IRB # 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 + 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, "::") end @@ -258,10 +264,10 @@ module IRB 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 @@ -273,6 +279,7 @@ module IRB begin instance = eval(receiver, bind) + if doc_namespace "#{instance.class.name}.#{message}" else @@ -283,7 +290,7 @@ module IRB if doc_namespace nil else - candidates = [] + [] end end @@ -305,7 +312,7 @@ module IRB if doc_namespace nil else - candidates = [] + [] end end @@ -313,6 +320,7 @@ module IRB # global var gvar = $1 all_gvars = global_variables.collect{|m| m.to_s} + if doc_namespace all_gvars.find{ |i| i == gvar } else @@ -356,6 +364,7 @@ module IRB 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 }}" @@ -370,6 +379,7 @@ module IRB message = $1 candidates = String.instance_methods(true).collect{|m| m.to_s} + if doc_namespace "String.#{candidates.find{ |i| i == message }}" else