zed2: Mostly port breadcrumbs & improve StyledText api

Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
Julia 2023-11-27 16:09:31 -05:00
parent cd4ea344a6
commit d551b41aae
14 changed files with 345 additions and 5720 deletions

18
Cargo.lock generated
View File

@ -1076,6 +1076,23 @@ dependencies = [
"workspace",
]
[[package]]
name = "breadcrumbs2"
version = "0.1.0"
dependencies = [
"collections",
"editor2",
"gpui2",
"itertools 0.10.5",
"language2",
"project2",
"search2",
"settings2",
"theme2",
"ui2",
"workspace2",
]
[[package]]
name = "bromberg_sl2"
version = "0.6.0"
@ -11646,6 +11663,7 @@ dependencies = [
"async-trait",
"auto_update2",
"backtrace",
"breadcrumbs2",
"call2",
"chrono",
"cli",

View File

@ -8,6 +8,7 @@ members = [
"crates/auto_update",
"crates/auto_update2",
"crates/breadcrumbs",
"crates/breadcrumbs2",
"crates/call",
"crates/call2",
"crates/channel",

View File

@ -0,0 +1,28 @@
[package]
name = "breadcrumbs2"
version = "0.1.0"
edition = "2021"
publish = false
[lib]
path = "src/breadcrumbs.rs"
doctest = false
[dependencies]
collections = { path = "../collections" }
editor = { package = "editor2", path = "../editor2" }
gpui = { package = "gpui2", path = "../gpui2" }
ui = { package = "ui2", path = "../ui2" }
language = { package = "language2", path = "../language2" }
project = { package = "project2", path = "../project2" }
search = { package = "search2", path = "../search2" }
settings = { package = "settings2", path = "../settings2" }
theme = { package = "theme2", path = "../theme2" }
workspace = { package = "workspace2", path = "../workspace2" }
# outline = { path = "../outline" }
itertools = "0.10"
[dev-dependencies]
editor = { package = "editor2", path = "../editor2", features = ["test-support"] }
gpui = { package = "gpui2", path = "../gpui2", features = ["test-support"] }
workspace = { package = "workspace2", path = "../workspace2", features = ["test-support"] }

View File

@ -0,0 +1,199 @@
use gpui::{
div, Div, Element, EventEmitter, InteractiveElement, IntoElement, ParentElement, Render,
Stateful, StatefulInteractiveElement, StyledText, Subscription, ViewContext, WeakView,
};
use itertools::Itertools;
use theme::ActiveTheme;
use ui::{h_stack, Label};
use workspace::{
item::{ItemEvent, ItemHandle},
ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView, Workspace,
};
pub enum Event {
UpdateLocation,
}
pub struct Breadcrumbs {
pane_focused: bool,
active_item: Option<Box<dyn ItemHandle>>,
subscription: Option<Subscription>,
workspace: WeakView<Workspace>,
}
impl Breadcrumbs {
pub fn new(workspace: &Workspace) -> Self {
Self {
pane_focused: false,
active_item: Default::default(),
subscription: Default::default(),
workspace: workspace.weak_handle(),
}
}
}
impl EventEmitter<Event> for Breadcrumbs {}
impl EventEmitter<ToolbarItemEvent> for Breadcrumbs {}
impl Render for Breadcrumbs {
type Element = Stateful<Div>;
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
let id = "breadcrumbs";
let default_style = cx.text_style();
let active_item = match &self.active_item {
Some(active_item) => active_item,
None => return div().id(id),
};
let not_editor = active_item.downcast::<editor::Editor>().is_none();
let breadcrumbs = match active_item.breadcrumbs(cx.theme(), cx) {
Some(breadcrumbs) => breadcrumbs,
None => return div().id(id),
}
.into_iter()
.map(|breadcrumb| {
StyledText::new(breadcrumb.text)
.with_highlights(&default_style, breadcrumb.highlights.unwrap_or_default())
.into_any()
});
let crumbs = h_stack().children(Itertools::intersperse_with(breadcrumbs, || {
Label::new(" ").into_any_element()
}));
if not_editor || !self.pane_focused {
return crumbs.id(id);
}
let this = cx.view().downgrade();
crumbs.id(id).on_click(move |_, cx| {
this.update(cx, |this, cx| {
if let Some(workspace) = this.workspace.upgrade() {
workspace.update(cx, |_workspace, _cx| {
todo!("outline::toggle");
// outline::toggle(workspace, &Default::default(), cx)
})
}
})
.ok();
})
}
}
// impl View for Breadcrumbs {
// fn ui_name() -> &'static str {
// "Breadcrumbs"
// }
// fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
// let active_item = match &self.active_item {
// Some(active_item) => active_item,
// None => return Empty::new().into_any(),
// };
// let not_editor = active_item.downcast::<editor::Editor>().is_none();
// let theme = theme::current(cx).clone();
// let style = &theme.workspace.toolbar.breadcrumbs;
// let breadcrumbs = match active_item.breadcrumbs(&theme, cx) {
// Some(breadcrumbs) => breadcrumbs,
// None => return Empty::new().into_any(),
// }
// .into_iter()
// .map(|breadcrumb| {
// Text::new(
// breadcrumb.text,
// theme.workspace.toolbar.breadcrumbs.default.text.clone(),
// )
// .with_highlights(breadcrumb.highlights.unwrap_or_default())
// .into_any()
// });
// let crumbs = Flex::row()
// .with_children(Itertools::intersperse_with(breadcrumbs, || {
// Label::new(" ", style.default.text.clone()).into_any()
// }))
// .constrained()
// .with_height(theme.workspace.toolbar.breadcrumb_height)
// .contained();
// if not_editor || !self.pane_focused {
// return crumbs
// .with_style(style.default.container)
// .aligned()
// .left()
// .into_any();
// }
// MouseEventHandler::new::<Breadcrumbs, _>(0, cx, |state, _| {
// let style = style.style_for(state);
// crumbs.with_style(style.container)
// })
// .on_click(MouseButton::Left, |_, this, cx| {
// if let Some(workspace) = this.workspace.upgrade(cx) {
// workspace.update(cx, |workspace, cx| {
// outline::toggle(workspace, &Default::default(), cx)
// })
// }
// })
// .with_tooltip::<Breadcrumbs>(
// 0,
// "Show symbol outline".to_owned(),
// Some(Box::new(outline::Toggle)),
// theme.tooltip.clone(),
// cx,
// )
// .aligned()
// .left()
// .into_any()
// }
// }
impl ToolbarItemView for Breadcrumbs {
fn set_active_pane_item(
&mut self,
active_pane_item: Option<&dyn ItemHandle>,
cx: &mut ViewContext<Self>,
) -> ToolbarItemLocation {
cx.notify();
self.active_item = None;
if let Some(item) = active_pane_item {
let this = cx.view().downgrade();
self.subscription = Some(item.subscribe_to_item_events(
cx,
Box::new(move |event, cx| {
if let ItemEvent::UpdateBreadcrumbs = event {
this.update(cx, |_, cx| {
cx.emit(Event::UpdateLocation);
cx.notify();
})
.ok();
}
}),
));
self.active_item = Some(item.boxed_clone());
item.breadcrumb_location(cx)
} else {
ToolbarItemLocation::Hidden
}
}
// fn location_for_event(
// &self,
// _: &Event,
// current_location: ToolbarItemLocation,
// cx: &AppContext,
// ) -> ToolbarItemLocation {
// if let Some(active_item) = self.active_item.as_ref() {
// active_item.breadcrumb_location(cx)
// } else {
// current_location
// }
// }
fn pane_focus_update(&mut self, pane_focused: bool, _: &mut ViewContext<Self>) {
self.pane_focused = pane_focused;
}
}

View File

@ -346,7 +346,7 @@ impl<T: Entity> Drop for PendingEntitySubscription<T> {
}
}
#[derive(Copy, Clone)]
#[derive(Debug, Copy, Clone)]
pub struct TelemetrySettings {
pub diagnostics: bool,
pub metrics: bool,

View File

@ -350,6 +350,7 @@ impl Telemetry {
milliseconds_since_first_event: self.milliseconds_since_first_event(),
};
dbg!(telemetry_settings);
self.report_clickhouse_event(event, telemetry_settings, true)
}

