WIP pull breadcrumb rendering out into item trait

This commit is contained in:
K Simmons 2022-09-06 14:39:58 -07:00
parent 1014d65e8e
commit ab81093ef5
10 changed files with 176 additions and 159 deletions

View file

@ -1,13 +1,12 @@
use editor::{Anchor, Editor};
use editor::Editor;
use gpui::{
elements::*, AppContext, Entity, ModelHandle, RenderContext, Subscription, View, ViewContext,
ViewHandle,
};
use language::{Buffer, OutlineItem};
use itertools::Itertools;
use project::Project;
use search::ProjectSearchView;
use settings::Settings;
use theme::SyntaxTheme;
use workspace::{ItemHandle, ToolbarItemLocation, ToolbarItemView};
pub enum Event {
@ -16,7 +15,7 @@ pub enum Event {
pub struct Breadcrumbs {
project: ModelHandle<Project>,
editor: Option<ViewHandle<Editor>>,
active_item: Option<Box<dyn ItemHandle>>,
project_search: Option<ViewHandle<ProjectSearchView>>,
subscriptions: Vec<Subscription>,
}
@ -25,24 +24,23 @@ impl Breadcrumbs {
pub fn new(project: ModelHandle<Project>) -> Self {
Self {
project,
editor: Default::default(),
active_item: Default::default(),
subscriptions: Default::default(),
project_search: Default::default(),
}
}
fn active_symbols(
&self,
theme: &SyntaxTheme,
cx: &AppContext,
) -> Option<(ModelHandle<Buffer>, Vec<OutlineItem<Anchor>>)> {
let editor = self.editor.as_ref()?.read(cx);
let cursor = editor.selections.newest_anchor().head();
let multibuffer = &editor.buffer().read(cx);
let (buffer_id, symbols) = multibuffer.symbols_containing(cursor, Some(theme), cx)?;
let buffer = multibuffer.buffer(buffer_id)?;
Some((buffer, symbols))
}
// fn active_symbols(
// &self,
// theme: &SyntaxTheme,
// cx: &AppContext,
// ) -> Option<(ModelHandle<Buffer>, Vec<OutlineItem<Anchor>>)> {
// let editor = self.active_item.as_ref()?.read(cx);
// let cursor = editor.selections.newest_anchor().head();
// let multibuffer = &editor.buffer().read(cx);
// let (buffer_id, symbols) = multibuffer.symbols_containing(cursor, Some(theme), cx)?;
// let buffer = multibuffer.buffer(buffer_id)?;
// Some((buffer, symbols))
// }
}
impl Entity for Breadcrumbs {
@ -55,41 +53,50 @@ impl View for Breadcrumbs {
}
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
let theme = cx.global::<Settings>().theme.clone();
let (buffer, symbols) =
if let Some((buffer, symbols)) = self.active_symbols(&theme.editor.syntax, cx) {
(buffer, symbols)
} else {
return Empty::new().boxed();
};
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 {
"untitled".to_string()
};
// let (buffer, symbols) =
// if let Some((buffer, symbols)) = self.active_symbols(&theme.editor.syntax, cx) {
// (buffer, symbols)
// } else {
// return Empty::new().boxed();
// };
// 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 {
// "untitled".to_string()
// };
Flex::row()
.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(),
Text::new(symbol.text, theme.breadcrumbs.text.clone())
.with_highlights(symbol.highlight_ranges)
.boxed(),
]
}))
.contained()
.with_style(theme.breadcrumbs.container)
.aligned()
.left()
.boxed()
let theme = cx.global::<Settings>().theme.clone();
if let Some(breadcrumbs) = self
.active_item
.and_then(|item| item.breadcrumbs(&theme, cx))
{
Flex::row()
.with_children(Itertools::intersperse_with(breadcrumbs.into_iter(), || {
Label::new("".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| {
// [
// Text::new(symbol.text, theme.breadcrumbs.text.clone())
// .with_highlights(symbol.highlight_ranges)
// .boxed(),
// ]
// }))
.contained()
.with_style(theme.breadcrumbs.container)
.aligned()
.left()
.boxed()
} else {
Empty::new().boxed()
}
}
}
@ -101,7 +108,7 @@ impl ToolbarItemView for Breadcrumbs {
) -> ToolbarItemLocation {
cx.notify();
self.subscriptions.clear();
self.editor = None;
self.active_item = None;
self.project_search = None;
if let Some(item) = active_pane_item {
if let Some(editor) = item.act_as::<Editor>(cx) {
@ -114,7 +121,7 @@ impl ToolbarItemView for Breadcrumbs {
editor::Event::SelectionsChanged { local } if *local => cx.notify(),
_ => {}
}));
self.editor = Some(editor);
self.active_item = Some(editor);
if let Some(project_search) = item.downcast::<ProjectSearchView>() {
self.subscriptions
.push(cx.subscribe(&project_search, |_, _, _, cx| {