Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Optionally allow to use some improvement to fuzzaldrin. #142

Merged
merged 2 commits into from
Sep 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 55 additions & 4 deletions lib/fuzzy-finder-view.coffee
Original file line number Diff line number Diff line change
@@ -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'
fuzzaldrinPlus = 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': =>
Expand All @@ -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'

Expand All @@ -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 = fuzzaldrinPlus.match(projectRelativePath, filterQuery)
else
matches = fuzzaldrin.match(projectRelativePath, filterQuery)

$$ ->

Expand Down Expand Up @@ -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 = fuzzaldrinPlus.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'))
Expand Down
4 changes: 4 additions & 0 deletions lib/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down