src/search/FindUtils.js
function _doReplaceOnDisk(fullPath, matchInfo, replaceText, isRegexp) {
        var file = FileSystem.getFileForPath(fullPath);
        return DocumentManager.getDocumentText(file, true).then(function (contents, timestamp, lineEndings) {
            if (timestamp.getTime() !== matchInfo.timestamp.getTime()) {
                // Return a promise that we'll reject immediately. (We can't just return the
                // error since this is the success handler.)
                return new $.Deferred().reject(exports.ERROR_FILE_CHANGED).promise();
            }

            // Note that this assumes that the matches are sorted.
            // TODO: is there a more efficient way to do this in a large string?
            var result = [],
                lastIndex = 0;
            matchInfo.matches.forEach(function (match) {
                if (match.isChecked) {
                    result.push(contents.slice(lastIndex, match.startOffset));
                    result.push(isRegexp ? parseDollars(replaceText, match.result) : replaceText);
                    lastIndex = match.endOffset;
                }
            });
            result.push(contents.slice(lastIndex));

            var newContents = result.join("");
            // TODO: duplicated logic from Document - should refactor this?
            if (lineEndings === FileUtils.LINE_ENDINGS_CRLF) {
                newContents = newContents.replace(/\n/g, "\r\n");
            }

            return Async.promisify(file, "write", newContents);
        });
    }
function _doReplaceOnDisk(fullPath, matchInfo, replaceText, isRegexp) {
        var file = FileSystem.getFileForPath(fullPath);
        return DocumentManager.getDocumentText(file, true).then(function (contents, timestamp, lineEndings) {
            if (timestamp.getTime() !== matchInfo.timestamp.getTime()) {
                // Return a promise that we'll reject immediately. (We can't just return the
                // error since this is the success handler.)
                return new $.Deferred().reject(exports.ERROR_FILE_CHANGED).promise();
            }

            // Note that this assumes that the matches are sorted.
            // TODO: is there a more efficient way to do this in a large string?
            var result = [],
                lastIndex = 0;
            matchInfo.matches.forEach(function (match) {
                if (match.isChecked) {
                    result.push(contents.slice(lastIndex, match.startOffset));
                    result.push(isRegexp ? parseDollars(replaceText, match.result) : replaceText);
                    lastIndex = match.endOffset;
                }
            });
            result.push(contents.slice(lastIndex));

            var newContents = result.join("");
            // TODO: duplicated logic from Document - should refactor this?
            if (lineEndings === FileUtils.LINE_ENDINGS_CRLF) {
                newContents = newContents.replace(/\n/g, "\r\n");
            }

            return Async.promisify(file, "write", newContents);
        });
    }
function _doReplaceOnDisk ( fullPath , matchInfo , replaceText , isRegexp ) { var file = FileSystem . getFileForPath ( fullPath ) ; return DocumentManager . getDocumentText ( file , true ) . then ( function ( contents , timestamp , lineEndings ) { if ( timestamp . getTime ( ) !== matchInfo . timestamp . getTime ( ) ) { // Return a promise that we'll reject immediately. (We can't just return the // error since this is the success handler.) return new $ . Deferred ( ) . reject ( exports . ERROR_FILE_CHANGED ) . promise ( ) ; } // Note that this assumes that the matches are sorted. // TODO: is there a more efficient way to do this in a large string? var result = [ ] , lastIndex = 0 ; matchInfo . matches . forEach ( function ( match ) { if ( match . isChecked ) { result . push ( contents . slice ( lastIndex , match . startOffset ) ) ; result . push ( isRegexp ? parseDollars ( replaceText , match . result ) : replaceText ) ; lastIndex = match . endOffset ; } } ) ; result . push ( contents . slice ( lastIndex ) ) ; var newContents = result . join ( "" ) ; // TODO: duplicated logic from Document - should refactor this? if ( lineEndings === FileUtils . LINE_ENDINGS_CRLF ) { newContents = newContents . replace ( / \n / g , "\r\n" ) ; } return Async . promisify ( file , "write" , newContents ) ; } ) ; }
Does a set of replacements in a single file on disk.
@param {string} fullPath The full path to the file.
@param {Object} matchInfo The match info for this file, as returned by `_addSearchMatches()`.
@param {string} replaceText The text to replace each result with.
@param {boolean=} isRegexp Whether the original query was a regexp.
@return {$.Promise} A promise that's resolved when the replacement is finished or rejected with an error if there were one or more errors.
Does a set of replacements in a single file on disk .