@@ -385,20 +385,8 @@ define(function (require, exports, module) {
385385 var self = this ;
386386 setTimeout ( function ( ) {
387387 if ( e . keyCode === KeyEvent . DOM_VK_ESCAPE ) {
388- self . close ( ) ;
389-
390- // This is tricky. We want to restore the original selection, and we don't actually want to wait
391- // until the modal bar has fully animated closed. However, we do need to wait until the next
392- // event loop after ModalBar.close() has (synchronously) finished, because ModalBar.close() itself
393- // tries to rescroll the editor (in order to restore the *apparent* scroll position that gets
394- // changed when the ModalBar pushes the editor down). So we do this on yet another timeout.
395- setTimeout ( function ( ) {
396- // Restore original selection / scroll pos
397- if ( self . _origSelection ) {
398- EditorManager . getCurrentFullEditor ( ) . setSelection ( self . _origSelection . start , self . _origSelection . end ) ;
399- EditorManager . getCurrentFullEditor ( ) . setScrollPos ( self . _origScrollPos . x , self . _origScrollPos . y ) ;
400- }
401- } , 0 ) ;
388+ // Restore original selection / scroll pos
389+ self . close ( self . _origScrollPos , self . _origSelection ) ;
402390 } else if ( e . keyCode === KeyEvent . DOM_VK_RETURN ) {
403391 self . _handleItemSelect ( null , $ ( ".smart_autocomplete_highlight" ) . get ( 0 ) ) ; // calls close() too
404392 }
@@ -452,9 +440,13 @@ define(function (require, exports, module) {
452440 /**
453441 * Closes the search dialog and notifies all quick open plugins that
454442 * searching is done.
443+ * @param {{x: number, y: number}= } scrollPos If specified, scroll to the given
444+ * position when closing the ModalBar.
445+ * @param {{start: {line: number, ch: number}, end: {line: number, ch: number} } selection If specified,
446+ * restore the given selection when closing the ModalBar.
455447 * @return {$.Promise } Resolved when the search bar is entirely closed.
456448 */
457- QuickNavigateDialog . prototype . close = function ( ) {
449+ QuickNavigateDialog . prototype . close = function ( scrollPos , selection ) {
458450 if ( ! this . isOpen ) {
459451 return this . _closeDeferred . promise ( ) ;
460452 }
@@ -481,9 +473,20 @@ define(function (require, exports, module) {
481473 // So we wait until after this call chain is complete before actually closing the dialog.
482474 var self = this ;
483475 setTimeout ( function ( ) {
484- self . modalBar . close ( ) . done ( function ( ) {
476+ self . modalBar . close ( ! scrollPos ) . done ( function ( ) {
485477 self . _closeDeferred . resolve ( ) ;
486478 } ) ;
479+
480+ // Note that we deliberately reset the scroll position synchronously on return from
481+ // `ModalBar.close()` (before the animation completes).
482+ // See description of `restoreScrollPos` in `ModalBar.close()`.
483+ var editor = EditorManager . getCurrentFullEditor ( ) ;
484+ if ( selection ) {
485+ editor . setSelection ( selection . start , selection . end ) ;
486+ }
487+ if ( scrollPos ) {
488+ editor . setScrollPos ( scrollPos . x , scrollPos . y ) ;
489+ }
487490 } , 0 ) ;
488491
489492 $ ( ".smart_autocomplete_container" ) . remove ( ) ;
0 commit comments