forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFindUtils.js
More file actions
1 lines (1 loc) · 7.53 KB
/
Copy pathFindUtils.js
File metadata and controls
1 lines (1 loc) · 7.53 KB
1
define(function(require,exports,module){var Async=require("utils/Async"),DocumentManager=require("document/DocumentManager"),MainViewManager=require("view/MainViewManager"),FileSystem=require("filesystem/FileSystem"),FileUtils=require("file/FileUtils"),ProjectManager=require("project/ProjectManager"),EventDispatcher=require("utils/EventDispatcher"),Strings=require("strings"),StringUtils=require("utils/StringUtils"),_=require("thirdparty/lodash");let instantSearchDisabled=!1,indexingSuspended=!1,indexingInProgress=!1,workerSearchCount=0,collapseResults=!1;function parseDollars(replaceWith,match){return replaceWith=(replaceWith=replaceWith.replace(/(\$+)(\d{1,2}|&)/g,function(whole,dollars,index){if(dollars.length%2==1){if("&"===index)return dollars.substr(1)+(match[0]||"");var parsedIndex=parseInt(index,10);if(0!==parsedIndex)return dollars.substr(1)+(match[parsedIndex]||"")}return whole})).replace(/\$\$/g,"$")}function _doReplaceInDocument(doc,matchInfo,replaceText,isRegexp){return doc.diskTimestamp.getTime()!==matchInfo.timestamp.getTime()?(new $.Deferred).reject(exports.ERROR_FILE_CHANGED).promise():(doc.batchOperation(function(){matchInfo.matches.reverse().forEach(function(match){match.isChecked&&doc.replaceRange(isRegexp?parseDollars(replaceText,match.result):replaceText,match.start,match.end)})}),(new $.Deferred).resolve().promise())}function _doReplaceOnDisk(fullPath,matchInfo,replaceText,isRegexp){var file=FileSystem.getFileForPath(fullPath);return DocumentManager.getDocumentText(file,!0).then(function(contents,timestamp,lineEndings){if(timestamp.getTime()!==matchInfo.timestamp.getTime())return(new $.Deferred).reject(exports.ERROR_FILE_CHANGED).promise();var result=[],lastIndex=0;matchInfo.matches.forEach(function(match){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("");return lineEndings===FileUtils.LINE_ENDINGS_CRLF&&(newContents=newContents.replace(/\n/g,"\r\n")),Async.promisify(file,"write",newContents)})}function _doReplaceInOneFile(fullPath,matchInfo,replaceText,options){var doc=DocumentManager.getOpenDocumentForPath(fullPath);return options=options||{},doc||!options.forceFilesOpen&&-1===MainViewManager.findInWorkingSet(MainViewManager.ALL_PANES,fullPath)?doc?_doReplaceInDocument(doc,matchInfo,replaceText,options.isRegexp):_doReplaceOnDisk(fullPath,matchInfo,replaceText,options.isRegexp):DocumentManager.getDocumentForPath(fullPath).then(function(newDoc){return _doReplaceInDocument(newDoc,matchInfo,replaceText,options.isRegexp)})}function hasCheckedMatches(result){return result.matches.some(function(match){return match.isChecked})}function performReplacements(results,replaceText,options){return Async.doInParallel_aggregateErrors(Object.keys(results),function(fullPath){return _doReplaceInOneFile(fullPath,results[fullPath],replaceText,options)}).done(function(){if(options&&options.forceFilesOpen){var doc=DocumentManager.getCurrentDocument();if(!doc||!results[doc.file.fullPath]||!hasCheckedMatches(results[doc.file.fullPath])){var sortedPaths=Object.keys(results).sort(FileUtils.comparePaths),firstPath=_.find(sortedPaths,function(path){return hasCheckedMatches(results[path])});if(firstPath){var newDoc=DocumentManager.getOpenDocumentForPath(firstPath);newDoc&&MainViewManager._edit(MainViewManager.ACTIVE_PANE,newDoc)}}}})}function labelForScope(scope){return scope?StringUtils.format(Strings.FIND_IN_FILES_SCOPED,StringUtils.breakableUrl(ProjectManager.makeProjectRelativeIfPossible(scope.fullPath))):Strings.FIND_IN_FILES_NO_SCOPE}function parseQueryInfo(queryInfo){var queryExpr;if(!queryInfo||!queryInfo.query)return{empty:!0};var flags="gm";if(queryInfo.isCaseSensitive||(flags+="i"),queryInfo.isRegexp)try{queryExpr=new RegExp(queryInfo.query,flags)}catch(e){return{valid:!1,error:e.message}}else queryExpr=new RegExp(StringUtils.regexEscape(queryInfo.query),flags);return{valid:!0,queryExpr:queryExpr}}function prioritizeOpenFile(files,firstFile){var workingSetFiles=MainViewManager.getWorkingSet(MainViewManager.ALL_PANES),workingSetFileFound={},fileSetWithoutWorkingSet=[],startingWorkingFileSet=[],propertyName="",i=0;for(firstFile=firstFile||"",i=0;i<workingSetFiles.length;i++)workingSetFileFound[workingSetFiles[i].fullPath]=!1;for(propertyName in fileSetWithoutWorkingSet=files.filter(function(key){return void 0===workingSetFileFound[key]||(workingSetFileFound[key]=!0,!1)}),!0===workingSetFileFound[firstFile]&&(startingWorkingFileSet.push(firstFile),workingSetFileFound[firstFile]=!1),workingSetFileFound)workingSetFileFound.hasOwnProperty(propertyName)&&workingSetFileFound[propertyName]&&startingWorkingFileSet.push(propertyName);return startingWorkingFileSet.concat(fileSetWithoutWorkingSet)}function getOpenFilePath(){var currentDoc=DocumentManager.getCurrentDocument();return currentDoc?currentDoc.file.fullPath:null}function setInstantSearchDisabled(disable){instantSearchDisabled=disable}function isInstantSearchDisabled(){return instantSearchDisabled}function setIndexingSuspended(suspended){indexingSuspended=suspended}function isIndexingSuspended(){return indexingSuspended}function isWorkerSearchInProgress(){return 0!==workerSearchCount}function notifyFileFiltersChanged(){exports.trigger(exports.SEARCH_FILE_FILTERS_CHANGED)}function notifySearchScopeChanged(){exports.trigger(exports.SEARCH_SCOPE_CHANGED)}function notifyWorkerSearchStarted(){workerSearchCount++}function notifyWorkerSearchFinished(){workerSearchCount--}function notifyIndexingStarted(){indexingInProgress=!0,exports.trigger(exports.SEARCH_INDEXING_STARTED)}function notifyIndexingProgress(progress,total,cacheSizeBytes){exports.trigger(exports.SEARCH_INDEXING_PROGRESS,progress,total,cacheSizeBytes)}function notifyIndexingFinished(){indexingInProgress=!1,exports.trigger(exports.SEARCH_INDEXING_FINISHED)}function isIndexingInProgress(){return indexingInProgress}function setCollapseResults(collapse){collapseResults=collapse,exports.trigger(exports.SEARCH_COLLAPSE_RESULTS)}function isCollapsedResults(){return collapseResults}EventDispatcher.makeEventDispatcher(exports),exports.parseDollars=parseDollars,exports.hasCheckedMatches=hasCheckedMatches,exports.performReplacements=performReplacements,exports.labelForScope=labelForScope,exports.parseQueryInfo=parseQueryInfo,exports.prioritizeOpenFile=prioritizeOpenFile,exports.getOpenFilePath=getOpenFilePath,exports.setInstantSearchDisabled=setInstantSearchDisabled,exports.isInstantSearchDisabled=isInstantSearchDisabled,exports.setIndexingSuspended=setIndexingSuspended,exports.isIndexingSuspended=isIndexingSuspended,exports.isWorkerSearchInProgress=isWorkerSearchInProgress,exports.isIndexingInProgress=isIndexingInProgress,exports.setCollapseResults=setCollapseResults,exports.isCollapsedResults=isCollapsedResults,exports.ERROR_FILE_CHANGED="fileChanged",exports.notifyFileFiltersChanged=notifyFileFiltersChanged,exports.notifySearchScopeChanged=notifySearchScopeChanged,exports.notifyWorkerSearchStarted=notifyWorkerSearchStarted,exports.notifyWorkerSearchFinished=notifyWorkerSearchFinished,exports.notifyIndexingStarted=notifyIndexingStarted,exports.notifyIndexingProgress=notifyIndexingProgress,exports.notifyIndexingFinished=notifyIndexingFinished,exports.SEARCH_FILE_FILTERS_CHANGED="fileFiltersChanged",exports.SEARCH_SCOPE_CHANGED="searchScopeChanged",exports.SEARCH_INDEXING_STARTED="searchIndexingStarted",exports.SEARCH_INDEXING_PROGRESS="searchIndexingProgress",exports.SEARCH_INDEXING_FINISHED="searchIndexingFinished",exports.SEARCH_COLLAPSE_RESULTS="searchCollapseResults"});