Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

markdown_preview: Show directory for preview tabs when tab title is ambiguous (#65736)

Kashish Sahu60e8c1859864
diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs
index 6dff2b30ae8..060cf0665bd 100644
--- a/crates/editor/src/items.rs
+++ b/crates/editor/src/items.rs
@@ -796,15 +796,9 @@ impl Item for Editor {
};
let description = params.detail.and_then(|detail| {
- let path = path_for_buffer(&self.buffer, detail, false, cx)?;
- let description = path.trim();
-
- if description.is_empty() {
- return None;
- }
-
+ let description = self.tab_description(detail, cx)?;
Some(util::truncate_and_trailoff(
- description,
+ &description,
params.max_title_len.unwrap_or(MAX_TAB_TITLE_LEN),
))
});
@@ -1653,6 +1647,17 @@ fn clip_ranges<'a>(
impl EventEmitter<SearchEvent> for Editor {}
impl Editor {
+ /// Panes keep increasing `detail` until tabs for files with the same name look different.
+ pub fn tab_description(&self, detail: usize, cx: &App) -> Option<SharedString> {
+ let path = path_for_buffer(&self.buffer, detail, false, cx)?;
+ let description = path.trim();
+ if description.is_empty() {
+ None
+ } else {
+ Some(description.to_string().into())
+ }
+ }
+
pub fn update_restoration_data(
&self,
cx: &mut Context<Self>,
diff --git a/crates/markdown_preview/src/markdown_preview_view.rs b/crates/markdown_preview/src/markdown_preview_view.rs
index 3b0e9f6eb46..10470ff9318 100644
--- a/crates/markdown_preview/src/markdown_preview_view.rs
+++ b/crates/markdown_preview/src/markdown_preview_view.rs
@@ -11,7 +11,8 @@ use anyhow::{Context as _, Result};
use editor::items::open_resolved_target;
use editor::scroll::Autoscroll;
use editor::{
- Editor, EditorEvent, EditorSettingsScrollbarProxy, MultiBufferOffset, SelectionEffects,
+ Editor, EditorEvent, EditorSettingsScrollbarProxy, MAX_TAB_TITLE_LEN, MultiBufferOffset,
+ SelectionEffects,
};
use gpui::{
Action, AnyElement, App, ClipboardItem, Context, Entity, EntityId, EventEmitter, FocusHandle,
@@ -1652,8 +1653,34 @@ impl Item for MarkdownPreviewView {
fn tab_content(&self, params: TabContentParams, _window: &Window, cx: &App) -> AnyElement {
let preview = self.self_handle.clone();
+ let editor = self
+ .active_editor
+ .as_ref()
+ .map(|editor_state| editor_state.editor.read(cx));
+ let max_title_len = params.max_title_len.unwrap_or(MAX_TAB_TITLE_LEN);
+
+ // Like editor tabs, the title is only the file name, since `tab_content_text` can be a
+ // full path for files outside the project. The directories needed to tell apart files
+ // with the same name are shown in the description.
+ let title = match editor {
+ Some(editor) if params.truncate_title_middle => format!("Preview {}", editor.title(cx)),
+ Some(editor) => format!(
+ "Preview {}",
+ util::truncate_and_trailoff(&editor.title(cx), max_title_len)
+ ),
+ None => "Markdown Preview".to_string(),
+ };
+ let description = editor
+ .zip(params.detail)
+ .and_then(|(editor, detail)| editor.tab_description(detail, cx))
+ .map(|description| util::truncate_and_trailoff(&description, max_title_len));
+
h_flex()
+ .gap_1()
.when(!params.selected, |tab| tab.track_focus(&self.focus_handle))
+ .when(params.truncate_title_middle, |this| {
+ this.w_full().min_w_0().overflow_hidden()
+ })
.on_action(move |action: &CloseAndReturnToEditor, window, cx| {
preview
.update(cx, |preview, cx| {
@@ -1662,24 +1689,46 @@ impl Item for MarkdownPreviewView {
.ok();
})
.child(
- Label::new(self.tab_content_text(params.detail.unwrap_or_default(), cx))
+ Label::new(title)
.single_line()
- .color(params.text_color()),
+ .color(params.text_color())
+ .when(params.truncate_title_middle, |this| {
+ this.truncate_middle().flex_1()
+ }),
)
+ .when_some(description, |this, description| {
+ this.child(
+ Label::new(description)
+ .single_line()
+ .size(LabelSize::XSmall)
+ .when(params.truncate_title_middle, |this| {
+ this.truncate_start().flex_shrink()
+ })
+ .color(Color::Muted),
+ )
+ })
.into_any_element()
}
- fn tab_content_text(&self, _detail: usize, cx: &App) -> SharedString {
+ fn tab_content_text(&self, detail: usize, cx: &App) -> SharedString {
self.active_editor
.as_ref()
.map(|editor_state| {
- let buffer = editor_state.editor.read(cx).buffer().read(cx);
- let title = buffer.title(cx);
- format!("Preview {}", title).into()
+ let title = editor_state.editor.read(cx).tab_content_text(detail, cx);
+ format!("Preview {title}").into()
})
.unwrap_or_else(|| SharedString::from("Markdown Preview"))
}
+ // Save As writes the source file, so suggest its name rather than the tab text, which can be a
+ // full path for files outside the project.
+ fn suggested_filename(&self, cx: &App) -> SharedString {
+ self.active_editor
+ .as_ref()
+ .map(|editor_state| editor_state.editor.read(cx).suggested_filename(cx))
+ .unwrap_or_else(|| SharedString::from("Markdown Preview"))
+ }
+
fn tab_tooltip_text(&self, cx: &App) -> Option<SharedString> {
self.active_editor
.as_ref()?
@@ -2322,7 +2371,8 @@ mod tests {
use workspace::item::{Item, ItemHandle, SerializableItem};
use workspace::path_link::{OpenTarget, OpenTargetFoundBy};
use workspace::{
- AppState, ItemId, MultiWorkspace, Pane, SaveIntent, Workspace, WorkspaceId, open_paths,
+ AppState, ItemId, MultiWorkspace, OpenOptions, OpenVisible, Pane, SaveIntent, Workspace,
+ WorkspaceId, open_paths,
};
use super::{
@@ -2376,6 +2426,101 @@ mod tests {
);
}
+ #[gpui::test]
+ async fn preview_tabs_for_files_with_the_same_name_show_their_directory(
+ cx: &mut TestAppContext,
+ ) {
+ let (project, _, multi_workspace) = markdown_workspace(
+ cx,
+ json!({
+ "docs": { "README.md": "# Docs\n" },
+ "guide": { "README.md": "# Guide\n" },
+ }),
+ false,
+ )
+ .await;
+ open_project_file(cx, &project, &multi_workspace, "docs/README.md", None, true).await;
+ let docs_preview = open_preview_for_active_editor(cx, &multi_workspace);
+ open_project_file(
+ cx,
+ &project,
+ &multi_workspace,
+ "guide/README.md",
+ None,
+ true,
+ )
+ .await;
+ open_preview_for_active_editor(cx, &multi_workspace);
+ cx.run_until_parked();
+
+ assert_eq!(
+ docs_preview.read_with(cx, |preview, cx| preview.tab_content_text(0, cx)),
+ "Preview README.md"
+ );
+
+ let mut preview_titles = multi_workspace
+ .update(cx, |multi_workspace, window, cx| {
+ let items = multi_workspace
+ .workspace()
+ .read(cx)
+ .active_pane()
+ .read(cx)
+ .items()
+ .map(|item| item.boxed_clone())
+ .collect::<Vec<_>>();
+ let details = workspace::tab_details(&items, window, cx);
+ items
+ .iter()
+ .zip(details)
+ .filter(|(item, _)| item.downcast::<MarkdownPreviewView>().is_some())
+ .map(|(item, detail)| item.tab_content_text(detail, cx).to_string())
+ .collect::<Vec<_>>()
+ })
+ .unwrap();
+ preview_titles.sort();
+
+ assert_eq!(
+ preview_titles,
+ [
+ format!("Preview {}", path!("docs/README.md")),
+ format!("Preview {}", path!("guide/README.md")),
+ ]
+ );
+ }
+
+ #[gpui::test]
+ async fn preview_suggests_source_file_name_for_file_outside_the_project(
+ cx: &mut TestAppContext,
+ ) {
+ let (project, workspace, multi_workspace) = markdown_workspace(cx, json!({}), false).await;
+ let fs = project.read_with(cx, |project, _| project.fs().as_fake());
+ fs.insert_tree(path!("/outside"), json!({ "README.md": "# Readme\n" }))
+ .await;
+ multi_workspace
+ .update(cx, |_, window, cx| {
+ workspace.update(cx, |workspace, cx| {
+ workspace.open_abs_path(
+ PathBuf::from(path!("/outside/README.md")),
+ OpenOptions {
+ visible: Some(OpenVisible::None),
+ ..Default::default()
+ },
+ window,
+ cx,
+ )
+ })
+ })
+ .unwrap()
+ .await
+ .unwrap();
+ let preview = open_preview_for_active_editor(cx, &multi_workspace);
+
+ assert_eq!(
+ preview.read_with(cx, |preview, cx| preview.suggested_filename(cx)),
+ "README.md"
+ );
+ }
+
#[gpui::test]
fn resetting_persisted_font_size_preserves_unrelated_settings(cx: &mut App) {
let settings_store = settings::SettingsStore::test(cx);