mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Fuzzy-filter scores basename matches higher.
Idea stolen from github/github https://github.com/github/github/blob/master/app/assets/javascripts/github/tree_finder.js
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
stringScore = require 'stringscore'
|
||||
|
||||
module.exports = (candidates, query, options) ->
|
||||
module.exports = (candidates, query, options={}) ->
|
||||
if query
|
||||
scoredCandidates = candidates.map (candidate) ->
|
||||
string = if options.key? then candidate[options.key] else candidate
|
||||
{ candidate, score: stringScore(string, query) }
|
||||
score = stringScore(string, query)
|
||||
|
||||
# Basename matches count for more.
|
||||
unless /\//.test(query)
|
||||
score += stringScore(candidate.replace(/^.*\//,''), query)
|
||||
|
||||
{ candidate, score }
|
||||
|
||||
scoredCandidates.sort (a, b) ->
|
||||
if a.score > b.score then -1
|
||||
@@ -13,4 +19,4 @@ module.exports = (candidates, query, options) ->
|
||||
candidates = (scoredCandidate.candidate for scoredCandidate in scoredCandidates when scoredCandidate.score > 0)
|
||||
|
||||
candidates = candidates[0...options.maxResults] if options.maxResults?
|
||||
candidates
|
||||
candidates
|
||||
Reference in New Issue
Block a user