forked from adobe/brackets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchModel.js
More file actions
231 lines (203 loc) · 8.23 KB
/
SearchModel.js
File metadata and controls
231 lines (203 loc) · 8.23 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
/*
* 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.
*
*/
/*global define, $ */
define(function (require, exports, module) {
"use strict";
var FileUtils = require("file/FileUtils"),
FindUtils = require("search/FindUtils");
/**
* @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();
}
/** @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.<fullPath: 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 () {
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;
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;
} else {
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;
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 -= 1;
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;
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: Object.keys(this.results).length, matches: this.numMatches};
};
/**
* Sorts the file keys to show the results from the selected file first and the rest sorted by path
* @param {?string} firstFile If specified, the path to the file that should be sorted to the top.
* @return {Array.<string>}
*/
SearchModel.prototype.getSortedFiles = function (firstFile) {
return Object.keys(this.results).sort(function (key1, key2) {
if (firstFile === key1) {
return -1;
} else if (firstFile === key2) {
return 1;
}
return FileUtils.comparePaths(key1, key2);
});
};
/**
* 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).triggerHandler("change", quickChange);
};
// Public API
exports.SearchModel = SearchModel;
});