@@ -304,27 +304,41 @@ private void Replace()
304304 for ( var i = editorIndex ; i < ( editors . Count + editorIndex + 1 ) ; ++ i )
305305 {
306306 var index = ValueUnderMap ( i , editors . Count ) ;
307+ var currEditor = editors [ index ] . editor ;
307308 string searchText ;
308309 var addToOffset = 0 ;
309310 if ( i == editorIndex )
310311 {
311- startFileCaretOffset = editors [ index ] . editor . CaretOffset ;
312+ // Set caret to beggining of the word its standing on
313+ var docText = currEditor . Text ;
314+ for ( int j = currEditor . CaretOffset - 1 ; j >= 0 ; j -- )
315+ {
316+ if ( ! char . IsLetterOrDigit ( docText [ j ] ) )
317+ {
318+ currEditor . CaretOffset = j ;
319+ break ;
320+ }
321+ if ( j == 0 )
322+ {
323+ currEditor . CaretOffset = 0 ;
324+ }
325+ }
326+
327+ startFileCaretOffset = currEditor . CaretOffset ;
312328 addToOffset = startFileCaretOffset ;
313329 if ( startFileCaretOffset < 0 )
314330 {
315331 startFileCaretOffset = 0 ;
316332 }
317- searchText = editors [ index ] . editor . Text . Substring ( startFileCaretOffset ) ;
333+ searchText = currEditor . Text . Substring ( startFileCaretOffset ) ;
318334 }
319- else if ( i == ( editors . Count + editorIndex ) )
335+ else if ( i == editors . Count + editorIndex )
320336 {
321- searchText = startFileCaretOffset == 0 ?
322- string . Empty :
323- editors [ index ] . editor . Text . Substring ( 0 , startFileCaretOffset ) ;
337+ searchText = startFileCaretOffset == 0 ? string . Empty : currEditor . Text . Substring ( 0 , startFileCaretOffset ) ;
324338 }
325339 else
326340 {
327- searchText = editors [ index ] . editor . Text ;
341+ searchText = currEditor . Text ;
328342 }
329343 if ( ! string . IsNullOrWhiteSpace ( searchText ) )
330344 {
@@ -334,11 +348,11 @@ private void Replace()
334348 foundOccurence = true ;
335349 editors [ index ] . Parent . IsSelected = true ;
336350 var result = m . Result ( replaceString ) ;
337- editors [ index ] . editor . Document . Replace ( m . Index + addToOffset , m . Length , result ) ;
338- editors [ index ] . editor . CaretOffset = m . Index + addToOffset + result . Length ;
339- editors [ index ] . editor . Select ( m . Index + addToOffset , result . Length ) ;
340- var location = editors [ index ] . editor . Document . GetLocation ( m . Index + addToOffset ) ;
341- editors [ index ] . editor . ScrollTo ( location . Line , location . Column ) ;
351+ currEditor . Document . Replace ( m . Index + addToOffset , m . Length , result ) ;
352+ currEditor . CaretOffset = m . Index + addToOffset + result . Length ;
353+ currEditor . Select ( m . Index + addToOffset , result . Length ) ;
354+ var location = currEditor . Document . GetLocation ( m . Index + addToOffset ) ;
355+ currEditor . ScrollTo ( location . Line , location . Column ) ;
342356 FindResultBlock . Text = string . Format ( Translate ( "ReplacedOff" ) , MinHeight + addToOffset ) ;
343357 break ;
344358 }
0 commit comments