-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathFindInFilesUI.js
More file actions
593 lines (531 loc) · 23.7 KB
/
FindInFilesUI.js
File metadata and controls
593 lines (531 loc) · 23.7 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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2014 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/*
* UI and controller logic for find/replace across multiple files within the project.
*
* FUTURE:
* - Handle matches that span multiple lines
*/
define(function (require, exports, module) {
const AppInit = require("utils/AppInit"),
CommandManager = require("command/CommandManager"),
Commands = require("command/Commands"),
Dialogs = require("widgets/Dialogs"),
DefaultDialogs = require("widgets/DefaultDialogs"),
EditorManager = require("editor/EditorManager"),
WorkspaceManager = require("view/WorkspaceManager"),
FileFilters = require("search/FileFilters"),
FileUtils = require("file/FileUtils"),
FindBar = require("search/FindBar").FindBar,
FindInFiles = require("search/FindInFiles"),
FindUtils = require("search/FindUtils"),
InMemoryFile = require("document/InMemoryFile"),
ProjectManager = require("project/ProjectManager"),
SearchResultsView = require("search/SearchResultsView").SearchResultsView,
TaskManager = require("features/TaskManager"),
Strings = require("strings"),
StringUtils = require("utils/StringUtils"),
Metrics = require("utils/Metrics"),
_ = require("thirdparty/lodash");
let searchTask;
/** @const Maximum number of files to do replacements in-memory instead of on disk. */
var MAX_IN_MEMORY = 20;
/** @type {SearchResultsView} The results view. Initialized in htmlReady() */
var _resultsView = null;
/** @type {FindBar} Find bar containing the search UI. */
var _findBar = null;
/**
* @private
* Forward declaration for JSLint.
* @type {Function}
*/
var _finishReplaceBatch;
function _hideBusyIndicator() {
if(searchTask){
searchTask.close();
searchTask=null;
}
}
function _showBusyIndicator(scope) {
if(searchTask){
searchTask.close();
}
let scopeName = scope ?
Phoenix.app.getDisplayPath(scope.fullPath) :
Phoenix.app.getDisplayPath(ProjectManager.getProjectRoot().fullPath);
scopeName = StringUtils.format(Strings.FIND_IN_FILES_SEARCHING_IN, scopeName);
searchTask = TaskManager.addNewTask(Strings.FIND_IN_FILES_SEARCHING, scopeName,
`<i class="fa-solid fa-magnifying-glass"></i>`);
}
/**
* Does a search in the given scope with the given filter. Shows the result list once the search is complete.
* @param {{query: string, caseSensitive: boolean, isRegexp: boolean}} queryInfo Query info object
* @param {?Entry} scope Project file/subfolder to search within; else searches whole project.
* @param {?string} filter A "compiled" filter as returned by FileFilters.compile(), or null for no filter
* @param {?string} replaceText If this is a replacement, the text to replace matches with.
* @param {?$.Promise} candidateFilesPromise If specified, a promise that should resolve with the same set of files that
* getCandidateFiles(scope) would return.
* @return {$.Promise} A promise that's resolved with the search results or rejected when the find competes.
*/
function searchAndShowResults(queryInfo, scope, filter, replaceText, candidateFilesPromise) {
return FindInFiles.doSearchInScope(queryInfo, scope, filter, replaceText, candidateFilesPromise)
.done(function (zeroFilesToken) {
// Done searching all files: show results
if (FindInFiles.searchModel.hasResults()) {
_resultsView.open();
if (_findBar) {
_findBar.enable(true);
_findBar.showError(null);
_findBar.showNoResults(false);
_findBar.focus();
}
} else {
_resultsView.close();
if (_findBar) {
var showMessage = false;
_findBar.enable(true);
if (zeroFilesToken === FindInFiles.ZERO_FILES_TO_SEARCH) {
_findBar.showError(StringUtils.format(Strings.FIND_IN_FILES_ZERO_FILES,
FindUtils.labelForScope(FindInFiles.searchModel.scope)), true, true);
} else {
showMessage = true;
}
_findBar.showNoResults(true, showMessage);
}
}
_hideBusyIndicator();
})
.fail(function (err) {
console.log("find in files failed: ", err);
_hideBusyIndicator();
});
}
/**
* Does a search in the given scope with the given filter. Replace the result list once the search is complete.
* @param {{query: string, caseSensitive: boolean, isRegexp: boolean}} queryInfo Query info object
* @param {?Entry} scope Project file/subfolder to search within; else searches whole project.
* @param {?string} filter A "compiled" filter as returned by FileFilters.compile(), or null for no filter
* @param {?string} replaceText If this is a replacement, the text to replace matches with.
* @param {?$.Promise} candidateFilesPromise If specified, a promise that should resolve with the same set of files that
* getCandidateFiles(scope) would return.
* @return {$.Promise} A promise that's resolved with the search results or rejected when the find competes.
*/
function searchAndReplaceResults(queryInfo, scope, filter, replaceText, candidateFilesPromise) {
return FindInFiles.doSearchInScope(queryInfo, scope, filter, replaceText, candidateFilesPromise)
.done(function (zeroFilesToken) {
// Done searching all files: replace all
if (FindInFiles.searchModel.hasResults()) {
_finishReplaceBatch(FindInFiles.searchModel);
if (_findBar) {
_findBar.enable(true);
_findBar.focus();
}
}
_hideBusyIndicator();
})
.fail(function (err) {
console.log("replace all failed: ", err);
_hideBusyIndicator();
});
}
function _getScopeLabel(scope, isReplace) {
if (scope) {
const scopeStr = isReplace ?
Strings.FIND_IN_FILES_PROJECT_SCOPE_REPLACE_FILTER :
Strings.FIND_IN_FILES_PROJECT_SCOPE_FILTER;
return StringUtils.format(
scopeStr,
StringUtils.breakableUrl(
ProjectManager.makeProjectRelativeIfPossible(scope.fullPath)
)
);
}
return isReplace ?
Strings.FIND_IN_FILES_PROJECT_SCOPE_REPLACE :
Strings.FIND_IN_FILES_PROJECT_SCOPE;
}
/**
* @private
* Displays a non-modal embedded dialog above the code mirror editor that allows the user to do
* a find operation across all files in the project.
* @param {?Entry} scope Project file/subfolder to search within; else searches whole project.
* @param {boolean=} showReplace If true, show the Replace controls.
*/
function _showFindBar(scope, showReplace) {
FindUtils.notifySearchScopeChanged();
// If the scope is a file with a custom viewer, then we
// don't show find in files dialog.
if (scope && !EditorManager.canOpenPath(scope.fullPath)) {
return;
}
if (scope instanceof InMemoryFile) {
CommandManager.execute(Commands.FILE_OPEN, { fullPath: scope.fullPath }).done(function () {
CommandManager.execute(Commands.CMD_FIND);
});
return;
}
// Get initial query/replace text
let currentEditor = EditorManager.getActiveEditor();
let focussedEditor = EditorManager.getFocusedEditor();
if(!focussedEditor && _resultsView._$previewEditor && _resultsView._$previewEditor.editor
&& _resultsView._$previewEditor.editor.hasFocus()){
currentEditor = _resultsView._$previewEditor.editor;
}
let initialQuery = FindBar.getInitialQuery(_findBar, currentEditor);
// Close our previous find bar, if any. (The open() of the new _findBar will
// take care of closing any other find bar instances.)
if (_findBar) {
_findBar.close();
}
_findBar = new FindBar({
multifile: true,
replace: showReplace,
initialQuery: initialQuery.query,
historyHelp: brackets.platform === "mac" ? Strings.FIND_HISTORY_TOOLTIP_MAC : Strings.FIND_HISTORY_TOOLTIP,
initialReplaceText: initialQuery.replaceText,
queryPlaceholder: Strings.FIND_QUERY_PLACEHOLDER,
scopeLabel: _getScopeLabel(scope, showReplace)
});
_findBar.open();
// TODO Should push this state into ModalBar (via a FindBar API) instead of installing a callback like this.
// Custom closing behavior: if in the middle of executing search, blur shouldn't close ModalBar yet. And
// don't close bar when opening Edit Filter dialog either.
_findBar._modalBar.isLockedOpen = function () {
// TODO: should have state for whether the search is executing instead of looking at find bar state
return !_findBar.isEnabled() || $(".modal.instance .exclusions-editor").length > 0;
};
var candidateFilesPromise = FindInFiles.getCandidateFiles(scope); // used for eventual search, and in exclusions editor UI
function handleQueryChange() {
// Check the query expression on every input event. This way the user is alerted
// to any RegEx syntax errors immediately.
var queryInfo = _findBar.getQueryInfo(),
queryResult = FindUtils.parseQueryInfo(queryInfo);
// Enable the replace button appropriately.
_findBar.enableReplace(queryResult.valid);
if (queryResult.valid || queryResult.empty) {
_findBar.showNoResults(false);
_findBar.showError(null);
} else {
_findBar.showNoResults(true, false);
_findBar.showError(queryResult.error);
}
}
function startSearch(replaceText) {
var queryInfo = _findBar.getQueryInfo(),
disableFindBar = (replaceText ? true : false);
if (queryInfo && queryInfo.query) {
_findBar.enable(!disableFindBar);
_showBusyIndicator(scope);
let queryType = "query";
if (queryInfo.isRegexp) {
queryType = queryType + ":regex";
}
if (queryInfo.isCaseSensitive) {
queryType = queryType + ":caseSensitive";
}
Metrics.countEvent(Metrics.EVENT_TYPE.SEARCH, "findInFiles", queryType);
let filter;
if (_findBar && _findBar._options.multifile) {
filter = FileFilters.getActiveFilter();
} else {
// Single-file scope: don't use any file filters
filter = null;
}
searchAndShowResults(queryInfo, scope, filter, replaceText, candidateFilesPromise);
}
return null;
}
function startReplace() {
startSearch(_findBar.getReplaceText());
}
_findBar
.on("doFind.FindInFiles", function () {
// Subtle issue: we can't just pass startSearch directly as the handler, because
// we don't want it to get the event object as an argument.
startSearch();
})
.on("queryChange.FindInFiles", handleQueryChange)
.on("close.FindInFiles", function (e) {
_findBar.off(".FindInFiles");
_findBar = null;
})
.on("selectNextResult", function () {
if (_findBar && _findBar._options.multifile){
_resultsView.selectNextResult();
}
})
.on("selectPrevResult", function () {
if (_findBar && _findBar._options.multifile){
_resultsView.selectPrevResult();
}
})
.on("selectNextPage", function () {
if (_findBar && _findBar._options.multifile){
_resultsView.selectNextPage();
}
})
.on("selectPrevPage", function () {
if (_findBar && _findBar._options.multifile){
_resultsView.selectPrevPage();
}
})
.on("openSelectedFile", function () {
if (_findBar && _findBar._options.multifile){
_resultsView.OpenSelectedFile();
}
});
if (showReplace) {
// We shouldn't get a "doReplace" in this case, since the Replace button
// is hidden when we set options.multifile.
_findBar.on("doReplaceBatch.FindInFiles", startReplace);
}
var oldModalBarHeight = _findBar._modalBar.height();
// Show file-exclusion UI *unless* search scope is just a single file
if (!scope || scope.isDirectory) {
const filterPicker = FileFilters.createFilterPicker();
_findBar._modalBar.getRoot().find(".scope-group").append(...filterPicker);
}
handleQueryChange();
startSearch();
// Appending FilterPicker and query text can change height of modal bar, so resize editor.
// Preserve scroll position of the current full editor across the editor refresh, adjusting
// for the height of the modal bar so the code doesn't appear to shift if possible.
var fullEditor = EditorManager.getCurrentFullEditor(),
scrollPos;
if (fullEditor) {
scrollPos = fullEditor.getScrollPos();
scrollPos.y -= oldModalBarHeight; // modalbar already showing, adjust for old height
}
WorkspaceManager.recomputeLayout();
if (fullEditor) {
fullEditor._codeMirror.scrollTo(scrollPos.x, scrollPos.y + _findBar._modalBar.height());
}
}
/**
* @private
* Finish a replace across files operation when the user clicks "Replace" on the results panel.
* @param {SearchModel} model The model for the search associated with ths replace.
*/
function _finishReplaceBatch(model) {
var replaceText = model.replaceText;
if (replaceText === null) {
return;
}
// Clone the search results so that they don't get updated in the middle of the replacement.
var resultsClone = _.cloneDeep(model.results),
replacedFiles = Object.keys(resultsClone).filter(function (path) {
return FindUtils.hasCheckedMatches(resultsClone[path]);
}),
isRegexp = model.queryInfo.isRegexp;
function processReplace(forceFilesOpen) {
_showBusyIndicator(model.scope);
FindInFiles.doReplace(resultsClone, replaceText, { forceFilesOpen: forceFilesOpen, isRegexp: isRegexp })
.fail(function (errors) {
var message = Strings.REPLACE_IN_FILES_ERRORS + FileUtils.makeDialogFileList(
errors.map(function (errorInfo) {
return ProjectManager.makeProjectRelativeIfPossible(errorInfo.item);
})
);
Dialogs.showModalDialog(
DefaultDialogs.DIALOG_ID_ERROR,
Strings.REPLACE_IN_FILES_ERRORS_TITLE,
message,
[
{
className: Dialogs.DIALOG_BTN_CLASS_PRIMARY,
id: Dialogs.DIALOG_BTN_OK,
text: Strings.BUTTON_REPLACE_WITHOUT_UNDO
}
]
);
})
.always(function () {
_hideBusyIndicator();
});
}
if (replacedFiles.length <= MAX_IN_MEMORY) {
// Just do the replacements in memory.
_resultsView.close();
processReplace(true);
} else {
Dialogs.showModalDialog(
DefaultDialogs.DIALOG_ID_INFO,
Strings.REPLACE_WITHOUT_UNDO_WARNING_TITLE,
StringUtils.format(Strings.REPLACE_WITHOUT_UNDO_WARNING, MAX_IN_MEMORY),
[
{
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.BUTTON_REPLACE_WITHOUT_UNDO
}
]
)
.done(function (id) {
if (id === Dialogs.DIALOG_BTN_OK) {
_resultsView.close();
processReplace(false);
}
});
}
}
// Command handlers
/**
* @private
* Bring up the Find in Files UI with the replace options.
*/
function _showReplaceBar() {
FindUtils.notifySearchScopeChanged();
_showFindBar(null, true);
}
/**
* @private
* Search within the file/subtree defined by the sidebar selection
*/
function _showFindBarForSubtree() {
FindUtils.notifySearchScopeChanged();
var selectedEntry = ProjectManager.getSelectedItem();
_showFindBar(selectedEntry);
}
/**
* @private
* Search within the file/subtree defined by the sidebar selection
*/
function _showReplaceBarForSubtree() {
FindUtils.notifySearchScopeChanged();
var selectedEntry = ProjectManager.getSelectedItem();
_showFindBar(selectedEntry, true);
}
/**
* @private
* Close the open search bar, if any. For unit tests.
*/
function _closeFindBar(suppressAnimation) {
if (_findBar) {
_findBar.close(suppressAnimation);
}
}
/**
* When the search indexing is started, we need to show the indexing status on the find bar if present.
*/
function _searchIndexingStarted() {
if (_findBar && _findBar._options.multifile && FindUtils.isIndexingInProgress()) {
_findBar.showIndexingSpinner();
}
}
/**
* When the search indexing is started, we need to show the indexing status on the find bar if present.
*/
function _searchIndexingProgressing(_evt, processed, total) {
if (_findBar && _findBar._options.multifile && FindUtils.isIndexingInProgress()) {
let progressMessage = StringUtils.format(Strings.FIND_IN_FILES_INDEXING_PROGRESS, processed, total);
_findBar.setIndexingMessage(progressMessage);
}
}
/**
* Once the indexing has finished, clear the indexing spinner
*/
function _searchIndexingFinished() {
if (_findBar) {
_findBar.hideIndexingSpinner();
}
}
/**
* Issues a search if find bar is visible and is multi file search and not instant search
*/
function _defferedSearch() {
if (_findBar && _findBar._options.multifile && !_findBar._options.replace) {
_findBar.redoInstantSearch();
}
}
/**
* Schedules a search on search scope/filter changes. Have to schedule as when we listen to this event, the file filters
* might not have been updated yet.
*/
function _searchIfRequired() {
if (!FindUtils.isInstantSearchDisabled() && _findBar && _findBar._options.multifile && !_findBar._options.replace) {
setTimeout(_defferedSearch, 100);
}
}
/**
* @public
* Closes the search results panel
*/
function closeResultsPanel() {
_resultsView.close();
_closeFindBar();
}
// Initialize items dependent on HTML DOM
AppInit.htmlReady(function () {
var model = FindInFiles.searchModel;
_resultsView = new SearchResultsView(model, "find-in-files-results", "find-in-files.results",
undefined, Strings.SEARCH_RESULTS_PANEL_TITLE);
_resultsView
.on("replaceBatch", function () {
_finishReplaceBatch(model);
})
.on("close", function () {
FindInFiles.clearSearch();
})
.on("getNextPage", function () {
FindInFiles.getNextPageofSearchResults().done(function () {
if (FindInFiles.searchModel.hasResults()) {
_resultsView.showNextPage();
}
});
})
.on("getLastPage", function () {
FindInFiles.getAllSearchResults().done(function () {
if (FindInFiles.searchModel.hasResults()) {
_resultsView.showLastPage();
}
});
});
});
// Initialize: register listeners
ProjectManager.on("beforeProjectClose", function () {
_resultsView.close();
if (_findBar && !_findBar.isClosed()) {
_closeFindBar(true);
}
});
// Initialize: command handlers
CommandManager.register(Strings.CMD_FIND_IN_FILES, Commands.CMD_FIND_IN_FILES, _showFindBar);
CommandManager.register(Strings.CMD_FIND_IN_SUBTREE, Commands.CMD_FIND_IN_SUBTREE, _showFindBarForSubtree);
CommandManager.register(Strings.CMD_REPLACE_IN_FILES, Commands.CMD_REPLACE_IN_FILES, _showReplaceBar);
CommandManager.register(Strings.CMD_REPLACE_IN_SUBTREE, Commands.CMD_REPLACE_IN_SUBTREE, _showReplaceBarForSubtree);
FindUtils.on(FindUtils.SEARCH_INDEXING_STARTED, _searchIndexingStarted);
FindUtils.on(FindUtils.SEARCH_INDEXING_PROGRESS, _searchIndexingProgressing);
FindUtils.on(FindUtils.SEARCH_INDEXING_FINISHED, _searchIndexingFinished);
FindUtils.on(FindUtils.SEARCH_FILE_FILTERS_CHANGED, _searchIfRequired);
FindUtils.on(FindUtils.SEARCH_SCOPE_CHANGED, _searchIfRequired);
// Public exports
exports.searchAndShowResults = searchAndShowResults;
exports.searchAndReplaceResults = searchAndReplaceResults;
exports.closeResultsPanel = closeResultsPanel;
// For unit testing
exports._showFindBar = _showFindBar;
exports._closeFindBar = _closeFindBar;
});