-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathSearchResultsView.js
More file actions
842 lines (754 loc) · 34.5 KB
/
SearchResultsView.js
File metadata and controls
842 lines (754 loc) · 34.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
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/*
* 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.
*
*/
// @INCLUDE_IN_API_DOCS
/*
* Panel showing search results for a Find/Replace in Files operation.
*/
define(function (require, exports, module) {
var CommandManager = require("command/CommandManager"),
EventDispatcher = require("utils/EventDispatcher"),
Commands = require("command/Commands"),
DocumentManager = require("document/DocumentManager"),
Editor = require("editor/Editor"),
EditorManager = require("editor/EditorManager"),
ProjectManager = require("project/ProjectManager"),
FileViewController = require("project/FileViewController"),
FileUtils = require("file/FileUtils"),
FindUtils = require("search/FindUtils"),
WorkspaceManager = require("view/WorkspaceManager"),
StringUtils = require("utils/StringUtils"),
Strings = require("strings"),
Metrics = require("utils/Metrics"),
_ = require("thirdparty/lodash"),
Mustache = require("thirdparty/mustache/mustache"),
searchPanelTemplate = require("text!htmlContent/search-panel.html"),
searchResultsTemplate = require("text!htmlContent/search-results.html"),
searchSummaryTemplate = require("text!htmlContent/search-summary.html");
/**
* @const
* The maximum results to show per page.
* @type {number}
* @private
*/
var RESULTS_PER_PAGE = 100;
/**
* @const
* Debounce time for document changes updating the search results view.
* @type {number}
* @private
*/
var UPDATE_TIMEOUT = 400;
/**
* @constructor
* Handles the search results panel.
* Dispatches the following events:
* replaceBatch - when the "Replace" button is clicked.
* close - when the panel is closed.
*
* @param {SearchModel} model The model that this view is showing.
* @param {string} panelID The CSS ID to use for the panel.
* @param {string} panelName The name to use for the panel, as passed to WorkspaceManager.createBottomPanel().
* @param {string} type type to identify if it is reference search or string match serach
* @param {string=} title Display title for the panel tab.
*/
function SearchResultsView(model, panelID, panelName, type, title) {
const self = this;
let panelHtml = Mustache.render(searchPanelTemplate, {panelID: panelID});
this._panel = WorkspaceManager.createBottomPanel(panelName, $(panelHtml), 100, title);
this._$summary = this._panel.$panel.find(".title");
this._$table = this._panel.$panel.find(".table-container");
this._$previewEditor = this._panel.$panel.find(".search-editor-preview");
this._model = model;
this._searchResultsType = type;
this._hasPreviousPage = false;
this._hasNextPage = false;
new ResizeObserver(()=>{
if(self._$previewEditor.editor){
self._$previewEditor.editor.updateLayout();
}
}).observe(this._panel.$panel[0]);
function _showPanelIfResultsAvailable(_e, shownPanelID) {
if(self._model.numMatches === 0){
self._panel.hide();
}
if(shownPanelID === self._panel.panelID && !self._model.isReplace){
// If it is replace, _handleModelChange will close the find bar as we dont
// do replace if there is a model change. So we wont enter this flow if it is a replace operation
self._handleModelChange();
}
}
WorkspaceManager.on(WorkspaceManager.EVENT_WORKSPACE_PANEL_SHOWN, _showPanelIfResultsAvailable);
}
EventDispatcher.makeEventDispatcher(SearchResultsView.prototype);
/**
* @type {SearchModel} The search results model we're viewing.
* @private
*/
SearchResultsView.prototype._model = null;
/**
* Array with content used in the Results Panel
* @type {Array.<{fileIndex: number, filename: string, fullPath: string, items: Array.<Object>}>}
* @private
*/
SearchResultsView.prototype._searchList = [];
/**
* @type {Panel} Bottom panel holding the search results
* @private
*/
SearchResultsView.prototype._panel = null;
/**
* @type {?string} The full path of the file that was open in the main editor on the initial search
* @private
*/
SearchResultsView.prototype._initialFilePath = null;
/**
* @type {number} The index of the first result that is displayed
* @private
*/
SearchResultsView.prototype._currentStart = 0;
/**
* @type {boolean} Used to remake the replace all summary after it is changed
* @private
*/
SearchResultsView.prototype._allChecked = false;
/**
* @type {$.Element} The currently selected row
* @private
*/
SearchResultsView.prototype._$selectedRow = null;
/**
* @type {$.Element} The element where the title is placed
* @private
*/
SearchResultsView.prototype._$summary = null;
/**
* @type {$.Element} The table that holds the results
* @private
*/
SearchResultsView.prototype._$table = null;
/**
* @type {number} The ID we use for timeouts when handling model changes.
* @private
*/
SearchResultsView.prototype._timeoutID = null;
/**
* @type {string} The Id we use to check if it is reference search or match search
* @private
*/
SearchResultsView.prototype._searchResultsType = null;
/**
* @private
* Handles when model changes. Updates the view, buffering changes if necessary so as not to churn too much.
*/
SearchResultsView.prototype._handleModelChange = function (quickChange) {
// If this is a replace, to avoid complications with updating, just close ourselves if we hear about
// a results model change after we've already shown the results initially.
// TODO: notify user, re-do search in file
if (this._model.isReplace) {
this.close();
return;
}
var self = this;
if (this._timeoutID) {
window.clearTimeout(this._timeoutID);
}
if (quickChange) {
this._timeoutID = window.setTimeout(function () {
self._updateResults();
self._timeoutID = null;
}, UPDATE_TIMEOUT);
} else {
this._updateResults();
}
};
SearchResultsView.prototype.OpenSelectedFile = function () {
const self = this;
if (self._$selectedRow) {
let searchItem = self._searchList[self._$selectedRow.data("file-index")];
let item = searchItem.items[self._$selectedRow.data("item-index")];
CommandManager.execute(Commands.FILE_OPEN, {fullPath: searchItem.fullPath})
.done(function () {
// Opened document is now the current main editor
EditorManager.getCurrentFullEditor().setSelection(item.start, item.end, true);
});
}
};
SearchResultsView.prototype._previewSelectedFile = function () {
const self = this;
if (self._$selectedRow) {
let searchItem = self._searchList[self._$selectedRow.data("file-index")];
let item = searchItem.items[self._$selectedRow.data("item-index")];
self._showPreviewEditor(searchItem.fullPath, item.start, item.end);
}
};
SearchResultsView.prototype.selectNextResult = function () {
const self = this;
if (self._$selectedRow) {
const selectedElement = self._$selectedRow[0];
let nextElement = selectedElement.nextElementSibling;
let searchItem = self._searchList[self._$selectedRow.data("file-index")];
if(!self._model.results[searchItem.fullPath]){
return;
}
let collapsed = self._model.results[searchItem.fullPath].collapsed;
while(nextElement && (collapsed || !$(nextElement).hasClass('file-search-item'))){
// skip collapsed elements too
nextElement = nextElement.nextElementSibling;
if(!nextElement){
break;
}
searchItem = self._searchList[$(nextElement).data("file-index")];
collapsed = self._model.results[searchItem.fullPath].collapsed;
}
if(nextElement){
self._$selectedRow.removeClass("selected");
self._$selectedRow = $(nextElement);
self._$selectedRow.addClass("selected");
nextElement.scrollIntoView({block: "nearest"});
self._previewSelectedFile();
} else if(self._hasNextPage){
self.trigger('getNextPage');
}
}
};
SearchResultsView.prototype.selectNextPage = function () {
const self = this;
if(self._hasNextPage){
self.trigger('getNextPage');
}
};
SearchResultsView.prototype.selectLastResultInPage = function () {
const self = this;
if (self._$selectedRow) {
let selectedElement = self._$selectedRow[0];
let lastElement = selectedElement.parentNode.lastChild;
while(lastElement && !$(lastElement).hasClass('file-search-item')){
lastElement = lastElement.previousElementSibling;
}
if(lastElement){
self._$selectedRow.removeClass("selected");
self._$selectedRow = $(lastElement);
self._$selectedRow.addClass("selected");
lastElement.scrollIntoView({block: "nearest"});
self._previewSelectedFile();
}
}
};
SearchResultsView.prototype.selectPrevResult = function () {
const self = this;
if (self._$selectedRow) {
let selectedElement = self._$selectedRow[0];
let prevElement = selectedElement.previousElementSibling;
let searchItem = self._searchList[self._$selectedRow.data("file-index")];
if(!self._model.results[searchItem.fullPath]){
return;
}
let collapsed = self._model.results[searchItem.fullPath].collapsed;
while(prevElement && (collapsed || !$(prevElement).hasClass('file-search-item'))){
// skip collapsed elements too
prevElement = prevElement.previousElementSibling;
if(!prevElement){
break;
}
searchItem = self._searchList[$(prevElement).data("file-index")];
collapsed = self._model.results[searchItem.fullPath].collapsed;
}
if(prevElement){
self._$selectedRow.removeClass("selected");
self._$selectedRow = $(prevElement);
self._$selectedRow.addClass("selected");
prevElement.scrollIntoView({block: "nearest"});
self._previewSelectedFile();
} else if(self._hasPreviousPage){
self.showPreviousPage();
self.selectLastResultInPage();
}
}
};
SearchResultsView.prototype.selectPrevPage = function () {
const self = this;
if(self._hasPreviousPage){
self.showPreviousPage();
}
};
/**
* @private
* Adds the listeners for close, prev, next, first, last and check all
*/
SearchResultsView.prototype._addPanelListeners = function () {
var self = this;
this._panel.$panel
.off(".searchResults") // Remove the old events
.on("dblclick.searchResults", ".toolbar", function() {
self._panel.hide();
})
.on("click.searchResults", ".close", function () {
self._panel.hide();
})
// The link to go the first page
.on("click.searchResults", ".first-page:not(.disabled)", function () {
self._currentStart = 0;
self._render();
Metrics.countEvent(Metrics.EVENT_TYPE.SEARCH, "result.panel.btn", "firstPage");
})
// The link to go the previous page
.on("click.searchResults", ".prev-page:not(.disabled)", function () {
self._currentStart -= RESULTS_PER_PAGE;
self._render();
Metrics.countEvent(Metrics.EVENT_TYPE.SEARCH, "result.panel.btn", "prevPage");
})
// The link to go to the next page
.on("click.searchResults", ".next-page:not(.disabled)", function () {
self.trigger('getNextPage');
Metrics.countEvent(Metrics.EVENT_TYPE.SEARCH, "result.panel.btn", "nextPage");
})
// The link to go to the last page
.on("click.searchResults", ".last-page:not(.disabled)", function () {
self.trigger('getLastPage');
Metrics.countEvent(Metrics.EVENT_TYPE.SEARCH, "result.panel.btn", "lastPage");
})
// Add the file to the working set on double click
.on("dblclick.searchResults", ".table-container tr:not(.file-section)", function (e) {
let $row = $(e.target).closest("tr");
let searchFile = self._searchList[$row.data("file-index")];
let item = searchFile.items[$row.data("item-index")];
FileViewController.openFileAndAddToWorkingSet(searchFile.fullPath).done(function () {
// Opened document is now the current main editor
EditorManager.getCurrentFullEditor().setSelection(item.start, item.end, true);
});
})
// Add the click event listener directly on the table parent
.on("click.searchResults .table-container", function (e) {
let $row = $(e.target).closest("tr");
let isLineNumberClick = $row.context && $($row.context).hasClass("line-number");
if ($row.length) {
if (self._$selectedRow) {
self._$selectedRow.removeClass("selected");
}
$row.addClass("selected");
self._$selectedRow = $row;
var searchItem = self._searchList[$row.data("file-index")],
fullPath = searchItem.fullPath;
// This is a file title row, expand/collapse on click
if ($row.hasClass("file-section")) {
var $titleRows,
collapsed = !self._model.results[fullPath].collapsed;
if (e.metaKey || e.ctrlKey) { //Expand all / Collapse all
$titleRows = $(e.target).closest("table").find(".file-section");
} else {
// Clicking the file section header collapses/expands result rows for that file
$titleRows = $row;
}
$titleRows.each(function () {
fullPath = self._searchList[$(this).data("file-index")].fullPath;
searchItem = self._model.results[fullPath];
if (searchItem.collapsed !== collapsed) {
searchItem.collapsed = collapsed;
$(this).nextUntil(".file-section").toggle();
$(this).find(".disclosure-triangle").toggleClass("expanded");
}
});
//In Expand/Collapse all, reset all search results 'collapsed' flag to same value(true/false).
if (e.metaKey || e.ctrlKey) {
FindUtils.setCollapseResults(collapsed);
_.forEach(self._model.results, function (item) {
item.collapsed = collapsed;
});
}
// This is a file row, show the result on click
} else {
// Grab the required item data
let item = searchItem.items[$row.data("item-index")];
self._showPreviewEditor(fullPath, item.start, item.end);
if(isLineNumberClick){
CommandManager.execute(Commands.FILE_OPEN, {fullPath: fullPath})
.done(function () {
// Opened document is now the current main editor
EditorManager.getCurrentFullEditor().setSelection(item.start, item.end, true);
});
}
}
}
});
function updateHeaderCheckbox($checkAll) {
var $allFileRows = self._panel.$panel.find(".file-section"),
$checkedFileRows = $allFileRows.filter(function (index) {
return $(this).find(".check-one-file").is(":checked");
});
if ($checkedFileRows.length === $allFileRows.length) {
$checkAll.prop("checked", true);
}
}
function updateFileAndHeaderCheckboxes($clickedRow, isChecked) {
var $firstMatch = ($clickedRow.data("item-index") === 0) ? $clickedRow :
$clickedRow.prevUntil(".file-section").last(),
$fileRow = $firstMatch.prev(),
$siblingRows = $fileRow.nextUntil(".file-section"),
$fileCheckbox = $fileRow.find(".check-one-file"),
$checkAll = self._panel.$panel.find(".check-all");
if (isChecked) {
if (!$fileCheckbox.is(":checked")) {
var $checkedSibilings = $siblingRows.filter(function (index) {
return $(this).find(".check-one").is(":checked");
});
if ($checkedSibilings.length === $siblingRows.length) {
$fileCheckbox.prop("checked", true);
if (!$checkAll.is(":checked")) {
updateHeaderCheckbox($checkAll);
}
}
}
} else {
if ($checkAll.is(":checked")) {
$checkAll.prop("checked", false);
}
if ($fileCheckbox.is(":checked")) {
$fileCheckbox.prop("checked", false);
}
}
}
// Add the Click handlers for replace functionality if required
if (this._model.isReplace) {
this._panel.$panel
.on("click.searchResults", ".check-all", function (e) {
var isChecked = $(this).is(":checked");
_.forEach(self._model.results, function (results) {
results.matches.forEach(function (match) {
match.isChecked = isChecked;
});
});
self._$table.find(".check-one").prop("checked", isChecked);
self._$table.find(".check-one-file").prop("checked", isChecked);
self._allChecked = isChecked;
})
.on("click.searchResults", ".check-one-file", function (e) {
var isChecked = $(this).is(":checked"),
$row = $(e.target).closest("tr"),
item = self._searchList[$row.data("file-index")],
$matchRows = $row.nextUntil(".file-section"),
$checkAll = self._panel.$panel.find(".check-all");
if (item) {
self._model.results[item.fullPath].matches.forEach(function (match) {
match.isChecked = isChecked;
});
}
$matchRows.find(".check-one").prop("checked", isChecked);
if (!isChecked) {
if ($checkAll.is(":checked")) {
$checkAll.prop("checked", false);
}
} else if (!$checkAll.is(":checked")) {
updateHeaderCheckbox($checkAll);
}
e.stopPropagation();
})
.on("click.searchResults", ".check-one", function (e) {
var $row = $(e.target).closest("tr"),
item = self._searchList[$row.data("file-index")],
match = self._model.results[item.fullPath].matches[$row.data("match-index")];
match.isChecked = $(this).is(":checked");
updateFileAndHeaderCheckboxes($row, match.isChecked);
e.stopPropagation();
})
.on("click.searchResults", ".replace-checked", function (e) {
self.trigger("replaceBatch");
});
}
};
/**
* @private
* Shows the Results Summary
*/
SearchResultsView.prototype._showSummary = function () {
let self = this;
let count = self._model.countFilesMatches(),
lastIndex = self._getLastIndex(count.matches),
typeStr = (count.matches > 1) ? Strings.FIND_IN_FILES_MATCHES : Strings.FIND_IN_FILES_MATCH,
filesStr,
summary;
if(this._searchResultsType === "reference") {
typeStr = (count.matches > 1) ? Strings.REFERENCES_IN_FILES : Strings.REFERENCE_IN_FILES;
}
filesStr = StringUtils.format(
Strings.FIND_NUM_FILES,
count.files,
(count.files > 1 ? Strings.FIND_IN_FILES_FILES : Strings.FIND_IN_FILES_FILE)
);
// This text contains some formatting, so all the strings are assumed to be already escaped
summary = StringUtils.format(
Strings.FIND_TITLE_SUMMARY,
self._model.exceedsMaximum ? Strings.FIND_IN_FILES_MORE_THAN : "",
String(count.matches),
typeStr,
filesStr
);
self._hasPreviousPage = self._currentStart > 0;
self._hasNextPage = lastIndex < count.matches;
self._$summary.html(Mustache.render(searchSummaryTemplate, {
query: (self._model.queryInfo && self._model.queryInfo.query && self._model.queryInfo.query.toString()) || "",
replaceWith: self._model.replaceText,
titleLabel: self._model.isReplace ? Strings.FIND_REPLACE_TITLE_LABEL : Strings.FIND_TITLE_LABEL,
scope: self._model.scope ? " " + FindUtils.labelForScope(self._model.scope) + " " : "",
summary: summary,
allChecked: self._allChecked,
hasPages: count.matches > RESULTS_PER_PAGE,
results: StringUtils.format(Strings.FIND_IN_FILES_PAGING, self._currentStart + 1, lastIndex),
hasPrev: self._hasPreviousPage,
hasNext: self._hasNextPage,
replace: self._model.isReplace,
Strings: Strings
}));
};
/**
* @private
* Shows the current set of results.
*/
SearchResultsView.prototype._render = function () {
let searchItems, match, i, item, multiLine,
count = this._model.countFilesMatches(),
searchFiles = this._model.prioritizeOpenFile(this._initialFilePath),
lastIndex = this._getLastIndex(count.matches),
matchesCounter = 0,
showMatches = false,
allInFileChecked = true,
self = this,
previewFileSelected = false;
this._showSummary();
this._searchList = [];
// Iterates throuh the files to display the results sorted by filenamess. The loop ends as soon as
// we filled the results for one page
searchFiles.some(function (fullPath) {
showMatches = true;
item = self._model.results[fullPath];
// Since the amount of matches on this item plus the amount of matches we skipped until
// now is still smaller than the first match that we want to display, skip these.
if (matchesCounter + item.matches.length < self._currentStart) {
matchesCounter += item.matches.length;
showMatches = false;
// If we still haven't skipped enough items to get to the first match, but adding the
// item matches to the skipped ones is greater the the first match we want to display,
// then we can display the matches from this item skipping the first ones
} else if (matchesCounter < self._currentStart) {
i = self._currentStart - matchesCounter;
matchesCounter = self._currentStart;
// If we already skipped enough matches to get to the first match to display, we can start
// displaying from the first match of this item
} else if (matchesCounter < lastIndex) {
i = 0;
// We can't display more items by now. Break the loop
} else {
return true;
}
if (showMatches && i < item.matches.length) {
// Add a row for each match in the file
searchItems = [];
allInFileChecked = true;
// Add matches until we get to the last match of this item, or filling the page
while (i < item.matches.length && matchesCounter < lastIndex) {
match = item.matches[i];
multiLine = match.start.line !== match.end.line;
if(!previewFileSelected){
previewFileSelected = true;
self._showPreviewEditor(fullPath, match.start, match.end);
}
searchItems.push({
fileIndex: self._searchList.length,
itemIndex: searchItems.length,
matchIndex: i,
line: match.start.line + 1,
pre: match.line.substr(0, match.start.ch - match.highlightOffset),
highlight: match.line.substring(match.start.ch - match.highlightOffset, multiLine ? undefined : match.end.ch - match.highlightOffset),
post: multiLine ? "\u2026" : match.line.substr(match.end.ch - match.highlightOffset),
start: match.start,
end: match.end,
isChecked: match.isChecked,
isCollapsed: item.collapsed
});
if (!match.isChecked) {
allInFileChecked = false;
}
matchesCounter++;
i++;
}
// Add a row for each file
let relativePath = FileUtils.getDirectoryPath(ProjectManager.makeProjectRelativeIfPossible(fullPath)),
directoryPath = FileUtils.getDirectoryPath(relativePath),
displayFileName = StringUtils.format(
Strings.FIND_IN_FILES_FILE_PATH,
StringUtils.breakableUrl(FileUtils.getBaseName(fullPath)),
StringUtils.breakableUrl(directoryPath),
directoryPath ? "—" : ""
);
self._searchList.push({
fileIndex: self._searchList.length,
filename: displayFileName,
fullPath: fullPath,
isChecked: allInFileChecked,
items: searchItems,
isCollapsed: item.collapsed
});
}
});
// Insert the search results
this._$table
.empty()
.append(Mustache.render(searchResultsTemplate, {
replace: this._model.isReplace,
searchList: this._searchList,
Strings: Strings
}));
if (this._$selectedRow) {
this._$selectedRow.removeClass("selected");
}
let searchResults = this._$table.find(".file-search-item");
if(searchResults.length > 0){
this._$selectedRow = $(searchResults[0]);
this._$selectedRow.addClass("selected");
}
this._panel.show();
this._$table.scrollTop(0); // Otherwise scroll pos from previous contents is remembered
};
SearchResultsView.prototype._showPreviewEditor = function (fullPath, selectStart, selectEnd) {
let self = this;
if(self._$previewEditor.editor
&& self._$previewEditor.editor.document.file.fullPath === fullPath){
self._$previewEditor.editor.setSelection(selectStart, selectEnd, true, Editor.BOUNDARY_BULLSEYE);
return;
}
DocumentManager.getDocumentForPath(fullPath).done(function (doc) {
if(self._$previewEditor.editor){
self._closePreviewEditor();
}
let editorOptions = {
isReadOnly: true
};
self._$previewEditor.editor =
new Editor.Editor(doc, false, self._$previewEditor, null, editorOptions);
exports._previewEditorForTests = self._$previewEditor.editor;
self._$previewEditor.editor.updateLayout();
self._$previewEditor.editor.setSelection(selectStart, selectEnd, true, Editor.BOUNDARY_BULLSEYE);
});
};
SearchResultsView.prototype._closePreviewEditor = function () {
let self = this;
if(self._$previewEditor.editor){
exports._previewEditorForTests = null;
self._$previewEditor.editor.destroy();
self._$previewEditor.editor = null;
}
};
/**
* Updates the results view after a model change, preserving scroll position and selection.
* @private
*/
SearchResultsView.prototype._updateResults = function () {
// In general this shouldn't get called if the panel is closed, but in case some
// asynchronous process kicks this (e.g. a debounced model change), we double-check.
if (this._panel.isVisible()) {
var scrollTop = this._$table.scrollTop(),
index = this._$selectedRow ? this._$selectedRow.index() : null,
numMatches = this._model.countFilesMatches().matches;
if (this._currentStart > numMatches) {
this._currentStart = this._getLastCurrentStart(numMatches);
}
this._render();
this._$table.scrollTop(scrollTop);
if (index) {
this._$selectedRow = this._$table.find("tr:eq(" + index + ")");
this._$selectedRow.addClass("selected");
}
}
};
/**
* @private
* Returns one past the last result index displayed for the current page.
* @param {number} numMatches
* @return {number}
*/
SearchResultsView.prototype._getLastIndex = function (numMatches) {
return Math.min(this._currentStart + RESULTS_PER_PAGE, numMatches);
};
/**
* Shows the next page of the resultrs view if possible
*/
SearchResultsView.prototype.showNextPage = function () {
this._currentStart += RESULTS_PER_PAGE;
this._render();
};
/**
* Shows the next page of the resultrs view if possible
*/
SearchResultsView.prototype.showPreviousPage = function () {
this._currentStart -= RESULTS_PER_PAGE;
if(this._currentStart < 0){
this._currentStart = 0;
}
this._render();
};
/**
* Shows the last page of the results view.
*/
SearchResultsView.prototype.showLastPage = function () {
this._currentStart = this._getLastCurrentStart();
this._render();
};
/**
* @private
* Returns the last possible current start based on the given number of matches
* @param {number=} numMatches
* @return {number}
*/
SearchResultsView.prototype._getLastCurrentStart = function (numMatches) {
numMatches = numMatches || this._model.countFilesMatches().matches;
return Math.floor((numMatches - 1) / RESULTS_PER_PAGE) * RESULTS_PER_PAGE;
};
/**
* Opens the results panel and displays the current set of results from the model.
*/
SearchResultsView.prototype.open = function () {
// Clear out any paging/selection state.
this._currentStart = 0;
this._$selectedRow = null;
this._allChecked = true;
// Save the currently open document's fullpath, if any, so we can sort it to the top of the result list.
var currentDoc = DocumentManager.getCurrentDocument();
this._initialFilePath = currentDoc ? currentDoc.file.fullPath : null;
this._render();
// Listen for user interaction events with the panel and change events from the model.
this._addPanelListeners();
this._model.off("change.SearchResultsView"); // reset old listeners
this._model.on("change.SearchResultsView", this._handleModelChange.bind(this));
};
/**
* Hides the Search Results Panel and unregisters listeners.
*/
SearchResultsView.prototype.close = function () {
if (this._panel && this._panel.isVisible()) {
this._$table.empty();
this._panel.hide();
this._panel.$panel.off(".searchResults");
this._model.off("change.SearchResultsView");
this.trigger("close");
}
};
// Public API
exports.SearchResultsView = SearchResultsView;
});