From c186877ff740c41a225c268f9c0bd47496d2acb2 Mon Sep 17 00:00:00 2001 From: Gaauwe Rombouts Date: Wed, 17 Dec 2025 15:29:48 +0100 Subject: [PATCH] lsp: Open updated imports in multibuffer after file rename (#45110) Fixes an issue where we would update the imports after a file rename in TypeScript, but those changes wouldn't surface anywhere until those buffers were manually opened (https://github.com/zed-industries/zed/issues/35930#issuecomment-3366852945). In https://github.com/zed-industries/zed/pull/36681 we already added support for opening a multibuffer with edits, but vtsls has a different flow for renames. Release Notes: - Files with updated imports now open in a multibuffer when renaming or moving TypeScript or JavaScript files --- crates/editor/src/editor.rs | 114 +++++++++++++++++++++----------- crates/project/src/lsp_store.rs | 5 +- crates/project/src/project.rs | 4 ++ 3 files changed, 82 insertions(+), 41 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 4072b1db7a1935e5dbb9c63d2a3aa19db270f131..a66d92ceb8b0b6fa1631d0636294fbe3bd42af06 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -2063,46 +2063,34 @@ impl Editor { }) }); }); - let edited_buffers_already_open = { - let other_editors: Vec> = workspace - .read(cx) - .panes() - .iter() - .flat_map(|pane| pane.read(cx).items_of_type::()) - .filter(|editor| editor.entity_id() != cx.entity_id()) - .collect(); - - transaction.0.keys().all(|buffer| { - other_editors.iter().any(|editor| { - let multi_buffer = editor.read(cx).buffer(); - multi_buffer.read(cx).is_singleton() - && multi_buffer.read(cx).as_singleton().map_or( - false, - |singleton| { - singleton.entity_id() == buffer.entity_id() - }, - ) - }) - }) - }; - if !edited_buffers_already_open { - let workspace = workspace.downgrade(); - let transaction = transaction.clone(); - cx.defer_in(window, move |_, window, cx| { - cx.spawn_in(window, async move |editor, cx| { - Self::open_project_transaction( - &editor, - workspace, - transaction, - "Rename".to_string(), - cx, - ) - .await - .ok() - }) - .detach(); - }); - } + + Self::open_transaction_for_hidden_buffers( + workspace, + transaction.clone(), + "Rename".to_string(), + window, + cx, + ); + } + } + + project::Event::WorkspaceEditApplied(transaction) => { + let Some(workspace) = editor.workspace() else { + return; + }; + let Some(active_editor) = workspace.read(cx).active_item_as::(cx) + else { + return; + }; + + if active_editor.entity_id() == cx.entity_id() { + Self::open_transaction_for_hidden_buffers( + workspace, + transaction.clone(), + "LSP Edit".to_string(), + window, + cx, + ); } } @@ -6672,6 +6660,52 @@ impl Editor { } } + fn open_transaction_for_hidden_buffers( + workspace: Entity, + transaction: ProjectTransaction, + title: String, + window: &mut Window, + cx: &mut Context, + ) { + if transaction.0.is_empty() { + return; + } + + let edited_buffers_already_open = { + let other_editors: Vec> = workspace + .read(cx) + .panes() + .iter() + .flat_map(|pane| pane.read(cx).items_of_type::()) + .filter(|editor| editor.entity_id() != cx.entity_id()) + .collect(); + + transaction.0.keys().all(|buffer| { + other_editors.iter().any(|editor| { + let multi_buffer = editor.read(cx).buffer(); + multi_buffer.read(cx).is_singleton() + && multi_buffer + .read(cx) + .as_singleton() + .map_or(false, |singleton| { + singleton.entity_id() == buffer.entity_id() + }) + }) + }) + }; + if !edited_buffers_already_open { + let workspace = workspace.downgrade(); + cx.defer_in(window, move |_, window, cx| { + cx.spawn_in(window, async move |editor, cx| { + Self::open_project_transaction(&editor, workspace, transaction, title, cx) + .await + .ok() + }) + .detach(); + }); + } + } + pub async fn open_project_transaction( editor: &WeakEntity, workspace: WeakEntity, diff --git a/crates/project/src/lsp_store.rs b/crates/project/src/lsp_store.rs index f3ebac277b027232d2043213aa393492dbc1dfdb..6696ec8c4c280199a55d098ab63a321f126eea5e 100644 --- a/crates/project/src/lsp_store.rs +++ b/crates/project/src/lsp_store.rs @@ -3311,8 +3311,10 @@ impl LocalLspStore { ) .await .log_err(); - this.update(cx, |this, _| { + this.update(cx, |this, cx| { if let Some(transaction) = transaction { + cx.emit(LspStoreEvent::WorkspaceEditApplied(transaction.clone())); + this.as_local_mut() .unwrap() .last_workspace_edits_by_language_server @@ -3852,6 +3854,7 @@ pub enum LspStoreEvent { edits: Vec<(lsp::Range, Snippet)>, most_recent_edit: clock::Lamport, }, + WorkspaceEditApplied(ProjectTransaction), } #[derive(Clone, Debug, Serialize)] diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index 9152096508b76d34fe3b2209cba94b4755b6ac67..b348fcdae2c414aa1b3f34f616ca3426899fe1d3 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -350,6 +350,7 @@ pub enum Event { SnippetEdit(BufferId, Vec<(lsp::Range, Snippet)>), ExpandedAllForEntry(WorktreeId, ProjectEntryId), EntryRenamed(ProjectTransaction, ProjectPath, PathBuf), + WorkspaceEditApplied(ProjectTransaction), AgentLocationChanged, } @@ -3249,6 +3250,9 @@ impl Project { cx.emit(Event::SnippetEdit(*buffer_id, edits.clone())) } } + LspStoreEvent::WorkspaceEditApplied(transaction) => { + cx.emit(Event::WorkspaceEditApplied(transaction.clone())) + } } }