Skip to content

Commit 612ebcb

Browse files
committed
Cache completion files to require
1 parent 527ffde commit 612ebcb

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

lib/irb/completion.rb

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ def defined? do
4040

4141
BASIC_WORD_BREAK_CHARACTERS = " \t\n`><=;|&{("
4242

43+
def self.retrieve_files_to_require_from_load_path
44+
@@files_from_load_path ||= $LOAD_PATH.flat_map { |path|
45+
begin
46+
Dir.glob("**/*.{rb,#{RbConfig::CONFIG['DLEXT']}}", base: path)
47+
rescue Errno::ENOENT
48+
[]
49+
end
50+
}.uniq.map { |path|
51+
path.sub(/\.(rb|#{RbConfig::CONFIG['DLEXT']})\z/, '')
52+
}
53+
end
54+
55+
def self.retrieve_files_to_require_relative_from_current_dir
56+
@@files_from_current_dir ||= Dir.glob("**/*.{rb,#{RbConfig::CONFIG['DLEXT']}}", base: '.').map { |path|
57+
path.sub(/\.(rb|#{RbConfig::CONFIG['DLEXT']})\z/, '')
58+
}
59+
end
60+
4361
CompletionRequireProc = lambda { |target, preposing = nil, postposing = nil|
4462
if target =~ /\A(['"])([^'"]+)\Z/
4563
quote = $1
@@ -55,26 +73,17 @@ def defined? do
5573
break
5674
end
5775
end
76+
result = []
5877
if tok && tok.event == :on_ident && tok.state == Ripper::EXPR_CMDARG
5978
case tok.tok
6079
when 'require'
61-
result = $LOAD_PATH.flat_map { |path|
62-
begin
63-
Dir.glob("**/*.{rb,#{RbConfig::CONFIG['DLEXT']}}", base: path)
64-
rescue Errno::ENOENT
65-
[]
66-
end
67-
}.uniq.map { |path|
68-
path.sub(/\.(rb|#{RbConfig::CONFIG['DLEXT']})\z/, '')
69-
}.select { |path|
80+
result = retrieve_files_to_require_from_load_path.select { |path|
7081
path.start_with?(actual_target)
7182
}.map { |path|
7283
quote + path
7384
}
7485
when 'require_relative'
75-
result = Dir.glob("**/*.{rb,#{RbConfig::CONFIG['DLEXT']}}", base: '.').map { |path|
76-
path.sub(/\.(rb|#{RbConfig::CONFIG['DLEXT']})\z/, '')
77-
}.select { |path|
86+
result = retrieve_files_to_require_relative_from_current_dir.select { |path|
7887
path.start_with?(actual_target)
7988
}.map { |path|
8089
quote + path

test/irb/test_completion.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ def test_complete_require
6161
%w['irb/init 'irb/ruby-lex].each do |word|
6262
assert_include candidates, word
6363
end
64+
# Test cache
65+
candidates = IRB::InputCompletor::CompletionProc.("'irb", "require ", "")
66+
%w['irb/init 'irb/ruby-lex].each do |word|
67+
assert_include candidates, word
68+
end
6469
end
6570

6671
def test_complete_require_relative
@@ -70,6 +75,13 @@ def test_complete_require_relative
7075
%w['lib/irb/init 'lib/irb/ruby-lex].each do |word|
7176
assert_include candidates, word
7277
end
78+
# Test cache
79+
candidates = Dir.chdir(__dir__ + "/../..") do
80+
IRB::InputCompletor::CompletionProc.("'lib/irb", "require_relative ", "")
81+
end
82+
%w['lib/irb/init 'lib/irb/ruby-lex].each do |word|
83+
assert_include candidates, word
84+
end
7385
end
7486
end
7587
end

0 commit comments

Comments
 (0)