This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathcommand-palette-view.coffee
134 lines (104 loc) · 4.04 KB
/
command-palette-view.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
_ = 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: true
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'
cancelled: -> @hide()
toggle: ->
if @panel?.isVisible()
@cancel()
else
@show()
show: ->
@panel ?= atom.workspace.addModalPanel(item: this)
@panel.show()
@storeFocusedElement()
if @previouslyFocusedElement[0] and @previouslyFocusedElement[0] isnt document.body
@eventElement = @previouslyFocusedElement[0]
else
@eventElement = atom.views.getView(atom.workspace)
@keyBindings = atom.keymaps.findKeyBindings(target: @eventElement)
commands = atom.commands.findCommands(target: @eventElement)
commands = _.sortBy(commands, 'displayName')
@setItems(commands)
@focusFilterEditor()
hide: ->
@panel?.hide()
viewForItem: ({name, displayName, eventDescription}) ->
keyBindings = @keyBindings
# Style matched characters in search results
filterQuery = @getFilterQuery()
if @alternateScoring
matches = fuzzaldrinPlus.match(displayName, filterQuery)
else
matches = match(displayName, filterQuery)
$$ ->
highlighter = (command, matches, offsetIndex) =>
lastIndex = 0
matchedChars = [] # Build up a set of matched chars to be more semantic
for matchIndex in matches
matchIndex -= offsetIndex
continue if matchIndex < 0 # If marking up the basename, omit command matches
unmatched = command.substring(lastIndex, matchIndex)
if unmatched
@span matchedChars.join(''), class: 'character-match' if matchedChars.length
matchedChars = []
@text unmatched
matchedChars.push(command[matchIndex])
lastIndex = matchIndex + 1
@span matchedChars.join(''), class: 'character-match' if matchedChars.length
# Remaining characters are plain text
@text command.substring(lastIndex)
@li class: 'event', 'data-event-name': name, =>
@div class: 'pull-right', =>
for binding in keyBindings when binding.command is name
@kbd _.humanizeKeystroke(binding.keystrokes), class: 'key-binding'
@span title: name, -> highlighter(displayName, matches, 0)
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))