From f63a52fab0a6626805d1e9656ada85f4d39c8e0e Mon Sep 17 00:00:00 2001 From: jc roy Date: Thu, 24 Sep 2015 00:16:12 -0400 Subject: [PATCH 1/2] Add support to use fuzzaldrin-plus as an option disabled by default. --- lib/fuzzy-finder-view.coffee | 59 +++++++++++++++++++++++++++++++++--- lib/main.coffee | 4 +++ package.json | 1 + 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/lib/fuzzy-finder-view.coffee b/lib/fuzzy-finder-view.coffee index 6245368f..f079ed04 100644 --- a/lib/fuzzy-finder-view.coffee +++ b/lib/fuzzy-finder-view.coffee @@ -1,20 +1,24 @@ path = require 'path' -{Point} = require 'atom' -{$$, SelectListView} = require 'atom-space-pen-views' +{Point, CompositeDisposable} = require 'atom' +{$, $$, SelectListView} = require 'atom-space-pen-views' {repositoryForPath} = require './helpers' fs = require 'fs-plus' -{match} = require 'fuzzaldrin' +fuzzaldrin = require 'fuzzaldrin' +fuzzaldrin_plus = require 'fuzzaldrin-plus' module.exports = class FuzzyFinderView extends SelectListView filePaths: null projectRelativePaths: null + subscriptions: null + alternateScoring: false initialize: -> super @addClass('fuzzy-finder') @setMaxItems(10) + @subscriptions = new CompositeDisposable atom.commands.add @element, 'pane:split-left': => @@ -28,6 +32,10 @@ class FuzzyFinderView extends SelectListView 'fuzzy-finder:invert-confirm': => @confirmInvertedSelection() + @alternateScoring = atom.config.get 'fuzzy-finder.useAlternateScoring' + @subscriptions.add atom.config.onDidChange 'fuzzy-finder.useAlternateScoring', ({newValue}) => @alternateScoring = newValue + + getFilterKey: -> 'projectRelativePath' @@ -44,12 +52,18 @@ class FuzzyFinderView extends SelectListView destroy: -> @cancel() @panel?.destroy() + @subscriptions?.dispose() + @subscriptions = null viewForItem: ({filePath, projectRelativePath}) -> # Style matched characters in search results filterQuery = @getFilterQuery() - matches = match(projectRelativePath, filterQuery) + + if @alternateScoring + matches = fuzzaldrin_plus.match(projectRelativePath, filterQuery) + else + matches = fuzzaldrin.match(projectRelativePath, filterQuery) $$ -> @@ -136,9 +150,46 @@ class FuzzyFinderView extends SelectListView if @isQueryALineJump() @list.empty() @setError('Jump to line in active editor') + else if @alternateScoring + @populateAlternateList() else super + + # Unfortunately SelectListView do not allow inheritor to handle their own filtering. + # That would be required to use external knowledge, for example: give a bonus to recent files. + # + # Or, in this case: test an alternate scoring algorithm. + # + # This is modified copy/paste from SelectListView#populateList, require jQuery! + # Should be temporary + + populateAlternateList: -> + + return unless @items? + + filterQuery = @getFilterQuery() + if filterQuery.length + filteredItems = fuzzaldrin_plus.filter(@items, filterQuery, key: @getFilterKey()) + else + filteredItems = @items + + @list.empty() + if filteredItems.length + @setError(null) + + for i in [0...Math.min(filteredItems.length, @maxItems)] + item = filteredItems[i] + itemView = $(@viewForItem(item)) + itemView.data('select-list-item', item) + @list.append(itemView) + + @selectItemView(@list.find('li:first')) + else + @setError(@getEmptyMessage(@items.length, filteredItems.length)) + + + confirmSelection: -> item = @getSelectedItem() @confirmed(item, searchAllPanes: atom.config.get('fuzzy-finder.searchAllPanes')) diff --git a/lib/main.coffee b/lib/main.coffee index 66f3f5cc..dbb3342c 100644 --- a/lib/main.coffee +++ b/lib/main.coffee @@ -10,6 +10,10 @@ module.exports = preserveLastSearch: type: 'boolean' default: false + useAlternateScoring: + description: "Prefers run of consecutive characters, acronyms and start of words. (Experimental)" + type: 'boolean' + default: false activate: (state) -> @active = true diff --git a/package.json b/package.json index 08ec1f4f..e70b6f8b 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "atom-space-pen-views": "^2.0.0", "fs-plus": "^2.0.0", "fuzzaldrin": "^2.0", + "fuzzaldrin-plus": "^0.1", "humanize-plus": "~1.4.2", "minimatch": "~0.3.0", "temp": "~0.8.1", From 866b4d0a3a80c608a23f33a010bad231ffe3687f Mon Sep 17 00:00:00 2001 From: jc roy Date: Thu, 24 Sep 2015 17:07:18 -0400 Subject: [PATCH 2/2] snake_case -> camelCase --- lib/fuzzy-finder-view.coffee | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/fuzzy-finder-view.coffee b/lib/fuzzy-finder-view.coffee index f079ed04..800e049c 100644 --- a/lib/fuzzy-finder-view.coffee +++ b/lib/fuzzy-finder-view.coffee @@ -4,7 +4,7 @@ path = require 'path' {repositoryForPath} = require './helpers' fs = require 'fs-plus' fuzzaldrin = require 'fuzzaldrin' -fuzzaldrin_plus = require 'fuzzaldrin-plus' +fuzzaldrinPlus = require 'fuzzaldrin-plus' module.exports = class FuzzyFinderView extends SelectListView @@ -61,7 +61,7 @@ class FuzzyFinderView extends SelectListView filterQuery = @getFilterQuery() if @alternateScoring - matches = fuzzaldrin_plus.match(projectRelativePath, filterQuery) + matches = fuzzaldrinPlus.match(projectRelativePath, filterQuery) else matches = fuzzaldrin.match(projectRelativePath, filterQuery) @@ -170,7 +170,7 @@ class FuzzyFinderView extends SelectListView filterQuery = @getFilterQuery() if filterQuery.length - filteredItems = fuzzaldrin_plus.filter(@items, filterQuery, key: @getFilterKey()) + filteredItems = fuzzaldrinPlus.filter(@items, filterQuery, key: @getFilterKey()) else filteredItems = @items