@@ -32,6 +32,7 @@ define(function (require, exports, module) {
3232 MainViewManager = require ( "view/MainViewManager" ) ,
3333 FileSystem = require ( "filesystem/FileSystem" ) ,
3434 FileUtils = require ( "file/FileUtils" ) ,
35+ FindBar = require ( "search/FindBar" ) . FindBar ,
3536 ProjectManager = require ( "project/ProjectManager" ) ,
3637 Strings = require ( "strings" ) ,
3738 StringUtils = require ( "utils/StringUtils" ) ,
@@ -71,19 +72,50 @@ define(function (require, exports, module) {
7172 return replaceWith ;
7273 }
7374
74- /*
75- * Returns the string used to prepopulate the find bar
76- * @param {!Editor } editor
77- * @return {string } first line of primary selection to populate the find bar
75+ /**
76+ * Gets you the right query and replace text to prepopulate the Find Bar.
77+ * @param {?FindBar } currentFindBar The currently open Find Bar, if any
78+ * @param {?Editor } The active editor, if any
79+ * @return {query: string, replaceText: string } Query and Replace text to prepopulate the Find Bar with
7880 */
79- function getInitialQueryFromSelection ( editor ) {
80- var selectionText = editor . getSelectedText ( ) ;
81- if ( selectionText ) {
82- return selectionText
83- . replace ( / ^ \n * / , "" ) // Trim possible newlines at the very beginning of the selection
84- . split ( "\n" ) [ 0 ] ;
81+ function getInitialQuery ( currentFindBar , editor ) {
82+ var query = "" ,
83+ replaceText = "" ;
84+
85+ /*
86+ * Returns the string used to prepopulate the find bar
87+ * @param {!Editor } editor
88+ * @return {string } first line of primary selection to populate the find bar
89+ */
90+ function getInitialQueryFromSelection ( editor ) {
91+ var selectionText = editor . getSelectedText ( ) ;
92+ if ( selectionText ) {
93+ return selectionText
94+ . replace ( / ^ \n * / , "" ) // Trim possible newlines at the very beginning of the selection
95+ . split ( "\n" ) [ 0 ] ;
96+ }
97+ return "" ;
8598 }
86- return "" ;
99+
100+ if ( currentFindBar && ! currentFindBar . isClosed ( ) ) {
101+ // The modalBar was already up. When creating the new modalBar, copy the
102+ // current query instead of using the passed-in selected text.
103+ query = currentFindBar . getQueryInfo ( ) . query ;
104+ replaceText = currentFindBar . getReplaceText ( ) ;
105+ } else {
106+ var openedFindBar = FindBar . _bars && _ . find ( FindBar . _bars , function ( bar ) {
107+ return ! bar . isClosed ( ) ;
108+ } ) ;
109+
110+ if ( openedFindBar ) {
111+ query = openedFindBar . getQueryInfo ( ) . query ;
112+ replaceText = openedFindBar . getReplaceText ( ) ;
113+ } else if ( editor ) {
114+ query = getInitialQueryFromSelection ( editor ) ;
115+ }
116+ }
117+
118+ return { query : query , replaceText : replaceText } ;
87119 }
88120
89121 /**
@@ -311,7 +343,7 @@ define(function (require, exports, module) {
311343 }
312344
313345 exports . parseDollars = parseDollars ;
314- exports . getInitialQueryFromSelection = getInitialQueryFromSelection ;
346+ exports . getInitialQuery = getInitialQuery ;
315347 exports . hasCheckedMatches = hasCheckedMatches ;
316348 exports . performReplacements = performReplacements ;
317349 exports . labelForScope = labelForScope ;
0 commit comments