Class: Yatte::FileFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/yatte/file_finder.rb

Constant Summary collapse

MAX_RESULTS =
100

Instance Method Summary collapse

Constructor Details

#initialize(root_dir = ".") ⇒ FileFinder

Returns a new instance of FileFinder.



9
10
11
12
13
# File 'lib/yatte/file_finder.rb', line 9

def initialize(root_dir = ".")
  @root_dir = File.expand_path(root_dir)
  @files = nil
  @downcased = nil
end

Instance Method Details

#filter(query) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yatte/file_finder.rb', line 20

def filter(query)
  scan unless @files
  return @files[0, MAX_RESULTS] if query.empty?

  terms = query.downcase.split
  regexes = terms.map { |t| fuzzy_regex(t) }
  matches = []

  @files.each_with_index do |file, i|
    dc = @downcased[i]
    if regexes.all? { |re| re.match?(dc) }
      matches << [file, match_score(dc, terms)]
    end
  end

  matches.sort_by! { |_, score| score }
  matches.first(MAX_RESULTS).map!(&:first)
end

#scanObject



15
16
17
18
# File 'lib/yatte/file_finder.rb', line 15

def scan
  @files = git_ls_files || dir_glob_files
  @downcased = @files.map(&:downcase)
end