diff --git a/crates/editor/src/display_map.rs b/crates/editor/src/display_map.rs index 917553c7911cd1e0b68b5b103954807b38ca6ddd..a305bf88f527fc8163c075a22a0ec12b40c92a5a 100644 --- a/crates/editor/src/display_map.rs +++ b/crates/editor/src/display_map.rs @@ -959,10 +959,10 @@ pub mod tests { }"# .unindent(); - let theme = SyntaxTheme::new(vec![ + let theme = Arc::new(SyntaxTheme::new(vec![ ("mod.body".to_string(), Color::red().into()), ("fn.name".to_string(), Color::blue().into()), - ]); + ])); let language = Arc::new( Language::new( LanguageConfig { @@ -980,7 +980,7 @@ pub mod tests { ) .unwrap(), ); - language.set_theme(&theme); + language.set_theme(theme.clone()); cx.update(|cx| { let mut settings = Settings::test(cx); settings.language_settings.tab_size = Some(2.try_into().unwrap()); @@ -1049,10 +1049,10 @@ pub mod tests { }"# .unindent(); - let theme = SyntaxTheme::new(vec![ + let theme = Arc::new(SyntaxTheme::new(vec![ ("mod.body".to_string(), Color::red().into()), ("fn.name".to_string(), Color::blue().into()), - ]); + ])); let language = Arc::new( Language::new( LanguageConfig { @@ -1070,7 +1070,7 @@ pub mod tests { ) .unwrap(), ); - language.set_theme(&theme); + language.set_theme(theme.clone()); cx.update(|cx| cx.set_global(Settings::test(cx))); @@ -1120,10 +1120,10 @@ pub mod tests { cx.foreground().set_block_on_ticks(usize::MAX..=usize::MAX); cx.update(|cx| cx.set_global(Settings::test(cx))); - let theme = SyntaxTheme::new(vec![ + let theme = Arc::new(SyntaxTheme::new(vec![ ("operator".to_string(), Color::red().into()), ("string".to_string(), Color::green().into()), - ]); + ])); let language = Arc::new( Language::new( LanguageConfig { @@ -1141,7 +1141,7 @@ pub mod tests { ) .unwrap(), ); - language.set_theme(&theme); + language.set_theme(theme.clone()); let (text, highlighted_ranges) = marked_text_ranges(r#"const[] [a]: B = "c [d]""#); diff --git a/crates/language/src/language.rs b/crates/language/src/language.rs index c5d546298f219a3c05f0c99c133a201ca9825410..c6a850feb8c2625baf71e9d07c21e5b1805316d3 100644 --- a/crates/language/src/language.rs +++ b/crates/language/src/language.rs @@ -279,6 +279,7 @@ pub struct Language { pub(crate) config: LanguageConfig, pub(crate) grammar: Option>, pub(crate) adapter: Option>, + pub(crate) theme: RwLock>>, #[cfg(any(test, feature = "test-support"))] fake_adapter: Option<( @@ -339,9 +340,8 @@ impl LanguageRegistry { Self::new(Task::ready(())) } - pub fn add(&self, language: Arc, theme: &SyntaxTheme) { + pub fn add(&self, language: Arc) { self.languages.write().push(language.clone()); - language.set_theme(theme); *self.subscription.write().0.borrow_mut() = (); } @@ -349,9 +349,9 @@ impl LanguageRegistry { self.subscription.read().1.clone() } - pub fn set_theme(&self, theme: &SyntaxTheme) { + pub fn set_theme(&self, theme: Arc) { for language in self.languages.read().iter() { - language.set_theme(theme); + language.set_theme(theme.clone()); } } @@ -576,6 +576,7 @@ impl Language { }) }), adapter: None, + theme: Default::default(), #[cfg(any(test, feature = "test-support"))] fake_adapter: None, @@ -712,12 +713,21 @@ impl Language { c.is_whitespace() || self.config.autoclose_before.contains(c) } - pub fn set_theme(&self, theme: &SyntaxTheme) { + /// Sets the theme to the given theme, and then calls [`highlight`]. + pub fn set_theme(&self, theme: Arc) { + *self.theme.write() = Some(theme.clone()); + self.highlight() + } + + /// Highlights the grammar according to the current theme, + /// if one has been set using [`set_theme`]. + pub fn highlight(&self) { if let Some(grammar) = self.grammar.as_ref() { if let Some(highlights_query) = &grammar.highlights_query { - *grammar.highlight_map.lock() = - HighlightMap::new(highlights_query.capture_names(), theme); - dbg!("highlighting"); + if let Some(theme) = self.theme.read().as_ref() { + *grammar.highlight_map.lock() = + HighlightMap::new(highlights_query.capture_names(), &theme); + } } } } diff --git a/crates/zed/src/languages/go.rs b/crates/zed/src/languages/go.rs index 8e8e4b300b63b7d84c18957d68fbc64a7f67105e..22857427b2decf2e9898f2255edb9fdefe716180 100644 --- a/crates/zed/src/languages/go.rs +++ b/crates/zed/src/languages/go.rs @@ -324,7 +324,7 @@ mod tests { ("number".into(), Color::yellow().into()), ("property".into(), Color::white().into()), ]); - language.set_theme(&theme); + language.set_theme(theme.into()); let grammar = language.grammar().unwrap(); let highlight_function = grammar.highlight_id_for_name("function").unwrap(); diff --git a/crates/zed/src/languages/rust.rs b/crates/zed/src/languages/rust.rs index 5a38c8d7c5b243a8779a9ec6ba05f081c7dfd382..796a28538f1e449f67842256de23cc73cfe594b8 100644 --- a/crates/zed/src/languages/rust.rs +++ b/crates/zed/src/languages/rust.rs @@ -315,7 +315,7 @@ mod tests { ("property".into(), Color::white().into()), ]); - language.set_theme(&theme); + language.set_theme(theme.into()); let highlight_function = grammar.highlight_id_for_name("function").unwrap(); let highlight_type = grammar.highlight_id_for_name("type").unwrap(); @@ -394,7 +394,7 @@ mod tests { ("property".into(), Color::white().into()), ]); - language.set_theme(&theme); + language.set_theme(theme.into()); let highlight_function = grammar.highlight_id_for_name("function").unwrap(); let highlight_type = grammar.highlight_id_for_name("type").unwrap(); diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index 71fc253453c2b26add013fef250c18abc8bc7f38..4961504448740e0a0429dc1bbec8519304451d58 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -217,7 +217,7 @@ fn main() { cx.observe_global::({ let languages = languages.clone(); move |cx| { - languages.set_theme(&cx.global::().theme.editor.syntax); + languages.set_theme(cx.global::().theme.editor.syntax.clone()); } }) .detach(); @@ -227,7 +227,9 @@ fn main() { |cx| async move { init_languages.await; dbg!("all languages initialized, starting setting highlighting"); - cx.read(|cx| languages.set_theme(&cx.global::().theme.editor.syntax)); + cx.read(|cx| { + languages.set_theme(cx.global::().theme.editor.syntax.clone()) + }); } }) .detach(); diff --git a/test.rs b/test.rs new file mode 100644 index 0000000000000000000000000000000000000000..0df70d1f2de25ce2f46bd42e52185f5c128de304 --- /dev/null +++ b/test.rs @@ -0,0 +1 @@ +const A: usize = 32;