@@ -26,6 +26,8 @@ const workers = [];
2626const results = [ ] ;
2727const filesSearched = [ ] ;
2828const filesReplaced = [ ] ;
29+ /** Number of files the current Replace All was confirmed for. */
30+ let filesToReplaceCount = 0 ;
2931
3032const $container = Ref ( ) ;
3133const $regExp = Ref ( ) ;
@@ -371,7 +373,7 @@ async function onWorkerMessage(e) {
371373 const { file, text } = data ;
372374 filesReplaced . push ( file ) ;
373375 openFile ( file . url , {
374- render : filesSearched . length === filesReplaced . length ,
376+ render : filesReplaced . length === filesToReplaceCount ,
375377 text,
376378 } ) ;
377379 break ;
@@ -801,7 +803,7 @@ function sendNativeSearch(
801803 case "replace-result" :
802804 filesReplaced . push ( event . file ) ;
803805 openFile ( event . file . url , {
804- render : filesSearched . length === filesReplaced . length ,
806+ render : filesReplaced . length === filesToReplaceCount ,
805807 text : event . text ,
806808 } ) ;
807809 break ;
@@ -930,35 +932,35 @@ function withTimeout(promise, ms) {
930932 * Sends a message to the worker threads to perform the replacement.
931933 */
932934async function replaceAll ( ) {
933- if ( filesSearched . length ) {
935+ // Snapshot everything before the dialog: the search keeps running while it
936+ // is open, so only the files the user was shown may be replaced.
937+ const files = [ ...filesSearched ] ;
938+ const search = $search . value ;
939+ const replace = $replace . value ;
940+ const options = getOptions ( ) ;
941+ // An empty replacement is valid: it deletes every match.
942+ if ( ! search ) return ;
943+ const regex = toRegex ( search , options ) ;
944+ if ( ! regex ) return ;
945+
946+ if ( files . length ) {
934947 const confirmed = await confirm (
935948 strings . warning ,
936949 strings [ "replace all confirm" ]
937950 . replace ( "{matches}" , resultOverview . matchesCount )
938- . replace ( "{files}" , resultOverview . filesCount ) ,
951+ . replace ( "{files}" , files . length ) ,
939952 ) ;
940953 if ( ! confirmed ) return ;
941954 }
942955
943956 terminateWorker ( ) ;
944957 filesReplaced . length = 0 ;
945-
946- const search = $search . value ;
947- const replace = $replace . value ;
948- const options = getOptions ( ) ;
949- // An empty replacement is valid: it deletes every match.
950- if ( ! search ) return ;
951- const regex = toRegex ( search , options ) ;
952- if ( ! regex ) return ;
958+ filesToReplaceCount = files . length ;
953959
954960 replacing = true ;
955961 activeReplaceTasks = 0 ;
956- const nativeFiles = filesSearched . filter ( ( file ) =>
957- supportsNativeSearch ( file . url ) ,
958- ) ;
959- const workerFiles = filesSearched . filter (
960- ( file ) => ! supportsNativeSearch ( file . url ) ,
961- ) ;
962+ const nativeFiles = files . filter ( ( file ) => supportsNativeSearch ( file . url ) ) ;
963+ const workerFiles = files . filter ( ( file ) => ! supportsNativeSearch ( file . url ) ) ;
962964 if ( nativeFiles . length ) {
963965 activeReplaceTasks += 1 ;
964966 sendNativeSearch ( "replace" , nativeFiles , search , options , replace ) ;
0 commit comments