Module: ReplTypeCompletor::RequirePaths

Defined in:
lib/repl_type_completor/require_paths.rb

Class Method Summary collapse

Class Method Details

.require_completions(target_path) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/repl_type_completor/require_paths.rb', line 6

def require_completions(target_path)
  *dir, target = target_path.split('/', -1)
  target ||= ''
  paths = with_cache [:require_completions, dir] do
    gem_and_system_load_paths.flat_map do |load_path|
      reject_prefixes = gem_and_system_load_paths.filter_map do |lp|
        lp.delete_prefix(load_path).delete_prefix('/').delete_suffix('/') + '/' if lp.start_with? load_path
      end.reject(&:empty?)
      base_dir = File.absolute_path(File.join(load_path, *dir))
      with_cache [:requireable_paths, base_dir] do
        requireable_paths(base_dir, reject_prefixes: reject_prefixes)
      end
    end.uniq.sort
  end
  paths.filter_map do |path|
    [*dir, path].join('/') if path.start_with?(target)
  end
end

.require_relative_completions(target_path, source_file) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/repl_type_completor/require_paths.rb', line 25

def require_relative_completions(target_path, source_file)
  source_dir = source_file ? File.dirname(source_file) : Dir.pwd
  *dir, target = target_path.split('/', -1)
  target ||= ''
  base_dir = File.absolute_path(File.join(source_dir, *dir))
  paths = with_cache [:requireable_paths, base_dir] do
    requireable_paths(base_dir)
  end
  paths.filter_map do |path|
    [*dir, path].join('/') if path.start_with?(target)
  end
end