Skip to content

Commit

Permalink
Merge pull request #284 from aycabta/use-gem-paths-to-complete-require
Browse files Browse the repository at this point in the history
Use gem paths to complete "require"
  • Loading branch information
aycabta committed Sep 7, 2021
2 parents 9ff0b1b + 49203a1 commit 16d3e59
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
36 changes: 32 additions & 4 deletions lib/irb/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,44 @@ def defined? do

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

def self.absolute_path?(p) # TODO Remove this method after 2.6 EOL.
if File.respond_to?(:absolute_path?)
File.absolute_path?(p)
else
if File.absolute_path(p) == p
true
else
false
end
end
end

def self.retrieve_gem_and_system_load_path
gem_paths = Gem::Specification.latest_specs(true).map { |s|
s.require_paths.map { |p|
if absolute_path?(p)
p
else
File.join(s.full_gem_path, p)
end
}
}.flatten
(gem_paths + $LOAD_PATH).uniq.sort
end

def self.retrieve_files_to_require_from_load_path
@@files_from_load_path ||= $LOAD_PATH.flat_map { |path|
@@files_from_load_path ||= retrieve_gem_and_system_load_path.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/, '')
}
}.inject([]) { |result, names|
shortest, *rest = names.map{ |n| n.sub(/\.(rb|#{RbConfig::CONFIG['DLEXT']})\z/, '') }.sort
result.unshift(shortest) if shortest
result.concat(rest)
result
}.uniq
end

def self.retrieve_files_to_require_relative_from_current_dir
Expand Down
5 changes: 5 additions & 0 deletions test/irb/test_completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def test_complete_require
end
end

def test_complete_require_library_name_first
candidates = IRB::InputCompletor::CompletionProc.("'csv", "require ", "")
assert_equal candidates.first, "'csv"
end

def test_complete_require_relative
candidates = Dir.chdir(__dir__ + "/../..") do
IRB::InputCompletor::CompletionProc.("'lib/irb", "require_relative ", "")
Expand Down

0 comments on commit 16d3e59

Please sign in to comment.