workspace: Fix weird behavior when save replaces the existing open file (#17123)

CharlesChen0823 created

Fixes this weird behavior:
  - open an file, like `test.rs`
  - `ctrl-n` create an new buffer
- `ctrl-s` save new buffer with name `test.rs`, select replace old file.
  - the older open file also exist, this is weird.

Release Notes:

- Fixed two panes staying opening when saving a new buffer with the same filename as a file that was already open.

Change summary

crates/workspace/src/pane.rs | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)

Detailed changes

crates/workspace/src/pane.rs 🔗

@@ -1556,8 +1556,16 @@ impl Pane {
                         .update(cx, |workspace, cx| workspace.prompt_for_new_path(cx))
                 })??;
                 if let Some(abs_path) = abs_path.await.ok().flatten() {
-                    pane.update(cx, |_, cx| item.save_as(project, abs_path, cx))?
-                        .await?;
+                    pane.update(cx, |pane, cx| {
+                        if let Some(item) = pane.item_for_path(abs_path.clone(), cx) {
+                            if let Some(idx) = pane.index_for_item(&*item) {
+                                pane.remove_item(idx, false, false, cx);
+                            }
+                        }
+
+                        item.save_as(project, abs_path, cx)
+                    })?
+                    .await?;
                 } else {
                     return Ok(false);
                 }