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
co-authored by Max Brunsfeld
parent cd4ea344a6
commit d551b41aae
14 changed files with 345 additions and 5720 deletions
+2 -32
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,
+36 -27
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>) {