@@ -304,27 +304,41 @@ private void Replace()
304
304
for ( var i = editorIndex ; i < ( editors . Count + editorIndex + 1 ) ; ++ i )
305
305
{
306
306
var index = ValueUnderMap ( i , editors . Count ) ;
307
+ var currEditor = editors [ index ] . editor ;
307
308
string searchText ;
308
309
var addToOffset = 0 ;
309
310
if ( i == editorIndex )
310
311
{
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 ;
312
328
addToOffset = startFileCaretOffset ;
313
329
if ( startFileCaretOffset < 0 )
314
330
{
315
331
startFileCaretOffset = 0 ;
316
332
}
317
- searchText = editors [ index ] . editor . Text . Substring ( startFileCaretOffset ) ;
333
+ searchText = currEditor . Text . Substring ( startFileCaretOffset ) ;
318
334
}
319
- else if ( i == ( editors . Count + editorIndex ) )
335
+ else if ( i == editors . Count + editorIndex )
320
336
{
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 ) ;
324
338
}
325
339
else
326
340
{
327
- searchText = editors [ index ] . editor . Text ;
341
+ searchText = currEditor . Text ;
328
342
}
329
343
if ( ! string . IsNullOrWhiteSpace ( searchText ) )
330
344
{
@@ -334,11 +348,11 @@ private void Replace()
334
348
foundOccurence = true ;
335
349
editors [ index ] . Parent . IsSelected = true ;
336
350
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 ) ;
342
356
FindResultBlock . Text = string . Format ( Translate ( "ReplacedOff" ) , MinHeight + addToOffset ) ;
343
357
break ;
344
358
}
0 commit comments