Skip to content

Commit 9229ae3

Browse files
committed
fix(search): only replace files that were in the confirm dialog
results kept coming in while the dialog was open, so replace all could touch files that weren't in the count
1 parent 1a825e8 commit 9229ae3

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

‎src/sidebarApps/searchInFiles/index.js‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const workers = [];
2626
const results = [];
2727
const filesSearched = [];
2828
const filesReplaced = [];
29+
/** Number of files the current Replace All was confirmed for. */
30+
let filesToReplaceCount = 0;
2931

3032
const $container = Ref();
3133
const $regExp = Ref();
@@ -366,7 +368,7 @@ async function onWorkerMessage(e) {
366368
const { file, text } = data;
367369
filesReplaced.push(file);
368370
openFile(file.url, {
369-
render: filesSearched.length === filesReplaced.length,
371+
render: filesReplaced.length === filesToReplaceCount,
370372
text,
371373
});
372374
break;
@@ -796,7 +798,7 @@ function sendNativeSearch(
796798
case "replace-result":
797799
filesReplaced.push(event.file);
798800
openFile(event.file.url, {
799-
render: filesSearched.length === filesReplaced.length,
801+
render: filesReplaced.length === filesToReplaceCount,
800802
text: event.text,
801803
});
802804
break;
@@ -925,34 +927,34 @@ function withTimeout(promise, ms) {
925927
* Sends a message to the worker threads to perform the replacement.
926928
*/
927929
async function replaceAll() {
928-
if (filesSearched.length) {
930+
// Snapshot everything before the dialog: the search keeps running while it
931+
// is open, so only the files the user was shown may be replaced.
932+
const files = [...filesSearched];
933+
const search = $search.value;
934+
const replace = $replace.value;
935+
const options = getOptions();
936+
if (!search || !replace) return;
937+
const regex = toRegex(search, options);
938+
if (!regex) return;
939+
940+
if (files.length) {
929941
const confirmed = await confirm(
930942
strings.warning,
931943
strings["replace all confirm"]
932944
.replace("{matches}", resultOverview.matchesCount)
933-
.replace("{files}", resultOverview.filesCount),
945+
.replace("{files}", files.length),
934946
);
935947
if (!confirmed) return;
936948
}
937949

938950
terminateWorker();
939951
filesReplaced.length = 0;
940-
941-
const search = $search.value;
942-
const replace = $replace.value;
943-
const options = getOptions();
944-
if (!search || !replace) return;
945-
const regex = toRegex(search, options);
946-
if (!regex) return;
952+
filesToReplaceCount = files.length;
947953

948954
replacing = true;
949955
activeReplaceTasks = 0;
950-
const nativeFiles = filesSearched.filter((file) =>
951-
supportsNativeSearch(file.url),
952-
);
953-
const workerFiles = filesSearched.filter(
954-
(file) => !supportsNativeSearch(file.url),
955-
);
956+
const nativeFiles = files.filter((file) => supportsNativeSearch(file.url));
957+
const workerFiles = files.filter((file) => !supportsNativeSearch(file.url));
956958
if (nativeFiles.length) {
957959
activeReplaceTasks += 1;
958960
sendNativeSearch("replace", nativeFiles, search, options, replace);

0 commit comments

Comments
 (0)