Repository navigation
Expand file tree
/
Copy pathSearchModel.js
More file actions
270 lines (236 loc) · 9.61 KB
/
Copy pathSearchModel.js
File metadata and controls
270 lines (236 loc) · 9.61 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
/*
* 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
define(function (require, exports, module) {
var FileUtils = require("file/FileUtils"),
EventDispatcher = require("utils/EventDispatcher"),
FindUtils = require("search/FindUtils"),
MainViewManager = require("view/MainViewManager");
/**
* @constructor
* Manages a set of search query and result data.
* Dispatches these events:
* "change" - whenever the results have been updated. Note that it's up to people who
* edit the model to call fireChange() when necessary - it doesn't automatically fire.
*/
function SearchModel() {
this.clear();
}
EventDispatcher.makeEventDispatcher(SearchModel.prototype);
/** @const Constant used to define the maximum results found.
* Note that this is a soft limit - we'll likely go slightly over it since
* we always add all the searches in a given file.
*/
SearchModel.MAX_TOTAL_RESULTS = 100000;
/**
* The current set of results.
* @type {Object.<string, {matches: Array.<Object>, collapsed: boolean, timestamp: Date}>}
*/
SearchModel.prototype.results = null;
/**
* The query that generated these results.
* @type {{query: string, caseSensitive: boolean, isRegexp: boolean}}
*/
SearchModel.prototype.queryInfo = null;
/**
* The compiled query, expressed as a regexp.
* @type {RegExp}
*/
SearchModel.prototype.queryExpr = null;
/**
* Whether this is a find/replace query.
* @type {boolean}
*/
SearchModel.prototype.isReplace = false;
/**
* The replacement text specified for this query, if any.
* @type {string}
*/
SearchModel.prototype.replaceText = null;
/**
* The file/folder path representing the scope that this query was performed in.
* @type {FileSystemEntry}
*/
SearchModel.prototype.scope = null;
/**
* A file filter (as returned from FileFilters) to apply within the main scope.
* @type {string}
*/
SearchModel.prototype.filter = null;
/**
* The total number of matches in the model.
* @type {number}
*/
SearchModel.prototype.numMatches = 0;
/**
* Whether or not we hit the maximum number of results for the type of search we did.
* @type {boolean}
*/
SearchModel.prototype.foundMaximum = false;
/**
* Whether or not we exceeded the maximum number of results in the search we did.
* @type {boolean}
*/
SearchModel.prototype.exceedsMaximum = false;
/**
* Clears out the model to an empty state.
*/
SearchModel.prototype.clear = function () {
var numMatchesBefore = this.numMatches;
this.results = {};
this.queryInfo = null;
this.queryExpr = null;
this.isReplace = false;
this.replaceText = null;
this.scope = null;
this.numMatches = 0;
this.foundMaximum = false;
this.exceedsMaximum = false;
if (numMatchesBefore !== 0) {
this.fireChanged();
}
};
/**
* Sets the given query info and stores a compiled RegExp query in this.queryExpr.
* @param {{query: string, caseSensitive: boolean, isRegexp: boolean}} queryInfo
* @return {boolean} true if the query was valid and properly set, false if it was
* invalid or empty.
*/
SearchModel.prototype.setQueryInfo = function (queryInfo) {
var parsedQuery = FindUtils.parseQueryInfo(queryInfo);
if (parsedQuery.valid) {
this.queryInfo = queryInfo;
this.queryExpr = parsedQuery.queryExpr;
return true;
}
return false;
};
/**
* Sets the list of matches for the given path, removing the previous match info, if any, and updating
* the total match count. Note that for the count to remain accurate, the previous match info must not have
* been mutated since it was set.
* @param {string} fullpath Full path to the file containing the matches.
* @param {!{matches: Object, timestamp: Date, collapsed: boolean=}} resultInfo Info for the matches to set:
* matches - Array of matches, in the format returned by FindInFiles._getSearchMatches()
* timestamp - The timestamp of the document at the time we searched it.
* collapsed - Optional: whether the results should be collapsed in the UI (default false).
*/
SearchModel.prototype.setResults = function (fullpath, resultInfo) {
this.removeResults(fullpath);
if (this.foundMaximum || !resultInfo.matches.length) {
return;
}
// Make sure that the optional `collapsed` property is explicitly set to either true or false,
// to avoid logic issues later with comparing values.
resultInfo.collapsed = !!resultInfo.collapsed;
if (!this.results[fullpath] && this.numFiles >= 0) {
this.numFiles++;
}
this.results[fullpath] = resultInfo;
this.numMatches += resultInfo.matches.length;
if (this.numMatches >= SearchModel.MAX_TOTAL_RESULTS) {
this.foundMaximum = true;
// Remove final result if there have been over MAX_TOTAL_RESULTS found
if (this.numMatches > SearchModel.MAX_TOTAL_RESULTS) {
this.results[fullpath].matches.pop();
this.numMatches--;
this.exceedsMaximum = true;
}
}
};
/**
* Removes the given result's matches from the search results and updates the total match count.
* @param {string} fullpath Full path to the file containing the matches.
*/
SearchModel.prototype.removeResults = function (fullpath) {
if (this.results[fullpath]) {
this.numMatches -= this.results[fullpath].matches.length;
if (this.numFiles) {
this.numFiles--;
}
delete this.results[fullpath];
}
};
/**
* @return {boolean} true if there are any results in this model.
*/
SearchModel.prototype.hasResults = function () {
return Object.keys(this.results).length > 0;
};
/**
* Counts the total number of matches and files
* @return {{files: number, matches: number}}
*/
SearchModel.prototype.countFilesMatches = function () {
return { files: (this.numFiles || Object.keys(this.results).length), matches: this.numMatches };
};
/**
* Prioritizes the open file and then the working set files to the starting of the list of files
* If node search is disabled, we sort the files too- Sorting is computation intensive, and our
* ProjectManager.getAllFiles with the sort flag is not working properly : TODO TOFIX
* @param {?string} firstFile If specified, the path to the file that should be sorted to the top.
* @return {Array.<string>}
*/
SearchModel.prototype.prioritizeOpenFile = function (firstFile) {
var workingSetFiles = MainViewManager.getWorkingSet(MainViewManager.ALL_PANES),
workingSetFileFound = {},
fileSetWithoutWorkingSet = [],
startingWorkingFileSet = [],
propertyName = "",
i = 0;
firstFile = firstFile || "";
// Create a working set path map which indicates if a file in working set is found in file list
for (i = 0; i < workingSetFiles.length; i++) {
workingSetFileFound[workingSetFiles[i].fullPath] = false;
}
// Remove all the working set files from the filtration list
fileSetWithoutWorkingSet = Object.keys(this.results).filter(function (key) {
if (workingSetFileFound[key] !== undefined) {
workingSetFileFound[key] = true;
return false;
}
return true;
});
//push in the first file
if (workingSetFileFound[firstFile] === true) {
startingWorkingFileSet.push(firstFile);
workingSetFileFound[firstFile] = false;
}
//push in the rest of working set files already present in file list
for (propertyName in workingSetFileFound) {
if (workingSetFileFound.hasOwnProperty(propertyName) && workingSetFileFound[propertyName]) {
startingWorkingFileSet.push(propertyName);
}
}
return startingWorkingFileSet.concat(fileSetWithoutWorkingSet);
};
/**
* Notifies listeners that the set of results has changed. Must be called after the
* model is changed.
* @param {boolean} quickChange Whether this type of change is one that might occur
* often, meaning that the view should buffer updates.
*/
SearchModel.prototype.fireChanged = function (quickChange) {
this.trigger("change", quickChange);
};
// Public API
exports.SearchModel = SearchModel;
});