aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/irb/completion.rb33
-rw-r--r--test/irb/test_completion.rb12
2 files changed, 33 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
diff --git a/test/irb/test_completion.rb b/test/irb/test_completion.rb
index 04ac6dba5b..535690ae22 100644
--- a/test/irb/test_completion.rb
+++ b/test/irb/test_completion.rb
@@ -61,6 +61,11 @@ module TestIRB
%w['irb/init 'irb/ruby-lex].each do |word|
assert_include candidates, word
end
+ # Test cache
+ candidates = IRB::InputCompletor::CompletionProc.("'irb", "require ", "")
+ %w['irb/init 'irb/ruby-lex].each do |word|
+ assert_include candidates, word
+ end
end
def test_complete_require_relative
@@ -70,6 +75,13 @@ module TestIRB
%w['lib/irb/init 'lib/irb/ruby-lex].each do |word|
assert_include candidates, word
end
+ # Test cache
+ candidates = Dir.chdir(__dir__ + "/../..") do
+ IRB::InputCompletor::CompletionProc.("'lib/irb", "require_relative ", "")
+ end
+ %w['lib/irb/init 'lib/irb/ruby-lex].each do |word|
+ assert_include candidates, word
+ end
end
end
end