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
297 lines (261 loc) · 12.4 KB
/
FileFilters.js
File metadata and controls
297 lines (261 loc) · 12.4 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
/*
* 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 */
/**
* 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");
/**
* 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 {!Array.<string>}
*/
function getLastFilter() {
return PreferencesManager.getViewState("search.exclusions") || [];
}
/**
* Sets the value of getLastFilter(). Automatically set when editFilter() is completed.
* @param {!Array.<string>} filter
* @return {!Array.<string>}
*/
function setLastFilter(filter) {
PreferencesManager.setViewState("search.exclusions", filter);
}
/**
* 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()
* @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()
* @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 getLastFilter() changes to
* reflect the edits. If the dialog was canceled, the preference is left unchanged.
* @param {!Array.<string>} filter
* @param {?{label:string, promise:$.Promise}} context Info on which files the filter will be applied to. If specified,
* editing UI will indicate how many files are excluded by the filter. Label should be of the form "in ..."
* @return {!$.Promise} Dialog box promise
*/
function editFilter(filter, context) {
var lastFocus = window.document.activeElement;
var html = StringUtils.format(Strings.FILE_FILTER_INSTRUCTIONS, brackets.config.glob_help_url) +
"<textarea class='exclusions-editor'></textarea><div class='exclusions-filecount'>" + Strings.FILTER_COUNTING_FILES + "</div>";
var buttons = [
{ className : Dialogs.DIALOG_BTN_CLASS_NORMAL, id: Dialogs.DIALOG_BTN_CANCEL, text: Strings.CANCEL },
{ className : Dialogs.DIALOG_BTN_CLASS_PRIMARY, id: Dialogs.DIALOG_BTN_OK, text: Strings.OK }
];
var dialog = Dialogs.showModalDialog(DefaultDialogs.DIALOG_ID_INFO, Strings.FILE_FILTER_DIALOG, html, buttons);
var $editField = dialog.getElement().find(".exclusions-editor");
$editField.val(filter.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
setLastFilter(getValue());
}
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(getValue()), 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 = getLastFilter();
return compile(filter);
}
/**
* Creates a UI element for selecting a filter, populated with a list of recently used filters and an option to
* edit the selected filter. The edit option is fully functional, but selecting any other item does nothing. 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 filter will apply to - see editFilter()
* @return {!jQueryObject} Picker UI. To retrieve the selected value, use commitPicker().
*/
function createFilterPicker(contextPromise) {
var $picker = $("<div class='filter-picker'><span class='filter-label'></span><button class='btn no-focus'></button></div>"),
$button = $picker.find("button");
function joinBolded(segments) {
return segments
.map(function (seg) {
return "<strong>" + _.escape(seg) + "</strong>";
})
.join(", ");
}
function itemRenderer(filter) {
// Format filter in condensed form
if (filter.length > 2) {
return Strings.FILE_FILTER_LIST_PREFIX + " " + joinBolded(filter.slice(0, 2)) + " " +
StringUtils.format(Strings.FILE_FILTER_CLIPPED_SUFFIX, filter.length - 2);
} else {
return Strings.FILE_FILTER_LIST_PREFIX + " " + joinBolded(filter);
}
}
function updatePicker() {
var filter = getLastFilter();
if (filter.length) {
$button.text(Strings.EDIT_FILE_FILTER);
$picker.find(".filter-label").html(itemRenderer(filter))
.attr("title", filter.join("\n"));
} else {
$button.text(Strings.NO_FILE_FILTER);
$picker.find(".filter-label").html("").attr("title", "");
}
}
updatePicker();
$button.click(function () {
editFilter(getLastFilter(), contextPromise)
.done(function (buttonId) {
if (buttonId === Dialogs.DIALOG_BTN_OK) {
updatePicker();
}
});
});
return $picker;
}
exports.createFilterPicker = createFilterPicker;
exports.commitPicker = commitPicker;
exports.getLastFilter = getLastFilter;
exports.editFilter = editFilter;
exports.compile = compile;
exports.filterPath = filterPath;
exports.filterFileList = filterFileList;
});