@@ -28,6 +28,22 @@ export default function(CodeMirror) {
2828 } } ;
2929 }
3030
31+ function replaceOverlay ( cursorFrom , cursorTo , flag ) {
32+ return { token : function ( stream ) {
33+ if ( ! flag && cursorFrom . line == stream . lineOracle . line ) {
34+ if ( stream . pos == 0 ) {
35+ stream . pos = cursorFrom . ch ;
36+ } else {
37+ stream . pos = cursorTo . ch ;
38+ flag = true ;
39+ return 'searching' ;
40+ }
41+ } else {
42+ stream . skipToEnd ( ) ;
43+ }
44+ } } ;
45+ }
46+
3147 function SearchState ( ) {
3248 this . posFrom = this . posTo = this . lastQuery = this . query = null ;
3349 this . overlay = null ;
@@ -227,12 +243,19 @@ export default function(CodeMirror) {
227243 buttons [ nextButton ] . focus ( ) ;
228244 }
229245 if ( e . keyCode === 27 ) { // esc
246+ // Remove replace-overlay & focus on the editor.
247+ const state = getSearchState ( cm ) ;
248+ cm . removeOverlay ( state . overlay , state . caseInsensitive ) ;
230249 cm . focus ( ) ;
231250 }
232251 } ) ;
233252 button . addEventListener ( "click" , function ( ) {
234253 if ( index === buttons . length - 1 ) { // "done"
235254 lastSelectedIndex = 0 ;
255+ // Remove replace-overlay & focus on the editor.
256+ const state = getSearchState ( cm ) ;
257+ cm . removeOverlay ( state . overlay , state . caseInsensitive ) ;
258+ cm . focus ( ) ;
236259 }
237260 } )
238261 } ) ( i ) ;
@@ -416,7 +439,13 @@ export default function(CodeMirror) {
416439 if ( ! ( match = cursor . findNext ( ) ) ||
417440 ( start && cursor . from ( ) . line == start . line && cursor . from ( ) . ch == start . ch ) ) return ;
418441 }
419- cm . setSelection ( cursor . from ( ) , cursor . to ( ) ) ;
442+
443+ // Add an overlay to the text to be replaced.
444+ cm . removeOverlay ( state . overlay , state . caseInsensitive ) ;
445+ var flag = false ;
446+ state . overlay = replaceOverlay ( cursor . from ( ) , cursor . to ( ) , flag ) ;
447+ cm . addOverlay ( state . overlay ) ;
448+
420449 cm . scrollIntoView ( { from : cursor . from ( ) , to : cursor . to ( ) } , 60 ) ;
421450 confirmDialog ( cm , doReplaceConfirm , "Replace?" ,
422451 [ function ( ) { doReplace ( match ) ; } , advance ,
0 commit comments