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

Commit

Permalink
Merge pull request #60 from jeancroy/master
Browse files Browse the repository at this point in the history
Add fuzzaldrin-plus as an option.
  • Loading branch information
benogle committed Nov 6, 2015
2 parents cae77c6 + 4b3a80b commit b4da689
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
48 changes: 47 additions & 1 deletion lib/command-palette-view.coffee
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
_ = require 'underscore-plus'
{SelectListView, $, $$} = require 'atom-space-pen-views'
{match} = require 'fuzzaldrin'
fuzzaldrinPlus = require 'fuzzaldrin-plus'

module.exports =
class CommandPaletteView extends SelectListView

@config:
useAlternateScoring:
type: 'boolean'
default: false
description: 'Use an alternative scoring approach which prefers run of consecutive characters, acronyms and start of words. (Experimental)'

@activate: ->
view = new CommandPaletteView
@disposable = atom.commands.add 'atom-workspace', 'command-palette:toggle', -> view.toggle()

@deactivate: ->
@disposable.dispose()
@scoreSubscription?.dispose()

keyBindings: null

initialize: ->
super

@addClass('command-palette')
@alternateScoring = atom.config.get 'command-palette.useAlternateScoring'
@scoreSubscription = atom.config.onDidChange 'command-palette.useAlternateScoring', ({newValue}) => @alternateScoring = newValue

getFilterKey: ->
'displayName'
Expand Down Expand Up @@ -54,7 +65,10 @@ class CommandPaletteView extends SelectListView
keyBindings = @keyBindings
# Style matched characters in search results
filterQuery = @getFilterQuery()
matches = match(displayName, filterQuery)
if @alternateScoring
matches = fuzzaldrinPlus.match(displayName, filterQuery)
else
matches = match(displayName, filterQuery)

$$ ->
highlighter = (command, matches, offsetIndex) =>
Expand Down Expand Up @@ -86,3 +100,35 @@ class CommandPaletteView extends SelectListView
confirmed: ({name}) ->
@cancel()
@eventElement.dispatchEvent(new CustomEvent(name, bubbles: true, cancelable: true))

populateList: ->
if @alternateScoring
@populateAlternateList()
else
super

# 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))
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dependencies": {
"atom-space-pen-views": "^2.0.0",
"fuzzaldrin": "^2.1.0",
"fuzzaldrin-plus": "^0.1.0",
"underscore-plus": "^1.0.0"
},
"devDependencies": {
Expand Down

0 comments on commit b4da689

Please sign in to comment.