function SearchModel() {
this.clear();
}
EventDispatcher.makeEventDispatcher(SearchModel.prototype);
SearchModel.MAX_TOTAL_RESULTS = 100000;
Whether or not we exceeded the maximum number of results in the search we did.
SearchModel.prototype.exceedsMaximum = false;
A file filter (as returned from FileFilters) to apply within the main scope.
SearchModel.prototype.filter = null;
Whether or not we hit the maximum number of results for the type of search we did.
SearchModel.prototype.foundMaximum = false;
Whether this is a find/replace query.
SearchModel.prototype.isReplace = false;
The total number of matches in the model.
SearchModel.prototype.numMatches = 0;
The compiled query, expressed as a regexp.
SearchModel.prototype.queryExpr = null;
The query that generated these results.
SearchModel.prototype.queryInfo = null;
The replacement text specified for this query, if any.
SearchModel.prototype.replaceText = null;
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();
}
};
Counts the total number of matches and files
SearchModel.prototype.countFilesMatches = function () {
return {files: (this.numFiles || Object.keys(this.results).length), matches: this.numMatches};
};
Notifies listeners that the set of results has changed. Must be called after the model is changed.
SearchModel.prototype.fireChanged = function (quickChange) {
this.trigger("change", quickChange);
};
// Public API
exports.SearchModel = SearchModel;
});
SearchModel.prototype.hasResults = function () {
return Object.keys(this.results).length > 0;
};
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
SearchModel.prototype.prioritizeOpenFile = function (firstFile) {
var workingSetFiles = MainViewManager.getWorkingSet(MainViewManager.ALL_PANES),
workingSetFileFound = {},
fileSetWithoutWorkingSet = [],
startingWorkingFileSet = [],
propertyName = "",
i = 0;
if (FindUtils.isNodeSearchDisabled()) {
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);
});
}
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);
};
Removes the given result's matches from the search results and updates the total match count.
SearchModel.prototype.removeResults = function (fullpath) {
if (this.results[fullpath]) {
this.numMatches -= this.results[fullpath].matches.length;
delete this.results[fullpath];
}
};
Sets the given query info and stores a compiled RegExp query in this.queryExpr.
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.
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--;
this.exceedsMaximum = true;
}
}
};