Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
markdown_preview: Fix navigating back to the preview tab (#65299)
Kashish Sahu9f94d1316036
diff --git a/assets/keymaps/vim.json b/assets/keymaps/vim.jsonindex 133e3aa16a1..b8152333bde 100644--- a/assets/keymaps/vim.json+++ b/assets/keymaps/vim.json@@ -1216,6 +1216,8 @@"ctrl-d": "markdown::ScrollPageDown","ctrl-y": "markdown::ScrollUp","ctrl-e": "markdown::ScrollDown",+ "ctrl-o": "pane::GoBack",+ "ctrl-i": "pane::GoForward","g g": "markdown::ScrollToTop","shift-g": "markdown::ScrollToBottom","/": "buffer_search::Deploy",diff --git a/crates/markdown_preview/src/markdown_preview_view.rs b/crates/markdown_preview/src/markdown_preview_view.rsindex 81cb5cde8d4..970f562135c 100644--- a/crates/markdown_preview/src/markdown_preview_view.rs+++ b/crates/markdown_preview/src/markdown_preview_view.rs@@ -47,7 +47,9 @@ use workspace::path_link::{PathMatching, resolve_open_target};use workspace::searchable::{Direction, SearchEvent, SearchOptions, SearchToken, SearchableItem, SearchableItemHandle,};-use workspace::{ItemId, Pane, SaveIntent, Workspace, WorkspaceId, delete_unloaded_items};+use workspace::{+ ItemId, ItemNavHistory, Pane, SaveIntent, Workspace, WorkspaceId, delete_unloaded_items,+};use zed_actions::{DecreaseBufferFontSize, IncreaseBufferFontSize, ResetBufferFontSize};use crate::markdown_preview_settings::MarkdownPreviewSettings;@@ -82,6 +84,7 @@ pub struct MarkdownPreviewView {/// Search results depend on the parsed markdown, which lags behind the source while a/// background parse is in flight. Tracked so matches can be invalidated once it lands.markdown_parse_pending: bool,+ nav_history: Option<ItemNavHistory>,}#[derive(Clone, Copy, Debug, PartialEq)]@@ -424,6 +427,7 @@ impl MarkdownPreviewView {hovered_url: None,mode,markdown_parse_pending: false,+ nav_history: None,};this.set_editor(active_editor, window, cx);@@ -1825,6 +1829,21 @@ impl Item for MarkdownPreviewView {cx,)))}++ fn set_nav_history(+ &mut self,+ history: ItemNavHistory,+ _window: &mut Window,+ _: &mut Context<Self>,+ ) {+ self.nav_history = Some(history);+ }++ fn deactivated(&mut self, _window: &mut Window, cx: &mut Context<Self>) {+ if let Some(nav_history) = self.nav_history.as_mut() {+ nav_history.push::<()>(None, None, cx);+ }+ }}impl Render for MarkdownPreviewView {@@ -3361,6 +3380,40 @@ mod tests {});}+ #[gpui::test]+ async fn go_back_returns_to_preview_after_opening_another_file(cx: &mut TestAppContext) {+ let (project, workspace, multi_workspace) = markdown_workspace(+ cx,+ json!({ "note.md": "# Note\n", "other.txt": "Other File\n" }),+ false,+ )+ .await;++ open_project_file(cx, &project, &multi_workspace, "note.md", None, true).await;+ let preview = open_preview_for_active_editor(cx, &multi_workspace);+ open_project_file(cx, &project, &multi_workspace, "other.txt", None, true).await;++ let go_back = multi_workspace+ .update(cx, |multi_workspace, window, cx| {+ multi_workspace.workspace().update(cx, |workspace, cx| {+ let pane = workspace.active_pane().downgrade();+ workspace.go_back(pane, window, cx)+ })+ })+ .unwrap();++ go_back.await.unwrap();+ cx.run_until_parked();++ workspace.read_with(cx, |workspace, cx| {+ assert_eq!(+ workspace.active_item_as::<MarkdownPreviewView>(cx),+ Some(preview.clone()),+ "going back should return to the preview"+ );+ });+ }+#[gpui::test]async fn close_and_return_to_editor_closes_preview_and_focuses_source_editor(cx: &mut TestAppContext,