diff options
author | wongjiahau | 2024-05-01 20:42:31 +0000 |
---|---|---|
committer | JJ | 2024-05-01 23:06:08 +0000 |
commit | 3bff36ab90aba7de8bb5bff7dbb8230d81cdf582 (patch) | |
tree | 733bdb4ff30a35c2212910d48a635ddf4c23b2e3 /helix-term/src/compositor.rs | |
parent | 2cadec0b1182332338a5a1cc3062776f834d8835 (diff) |
Add file explorer and tree helper
ref: https://github.com/helix-editor/helix/issues/200
ref: https://github.com/helix-editor/helix/pull/2377
ref: https://github.com/helix-editor/helix/pull/5566
ref: https://github.com/helix-editor/helix/pull/5768
Co-authored-by: cossonleo <cossonleo@foxmail.com>
Co-authored-by: JJ <git@toki.la>
Co-authored-by: Quan Tong <quantonganh@gmail.com>
Diffstat (limited to 'helix-term/src/compositor.rs')
-rw-r--r-- | helix-term/src/compositor.rs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/helix-term/src/compositor.rs b/helix-term/src/compositor.rs index 3dcb5f2b..c21c0b23 100644 --- a/helix-term/src/compositor.rs +++ b/helix-term/src/compositor.rs @@ -35,6 +35,56 @@ impl<'a> Context<'a> { tokio::task::block_in_place(|| helix_lsp::block_on(self.editor.flush_writes()))?; Ok(()) } + + /// Purpose: to test `handle_event` without escalating the test case to integration test + /// Usage: + /// ``` + /// let mut editor = Context::dummy_editor(); + /// let mut jobs = Context::dummy_jobs(); + /// let mut cx = Context::dummy(&mut jobs, &mut editor); + /// ``` + #[cfg(test)] + pub fn dummy(jobs: &'a mut Jobs, editor: &'a mut helix_view::Editor) -> Context<'a> { + Context { + jobs, + scroll: None, + editor, + } + } + + #[cfg(test)] + pub fn dummy_jobs() -> Jobs { + Jobs::new() + } + + #[cfg(test)] + pub fn dummy_editor() -> Editor { + use crate::config::Config; + use crate::handlers; + use arc_swap::{access::Map, ArcSwap}; + use helix_core::syntax::{self, Configuration}; + use helix_view::theme; + use std::{collections::HashMap, sync::Arc}; + + let config = Arc::new(ArcSwap::from_pointee(Config::default())); + let handlers = handlers::setup(config.clone()); + Editor::new( + Rect::new(0, 0, 60, 120), + Arc::new(theme::Loader::new(&[])), + Arc::new(ArcSwap::from_pointee( + syntax::Loader::new(Configuration { + language: vec![], + language_server: HashMap::new(), + }) + .unwrap(), + )), + Arc::new(Arc::new(Map::new( + Arc::clone(&config), + |config: &Config| &config.editor, + ))), + handlers, + ) + } } pub trait Component: Any + AnyComponent { @@ -73,6 +123,21 @@ pub trait Component: Any + AnyComponent { fn id(&self) -> Option<&'static str> { None } + + #[cfg(test)] + /// Utility method for testing `handle_event` without using integration test. + /// Especially useful for testing helper components such as `Prompt`, `TreeView` etc + fn handle_events(&mut self, events: &str) -> anyhow::Result<()> { + use helix_view::input::parse_macro; + + let mut editor = Context::dummy_editor(); + let mut jobs = Context::dummy_jobs(); + let mut cx = Context::dummy(&mut jobs, &mut editor); + for event in parse_macro(events)? { + self.handle_event(&Event::Key(event), &mut cx); + } + Ok(()) + } } pub struct Compositor { |