From c39de1f9dc1d59e27be549e62fd99ffe56ecfb0f Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Sat, 2 Apr 2022 16:10:10 +0200 Subject: [PATCH] Show full path for file worktrees or when there is more than 1 worktree --- Cargo.lock | 1 + crates/breadcrumbs/Cargo.toml | 1 + crates/breadcrumbs/src/breadcrumbs.rs | 35 +++++++++++++++++---------- crates/editor/src/multi_buffer.rs | 11 +++++++-- crates/zed/src/zed.rs | 27 ++++++++++++--------- 5 files changed, 48 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 276fbf1525..2c64dd0ba7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -727,6 +727,7 @@ dependencies = [ "editor", "gpui", "language", + "project", "search", "theme", "workspace", diff --git a/crates/breadcrumbs/Cargo.toml b/crates/breadcrumbs/Cargo.toml index 2e74fd2090..7dbafdb3be 100644 --- a/crates/breadcrumbs/Cargo.toml +++ b/crates/breadcrumbs/Cargo.toml @@ -12,6 +12,7 @@ collections = { path = "../collections" } editor = { path = "../editor" } gpui = { path = "../gpui" } language = { path = "../language" } +project = { path = "../project" } search = { path = "../search" } theme = { path = "../theme" } workspace = { path = "../workspace" } diff --git a/crates/breadcrumbs/src/breadcrumbs.rs b/crates/breadcrumbs/src/breadcrumbs.rs index 7085712f8c..0f8ac9274e 100644 --- a/crates/breadcrumbs/src/breadcrumbs.rs +++ b/crates/breadcrumbs/src/breadcrumbs.rs @@ -1,10 +1,11 @@ use editor::{Anchor, Editor}; use gpui::{ - elements::*, AppContext, Entity, RenderContext, Subscription, View, ViewContext, ViewHandle, + elements::*, AppContext, Entity, ModelHandle, RenderContext, Subscription, View, ViewContext, + ViewHandle, }; -use language::{BufferSnapshot, OutlineItem}; +use language::{Buffer, OutlineItem}; +use project::Project; use search::ProjectSearchView; -use std::borrow::Cow; use theme::SyntaxTheme; use workspace::{ItemHandle, Settings, ToolbarItemLocation, ToolbarItemView}; @@ -13,14 +14,16 @@ pub enum Event { } pub struct Breadcrumbs { + project: ModelHandle, editor: Option>, project_search: Option>, subscriptions: Vec, } impl Breadcrumbs { - pub fn new() -> Self { + pub fn new(project: ModelHandle) -> Self { Self { + project, editor: Default::default(), subscriptions: Default::default(), project_search: Default::default(), @@ -31,14 +34,14 @@ impl Breadcrumbs { &self, theme: &SyntaxTheme, cx: &AppContext, - ) -> Option<(BufferSnapshot, Vec>)> { + ) -> Option<(ModelHandle, Vec>)> { let editor = self.editor.as_ref()?.read(cx); let cursor = editor.newest_anchor_selection().head(); - let (buffer, symbols) = editor - .buffer() - .read(cx) + let multibuffer = &editor.buffer().read(cx); + let (buffer_id, symbols) = multibuffer .read(cx) .symbols_containing(cursor, Some(theme))?; + let buffer = multibuffer.buffer(buffer_id)?; Some((buffer, symbols)) } } @@ -60,15 +63,21 @@ impl View for Breadcrumbs { } else { return Empty::new().boxed(); }; - - let filename = if let Some(path) = buffer.path() { - path.to_string_lossy() + let buffer = buffer.read(cx); + let filename = if let Some(file) = buffer.file() { + if file.path().file_name().is_none() + || self.project.read(cx).visible_worktrees(cx).count() > 1 + { + file.full_path(cx).to_string_lossy().to_string() + } else { + file.path().to_string_lossy().to_string() + } } else { - Cow::Borrowed("untitled") + "untitled".to_string() }; Flex::row() - .with_child(Label::new(filename.to_string(), theme.breadcrumbs.text.clone()).boxed()) + .with_child(Label::new(filename, theme.breadcrumbs.text.clone()).boxed()) .with_children(symbols.into_iter().flat_map(|symbol| { [ Label::new(" 〉 ".to_string(), theme.breadcrumbs.text.clone()).boxed(), diff --git a/crates/editor/src/multi_buffer.rs b/crates/editor/src/multi_buffer.rs index ad3fc3202d..c07df895c9 100644 --- a/crates/editor/src/multi_buffer.rs +++ b/crates/editor/src/multi_buffer.rs @@ -1001,6 +1001,13 @@ impl MultiBuffer { .collect() } + pub fn buffer(&self, buffer_id: usize) -> Option> { + self.buffers + .borrow() + .get(&buffer_id) + .map(|state| state.buffer.clone()) + } + pub fn save(&mut self, cx: &mut ModelContext) -> Task> { let mut save_tasks = Vec::new(); for BufferState { buffer, .. } in self.buffers.borrow().values() { @@ -2268,12 +2275,12 @@ impl MultiBufferSnapshot { &self, offset: T, theme: Option<&SyntaxTheme>, - ) -> Option<(BufferSnapshot, Vec>)> { + ) -> Option<(usize, Vec>)> { let anchor = self.anchor_before(offset); let excerpt_id = anchor.excerpt_id(); let excerpt = self.excerpt(excerpt_id)?; Some(( - excerpt.buffer.clone(), + excerpt.buffer_id, excerpt .buffer .symbols_containing(anchor.text_anchor, theme) diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index 7b54b3df9a..aec0bc533e 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -106,18 +106,21 @@ pub fn build_workspace( app_state: &Arc, cx: &mut ViewContext, ) -> Workspace { - cx.subscribe(&cx.handle(), |_, _, event, cx| { - let workspace::Event::PaneAdded(pane) = event; - pane.update(cx, |pane, cx| { - pane.toolbar().update(cx, |toolbar, cx| { - let breadcrumbs = cx.add_view(|_| Breadcrumbs::new()); - toolbar.add_item(breadcrumbs, cx); - let buffer_search_bar = cx.add_view(|cx| BufferSearchBar::new(cx)); - toolbar.add_item(buffer_search_bar, cx); - let project_search_bar = cx.add_view(|_| ProjectSearchBar::new()); - toolbar.add_item(project_search_bar, cx); - }) - }); + cx.subscribe(&cx.handle(), { + let project = project.clone(); + move |_, _, event, cx| { + let workspace::Event::PaneAdded(pane) = event; + pane.update(cx, |pane, cx| { + pane.toolbar().update(cx, |toolbar, cx| { + let breadcrumbs = cx.add_view(|_| Breadcrumbs::new(project.clone())); + toolbar.add_item(breadcrumbs, cx); + let buffer_search_bar = cx.add_view(|cx| BufferSearchBar::new(cx)); + toolbar.add_item(buffer_search_bar, cx); + let project_search_bar = cx.add_view(|_| ProjectSearchBar::new()); + toolbar.add_item(project_search_bar, cx); + }) + }); + } }) .detach();