From 4a0c620b77237e16aa0f85539f981e8016e8a723 Mon Sep 17 00:00:00 2001 From: wongjiahau Date: Sun, 26 Feb 2023 23:10:39 +0800 Subject: [PATCH] fix(explorer/filter): not working for newly opened folder --- helix-term/src/ui/explorer.rs | 14 +++++--------- helix-term/src/ui/tree.rs | 31 ++++++++++++++++--------------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/helix-term/src/ui/explorer.rs b/helix-term/src/ui/explorer.rs index 6e654f6e..026dd4e3 100644 --- a/helix-term/src/ui/explorer.rs +++ b/helix-term/src/ui/explorer.rs @@ -144,7 +144,6 @@ struct State { open: bool, current_root: PathBuf, area_width: u16, - filter: String, } impl State { @@ -154,7 +153,6 @@ impl State { current_root, open: true, area_width: 0, - filter: "".to_string(), } } } @@ -258,7 +256,7 @@ impl Explorer { .map(|c| c.as_os_str().to_string_lossy().to_string()) .collect::>() }; - self.tree.reveal_item(segments, &self.state.filter)?; + self.tree.reveal_item(segments)?; Ok(()) } @@ -489,8 +487,7 @@ impl Explorer { area.width.into(), title_style, ); - self.tree - .render(area.clip_top(1), surface, cx, &self.state.filter); + self.tree.render(area.clip_top(1), surface, cx); } pub fn render_embed( @@ -786,9 +783,8 @@ fn close_documents(current_item_path: PathBuf, cx: &mut Context) -> Result<()> { impl Component for Explorer { /// Process input events, return true if handled. fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult { - let filter = self.state.filter.clone(); if self.tree.prompting() { - return self.tree.handle_event(event, cx, &mut self.state, &filter); + return self.tree.handle_event(event, cx, &mut self.state); } let key_event = match event { Event::Key(event) => event, @@ -823,7 +819,7 @@ impl Component for Explorer { ctrl!('t') => self.toggle_preview(), _ => { self.tree - .handle_event(&Event::Key(*key_event), cx, &mut self.state, &filter); + .handle_event(&Event::Key(*key_event), cx, &mut self.state); } }; Ok(()) @@ -942,7 +938,7 @@ mod test_explorer { } fn render(explorer: &mut Explorer) -> String { - explorer.tree.render_to_string(Rect::new(0, 0, 50, 10), "") + explorer.tree.render_to_string(Rect::new(0, 0, 50, 10)) } fn new_explorer(name: &str) -> (PathBuf, Explorer) { diff --git a/helix-term/src/ui/tree.rs b/helix-term/src/ui/tree.rs index 73542430..9892282c 100644 --- a/helix-term/src/ui/tree.rs +++ b/helix-term/src/ui/tree.rs @@ -373,8 +373,8 @@ impl TreeView { /// /// vec!["helix-term", "src", "ui", "tree.rs"] /// - pub fn reveal_item(&mut self, segments: Vec, filter: &str) -> Result<()> { - self.refresh_with_filter(filter)?; + pub fn reveal_item(&mut self, segments: Vec) -> Result<()> { + self.refresh()?; // Expand the tree let root = self.tree.item.name(); @@ -390,7 +390,7 @@ impl TreeView { { Some(tree) => { if !tree.is_opened { - tree.open(filter)?; + tree.open(&self.filter)?; } Ok(tree) } @@ -784,7 +784,7 @@ fn render_tree( } impl TreeView { - pub fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context, filter: &str) { + pub fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) { let style = cx.editor.theme.get(&self.tree_symbol_style); let filter_prompt_area = area.with_height(1); @@ -818,7 +818,7 @@ impl TreeView { let ancestor_style = cx.editor.theme.get("ui.text.focus"); let area = area.clip_top(2); - let iter = self.render_lines(area, filter).into_iter().enumerate(); + let iter = self.render_lines(area).into_iter().enumerate(); for (index, line) in iter { let area = Rect::new(area.x, area.y.saturating_add(index as u16), area.width, 1); @@ -855,8 +855,8 @@ impl TreeView { } #[cfg(test)] - pub fn render_to_string(&mut self, area: Rect, filter: &str) -> String { - let lines = self.render_lines(area, filter); + pub fn render_to_string(&mut self, area: Rect) -> String { + let lines = self.render_lines(area); lines .into_iter() .map(|line| { @@ -873,7 +873,7 @@ impl TreeView { .join("\n") } - fn render_lines(&mut self, area: Rect, filter: &str) -> Vec { + fn render_lines(&mut self, area: Rect) -> Vec { if let Some(pre_render) = self.pre_render.take() { pre_render(self, area); } @@ -885,7 +885,7 @@ impl TreeView { prefix: &"".to_string(), level: 0, selected: self.selected, - filter, + filter: &self.filter, }; let lines = render_tree(params); @@ -1003,7 +1003,6 @@ impl TreeView { event: &Event, cx: &mut Context, params: &mut T::Params, - filter: &str, ) -> EventResult { let key_event = match event { Event::Key(event) => event, @@ -1024,6 +1023,8 @@ impl TreeView { } let count = std::mem::replace(&mut self.count, 0); + + let filter = self.filter.clone(); (|| -> Result { match key_event { key!(i @ '0'..='9') => { @@ -1040,10 +1041,10 @@ impl TreeView { })); } key!('h') | key!(Left) => self.move_to_parent()?, - key!('l') | key!(Right) => self.move_to_children(filter)?, + key!('l') | key!(Right) => self.move_to_children(&filter)?, shift!('H') => self.move_left(1), shift!('L') => self.move_right(1), - key!(Enter) | key!('o') => self.on_enter(cx, params, self.selected, filter)?, + key!(Enter) | key!('o') => self.on_enter(cx, params, self.selected, &filter)?, ctrl!('d') => self.move_down_half_page(), ctrl!('u') => self.move_up_half_page(), key!('g') => { @@ -1275,7 +1276,7 @@ mod test_tree_view { } fn render(view: &mut TreeView) -> String { - view.render_to_string(dummy_area(), "") + view.render_to_string(dummy_area()) } #[test] @@ -1626,7 +1627,7 @@ mod test_tree_view { let mut view = dummy_tree_view(); fn render(view: &mut TreeView) -> String { - view.render_to_string(dummy_area().with_width(20), "") + view.render_to_string(dummy_area().with_width(20)) } assert_eq!( @@ -2145,7 +2146,7 @@ krabby_patty } fn render(view: &mut TreeView>) -> String { - view.render_to_string(dummy_area().with_height(3), "") + view.render_to_string(dummy_area().with_height(3)) } let mut view = TreeView::new(