Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
language: Improve highlight map resolution (#52183)
Finn Everse63bd1ab326c
diff --git a/Cargo.lock b/Cargo.lockindex ae05451fb1e..a3b1e7b48ad 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -9331,6 +9331,7 @@ dependencies = ["async-trait","clock","collections",+ "criterion","ctor","diffy","ec4rs",diff --git a/crates/debugger_ui/src/session/running/variable_list.rs b/crates/debugger_ui/src/session/running/variable_list.rsindex 8329a6baf04..fd8fd736b9e 100644--- a/crates/debugger_ui/src/session/running/variable_list.rs+++ b/crates/debugger_ui/src/session/running/variable_list.rs@@ -1076,7 +1076,12 @@ impl VariableList {presentation_hint: Option<&VariablePresentationHint>,cx: &Context<Self>,) -> VariableColor {- let syntax_color_for = |name| cx.theme().syntax().get(name).color;+ let syntax_color_for = |name| {+ cx.theme()+ .syntax()+ .style_for_name(name)+ .and_then(|style| style.color)+ };let name = if self.disabled {Some(Color::Disabled.color(cx))} else {diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rsindex ee659f28705..b633b1d7735 100644--- a/crates/editor/src/editor.rs+++ b/crates/editor/src/editor.rs@@ -601,7 +601,11 @@ pub fn make_inlay_hints_style(cx: &App) -> HighlightStyle {.inlay_hints.show_background;- let mut style = cx.theme().syntax().get("hint");+ let mut style = cx+ .theme()+ .syntax()+ .style_for_name("hint")+ .unwrap_or_default();if style.color.is_none() {style.color = Some(cx.theme().status().hint);diff --git a/crates/editor/src/semantic_tokens.rs b/crates/editor/src/semantic_tokens.rsindex 6a82068410f..1a895465277 100644--- a/crates/editor/src/semantic_tokens.rs+++ b/crates/editor/src/semantic_tokens.rs@@ -377,7 +377,10 @@ fn convert_token(for rule in matching {empty = false;- let style = rule.style.iter().find_map(|style| theme.get_opt(style));+ let style = rule+ .style+ .iter()+ .find_map(|style| theme.style_for_name(style));macro_rules! overwrite {(diff --git a/crates/language/Cargo.toml b/crates/language/Cargo.tomlindex 37c19172f7c..5cc2550d471 100644--- a/crates/language/Cargo.toml+++ b/crates/language/Cargo.toml@@ -101,6 +101,11 @@ toml.workspace = trueunindent.workspace = trueutil = { workspace = true, features = ["test-support"] }zlog.workspace = true+criterion.workspace = true++[[bench]]+name = "highlight_map"+harness = false[package.metadata.cargo-machete]ignored = ["tracing"]diff --git a/crates/language/benches/highlight_map.rs b/crates/language/benches/highlight_map.rsnew file mode 100644index 00000000000..97c7204deec--- /dev/null+++ b/crates/language/benches/highlight_map.rs@@ -0,0 +1,144 @@+use criterion::{BenchmarkId, Criterion, black_box, criterion_group, criterion_main};+use gpui::rgba;+use language::HighlightMap;+use theme::SyntaxTheme;++fn syntax_theme(highlight_names: &[&str]) -> SyntaxTheme {+ SyntaxTheme::new(highlight_names.iter().enumerate().map(|(i, name)| {+ let r = ((i * 37) % 256) as u8;+ let g = ((i * 53) % 256) as u8;+ let b = ((i * 71) % 256) as u8;+ let color = rgba(u32::from_be_bytes([r, g, b, 0xff]));+ (name.to_string(), color.into())+ }))+}++static SMALL_THEME_KEYS: &[&str] = &[+ "comment", "function", "keyword", "string", "type", "variable",+];++static LARGE_THEME_KEYS: &[&str] = &[+ "attribute",+ "boolean",+ "comment",+ "comment.doc",+ "constant",+ "constant.builtin",+ "constructor",+ "embedded",+ "emphasis",+ "emphasis.strong",+ "function",+ "function.builtin",+ "function.method",+ "function.method.builtin",+ "function.special.definition",+ "keyword",+ "keyword.control",+ "keyword.control.conditional",+ "keyword.control.import",+ "keyword.control.repeat",+ "keyword.control.return",+ "keyword.modifier",+ "keyword.operator",+ "label",+ "link_text",+ "link_uri",+ "number",+ "operator",+ "property",+ "punctuation",+ "punctuation.bracket",+ "punctuation.delimiter",+ "punctuation.list_marker",+ "punctuation.special",+ "string",+ "string.escape",+ "string.regex",+ "string.special",+ "string.special.symbol",+ "tag",+ "text.literal",+ "title",+ "type",+ "type.builtin",+ "type.super",+ "variable",+ "variable.builtin",+ "variable.member",+ "variable.parameter",+ "variable.special",+];++static SMALL_CAPTURE_NAMES: &[&str] = &[+ "function",+ "keyword",+ "string.escape",+ "type.builtin",+ "variable.builtin",+];++static LARGE_CAPTURE_NAMES: &[&str] = &[+ "attribute",+ "boolean",+ "comment",+ "comment.doc",+ "constant",+ "constant.builtin",+ "constructor",+ "function",+ "function.builtin",+ "function.method",+ "keyword",+ "keyword.control",+ "keyword.control.conditional",+ "keyword.control.import",+ "keyword.modifier",+ "keyword.operator",+ "label",+ "number",+ "operator",+ "property",+ "punctuation.bracket",+ "punctuation.delimiter",+ "punctuation.special",+ "string",+ "string.escape",+ "string.regex",+ "string.special",+ "tag",+ "type",+ "type.builtin",+ "variable",+ "variable.builtin",+ "variable.member",+ "variable.parameter",+];++fn bench_highlight_map_new(c: &mut Criterion) {+ let mut group = c.benchmark_group("HighlightMap::new");++ for (capture_label, capture_names) in [+ ("small_captures", SMALL_CAPTURE_NAMES as &[&str]),+ ("large_captures", LARGE_CAPTURE_NAMES as &[&str]),+ ] {+ for (theme_label, theme_keys) in [+ ("small_theme", SMALL_THEME_KEYS as &[&str]),+ ("large_theme", LARGE_THEME_KEYS as &[&str]),+ ] {+ let theme = syntax_theme(theme_keys);+ group.bench_with_input(+ BenchmarkId::new(capture_label, theme_label),+ &(capture_names, &theme),+ |b, (capture_names, theme)| {+ b.iter(|| HighlightMap::new(black_box(capture_names), black_box(theme)));+ },+ );+ }+ }++ group.finish();+}++criterion_group!(benches, bench_highlight_map_new);+criterion_main!(benches);diff --git a/crates/language/src/highlight_map.rs b/crates/language/src/highlight_map.rsindex ed9eb5d11d7..caab0e47f1e 100644--- a/crates/language/src/highlight_map.rs+++ b/crates/language/src/highlight_map.rs@@ -11,7 +11,7 @@ pub struct HighlightId(pub u32);const DEFAULT_SYNTAX_HIGHLIGHT_ID: HighlightId = HighlightId(u32::MAX);impl HighlightMap {- pub(crate) fn new(capture_names: &[&str], theme: &SyntaxTheme) -> Self {+ pub fn new(capture_names: &[&str], theme: &SyntaxTheme) -> Self {// For each capture name in the highlight query, find the longest// key in the theme's syntax styles that matches all of the// dot-separated components of the capture name.@@ -20,23 +20,8 @@ impl HighlightMap {.iter().map(|capture_name| {theme- .highlights- .iter()- .enumerate()- .filter_map(|(i, (key, _))| {- let mut len = 0;- let capture_parts = capture_name.split('.');- for key_part in key.split('.') {- if capture_parts.clone().any(|part| part == key_part) {- len += 1;- } else {- return None;- }- }- Some((i, len))- })- .max_by_key(|(_, len)| *len)- .map_or(DEFAULT_SYNTAX_HIGHLIGHT_ID, |(i, _)| HighlightId(i as u32))+ .highlight_id(capture_name)+ .map_or(DEFAULT_SYNTAX_HIGHLIGHT_ID, HighlightId)}).collect(),)@@ -59,11 +44,11 @@ impl HighlightId {}pub fn style(&self, theme: &SyntaxTheme) -> Option<HighlightStyle> {- theme.highlights.get(self.0 as usize).map(|entry| entry.1)+ theme.get(self.0 as usize).cloned()}pub fn name<'a>(&self, theme: &'a SyntaxTheme) -> Option<&'a str> {- theme.highlights.get(self.0 as usize).map(|e| e.0.as_str())+ theme.get_capture_name(self.0 as usize)}}@@ -86,8 +71,8 @@ mod tests {#[test]fn test_highlight_map() {- let theme = SyntaxTheme {- highlights: [+ let theme = SyntaxTheme::new(+ [("function", rgba(0x100000ff)),("function.method", rgba(0x200000ff)),("function.async", rgba(0x300000ff)),@@ -96,9 +81,8 @@ mod tests {("variable", rgba(0x600000ff)),].iter()- .map(|(name, color)| (name.to_string(), (*color).into()))- .collect(),- };+ .map(|(name, color)| (name.to_string(), (*color).into())),+ );let capture_names = &["function.special",diff --git a/crates/language_tools/src/highlights_tree_view.rs b/crates/language_tools/src/highlights_tree_view.rsindex 8a139958897..1e19ee47b3c 100644--- a/crates/language_tools/src/highlights_tree_view.rs+++ b/crates/language_tools/src/highlights_tree_view.rs@@ -375,7 +375,9 @@ impl HighlightsTreeView {rule.style.iter().find(|style_name| {- semantic_theme.get_opt(style_name).is_some()+ semantic_theme+ .style_for_name(style_name)+ .is_some()}).map(|style_name| {SharedString::from(style_name.clone())diff --git a/crates/onboarding/src/theme_preview.rs b/crates/onboarding/src/theme_preview.rsindex 8bd65d8a270..602695cca6a 100644--- a/crates/onboarding/src/theme_preview.rs+++ b/crates/onboarding/src/theme_preview.rs@@ -87,13 +87,13 @@ impl ThemePreviewTile {let colors = theme.colors();let syntax = theme.syntax();- let keyword_color = syntax.get("keyword").color;- let function_color = syntax.get("function").color;- let string_color = syntax.get("string").color;- let comment_color = syntax.get("comment").color;- let variable_color = syntax.get("variable").color;- let type_color = syntax.get("type").color;- let punctuation_color = syntax.get("punctuation").color;+ let keyword_color = syntax.style_for_name("keyword").and_then(|s| s.color);+ let function_color = syntax.style_for_name("function").and_then(|s| s.color);+ let string_color = syntax.style_for_name("string").and_then(|s| s.color);+ let comment_color = syntax.style_for_name("comment").and_then(|s| s.color);+ let variable_color = syntax.style_for_name("variable").and_then(|s| s.color);+ let type_color = syntax.style_for_name("type").and_then(|s| s.color);+ let punctuation_color = syntax.style_for_name("punctuation").and_then(|s| s.color);let syntax_colors = [keyword_color,diff --git a/crates/theme/src/fallback_themes.rs b/crates/theme/src/fallback_themes.rsindex 72b65f85c9e..bfff86b5c61 100644--- a/crates/theme/src/fallback_themes.rs+++ b/crates/theme/src/fallback_themes.rs@@ -314,70 +314,68 @@ pub(crate) fn zed_default_dark() -> Theme {warning_border: yellow,},player,- syntax: Arc::new(SyntaxTheme {- highlights: vec![- ("attribute".into(), purple.into()),- ("boolean".into(), orange.into()),- ("comment".into(), gray.into()),- ("comment.doc".into(), gray.into()),- ("constant".into(), yellow.into()),- ("constructor".into(), blue.into()),- ("embedded".into(), HighlightStyle::default()),- (- "emphasis".into(),- HighlightStyle {- font_style: Some(FontStyle::Italic),- ..HighlightStyle::default()- },- ),- (- "emphasis.strong".into(),- HighlightStyle {- font_weight: Some(FontWeight::BOLD),- ..HighlightStyle::default()- },- ),- ("enum".into(), teal.into()),- ("function".into(), blue.into()),- ("function.method".into(), blue.into()),- ("function.definition".into(), blue.into()),- ("hint".into(), blue.into()),- ("keyword".into(), purple.into()),- ("label".into(), HighlightStyle::default()),- ("link_text".into(), blue.into()),- (- "link_uri".into(),- HighlightStyle {- color: Some(teal),- font_style: Some(FontStyle::Italic),- ..HighlightStyle::default()- },- ),- ("number".into(), orange.into()),- ("operator".into(), HighlightStyle::default()),- ("predictive".into(), HighlightStyle::default()),- ("preproc".into(), HighlightStyle::default()),- ("primary".into(), HighlightStyle::default()),- ("property".into(), red.into()),- ("punctuation".into(), HighlightStyle::default()),- ("punctuation.bracket".into(), HighlightStyle::default()),- ("punctuation.delimiter".into(), HighlightStyle::default()),- ("punctuation.list_marker".into(), HighlightStyle::default()),- ("punctuation.special".into(), HighlightStyle::default()),- ("string".into(), green.into()),- ("string.escape".into(), HighlightStyle::default()),- ("string.regex".into(), red.into()),- ("string.special".into(), HighlightStyle::default()),- ("string.special.symbol".into(), HighlightStyle::default()),- ("tag".into(), HighlightStyle::default()),- ("text.literal".into(), HighlightStyle::default()),- ("title".into(), HighlightStyle::default()),- ("type".into(), teal.into()),- ("variable".into(), HighlightStyle::default()),- ("variable.special".into(), red.into()),- ("variant".into(), HighlightStyle::default()),- ],- }),+ syntax: Arc::new(SyntaxTheme::new(vec![+ ("attribute".into(), purple.into()),+ ("boolean".into(), orange.into()),+ ("comment".into(), gray.into()),+ ("comment.doc".into(), gray.into()),+ ("constant".into(), yellow.into()),+ ("constructor".into(), blue.into()),+ ("embedded".into(), HighlightStyle::default()),+ (+ "emphasis".into(),+ HighlightStyle {+ font_style: Some(FontStyle::Italic),+ ..HighlightStyle::default()+ },+ ),+ (+ "emphasis.strong".into(),+ HighlightStyle {+ font_weight: Some(FontWeight::BOLD),+ ..HighlightStyle::default()+ },+ ),+ ("enum".into(), teal.into()),+ ("function".into(), blue.into()),+ ("function.method".into(), blue.into()),+ ("function.definition".into(), blue.into()),+ ("hint".into(), blue.into()),+ ("keyword".into(), purple.into()),+ ("label".into(), HighlightStyle::default()),+ ("link_text".into(), blue.into()),+ (+ "link_uri".into(),+ HighlightStyle {+ color: Some(teal),+ font_style: Some(FontStyle::Italic),+ ..HighlightStyle::default()+ },+ ),+ ("number".into(), orange.into()),+ ("operator".into(), HighlightStyle::default()),+ ("predictive".into(), HighlightStyle::default()),+ ("preproc".into(), HighlightStyle::default()),+ ("primary".into(), HighlightStyle::default()),+ ("property".into(), red.into()),+ ("punctuation".into(), HighlightStyle::default()),+ ("punctuation.bracket".into(), HighlightStyle::default()),+ ("punctuation.delimiter".into(), HighlightStyle::default()),+ ("punctuation.list_marker".into(), HighlightStyle::default()),+ ("punctuation.special".into(), HighlightStyle::default()),+ ("string".into(), green.into()),+ ("string.escape".into(), HighlightStyle::default()),+ ("string.regex".into(), red.into()),+ ("string.special".into(), HighlightStyle::default()),+ ("string.special.symbol".into(), HighlightStyle::default()),+ ("tag".into(), HighlightStyle::default()),+ ("text.literal".into(), HighlightStyle::default()),+ ("title".into(), HighlightStyle::default()),+ ("type".into(), teal.into()),+ ("variable".into(), HighlightStyle::default()),+ ("variable.special".into(), red.into()),+ ("variant".into(), HighlightStyle::default()),+ ])),},}}diff --git a/crates/theme/src/styles/syntax.rs b/crates/theme/src/styles/syntax.rsindex 6a161538783..aa2590547c2 100644--- a/crates/theme/src/styles/syntax.rs+++ b/crates/theme/src/styles/syntax.rs@@ -1,15 +1,38 @@#![allow(missing_docs)]-use std::sync::Arc;+use std::{+ collections::{BTreeMap, btree_map::Entry},+ sync::Arc,+};-use gpui::{HighlightStyle, Hsla};+use gpui::HighlightStyle;+#[cfg(any(test, feature = "test-support"))]+use gpui::Hsla;#[derive(Debug, PartialEq, Eq, Clone, Default)]pub struct SyntaxTheme {- pub highlights: Vec<(String, HighlightStyle)>,+ pub(self) highlights: Vec<HighlightStyle>,+ pub(self) capture_name_map: BTreeMap<String, usize>,}impl SyntaxTheme {+ pub fn new(highlights: impl IntoIterator<Item = (String, HighlightStyle)>) -> Self {+ let (capture_names, highlights) = highlights.into_iter().unzip();++ Self {+ capture_name_map: Self::create_capture_name_map(capture_names),+ highlights,+ }+ }++ fn create_capture_name_map(highlights: Vec<String>) -> BTreeMap<String, usize> {+ highlights+ .into_iter()+ .enumerate()+ .map(|(i, key)| (key, i))+ .collect()+ }+#[cfg(any(test, feature = "test-support"))]pub fn new_test(colors: impl IntoIterator<Item = (&'static str, Hsla)>) -> Self {Self::new_test_styles(colors.into_iter().map(|(key, color)| {@@ -27,34 +50,45 @@ impl SyntaxTheme {pub fn new_test_styles(colors: impl IntoIterator<Item = (&'static str, HighlightStyle)>,) -> Self {- Self {- highlights: colors+ Self::new(+ colors.into_iter()- .map(|(key, style)| (key.to_owned(), style))- .collect(),- }+ .map(|(key, style)| (key.to_owned(), style)),+ )}- pub fn get(&self, name: &str) -> HighlightStyle {- self.highlights- .iter()- .find_map(|entry| if entry.0 == name { Some(entry.1) } else { None })- .unwrap_or_default()+ pub fn get(&self, highlight_index: usize) -> Option<&HighlightStyle> {+ self.highlights.get(highlight_index)}- pub fn get_opt(&self, name: &str) -> Option<HighlightStyle> {- self.highlights- .iter()- .find_map(|entry| if entry.0 == name { Some(entry.1) } else { None })+ pub fn style_for_name(&self, name: &str) -> Option<HighlightStyle> {+ self.capture_name_map+ .get(name)+ .map(|highlight_idx| self.highlights[*highlight_idx])}- pub fn color(&self, name: &str) -> Hsla {- self.get(name).color.unwrap_or_default()+ pub fn get_capture_name(&self, idx: usize) -> Option<&str> {+ self.capture_name_map+ .iter()+ .find(|(_, value)| **value == idx)+ .map(|(key, _)| key.as_ref())}- pub fn highlight_id(&self, name: &str) -> Option<u32> {- let ix = self.highlights.iter().position(|entry| entry.0 == name)?;- Some(ix as u32)+ pub fn highlight_id(&self, capture_name: &str) -> Option<u32> {+ self.capture_name_map+ .range::<str, _>((+ capture_name.split(".").next().map_or(+ std::ops::Bound::Included(capture_name),+ std::ops::Bound::Included,+ ),+ std::ops::Bound::Included(capture_name),+ ))+ .rfind(|(prefix, _)| {+ capture_name+ .strip_prefix(*prefix)+ .is_some_and(|remainder| remainder.is_empty() || remainder.starts_with('.'))+ })+ .map(|(_, index)| *index as u32)}/// Returns a new [`Arc<SyntaxTheme>`] with the given syntax styles merged in.@@ -63,33 +97,36 @@ impl SyntaxTheme {return base;}- let mut merged_highlights = base.highlights.clone();+ let mut base = Arc::try_unwrap(base).unwrap_or_else(|base| (*base).clone());for (name, highlight) in user_syntax_styles {- if let Some((_, existing_highlight)) = merged_highlights- .iter_mut()- .find(|(existing_name, _)| existing_name == &name)- {- existing_highlight.color = highlight.color.or(existing_highlight.color);- existing_highlight.font_weight =- highlight.font_weight.or(existing_highlight.font_weight);- existing_highlight.font_style =- highlight.font_style.or(existing_highlight.font_style);- existing_highlight.background_color = highlight- .background_color- .or(existing_highlight.background_color);- existing_highlight.underline = highlight.underline.or(existing_highlight.underline);- existing_highlight.strikethrough =- highlight.strikethrough.or(existing_highlight.strikethrough);- existing_highlight.fade_out = highlight.fade_out.or(existing_highlight.fade_out);- } else {- merged_highlights.push((name, highlight));+ match base.capture_name_map.entry(name) {+ Entry::Occupied(entry) => {+ if let Some(existing_highlight) = base.highlights.get_mut(*entry.get()) {+ existing_highlight.color = highlight.color.or(existing_highlight.color);+ existing_highlight.font_weight =+ highlight.font_weight.or(existing_highlight.font_weight);+ existing_highlight.font_style =+ highlight.font_style.or(existing_highlight.font_style);+ existing_highlight.background_color = highlight+ .background_color+ .or(existing_highlight.background_color);+ existing_highlight.underline =+ highlight.underline.or(existing_highlight.underline);+ existing_highlight.strikethrough =+ highlight.strikethrough.or(existing_highlight.strikethrough);+ existing_highlight.fade_out =+ highlight.fade_out.or(existing_highlight.fade_out);+ }+ }+ Entry::Vacant(vacant) => {+ vacant.insert(base.highlights.len());+ base.highlights.push(highlight);+ }}}- Arc::new(Self {- highlights: merged_highlights,- })+ Arc::new(base)}}diff --git a/crates/theme/src/theme.rs b/crates/theme/src/theme.rsindex ca330beee3c..3449e039a9e 100644--- a/crates/theme/src/theme.rs+++ b/crates/theme/src/theme.rs@@ -258,30 +258,25 @@ impl ThemeFamily {};refined_accent_colors.merge(&theme.style.accents);- let syntax_highlights = theme- .style- .syntax- .iter()- .map(|(syntax_token, highlight)| {- (- syntax_token.clone(),- HighlightStyle {- color: highlight- .color- .as_ref()- .and_then(|color| try_parse_color(color).ok()),- background_color: highlight- .background_color- .as_ref()- .and_then(|color| try_parse_color(color).ok()),- font_style: highlight.font_style.map(|s| s.into_gpui()),- font_weight: highlight.font_weight.map(|w| w.into_gpui()),- ..Default::default()- },- )- })- .collect::<Vec<_>>();- let syntax_theme = SyntaxTheme::merge(Arc::new(SyntaxTheme::default()), syntax_highlights);+ let syntax_highlights = theme.style.syntax.iter().map(|(syntax_token, highlight)| {+ (+ syntax_token.clone(),+ HighlightStyle {+ color: highlight+ .color+ .as_ref()+ .and_then(|color| try_parse_color(color).ok()),+ background_color: highlight+ .background_color+ .as_ref()+ .and_then(|color| try_parse_color(color).ok()),+ font_style: highlight.font_style.map(|s| s.into_gpui()),+ font_weight: highlight.font_weight.map(|w| w.into_gpui()),+ ..Default::default()+ },+ )+ });+ let syntax_theme = Arc::new(SyntaxTheme::new(syntax_highlights));let window_background_appearance = theme.style@@ -381,12 +376,6 @@ impl Theme {&self.styles.status}- /// Returns the color for the syntax node with the given name.- #[inline(always)]- pub fn syntax_color(&self, name: &str) -> Hsla {- self.syntax().color(name)- }-/// Returns the [`Appearance`] for the theme.#[inline(always)]pub fn appearance(&self) -> Appearance {