View File

@ -151,7 +151,6 @@ pub fn render_parsed_markdown(
}
}),
);
let runs = text_runs_for_highlights(&parsed.text, &editor_style.text, highlights);
// todo!("add the ability to change cursor style for link ranges")
let mut links = Vec::new();
@ -165,7 +164,7 @@ pub fn render_parsed_markdown(
InteractiveText::new(
element_id,
StyledText::new(parsed.text.clone()).with_runs(runs),
StyledText::new(parsed.text.clone()).with_highlights(&editor_style.text, highlights),
)
.on_click(link_ranges, move |clicked_range_ix, cx| {
match &links[clicked_range_ix] {
@ -1313,11 +1312,7 @@ impl CompletionsMenu {
),
);
let completion_label = StyledText::new(completion.label.text.clone())
.with_runs(text_runs_for_highlights(
&completion.label.text,
&style.text,
highlights,
));
.with_highlights(&style.text, highlights);
let documentation_label =
if let Some(Documentation::SingleLine(text)) = documentation {
Some(SharedString::from(text.clone()))
@ -10027,31 +10022,6 @@ pub fn diagnostic_style(
}
}
pub fn text_runs_for_highlights(
text: &str,
default_style: &TextStyle,
highlights: impl IntoIterator<Item = (Range<usize>, HighlightStyle)>,
) -> Vec<TextRun> {
let mut runs = Vec::new();
let mut ix = 0;
for (range, highlight) in highlights {
if ix < range.start {
runs.push(default_style.clone().to_run(range.start - ix));
}
runs.push(
default_style
.clone()
.highlight(highlight)
.to_run(range.len()),
);
ix = range.end;
}
if ix < text.len() {
runs.push(default_style.to_run(text.len() - ix));
}
runs
}
pub fn styled_runs_for_code_label<'a>(
label: &'a CodeLabel,
syntax_theme: &'a theme::SyntaxTheme,

View File

@ -765,35 +765,44 @@ impl Item for Editor {
}
fn breadcrumbs(&self, variant: &Theme, cx: &AppContext) -> Option<Vec<BreadcrumbText>> {
todo!();
// let cursor = self.selections.newest_anchor().head();
// let multibuffer = &self.buffer().read(cx);
// let (buffer_id, symbols) =
// multibuffer.symbols_containing(cursor, Some(&theme.editor.syntax), cx)?;
// let buffer = multibuffer.buffer(buffer_id)?;
let cursor = self.selections.newest_anchor().head();
let multibuffer = &self.buffer().read(cx);
let (buffer_id, symbols) =
multibuffer.symbols_containing(cursor, Some(&cx.theme().styles.syntax), cx)?;
let buffer = multibuffer.buffer(buffer_id)?;
// let buffer = buffer.read(cx);
// let filename = buffer
// .snapshot()
// .resolve_file_path(
// cx,
// self.project
// .as_ref()
// .map(|project| project.read(cx).visible_worktrees(cx).count() > 1)
// .unwrap_or_default(),
// )
// .map(|path| path.to_string_lossy().to_string())
// .unwrap_or_else(|| "untitled".to_string());
let buffer = buffer.read(cx);
let filename = buffer
.snapshot()
.resolve_file_path(
cx,
self.project
.as_ref()
.map(|project| project.read(cx).visible_worktrees(cx).count() > 1)
.unwrap_or_default(),
)
.map(|path| path.to_string_lossy().to_string())
.unwrap_or_else(|| "untitled".to_string());
// let mut breadcrumbs = vec![BreadcrumbText {
// text: filename,
// highlights: None,
// }];
// breadcrumbs.extend(symbols.into_iter().map(|symbol| BreadcrumbText {
// text: symbol.text,
// highlights: Some(symbol.highlight_ranges),
// }));
// Some(breadcrumbs)
let mut breadcrumbs = vec![BreadcrumbText {
text: filename,
highlights: None,
}];
breadcrumbs.extend(symbols.into_iter().map(|symbol| {
// eprintln!(
// "ranges: {:?}",
// symbol
// .highlight_ranges
// .iter()
// .map(|range| &symbol.text[range.0.clone()])
// .collect::<Vec<_>>()
// );
BreadcrumbText {
text: symbol.text,
highlights: Some(symbol.highlight_ranges),
}
}));
Some(breadcrumbs)
}
fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext<Self>) {

View File

@ -1,6 +1,7 @@
use crate::{
Bounds, DispatchPhase, Element, ElementId, IntoElement, LayoutId, MouseDownEvent, MouseUpEvent,
Pixels, Point, SharedString, Size, TextRun, WhiteSpace, WindowContext, WrappedLine,
Bounds, DispatchPhase, Element, ElementId, HighlightStyle, IntoElement, LayoutId,
MouseDownEvent, MouseUpEvent, Pixels, Point, SharedString, Size, TextRun, TextStyle,
WhiteSpace, WindowContext, WrappedLine,
};
use anyhow::anyhow;
use parking_lot::{Mutex, MutexGuard};
@ -87,7 +88,28 @@ impl StyledText {
}
}
pub fn with_runs(mut self, runs: Vec<TextRun>) -> Self {
pub fn with_highlights(
mut self,
default_style: &TextStyle,
highlights: impl IntoIterator<Item = (Range<usize>, HighlightStyle)>,
) -> Self {
let mut runs = Vec::new();
let mut ix = 0;
for (range, highlight) in highlights {
if ix < range.start {
runs.push(default_style.clone().to_run(range.start - ix));
}
runs.push(
default_style
.clone()
.highlight(highlight)
.to_run(range.len()),
);
ix = range.end;
}
if ix < self.text.len() {
runs.push(default_style.to_run(self.text.len() - ix));
}
self.runs = Some(runs);
self
}

View File

@ -1,6 +1,6 @@
use gpui::{
blue, div, green, red, white, Div, InteractiveText, ParentElement, Render, Styled, StyledText,
TextRun, View, VisualContext, WindowContext,
blue, div, green, red, white, Div, HighlightStyle, InteractiveText, ParentElement, Render,
Styled, StyledText, View, VisualContext, WindowContext,
};
use ui::v_stack;
@ -59,13 +59,11 @@ impl Render for TextStory {
))).child(
InteractiveText::new(
"interactive",
StyledText::new("Hello world, how is it going?").with_runs(vec![
cx.text_style().to_run(6),
TextRun {
StyledText::new("Hello world, how is it going?").with_highlights(&cx.text_style(), [
(6..11, HighlightStyle {
background_color: Some(green()),
..cx.text_style().to_run(5)
},
cx.text_style().to_run(18),
..Default::default()
}),
]),
)
.on_click(vec![2..4, 1..3, 7..9], |range_ix, cx| {

View File

@ -1,6 +1,8 @@
use std::ops::Range;
use crate::prelude::*;
use crate::styled_ext::StyledExt;
use gpui::{relative, Div, Hsla, IntoElement, StyledText, TextRun, WindowContext};
use gpui::{relative, Div, HighlightStyle, Hsla, IntoElement, StyledText, WindowContext};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Default)]
pub enum LabelSize {
@ -99,38 +101,32 @@ impl RenderOnce for HighlightedLabel {
fn render(self, cx: &mut WindowContext) -> Self::Rendered {
let highlight_color = cx.theme().colors().text_accent;
let mut text_style = cx.text_style().clone();
let mut highlight_indices = self.highlight_indices.iter().copied().peekable();
let mut highlights: Vec<(Range<usize>, HighlightStyle)> = Vec::new();
let mut runs: Vec<TextRun> = Vec::new();
while let Some(start_ix) = highlight_indices.next() {
let mut end_ix = start_ix;
for (char_ix, char) in self.label.char_indices() {
let mut color = self.color.color(cx);
if let Some(highlight_ix) = highlight_indices.peek() {
if char_ix == *highlight_ix {
color = highlight_color;
highlight_indices.next();
loop {
end_ix = end_ix + self.label[end_ix..].chars().next().unwrap().len_utf8();
if let Some(&next_ix) = highlight_indices.peek() {
if next_ix == end_ix {
end_ix = next_ix;
highlight_indices.next();
continue;
}
}
break;
}
let last_run = runs.last_mut();
let start_new_run = if let Some(last_run) = last_run {
if color == last_run.color {
last_run.len += char.len_utf8();
false
} else {
true
}
} else {
true
};
if start_new_run {
text_style.color = color;
runs.push(text_style.to_run(char.len_utf8()))
}
highlights.push((
start_ix..end_ix,
HighlightStyle {
color: Some(highlight_color),
..Default::default()
},
));
}
div()
@ -150,7 +146,7 @@ impl RenderOnce for HighlightedLabel {
LabelSize::Default => this.text_ui(),
LabelSize::Small => this.text_ui_sm(),
})
.child(StyledText::new(self.label).with_runs(runs))
.child(StyledText::new(self.label).with_highlights(&cx.text_style(), highlights))
}
}

View File

@ -19,7 +19,7 @@ ai = { package = "ai2", path = "../ai2"}
# audio = { path = "../audio" }
# activity_indicator = { path = "../activity_indicator" }
auto_update = { package = "auto_update2", path = "../auto_update2" }
# breadcrumbs = { path = "../breadcrumbs" }
breadcrumbs = { package = "breadcrumbs2", path = "../breadcrumbs2" }
call = { package = "call2", path = "../call2" }
# channel = { path = "../channel" }
cli = { path = "../cli" }

View File

@ -7,6 +7,7 @@ mod only_instance;
mod open_listener;
pub use assets::*;
use breadcrumbs::Breadcrumbs;
use collections::VecDeque;
use editor::{Editor, MultiBuffer};
use gpui::{
@ -95,11 +96,11 @@ pub fn initialize_workspace(app_state: Arc<AppState>, cx: &mut AppContext) {
if let workspace::Event::PaneAdded(pane) = event {
pane.update(cx, |pane, cx| {
pane.toolbar().update(cx, |toolbar, cx| {
// todo!()
// let breadcrumbs = cx.add_view(|_| Breadcrumbs::new(workspace));
// toolbar.add_item(breadcrumbs, cx);
let breadcrumbs = cx.build_view(|_| Breadcrumbs::new(workspace));
toolbar.add_item(breadcrumbs, cx);
let buffer_search_bar = cx.build_view(search::BufferSearchBar::new);
toolbar.add_item(buffer_search_bar.clone(), cx);
// todo!()
// let quick_action_bar = cx.add_view(|_| {
// QuickActionBar::new(buffer_search_bar, workspace)
// });

5618
test.rs

File diff suppressed because it is too large Load Diff