This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathFileFilters.js
More file actions
522 lines (453 loc) · 21.5 KB
/
FileFilters.js
File metadata and controls
522 lines (453 loc) · 21.5 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
* Copyright (c) 2014 Adobe Systems Incorporated. All rights reserved.
*
* 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, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, window, Mustache */
/**
* Utilities for managing file-set filters, as used in Find in Files.
* Includes both UI for selecting/editing filters, as well as the actual file-filtering implementation.
*/
define(function (require, exports, module) {
"use strict";
var _ = require("thirdparty/lodash"),
ProjectManager = require("project/ProjectManager"),
DefaultDialogs = require("widgets/DefaultDialogs"),
Dialogs = require("widgets/Dialogs"),
DropdownButton = require("widgets/DropdownButton").DropdownButton,
StringUtils = require("utils/StringUtils"),
Strings = require("strings"),
PreferencesManager = require("preferences/PreferencesManager"),
EditFilterTemplate = require("text!htmlContent/edit-filter-dialog.html"),
FilterNameTemplate = require("text!htmlContent/filter-name.html");
/** @type {number} Constant: first filter index in the filter dropdown list */
var FIRST_FILTER_INDEX = 3;
/** @type {?{label:string, promise:$.Promise}} context Info on which files the filter will be applied to.
* It will be initialized when createFilterPicker is called and if specified, editing UI will
* indicate how many files are excluded by the filter. Label should be of the form "in ..."
*/
var _context = null;
/** @type {DropdownButton} */
var _picker = null;
/**
* Populate the list of dropdown menu with two filter commands and
* the list of saved filter sets.
*/
function _doPopulate() {
var dropdownItems = [Strings.NEW_FILE_FILTER, Strings.CLEAR_FILE_FILTER],
filterSets = PreferencesManager.get("fileFilters") || [];
if (filterSets.length) {
dropdownItems.push("---");
// FIRST_FILTER_INDEX needs to stay in sync with the number of static items (plus separator)
// ie. the number of items populated so far before we concatenate with the actual filter sets.
dropdownItems = dropdownItems.concat(filterSets);
}
_picker.items = dropdownItems;
}
/**
* Find the index of a filter set in the list of saved filter sets.
* @param {Array.<{name: string, patterns: Array.<string>}>} filterSets
* @param {{name: string, patterns: Array.<string>}} filter
*/
function _getFilterIndex(filterSets, filter) {
var index = -1,
found = false;
if (!filter || !filterSets.length) {
return index;
}
return _.findIndex(filterSets, _.partial(_.isEqual, filter));
}
/**
* A search filter is an array of one or more glob strings. The filter must be 'compiled' via compile()
* before passing to filterPath()/filterFileList().
* @return {?{name: string, patterns: Array.<string>}}
*/
function getActiveFilter() {
var filterSets = PreferencesManager.get("fileFilters") || [],
activeFilterIndex = PreferencesManager.getViewState("activeFileFilter"),
oldFilter = PreferencesManager.getViewState("search.exclusions") || [],
activeFilter = null;
if (activeFilterIndex === undefined && oldFilter.length) {
activeFilter = { name: "", patterns: oldFilter };
activeFilterIndex = _getFilterIndex(filterSets, activeFilter);
// Migrate the old filter into the new filter storage
if (activeFilterIndex === -1) {
activeFilterIndex = filterSets.length;
filterSets.push(activeFilter);
PreferencesManager.set("fileFilters", filterSets);
}
PreferencesManager.setViewState("activeFileFilter", activeFilterIndex);
} else if (activeFilterIndex > -1 && activeFilterIndex < filterSets.length) {
activeFilter = filterSets[activeFilterIndex];
}
return activeFilter;
}
/**
* Get the condensed form of the filter set by joining the first two in the set with
* a comma separator and appending a short message with the number of filters being clipped.
* @param {Array.<string>} filter
* @param {string} condensed form of filter set
*/
function _getCondensedForm(filter) {
// Format filter in condensed form
if (filter.length > 2) {
return filter.slice(0, 2).join(", ") + " " +
StringUtils.format(Strings.FILE_FILTER_CLIPPED_SUFFIX, filter.length - 2);
} else {
return filter.join(", ");
}
}
/**
* Update the picker button label with the name/patterns of the selected filter or
* No Files Excluded if no filter is selected.
*/
function _updatePicker() {
var filter = getActiveFilter();
if (filter && filter.patterns.length) {
var label = filter.name || _getCondensedForm(filter.patterns);
_picker.setButtonLabel(StringUtils.format(Strings.EXCLUDE_FILE_FILTER, label));
} else {
_picker.setButtonLabel(Strings.NO_FILE_FILTER);
}
}
/**
* Sets and save the index of the active filter. Automatically set when editFilter() is completed.
* If no filter is passed in, then clear the last active filter index by setting it to -1.
*
* @param {{name: string, patterns: Array.<string>}=} filter
* @param {number=} index The index of the filter set in the list of saved filter sets or -1 if it is a new one
*/
function setActiveFilter(filter, index) {
var filterSets = PreferencesManager.get("fileFilters") || [];
if (filter) {
if (index === -1) {
// Add a new filter set
index = filterSets.length;
filterSets.push(filter);
} else if (index > -1 && index < filterSets.length) {
// Update an existing filter set only if the filter set has some changes
if (!_.isEqual(filterSets[index], filter)) {
filterSets[index] = filter;
}
} else {
// Should not have been called with an invalid index to the available filter sets.
console.log("setActiveFilter is called with an invalid index: " + index);
return;
}
PreferencesManager.set("fileFilters", filterSets);
PreferencesManager.setViewState("activeFileFilter", index);
} else {
// Explicitly set to -1 to remove the active file filter
PreferencesManager.setViewState("activeFileFilter", -1);
}
}
/**
* Converts a user-specified filter object (as chosen in picker or retrieved from getFilters()) to a 'compiled' form
* that can be used with filterPath()/filterFileList().
* @param {!Array.<string>} userFilter
* @return {!string} 'compiled' filter that can be passed to filterPath()/filterFileList().
*/
function compile(userFilter) {
// Automatically apply ** prefix/suffix to make writing simple substring-match filters more intuitive
var wrappedGlobs = userFilter.map(function (glob) {
// Automatic "**" prefix if not explicitly present
if (glob.substr(0, 2) !== "**") {
glob = "**" + glob;
}
// Automatic "**" suffix if not explicitly present and no "." in last path segment of filter string
if (glob.substr(-2, 2) !== "**") {
var lastSeg = glob.lastIndexOf("/");
if (glob.indexOf(".", lastSeg + 1) === -1) { // if no "/" present, this treats whole string as 'last segment'
glob += "**";
}
}
return glob;
});
// Convert to regular expression for fast matching
var regexStrings = wrappedGlobs.map(function (glob) {
var reStr = "", i;
for (i = 0; i < glob.length; i++) {
var ch = glob[i];
if (ch === "*") {
// Check for `**`
if (glob[i + 1] === "*") {
// Special case: `/**/` can collapse - that is, it shouldn't require matching both slashes
if (glob[i + 2] === "/" && glob[i - 1] === "/") {
reStr += "(.*/)?";
i += 2; // skip 2nd * and / after it
} else {
reStr += ".*";
i++; // skip 2nd *
}
} else {
// Single `*`
reStr += "[^/]*";
}
} else if (ch === "?") {
reStr += "[^/]"; // unlike '?' in regexp, in globs this requires exactly 1 char
} else {
// Regular char with no special meaning
reStr += StringUtils.regexEscape(ch);
}
}
return "^" + reStr + "$";
});
return regexStrings.join("|");
}
/**
* Returns false if the given path matches any of the exclusion globs in the given filter. Returns true
* if the path does not match any of the globs. If filtering many paths at once, use filterFileList()
* for much better performance.
*
* @param {?string} compiledFilter 'Compiled' filter object as returned by compile(), or null to no-op
* @param {!string} fullPath
* @return {boolean}
*/
function filterPath(compiledFilter, fullPath) {
if (!compiledFilter) {
return true;
}
var re = new RegExp(compiledFilter);
return !fullPath.match(re);
}
/**
* Returns a copy of 'files' filtered to just those that don't match any of the exclusion globs in the filter.
*
* @param {?string} compiledFilter 'Compiled' filter object as returned by compile(), or null to no-op
* @param {!Array.<File>} files
* @return {!Array.<File>}
*/
function filterFileList(compiledFilter, files) {
if (!compiledFilter) {
return files;
}
var re = new RegExp(compiledFilter);
return files.filter(function (f) {
return !f.fullPath.match(re);
});
}
/**
* Opens a dialog box to edit the given filter. When editing is finished, the value of getActiveFilter() changes to
* reflect the edits. If the dialog was canceled, the preference is left unchanged.
* @param {!{name: string, patterns: Array.<string>}} filter
* @param {number} index The index of the filter set to be edited or created. The value is -1 if it is for a new one
* to be created.
* @return {!$.Promise} Dialog box promise
*/
function editFilter(filter, index) {
var lastFocus = window.document.activeElement;
var templateVars = {
instruction: StringUtils.format(Strings.FILE_FILTER_INSTRUCTIONS, brackets.config.glob_help_url),
Strings: Strings
};
var dialog = Dialogs.showModalDialogUsingTemplate(Mustache.render(EditFilterTemplate, templateVars)),
$nameField = dialog.getElement().find(".exclusions-name"),
$editField = dialog.getElement().find(".exclusions-editor");
$nameField.val(filter.name);
$editField.val(filter.patterns.join("\n")).focus();
function getValue() {
var newFilter = $editField.val().split("\n");
// Remove blank lines
return newFilter.filter(function (glob) {
return glob.trim().length;
});
}
dialog.done(function (buttonId) {
if (buttonId === Dialogs.DIALOG_BTN_OK) {
// Update saved filter preference
setActiveFilter({ name: $nameField.val(), patterns: getValue() }, index);
_updatePicker();
_doPopulate();
}
lastFocus.focus(); // restore focus to old pos
});
// Code to update the file count readout at bottom of dialog (if context provided)
var $fileCount = dialog.getElement().find(".exclusions-filecount");
function updateFileCount() {
_context.promise.done(function (files) {
var filter = getValue();
if (filter.length) {
var filtered = filterFileList(compile(filter), files);
$fileCount.html(StringUtils.format(Strings.FILTER_FILE_COUNT, filtered.length, files.length, _context.label));
} else {
$fileCount.html(StringUtils.format(Strings.FILTER_FILE_COUNT_ALL, files.length, _context.label));
}
});
}
if (_context) {
$editField.on("input", _.debounce(updateFileCount, 400));
updateFileCount();
} else {
$fileCount.hide();
}
return dialog.getPromise();
}
/**
* Marks the filter picker's currently selected item as most-recently used, and returns the corresponding
* 'compiled' filter object ready for use with filterPath().
* @param {!jQueryObject} picker UI returned from createFilterPicker()
* @return {!string} 'compiled' filter that can be passed to filterPath()/filterFileList().
*/
function commitPicker(picker) {
var filter = getActiveFilter();
return (filter && filter.patterns.length) ? compile(filter.patterns) : "";
}
/**
* Remove the target item from the filter dropdown list and update dropdown button
* and dropdown list UI.
* @param {!Event} e Mouse events
*/
function _handleDeleteFilter(e) {
// Remove the filter set from the preferences and
// clear the active filter set index from view state.
var filterSets = PreferencesManager.get("fileFilters") || [],
activeFilterIndex = PreferencesManager.getViewState("activeFileFilter"),
filterIndex = $(e.target).parent().data("index") - FIRST_FILTER_INDEX;
// Don't let the click bubble upward.
e.stopPropagation();
filterSets.splice(filterIndex, 1);
PreferencesManager.set("fileFilters", filterSets);
if (activeFilterIndex === filterIndex) {
// Removing the active filter, so clear the active filter
// both in the view state.
setActiveFilter(null);
} else if (activeFilterIndex > filterIndex) {
// Adjust the active filter index after the removal of a filter set before it.
--activeFilterIndex;
setActiveFilter(filterSets[activeFilterIndex], activeFilterIndex);
}
_updatePicker();
_doPopulate();
_picker.refresh();
}
/**
* Close filter dropdwon list and launch edit filter dialog.
* @param {!Event} e Mouse events
*/
function _handleEditFilter(e) {
var filterSets = PreferencesManager.get("fileFilters") || [],
filterIndex = $(e.target).parent().data("index") - FIRST_FILTER_INDEX;
// Don't let the click bubble upward.
e.stopPropagation();
// Close the dropdown first before opening the edit filter dialog
// so that it will restore focus to the DOM element that has focus
// prior to opening it.
_picker.closeDropdown();
editFilter(filterSets[filterIndex], filterIndex);
}
/**
* Set up mouse click event listeners for 'Delete' and 'Edit' buttons
* when the dropdown is open. Also set check mark on the active filter.
* @param {!Event>} event listRendered event triggered when the dropdown is open
* @param {!jQueryObject} $dropdown the jQuery DOM node of the dropdown list
*/
function _handleListRendered(event, $dropdown) {
var activeFilterIndex = PreferencesManager.getViewState("activeFileFilter"),
checkedItemIndex = (activeFilterIndex > -1) ? (activeFilterIndex + FIRST_FILTER_INDEX) : -1;
_picker.setChecked(checkedItemIndex, true);
$dropdown.find(".filter-trash-icon")
.on("click", _handleDeleteFilter);
$dropdown.find(".filter-edit-icon")
.on("click", _handleEditFilter);
}
/**
* Creates a UI element for selecting a filter, populated with a list of recently used filters, an option to
* edit the selected filter and another option to create a new filter. The client should call commitDropdown()
* when the UI containing the filter picker is confirmed (which updates the MRU order) and then use the
* returned filter object as needed.
*
* @param {?{label:string, promise:$.Promise}} context Info on files that filter will apply to.
* This will be saved as _context for later use in creating a new filter or editing an
* existing filter in Edit Filter dialog.
* @return {!jQueryObject} Picker UI. To retrieve the selected value, use commitPicker().
*/
function createFilterPicker(context) {
function itemRenderer(item, index) {
if (index < FIRST_FILTER_INDEX) {
// Prefix the two filter commands with 'recent-filter-name' so that
// they also get the same margin-left as the actual filters.
return "<span class='recent-filter-name'></span>" + _.escape(item);
}
var condensedPatterns = _getCondensedForm(item.patterns),
templateVars = {
"filter-name" : _.escape(item.name || condensedPatterns),
"filter-patterns": item.name ? " - " + _.escape(condensedPatterns) : ""
};
return Mustache.render(FilterNameTemplate, templateVars);
}
_context = context;
_picker = new DropdownButton("", [], itemRenderer);
_updatePicker();
_doPopulate();
// Add 'file-filter-picker' to keep some margin space on the left of the button
_picker.$button.addClass("file-filter-picker no-focus");
// Set up mouse click event listeners for 'Delete' and 'Edit' buttons
$(_picker).on("listRendered", _handleListRendered);
$(_picker).on("select", function (event, item, itemIndex) {
if (itemIndex === 0) {
// Close the dropdown first before opening the edit filter dialog
// so that it will restore focus to the DOM element that has focus
// prior to opening it.
_picker.closeDropdown();
// Create a new filter set
editFilter({ name: "", patterns: [] }, -1);
} else if (itemIndex === 1) {
// Uncheck the prior active filter in the dropdown list.
_picker.setChecked(itemIndex, false);
// Clear the active filter
setActiveFilter(null);
_updatePicker();
} else if (itemIndex >= FIRST_FILTER_INDEX && item) {
setActiveFilter(item, itemIndex - FIRST_FILTER_INDEX);
_picker.setChecked(itemIndex, true);
_updatePicker();
}
});
return _picker.$button;
}
/**
* Allows unit tests to open the file filter dropdown list.
*/
function showDropdown() {
if (_picker) {
_picker.showDropdown();
}
}
/**
* Allows unit tests to close the file filter dropdown list.
*/
function closeDropdown() {
if (_picker) {
_picker.closeDropdown();
}
}
// For unit tests only
exports.showDropdown = showDropdown;
exports.closeDropdown = closeDropdown;
exports.createFilterPicker = createFilterPicker;
exports.commitPicker = commitPicker;
exports.getActiveFilter = getActiveFilter;
exports.setActiveFilter = setActiveFilter;
exports.editFilter = editFilter;
exports.compile = compile;
exports.filterPath = filterPath;
exports.filterFileList = filterFileList;
});