1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/irb] Cache completion files to require

https://github.com/ruby/irb/commit/612ebcb311
This commit is contained in:
aycabta 2021-03-24 15:33:07 +09:00 committed by git
parent f45bed0a6e
commit ad8842c06d
2 changed files with 33 additions and 12 deletions

View file

@ -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

View file

@ -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