ced/src/app/state/find.rs

76 lines
2.1 KiB
Rust
Raw Normal View History

2025-07-05 14:42:45 -04:00
use super::editor::TextEditor;
impl TextEditor {
pub fn update_find_matches(&mut self) {
self.find_matches.clear();
self.current_match_index = None;
if self.find_query.is_empty() {
return;
}
if let Some(tab) = self.get_active_tab() {
let content = &tab.content;
let query = if self.case_sensitive_search {
self.find_query.clone()
} else {
self.find_query.to_lowercase()
};
let search_content = if self.case_sensitive_search {
content.clone()
} else {
content.to_lowercase()
};
let mut start = 0;
while let Some(pos) = search_content[start..].find(&query) {
let absolute_pos = start + pos;
self.find_matches
.push((absolute_pos, absolute_pos + query.len()));
start = absolute_pos + 1;
}
if !self.find_matches.is_empty() {
self.current_match_index = Some(0);
}
}
}
pub fn find_next(&mut self) {
if self.find_matches.is_empty() {
return;
}
if let Some(current) = self.current_match_index {
self.current_match_index = Some((current + 1) % self.find_matches.len());
} else {
self.current_match_index = Some(0);
}
}
pub fn find_previous(&mut self) {
if self.find_matches.is_empty() {
return;
}
if let Some(current) = self.current_match_index {
self.current_match_index = Some(if current == 0 {
self.find_matches.len() - 1
} else {
current - 1
});
} else {
self.current_match_index = Some(0);
}
}
pub fn get_current_match_position(&self) -> Option<(usize, usize)> {
if let Some(index) = self.current_match_index {
self.find_matches.get(index).copied()
} else {
None
}
}
}