From 5e5375528b456c38e38a13e88e70bab973f7ef72 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 17 Oct 2013 09:38:41 -0700 Subject: [PATCH 1/2] Replace stringscore/fuzzy-filter with fuzzaldrin --- exports/atom.coffee | 1 - package.json | 1 + src/fuzzy-filter.coffee | 27 --------- src/select-list.coffee | 2 +- vendor/stringscore.js | 118 ---------------------------------------- 5 files changed, 2 insertions(+), 147 deletions(-) delete mode 100644 src/fuzzy-filter.coffee delete mode 100644 vendor/stringscore.js diff --git a/exports/atom.coffee b/exports/atom.coffee index 3b9537007..0324e933a 100644 --- a/exports/atom.coffee +++ b/exports/atom.coffee @@ -16,7 +16,6 @@ module.exports = Point: Point Range: Range Site: Site - stringscore: require '../vendor/stringscore' # The following classes can't be used from a Task handler and should therefore # only be exported when not running as a child node process diff --git a/package.json b/package.json index 47a75378b..2ded5af18 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "coffeestack": "0.6.0", "emissary": "0.6.0", "first-mate": "0.4.0", + "fuzzaldrin": "0.1.0", "git-utils": "0.26.0", "guid": "0.0.10", "jasmine-focused": "~0.15.0", diff --git a/src/fuzzy-filter.coffee b/src/fuzzy-filter.coffee deleted file mode 100644 index 1288eb6a9..000000000 --- a/src/fuzzy-filter.coffee +++ /dev/null @@ -1,27 +0,0 @@ -stringScore = require '../vendor/stringscore' -path = require 'path' - -module.exports = (candidates, query, options={}) -> - if query - scoredCandidates = candidates.map (candidate) -> - string = if options.key? then candidate[options.key] else candidate - score = stringScore(string, query) - - unless /\//.test(query) - # Basename matches count for more. - score += stringScore(path.basename(string), query) - - # Shallow files are scored higher - depth = Math.max(1, 10 - string.split('/').length - 1) - score *= depth * 0.01 - - { candidate, score } - - scoredCandidates.sort (a, b) -> - if a.score > b.score then -1 - else if a.score < b.score then 1 - else 0 - candidates = (scoredCandidate.candidate for scoredCandidate in scoredCandidates when scoredCandidate.score > 0) - - candidates = candidates[0...options.maxResults] if options.maxResults? - candidates diff --git a/src/select-list.coffee b/src/select-list.coffee index d669b78ff..78a305945 100644 --- a/src/select-list.coffee +++ b/src/select-list.coffee @@ -1,6 +1,6 @@ {$, View} = require './space-pen-extensions' Editor = require './editor' -fuzzyFilter = require './fuzzy-filter' +fuzzyFilter = require('fuzzaldrin').filter # Public: Provides a widget for users to make a selection from a list of # choices. diff --git a/vendor/stringscore.js b/vendor/stringscore.js deleted file mode 100644 index 023460407..000000000 --- a/vendor/stringscore.js +++ /dev/null @@ -1,118 +0,0 @@ -// MODIFIED BY NS/CJ - Don't extend the prototype of String -// MODIFIED BY CJ - Remove start_of_string_bonus - -/*! - * string_score.js: String Scoring Algorithm 0.1.10 - * - * http://joshaven.com/string_score - * https://github.com/joshaven/string_score - * - * Copyright (C) 2009-2011 Joshaven Potter - * Special thanks to all of the contributors listed here https://github.com/joshaven/string_score - * MIT license: http://www.opensource.org/licenses/mit-license.php - * - * Date: Tue Mar 1 2011 -*/ - -/** - * Scores a string against another string. - * 'Hello World'.score('he'); //=> 0.5931818181818181 - * 'Hello World'.score('Hello'); //=> 0.7318181818181818 - */ -module.exports = function(string, abbreviation, fuzziness) { - // If the string is equal to the abbreviation, perfect match. - if (string == abbreviation) {return 1;} - // If it's not a perfect match and is empty return 0 - if (abbreviation == "") {return 0;} - - var total_character_score = 0, - abbreviation_length = abbreviation.length, - string_length = string.length, - start_of_string_bonus, - abbreviation_score, - fuzzies=1, - final_score; - - // Walk through abbreviation and add up scores. - for (var i = 0, - character_score/* = 0*/, - index_in_string/* = 0*/, - c/* = ''*/, - index_c_lowercase/* = 0*/, - index_c_uppercase/* = 0*/, - min_index/* = 0*/; - i < abbreviation_length; - ++i) { - - // Find the first case-insensitive match of a character. - c = abbreviation.charAt(i); - - index_c_lowercase = string.indexOf(c.toLowerCase()); - index_c_uppercase = string.indexOf(c.toUpperCase()); - min_index = Math.min(index_c_lowercase, index_c_uppercase); - index_in_string = (min_index > -1) ? min_index : Math.max(index_c_lowercase, index_c_uppercase); - - if (index_in_string === -1) { - if (fuzziness) { - fuzzies += 1-fuzziness; - continue; - } else { - return 0; - } - } else { - character_score = 0.1; - } - - // Set base score for matching 'c'. - - // Same case bonus. - if (string[index_in_string] === c) { - character_score += 0.1; - } - - // Consecutive letter & start-of-string Bonus - if (index_in_string === 0) { - // Increase the score when matching first character of the remainder of the string - character_score += 0.6; - if (i === 0) { - // If match is the first character of the string - // & the first character of abbreviation, add a - // start-of-string match bonus. - // start_of_string_bonus = 1 //true; - } - } - else { - // Acronym Bonus - // Weighing Logic: Typing the first character of an acronym is as if you - // preceded it with two perfect character matches. - if (string.charAt(index_in_string - 1) === ' ') { - character_score += 0.8; // * Math.min(index_in_string, 5); // Cap bonus at 0.4 * 5 - } - } - - // Left trim the already matched part of the string - // (forces sequential matching). - string = string.substring(index_in_string + 1, string_length); - - total_character_score += character_score; - } // end of for loop - - // Uncomment to weigh smaller words higher. - // return total_character_score / string_length; - - abbreviation_score = total_character_score / abbreviation_length; - //percentage_of_matched_string = abbreviation_length / string_length; - //word_score = abbreviation_score * percentage_of_matched_string; - - // Reduce penalty for longer strings. - //final_score = (word_score + abbreviation_score) / 2; - final_score = ((abbreviation_score * (abbreviation_length / string_length)) + abbreviation_score) / 2; - - final_score = final_score / fuzzies; - - if (start_of_string_bonus && (final_score + 0.15 < 1)) { - final_score += 0.15; - } - - return final_score; -}; From 9b84271fee25816bef5aefda961ce57b9ad2fa7d Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 17 Oct 2013 09:43:24 -0700 Subject: [PATCH 2/2] Upgrade to settings-view@0.29.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2ded5af18..69a85bc4b 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,7 @@ "metrics": "0.8.0", "package-generator": "0.12.0", "release-notes": "0.4.0", - "settings-view": "0.28.0", + "settings-view": "0.29.0", "snippets": "0.9.0", "spell-check": "0.7.0", "status-bar": "0.14.0",