summaryrefslogtreecommitdiff
path: root/lib/irb/completion.rb
diff options
context:
space:
mode:
authoraycabta <aycabta@gmail.com>2021-03-24 15:33:07 +0900
committergit <svn-admin@ruby-lang.org>2021-03-25 18:54:02 +0900
commitad8842c06d26ee634f90008efecf1cd4d76342df (patch)
tree06a162d412417ec31ced7eaf4d8f87951201ebda /lib/irb/completion.rb
parentf45bed0a6e983abfe616455ced2e50db381ba2dd (diff)
[ruby/irb] Cache completion files to require
https://github.com/ruby/irb/commit/612ebcb311
Diffstat (limited to 'lib/irb/completion.rb')
-rw-r--r--lib/irb/completion.rb33
1 files changed, 21 insertions, 12 deletions
diff --git a/lib/irb/completion.rb b/lib/irb/completion.rb
index 0385142a3c..099dd85627 100644
--- a/lib/irb/completion.rb
+++ b/lib/irb/completion.rb
@@ -40,6 +40,24 @@ module IRB
BASIC_WORD_BREAK_CHARACTERS = " \t\n`><=;|&{("
+ def self.retrieve_files_to_require_from_load_path
+ @@files_from_load_path ||= $LOAD_PATH.flat_map { |path|
+ begin
+ Dir.glob("**/*.{rb,#{RbConfig::CONFIG['DLEXT']}}", base: path)
+ rescue Errno::ENOENT
+ []
+ end
+ }.uniq.map { |path|
+ path.sub(/\.(rb|#{RbConfig::CONFIG['DLEXT']})\z/, '')
+ }
+ end
+
+ def self.retrieve_files_to_require_relative_from_current_dir
+ @@files_from_current_dir ||= Dir.glob("**/*.{rb,#{RbConfig::CONFIG['DLEXT']}}", base: '.').map { |path|
+ path.sub(/\.(rb|#{RbConfig::CONFIG['DLEXT']})\z/, '')
+ }
+ end
+
CompletionRequireProc = lambda { |target, preposing = nil, postposing = nil|
if target =~ /\A(['"])([^'"]+)\Z/
quote = $1
@@ -55,26 +73,17 @@ module IRB
break
end
end
+ result = []
if tok && tok.event == :on_ident && tok.state == Ripper::EXPR_CMDARG
case tok.tok
when 'require'
- result = $LOAD_PATH.flat_map { |path|
- begin
- Dir.glob("**/*.{rb,#{RbConfig::CONFIG['DLEXT']}}", base: path)
- rescue Errno::ENOENT
- []
- end
- }.uniq.map { |path|
- path.sub(/\.(rb|#{RbConfig::CONFIG['DLEXT']})\z/, '')
- }.select { |path|
+ result = retrieve_files_to_require_from_load_path.select { |path|
path.start_with?(actual_target)
}.map { |path|
quote + path
}
when 'require_relative'
- result = Dir.glob("**/*.{rb,#{RbConfig::CONFIG['DLEXT']}}", base: '.').map { |path|
- path.sub(/\.(rb|#{RbConfig::CONFIG['DLEXT']})\z/, '')
- }.select { |path|
+ result = retrieve_files_to_require_relative_from_current_dir.select { |path|
path.start_with?(actual_target)
}.map { |path|
quote + path