Skip to content

Commit

Permalink
Cache completion files to require
Browse files Browse the repository at this point in the history
  • Loading branch information
aycabta committed Mar 24, 2021
1 parent 527ffde commit 612ebcb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
33 changes: 21 additions & 12 deletions lib/irb/completion.rb
Expand Up @@ -40,6 +40,24 @@ def defined? do

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
Expand All @@ -55,26 +73,17 @@ def defined? do
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
Expand Down
12 changes: 12 additions & 0 deletions test/irb/test_completion.rb
Expand Up @@ -61,6 +61,11 @@ def test_complete_require
%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
Expand All @@ -70,6 +75,13 @@ def test_complete_require_relative
%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

0 comments on commit 612ebcb

Please sign in to comment.