use crate::analyzer::TreeNode;
use std::collections::HashSet;
#[derive(Debug, Clone)]
pub struct SearchState {
pub query: String,
pub node_types: HashSet<String>, pub matches: Vec<String>, pub current_match: usize,
}
impl SearchState {
pub fn new(query: String) -> Self {
SearchState {
query,
node_types: HashSet::new(),
matches: vec![],
current_match: 0,
}
}
pub fn parse_query(&mut self) {
self.node_types.clear();
for node_type in self.query.split(',') {
let trimmed = node_type.trim().to_lowercase();
if !trimmed.is_empty() {
self.node_types.insert(trimmed);
}
}
}
pub fn matches_node(&self, node: &TreeNode) -> bool {
if self.node_types.is_empty() {
return true; }
self.node_types
.contains(&node.node.node_type.to_lowercase())
}
pub fn next_match(&mut self) {
if !self.matches.is_empty() {
self.current_match = (self.current_match + 1) % self.matches.len();
}
}
pub fn prev_match(&mut self) {
if !self.matches.is_empty() {
self.current_match = if self.current_match == 0 {
self.matches.len() - 1
} else {
self.current_match - 1
};
}
}
pub fn current_match_uuid(&self) -> Option<&str> {
self.matches.get(self.current_match).map(|s| s.as_str())
}
}