fix(explorer/filter): not working for newly opened folder

pull/9/head
wongjiahau 2 years ago
parent c0073edebf
commit 4a0c620b77

@ -144,7 +144,6 @@ struct State {
open: bool, open: bool,
current_root: PathBuf, current_root: PathBuf,
area_width: u16, area_width: u16,
filter: String,
} }
impl State { impl State {
@ -154,7 +153,6 @@ impl State {
current_root, current_root,
open: true, open: true,
area_width: 0, area_width: 0,
filter: "".to_string(),
} }
} }
} }
@ -258,7 +256,7 @@ impl Explorer {
.map(|c| c.as_os_str().to_string_lossy().to_string()) .map(|c| c.as_os_str().to_string_lossy().to_string())
.collect::<Vec<_>>() .collect::<Vec<_>>()
}; };
self.tree.reveal_item(segments, &self.state.filter)?; self.tree.reveal_item(segments)?;
Ok(()) Ok(())
} }
@ -489,8 +487,7 @@ impl Explorer {
area.width.into(), area.width.into(),
title_style, title_style,
); );
self.tree self.tree.render(area.clip_top(1), surface, cx);
.render(area.clip_top(1), surface, cx, &self.state.filter);
} }
pub fn render_embed( pub fn render_embed(
@ -786,9 +783,8 @@ fn close_documents(current_item_path: PathBuf, cx: &mut Context) -> Result<()> {
impl Component for Explorer { impl Component for Explorer {
/// Process input events, return true if handled. /// Process input events, return true if handled.
fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult { fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult {
let filter = self.state.filter.clone();
if self.tree.prompting() { 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 { let key_event = match event {
Event::Key(event) => event, Event::Key(event) => event,
@ -823,7 +819,7 @@ impl Component for Explorer {
ctrl!('t') => self.toggle_preview(), ctrl!('t') => self.toggle_preview(),
_ => { _ => {
self.tree self.tree
.handle_event(&Event::Key(*key_event), cx, &mut self.state, &filter); .handle_event(&Event::Key(*key_event), cx, &mut self.state);
} }
}; };
Ok(()) Ok(())
@ -942,7 +938,7 @@ mod test_explorer {
} }
fn render(explorer: &mut Explorer) -> String { 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) { fn new_explorer(name: &str) -> (PathBuf, Explorer) {

@ -373,8 +373,8 @@ impl<T: TreeViewItem> TreeView<T> {
/// ///
/// vec!["helix-term", "src", "ui", "tree.rs"] /// vec!["helix-term", "src", "ui", "tree.rs"]
/// ///
pub fn reveal_item(&mut self, segments: Vec<String>, filter: &str) -> Result<()> { pub fn reveal_item(&mut self, segments: Vec<String>) -> Result<()> {
self.refresh_with_filter(filter)?; self.refresh()?;
// Expand the tree // Expand the tree
let root = self.tree.item.name(); let root = self.tree.item.name();
@ -390,7 +390,7 @@ impl<T: TreeViewItem> TreeView<T> {
{ {
Some(tree) => { Some(tree) => {
if !tree.is_opened { if !tree.is_opened {
tree.open(filter)?; tree.open(&self.filter)?;
} }
Ok(tree) Ok(tree)
} }
@ -784,7 +784,7 @@ fn render_tree<T: TreeViewItem>(
} }
impl<T: TreeViewItem + Clone> TreeView<T> { impl<T: TreeViewItem + Clone> TreeView<T> {
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 style = cx.editor.theme.get(&self.tree_symbol_style);
let filter_prompt_area = area.with_height(1); let filter_prompt_area = area.with_height(1);
@ -818,7 +818,7 @@ impl<T: TreeViewItem + Clone> TreeView<T> {
let ancestor_style = cx.editor.theme.get("ui.text.focus"); let ancestor_style = cx.editor.theme.get("ui.text.focus");
let area = area.clip_top(2); 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 { for (index, line) in iter {
let area = Rect::new(area.x, area.y.saturating_add(index as u16), area.width, 1); let area = Rect::new(area.x, area.y.saturating_add(index as u16), area.width, 1);
@ -855,8 +855,8 @@ impl<T: TreeViewItem + Clone> TreeView<T> {
} }
#[cfg(test)] #[cfg(test)]
pub fn render_to_string(&mut self, area: Rect, filter: &str) -> String { pub fn render_to_string(&mut self, area: Rect) -> String {
let lines = self.render_lines(area, filter); let lines = self.render_lines(area);
lines lines
.into_iter() .into_iter()
.map(|line| { .map(|line| {
@ -873,7 +873,7 @@ impl<T: TreeViewItem + Clone> TreeView<T> {
.join("\n") .join("\n")
} }
fn render_lines(&mut self, area: Rect, filter: &str) -> Vec<RenderedLine> { fn render_lines(&mut self, area: Rect) -> Vec<RenderedLine> {
if let Some(pre_render) = self.pre_render.take() { if let Some(pre_render) = self.pre_render.take() {
pre_render(self, area); pre_render(self, area);
} }
@ -885,7 +885,7 @@ impl<T: TreeViewItem + Clone> TreeView<T> {
prefix: &"".to_string(), prefix: &"".to_string(),
level: 0, level: 0,
selected: self.selected, selected: self.selected,
filter, filter: &self.filter,
}; };
let lines = render_tree(params); let lines = render_tree(params);
@ -1003,7 +1003,6 @@ impl<T: TreeViewItem + Clone> TreeView<T> {
event: &Event, event: &Event,
cx: &mut Context, cx: &mut Context,
params: &mut T::Params, params: &mut T::Params,
filter: &str,
) -> EventResult { ) -> EventResult {
let key_event = match event { let key_event = match event {
Event::Key(event) => event, Event::Key(event) => event,
@ -1024,6 +1023,8 @@ impl<T: TreeViewItem + Clone> TreeView<T> {
} }
let count = std::mem::replace(&mut self.count, 0); let count = std::mem::replace(&mut self.count, 0);
let filter = self.filter.clone();
(|| -> Result<EventResult> { (|| -> Result<EventResult> {
match key_event { match key_event {
key!(i @ '0'..='9') => { key!(i @ '0'..='9') => {
@ -1040,10 +1041,10 @@ impl<T: TreeViewItem + Clone> TreeView<T> {
})); }));
} }
key!('h') | key!(Left) => self.move_to_parent()?, 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!('H') => self.move_left(1),
shift!('L') => self.move_right(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!('d') => self.move_down_half_page(),
ctrl!('u') => self.move_up_half_page(), ctrl!('u') => self.move_up_half_page(),
key!('g') => { key!('g') => {
@ -1275,7 +1276,7 @@ mod test_tree_view {
} }
fn render(view: &mut TreeView<Item>) -> String { fn render(view: &mut TreeView<Item>) -> String {
view.render_to_string(dummy_area(), "") view.render_to_string(dummy_area())
} }
#[test] #[test]
@ -1626,7 +1627,7 @@ mod test_tree_view {
let mut view = dummy_tree_view(); let mut view = dummy_tree_view();
fn render(view: &mut TreeView<Item>) -> String { fn render(view: &mut TreeView<Item>) -> String {
view.render_to_string(dummy_area().with_width(20), "") view.render_to_string(dummy_area().with_width(20))
} }
assert_eq!( assert_eq!(
@ -2145,7 +2146,7 @@ krabby_patty
} }
fn render(view: &mut TreeView<Item<'_>>) -> String { fn render(view: &mut TreeView<Item<'_>>) -> String {
view.render_to_string(dummy_area().with_height(3), "") view.render_to_string(dummy_area().with_height(3))
} }
let mut view = TreeView::new( let mut view = TreeView::new(

Loading…
Cancel
Save