@@ -53,7 +53,8 @@ define(function (require, exports, module) {
5353 let intervalId = 0 ,
5454 lastQueriedText = "" ,
5555 lastTypedText = "" ,
56- lastTypedTextWasRegexp = false ;
56+ lastTypedTextWasRegexp = false ,
57+ lastClosedQuery = null ;
5758 const MAX_HISTORY_RESULTS = 50 ;
5859 const PREF_MAX_HISTORY = "maxSearchHistory" ;
5960
@@ -631,6 +632,12 @@ define(function (require, exports, module) {
631632 FindBar . prototype . close = function ( suppressAnimation ) {
632633 lastQueriedText = "" ;
633634 if ( this . _modalBar ) {
635+ lastClosedQuery = {
636+ query : this . $ ( "#find-what" ) . val ( ) || "" ,
637+ replaceText : this . getReplaceText ( ) ,
638+ isCaseSensitive : this . $ ( "#find-case-sensitive" ) . is ( ".active" ) ,
639+ isRegexp : this . $ ( "#find-regexp" ) . is ( ".active" )
640+ } ;
634641 this . _addElementToSearchHistory ( $ ( "#find-what" ) . val ( ) , $ ( "#fif-filter-input" ) . val ( ) ) ;
635642 // 1st arg = restore scroll pos; 2nd arg = no animation, since getting replaced immediately
636643 this . _modalBar . close ( true , ! suppressAnimation ) ;
@@ -656,15 +663,18 @@ define(function (require, exports, module) {
656663 * @return {{query: string, caseSensitive: boolean, isRegexp: boolean} }
657664 */
658665 FindBar . prototype . getQueryInfo = function ( usePlatformLineEndings = true ) {
659- let query = this . $ ( "#find-what" ) . val ( ) || "" ;
666+ const $findWhat = this . $ ( "#find-what" ) ;
667+ const findTextArea = $findWhat [ 0 ] ;
668+ let query = $findWhat . val ( ) || "" ;
660669 const lineEndings = FileUtils . sniffLineEndings ( query ) ;
661670 if ( usePlatformLineEndings && lineEndings === FileUtils . LINE_ENDINGS_LF && brackets . platform === "win" ) {
662671 query = query . replace ( / \n / g, "\r\n" ) ;
663672 }
664673 return {
665674 query : query ,
666675 isCaseSensitive : this . $ ( "#find-case-sensitive" ) . is ( ".active" ) ,
667- isRegexp : this . $ ( "#find-regexp" ) . is ( ".active" )
676+ isRegexp : this . $ ( "#find-regexp" ) . is ( ".active" ) ,
677+ isQueryTextSelected : findTextArea . selectionStart !== findTextArea . selectionEnd
668678 } ;
669679 } ;
670680
@@ -851,7 +861,7 @@ define(function (require, exports, module) {
851861 * @private
852862 * @static
853863 * @param {?FindBar } currentFindBar - The currently open Find Bar, if any.
854- * @param {?Editor } activeEditor - The active editor, if any.
864+ * @param {?Editor } editor - The active editor, if any.
855865 * @return {{query: string, replaceText: string} } An object containing the query and replacement text
856866 * to prepopulate the Find Bar.
857867 */
@@ -872,11 +882,31 @@ define(function (require, exports, module) {
872882 return ! bar . isClosed ( ) ;
873883 }
874884 ) ;
875-
876- if ( openedFindBar ) {
885+ // this happens when the find in files bar is opened and we are trying to open single file search or
886+ // vice versa. we need to detect the other findbar and determine what is the search term to use
887+
888+ //debugger
889+ const currentQueryInfo = openedFindBar && openedFindBar . getQueryInfo ( ) ;
890+ if ( ! openedFindBar && selection ) {
891+ // when no findbar is open, the selected text always takes precedence in both single and multi file
892+ query = selection ;
893+ } else if ( openedFindBar && selection && currentQueryInfo && ! currentQueryInfo . isRegexp && currentQueryInfo . isQueryTextSelected ) {
894+ // we are switching between single<>multi file search without the user editing the search text in between
895+ // while there is an active selection, the selection takes precedence.
896+ query = selection ;
897+ replaceText = openedFindBar . getReplaceText ( ) ;
898+ } else if ( openedFindBar ) {
899+ // there is no selection and we are switching between single<>multi file search, copy the
900+ // current query from the open findbar as is
877901 query = openedFindBar . getQueryInfo ( ) . query ;
878902 replaceText = openedFindBar . getReplaceText ( ) ;
903+ } else if ( lastClosedQuery ) {
904+ // these is no open find bar currently and there is no selection, but there is a last saved query, so
905+ // load the last query. this happenes on all freash search cases apart from the very first time
906+ query = lastClosedQuery . query ;
907+ replaceText = lastClosedQuery . replaceText ;
879908 } else if ( editor ) {
909+ // the very first query after app start, nothing to restore.
880910 query = ( ! lastTypedTextWasRegexp && selection ) || lastQueriedText || lastTypedText ;
881911 }
882912 }
0 commit comments