-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.js
213 lines (177 loc) · 7.77 KB
/
main.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Copyright (c) 2012 Peter Flynn.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, brackets, $, setTimeout */
define(function (require, exports, module) {
"use strict";
// Brackets modules
var CommandManager = brackets.getModule("command/CommandManager"),
Commands = brackets.getModule("command/Commands"),
KeyBindingManager = brackets.getModule("command/KeyBindingManager"),
Menus = brackets.getModule("command/Menus"),
EditorManager = brackets.getModule("editor/EditorManager"),
QuickOpen = brackets.getModule("search/QuickOpen"),
StringUtils = brackets.getModule("utils/StringUtils");
/** @type {Array.<{ id:string, name:string }>} */
var _commandList;
/**
* Editor that should be focused when executing the command (that had focus before opening search bar)
* @type {Editor}
*/
var whichEditor;
function ensureCommandList() {
if (_commandList) {
return;
}
_commandList = [];
// We don't know which of these commands have no required arguments. But we can safely
// assume that any commands attached to menus or keyboard shortcuts fit the bill.
var ids = CommandManager.getAll();
// Get list of all top-level menu bar menus
// Ignore context menus since it seems less safe to assume those commands can be run in isolation
var menuIds = $.map(Menus.AppMenuBar, function (menuConstVal, menuConstName) {
return menuConstVal;
});
// Filter command list accordingly
ids.forEach(function (id) {
var noArgsOk = false;
// Does it have a keybinding?
if (KeyBindingManager.getKeyBindings(id).length > 0) {
noArgsOk = true;
} else {
// Is it in the menu bar?
menuIds.forEach(function (menuId) {
var menu = Menus.getMenu(menuId);
var menuItemId = menu && menu._getMenuItemId(id);
if (Menus.getMenuItem(menuItemId)) {
noArgsOk = true;
}
});
}
if (noArgsOk) {
_commandList.push({
id: id,
name: CommandManager.get(id).getName()
// (getName() undefined for CommandManager.registerInternal(), but those commands should have been filtered out above anyway)
});
}
});
}
function done() {
// No cleanup - keep cached list of commands for next invocation
}
/**
* @param {string} query User query/filter string
* @return {Array.<string>} Sorted and filtered results that match the query
*/
function search(query, matcher) {
ensureCommandList();
query = query.substr(1); // lose the "?" prefix
var stringMatch = (matcher && matcher.match) ? matcher.match.bind(matcher) : QuickOpen.stringMatch;
// Filter and rank how good each match is
var filteredList = $.map(_commandList, function (commandInfo) {
// TODO: filter out disabled commands?
var searchResult = stringMatch(commandInfo.name, query);
if (searchResult) {
searchResult.id = commandInfo.id;
}
return searchResult;
});
// Sort based on ranking & basic alphabetical order
QuickOpen.basicMatchSort(filteredList);
return filteredList;
}
/**
* @param {string} query
* @return {boolean} true if this plugin wants to provide results for this query
*/
function match(query) {
if (query.indexOf("?") === 0) {
return true;
}
}
/**
* @param {SearchResult} selectedItem
*/
function itemSelect(selectedItem) {
// Many commands are focus-sensitive, so we have to carefully make sure that focus is restored to
// the (correct) editor before running the command
// First wait for Quick Open to restore focus to the master editor
setTimeout(function () {
// Now set focus on the correct editor (which might be an inline editor)
if (whichEditor) {
whichEditor.focus();
whichEditor = null;
}
// One more timeout to wait for focus to move to that editor
setTimeout(function () {
CommandManager.execute(selectedItem.id);
}, 0);
}, 0);
}
/**
* Similar to default formatting, but with added text showing keybinding
*
* @param {SearchResult} fileEntry
* @param {string} query
* @return {string}
*/
function resultFormatter(item, query) {
var displayName = QuickOpen.highlightMatch(item);
// Show shortcut on right of item
// TODO: display multiple shortcuts
// TODO: display which menu it's in also
// TODO: display checkmark if command.getChecked() is true
var shortcuts = KeyBindingManager.getKeyBindings(item.id);
var shortcut = shortcuts.length ? KeyBindingManager.formatKeyDescriptor(shortcuts[0].displayKey) : "";
return "<li>" + displayName + "<span style='float:right'>" + shortcut + "</span></li>";
}
// Register as a new Quick Open mode
QuickOpen.addQuickOpenPlugin(
{
name: "Commands",
label: "Commands", // ignored before Sprint 34
languageIds: [], // empty array = all file types (Sprint 23+)
fileTypes: [], // (< Sprint 23)
done: done,
search: search,
match: match,
itemFocus: function () {},
itemSelect: itemSelect,
resultsFormatter: resultFormatter
}
);
function beginSearchForCommands() {
whichEditor = EditorManager.getFocusedEditor();
// Begin Quick Open in our search mode
QuickOpen.beginSearch("?");
}
// Register command as shortcut to launch this Quick Open mode
var SEARCH_COMMAND_ID = "pflynn.searchCommands";
CommandManager.register("Search Commands", SEARCH_COMMAND_ID, beginSearchForCommands);
var menu = Menus.getMenu(Menus.AppMenuBar.HELP_MENU);
menu.addMenuDivider(Menus.FIRST);
menu.addMenuItem(SEARCH_COMMAND_ID, [
{key: "Ctrl-Alt-/", displayKey: "Ctrl-Alt-?", platform: "win"},
{key: "Ctrl-Cmd-/", displayKey: "Ctrl-Cmd-?", platform: "mac"}
], Menus.FIRST);
});