From 0fd0f08865c0421bbcf60e6332dd73da77a66ec8 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Wed, 18 Nov 2026 19:35:26 +0800 Subject: [PATCH] fix(search): Replace All saves open documents that had no unsaved changes (#4286) Replace All saved only files it opened itself, so a file the search preview held open got the replacement in memory but not on disk. Documents without unsaved changes are now saved after the replacement; buffers the user is editing are left unsaved. Fixes #4202. --- crates/ide-host/src/ops/search/commands.rs | 9 ++-- crates/ide-host/tests/replace_all.rs | 49 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/crates/ide-host/src/ops/search/commands.rs b/crates/ide-host/src/ops/search/commands.rs index fba2c4393..dea2aa20e 100644 --- a/crates/ide-host/src/ops/search/commands.rs +++ b/crates/ide-host/src/ops/search/commands.rs @@ -15,6 +15,9 @@ impl ReplaceHost for DocumentsHost<'_> { Some(doc) => (doc, false), None => (docs.open(path)?.id, true), }; + // A buffer without unsaved changes (open only for the search preview, or a clean + // tab) is saved like a closed file; one the user is editing is left unsaved. + let save = transient || !docs.is_dirty(doc)?; let result = (|| { // Recompute if the user typed between reading the text and applying. for _ in 0..3 { @@ -27,7 +30,7 @@ impl ReplaceHost for DocumentsHost<'_> { continue; } docs.apply_edits(doc, &edits)?; - if transient { + if save { docs.save(doc)?; } return Ok(edits.len() as u32); @@ -46,8 +49,8 @@ impl ReplaceHost for DocumentsHost<'_> { /// /// Edits go through the document layer as one patch per file, so open editors /// update live and undo the whole file's replacements in one step. Unsaved -/// editor buffers are replaced in place and left unsaved; files that were not -/// open are opened transiently, saved and closed. +/// editor buffers are replaced in place and left unsaved; other open documents +/// are saved, and files that were not open are opened transiently, saved and closed. /// /// With `all_in_scope` (Replace All after the search hit its match limit) the /// scope is searched again without the limit and every match in it is replaced, diff --git a/crates/ide-host/tests/replace_all.rs b/crates/ide-host/tests/replace_all.rs index 82fce9917..29c8fb3d2 100644 --- a/crates/ide-host/tests/replace_all.rs +++ b/crates/ide-host/tests/replace_all.rs @@ -146,3 +146,52 @@ async fn replace_all_covers_matches_past_the_limit() { host.close_project("main"); } } + +/// #4202: a file the search preview holds open (no unsaved changes) is saved like a closed +/// one; a buffer with unsaved edits is replaced in place and left for the editor to save. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn replace_all_saves_open_buffers_without_unsaved_changes() { + let temp = tempfile::tempdir().unwrap(); + let base = dunce::canonicalize(temp.path()).unwrap(); + let root = base.join("project"); + std::fs::create_dir_all(&root).unwrap(); + for name in ["previewed.txt", "edited.txt", "closed.txt"] { + std::fs::write(root.join(name), "TOKEN\n").unwrap(); + } + let host = Host::new(HostConfig { + config_dir: base.join("config"), + data_dir: base.join("data"), + local_data_dir: base.join("data"), + languages: Arc::new(ide_language::LanguageRegistry::load(None).unwrap()), + syntax: Arc::default(), + sink_for: Box::new(|_| Arc::new(NullSink)), + owner: |w| w, + runtime: tokio::runtime::Handle::current(), + project_environment: false, + }); + host.trust.decide(&root, true, false).unwrap(); + host.open_project("main", &root).unwrap(); + let project = host.project("main").unwrap(); + project.documents.open(&root.join("previewed.txt")).unwrap(); + let edited = project.documents.open(&root.join("edited.txt")).unwrap().id; + project.documents.replace_all(edited, "TOKEN\nmore\n").unwrap(); + ops::search::replace_in_files( + &host, + "main", + RequestId::next(), + query("TOKEN", false), + "DONE".into(), + false, + vec![], + true, + ops::Channel::new(|_| {}), + ) + .await + .unwrap(); + let disk = |name: &str| std::fs::read_to_string(root.join(name)).unwrap(); + assert_eq!(disk("previewed.txt"), "DONE\n"); + assert_eq!(disk("closed.txt"), "DONE\n"); + assert_eq!(disk("edited.txt"), "TOKEN\n", "unsaved edits stay unsaved"); + assert_eq!(project.documents.text_snapshot(edited).unwrap().text, "DONE\nmore\n"); + host.close_project("main"); +}