Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,24 @@ impl<'a, 'b, T: ToJsonTreeValue> JsonTreeNode<'a, 'b, T> {
DefaultExpand::All => (InnerExpand::All, None),
DefaultExpand::None => (InnerExpand::None, None),
DefaultExpand::ToLevel(l) => (InnerExpand::ToLevel(l), None),
DefaultExpand::SearchResultsOrAll("") => (InnerExpand::All, None),
DefaultExpand::SearchResults(search_str)
| DefaultExpand::SearchResultsOrAll(search_str) => {
let search_term = SearchTerm::parse(search_str);
let search_match_path_ids = search_term
.as_ref()
.map(|search_term| {
search_term.find_matching_paths_in(
tree.value,
style.abbreviate_root,
&make_persistent_id,
&mut reset_path_ids,
)
})
.unwrap_or_default();
(InnerExpand::Paths(search_match_path_ids), search_term)
// Important: when searching for anything (even an empty string), we must always traverse the entire JSON value to
// capture all reset_path_ids so all the open/closed states can be reset to respect the new search term when it changes.
let search_term = SearchTerm::new(search_str);
let search_match_path_ids = search_term.find_matching_paths_in(
tree.value,
style.abbreviate_root,
&make_persistent_id,
&mut reset_path_ids,
);
let inner_expand =
if matches!(default_expand, DefaultExpand::SearchResultsOrAll("")) {
InnerExpand::All
} else {
InnerExpand::Paths(search_match_path_ids)
};
(inner_expand, Some(search_term))
}
};

Expand Down
10 changes: 3 additions & 7 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ use crate::{
pub struct SearchTerm(String);

impl SearchTerm {
pub(crate) fn parse(search_str: &str) -> Option<Self> {
SearchTerm::is_valid(search_str).then_some(Self(search_str.to_ascii_lowercase()))
}

fn is_valid(search_str: &str) -> bool {
!search_str.is_empty()
pub(crate) fn new(search_str: &str) -> Self {
Self(search_str.to_ascii_lowercase())
}

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

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

Expand Down