From 0022c6b8288604a8e94a85c7ecf29994bfae25e4 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 5 Oct 2021 11:00:33 +0200 Subject: [PATCH] Move `settings::test` helper into `zed::test` --- crates/zed/src/main.rs | 3 ++- crates/zed/src/settings.rs | 50 +++++++++----------------------------- crates/zed/src/test.rs | 34 +++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 42 deletions(-) diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index 020a011301..9a1f93fc89 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -31,7 +31,8 @@ fn main() { app.platform().fonts().add_fonts(&embedded_fonts).unwrap(); let themes = settings::ThemeRegistry::new(Assets, app.font_cache()); - let (settings_tx, settings) = settings::channel(&app.font_cache(), &themes).unwrap(); + let (settings_tx, settings) = + settings::channel("Inconsolata", &app.font_cache(), &themes).unwrap(); let languages = Arc::new(language::build_language_registry()); languages.set_theme(&settings.borrow().theme.editor.syntax); diff --git a/crates/zed/src/settings.rs b/crates/zed/src/settings.rs index d1c601b521..52c0f7f61e 100644 --- a/crates/zed/src/settings.rs +++ b/crates/zed/src/settings.rs @@ -14,39 +14,13 @@ pub struct Settings { } impl Settings { - #[cfg(any(test, feature = "test-support"))] - pub fn test(cx: &gpui::AppContext) -> Self { - use crate::assets::Assets; - use gpui::AssetSource; - - lazy_static::lazy_static! { - static ref DEFAULT_THEME: parking_lot::Mutex>> = Default::default(); - static ref FONTS: Vec>> = Assets - .list("fonts") - .into_iter() - .map(|f| Arc::new(Assets.load(&f).unwrap().to_vec())) - .collect(); - } - - cx.platform().fonts().add_fonts(&FONTS).unwrap(); - - let mut theme_guard = DEFAULT_THEME.lock(); - let theme = if let Some(theme) = theme_guard.as_ref() { - theme.clone() - } else { - let theme = ThemeRegistry::new(Assets, cx.font_cache().clone()) - .get(DEFAULT_THEME_NAME) - .expect("failed to load default theme in tests"); - *theme_guard = Some(theme.clone()); - theme - }; - - Self::new(cx.font_cache(), theme).unwrap() - } - - pub fn new(font_cache: &FontCache, theme: Arc) -> Result { + pub fn new( + buffer_font_family: &str, + font_cache: &FontCache, + theme: Arc, + ) -> Result { Ok(Self { - buffer_font_family: font_cache.load_family(&["Inconsolata"])?, + buffer_font_family: font_cache.load_family(&[buffer_font_family])?, buffer_font_size: 16., tab_size: 4, theme, @@ -59,12 +33,8 @@ impl Settings { } } -#[cfg(any(test, feature = "test-support"))] -pub fn test(cx: &gpui::AppContext) -> (watch::Sender, watch::Receiver) { - watch::channel_with(Settings::test(cx)) -} - pub fn channel( + buffer_font_family: &str, font_cache: &FontCache, themes: &ThemeRegistry, ) -> Result<(watch::Sender, watch::Receiver)> { @@ -74,5 +44,9 @@ pub fn channel( panic!("failed to deserialize default theme: {:?}", err) } }; - Ok(watch::channel_with(Settings::new(font_cache, theme)?)) + Ok(watch::channel_with(Settings::new( + buffer_font_family, + font_cache, + theme, + )?)) } diff --git a/crates/zed/src/test.rs b/crates/zed/src/test.rs index 37ac9a869d..ac50a7af5e 100644 --- a/crates/zed/src/test.rs +++ b/crates/zed/src/test.rs @@ -1,13 +1,15 @@ use crate::{ assets::Assets, language, - settings::{self, ThemeRegistry}, + settings::Settings, + theme::{Theme, ThemeRegistry, DEFAULT_THEME_NAME}, AppState, }; use buffer::LanguageRegistry; use client::{http::ServerResponse, test::FakeHttpClient, ChannelList, Client, UserStore}; -use gpui::MutableAppContext; +use gpui::{AssetSource, MutableAppContext}; use parking_lot::Mutex; +use postage::watch; use project::fs::FakeFs; use std::sync::Arc; @@ -18,7 +20,7 @@ fn init_logger() { } pub fn test_app_state(cx: &mut MutableAppContext) -> Arc { - let (settings_tx, settings) = settings::test(cx); + let (settings_tx, settings) = watch::channel_with(build_settings(cx)); let mut languages = LanguageRegistry::new(); languages.add(Arc::new(language::rust())); let themes = ThemeRegistry::new(Assets, cx.font_cache().clone()); @@ -36,3 +38,29 @@ pub fn test_app_state(cx: &mut MutableAppContext) -> Arc { fs: Arc::new(FakeFs::new()), }) } + +fn build_settings(cx: &gpui::AppContext) -> Settings { + lazy_static::lazy_static! { + static ref DEFAULT_THEME: parking_lot::Mutex>> = Default::default(); + static ref FONTS: Vec>> = Assets + .list("fonts") + .into_iter() + .map(|f| Arc::new(Assets.load(&f).unwrap().to_vec())) + .collect(); + } + + cx.platform().fonts().add_fonts(&FONTS).unwrap(); + + let mut theme_guard = DEFAULT_THEME.lock(); + let theme = if let Some(theme) = theme_guard.as_ref() { + theme.clone() + } else { + let theme = ThemeRegistry::new(Assets, cx.font_cache().clone()) + .get(DEFAULT_THEME_NAME) + .expect("failed to load default theme in tests"); + *theme_guard = Some(theme.clone()); + theme + }; + + Settings::new("Inconsolata", cx.font_cache(), theme).unwrap() +}