Skip to content

Commit 1be1cf0

Browse files
authored
Merge pull request #46 from dmackdev/fix-searchresultsorall-reset-expanded
fix resetting expanded for `DefaultExpand::SearchResultsOrAll("")`
2 parents 61ba882 + bc7197f commit 1be1cf0

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

src/node.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,24 @@ impl<'a, 'b, T: ToJsonTreeValue> JsonTreeNode<'a, 'b, T> {
4141
DefaultExpand::All => (InnerExpand::All, None),
4242
DefaultExpand::None => (InnerExpand::None, None),
4343
DefaultExpand::ToLevel(l) => (InnerExpand::ToLevel(l), None),
44-
DefaultExpand::SearchResultsOrAll("") => (InnerExpand::All, None),
4544
DefaultExpand::SearchResults(search_str)
4645
| DefaultExpand::SearchResultsOrAll(search_str) => {
47-
let search_term = SearchTerm::parse(search_str);
48-
let search_match_path_ids = search_term
49-
.as_ref()
50-
.map(|search_term| {
51-
search_term.find_matching_paths_in(
52-
tree.value,
53-
style.abbreviate_root,
54-
&make_persistent_id,
55-
&mut reset_path_ids,
56-
)
57-
})
58-
.unwrap_or_default();
59-
(InnerExpand::Paths(search_match_path_ids), search_term)
46+
// Important: when searching for anything (even an empty string), we must always traverse the entire JSON value to
47+
// capture all reset_path_ids so all the open/closed states can be reset to respect the new search term when it changes.
48+
let search_term = SearchTerm::new(search_str);
49+
let search_match_path_ids = search_term.find_matching_paths_in(
50+
tree.value,
51+
style.abbreviate_root,
52+
&make_persistent_id,
53+
&mut reset_path_ids,
54+
);
55+
let inner_expand =
56+
if matches!(default_expand, DefaultExpand::SearchResultsOrAll("")) {
57+
InnerExpand::All
58+
} else {
59+
InnerExpand::Paths(search_match_path_ids)
60+
};
61+
(inner_expand, Some(search_term))
6062
}
6163
};
6264

src/search.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@ use crate::{
1111
pub struct SearchTerm(String);
1212

1313
impl SearchTerm {
14-
pub(crate) fn parse(search_str: &str) -> Option<Self> {
15-
SearchTerm::is_valid(search_str).then_some(Self(search_str.to_ascii_lowercase()))
16-
}
17-
18-
fn is_valid(search_str: &str) -> bool {
19-
!search_str.is_empty()
14+
pub(crate) fn new(search_str: &str) -> Self {
15+
Self(search_str.to_ascii_lowercase())
2016
}
2117

2218
pub(crate) fn find_match_indices_in(&self, other: &str) -> Vec<usize> {
@@ -58,7 +54,7 @@ impl SearchTerm {
5854
}
5955

6056
fn matches<V: ToString + ?Sized>(&self, other: &V) -> bool {
61-
other.to_string().to_ascii_lowercase().contains(&self.0)
57+
!&self.0.is_empty() && other.to_string().to_ascii_lowercase().contains(&self.0)
6258
}
6359
}
6460

0 commit comments

Comments
 (0)