Modules (188)

SearchModel

Description

Dependencies

Classes

Constructor

SearchModel

    function SearchModel() {
        this.clear();
    }
    EventDispatcher.makeEventDispatcher(SearchModel.prototype);

Properties

MAX_TOTAL_RESULTS STATIC Constant

Type
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;

exceedsMaximum

Whether or not we exceeded the maximum number of results in the search we did.

Type
boolean
    SearchModel.prototype.exceedsMaximum = false;

filter

A file filter (as returned from FileFilters) to apply within the main scope.

Type
string
    SearchModel.prototype.filter = null;

foundMaximum

Whether or not we hit the maximum number of results for the type of search we did.

Type
boolean
    SearchModel.prototype.foundMaximum = false;

isReplace

Whether this is a find/replace query.

Type
boolean
    SearchModel.prototype.isReplace = false;

numMatches

The total number of matches in the model.

Type
number
    SearchModel.prototype.numMatches = 0;

queryExpr

The compiled query, expressed as a regexp.

Type
RegExp
    SearchModel.prototype.queryExpr = null;

queryInfo

The query that generated these results.

Type
{query: string, caseSensitive: boolean, isRegexp: boolean}
    SearchModel.prototype.queryInfo = null;

replaceText

The replacement text specified for this query, if any.

Type
string
    SearchModel.prototype.replaceText = null;

results

The current set of results.

Type
Object.<fullPath: string, {matches: Array.<Object>, collapsed: boolean, timestamp: Date}>
    SearchModel.prototype.results = null;

scope

The file/folder path representing the scope that this query was performed in.

Type
FileSystemEntry
    SearchModel.prototype.scope = null;

Methods

clear

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();
        }
    };

countFilesMatches

Counts the total number of matches and files

Returns: {files: number,matches: number}
    SearchModel.prototype.countFilesMatches = function () {
        return {files: (this.numFiles || Object.keys(this.results).length), matches: this.numMatches};
    };

fireChanged

Notifies listeners that the set of results has changed. Must be called after the model is changed.

quickChange boolean
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;
});

hasResults

Returns: boolean
true if there are any results in this model.
    SearchModel.prototype.hasResults = function () {
        return Object.keys(this.results).length > 0;
    };

prioritizeOpenFile

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

firstFile nullable string
If specified, the path to the file that should be sorted to the top.
Returns: Array.<string>
    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);
    };

removeResults

Removes the given result's matches from the search results and updates the total match count.

fullpath string
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];
        }
    };

setQueryInfo

Sets the given query info and stores a compiled RegExp query in this.queryExpr.

queryInfo {query: string,caseSensitive: boolean,isRegexp: boolean}
Returns: 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;
        }
    };

setResults

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.

fullpath string
Full path to the file containing the matches.
resultInfo non-nullable {matches: Object, timestamp: Date, collapsed: boolean=}
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--;
                this.exceedsMaximum = true;
            }
        }
    };