use std::{ops::Range, sync::Arc};
use crate::{
AccessibilityRole, Color, Element, ElementId, HighlightStyle, KeyBinding, TextHighlight,
};
pub const MAX_FIND_MATCHES: usize = 4_096;
pub const MAX_FIND_QUERY_BYTES: usize = 1_024;
pub const MAX_FIND_REPLACEMENT_BYTES: usize = 4 * 1_024;
pub const FIND_BAR_KEY_CONTEXT: &str = "FindBar";
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct FindNext;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct FindPrevious;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct FindReplace;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct FindReplaceAll;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct FindClose;
pub fn find_bar_key_bindings() -> [KeyBinding; 3] {
[
KeyBinding::new("enter", FindNext, Some(FIND_BAR_KEY_CONTEXT)),
KeyBinding::new("shift-enter", FindPrevious, Some(FIND_BAR_KEY_CONTEXT)),
KeyBinding::new("escape", FindClose, Some(FIND_BAR_KEY_CONTEXT)),
]
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct FindOptions {
pub case_sensitive: bool,
pub whole_word: bool,
}
#[derive(Clone, Debug)]
pub struct FindState {
query: Arc<str>,
replacement: Arc<str>,
options: FindOptions,
matches: Vec<Range<usize>>,
truncated: bool,
current: Option<usize>,
match_style: HighlightStyle,
current_match_style: HighlightStyle,
}
impl Default for FindState {
fn default() -> Self {
Self::new()
}
}
impl FindState {
pub fn new() -> Self {
Self {
query: Arc::from(""),
replacement: Arc::from(""),
options: FindOptions::default(),
matches: Vec::new(),
truncated: false,
current: None,
match_style: HighlightStyle::default().background(Color::rgba8(250, 204, 21, 72)),
current_match_style: HighlightStyle::default()
.background(Color::rgba8(249, 115, 22, 160)),
}
}
#[must_use]
pub fn match_style(mut self, style: HighlightStyle) -> Self {
self.match_style = style;
self
}
#[must_use]
pub fn current_match_style(mut self, style: HighlightStyle) -> Self {
self.current_match_style = style;
self
}
pub fn query(&self) -> &Arc<str> {
&self.query
}
pub fn replacement(&self) -> &Arc<str> {
&self.replacement
}
pub const fn options(&self) -> FindOptions {
self.options
}
pub fn matches(&self) -> &[Range<usize>] {
&self.matches
}
pub fn match_count(&self) -> usize {
self.matches.len()
}
pub const fn is_truncated(&self) -> bool {
self.truncated
}
pub const fn current_index(&self) -> Option<usize> {
self.current
}
pub fn current_match(&self) -> Option<Range<usize>> {
self.matches.get(self.current?).cloned()
}
pub fn set_query(&mut self, query: impl Into<Arc<str>>, text: &str) -> bool {
let query = query.into();
let query = if query.len() > MAX_FIND_QUERY_BYTES {
Arc::from("")
} else {
query
};
if self.query == query {
return false;
}
self.query = query;
self.refresh(text);
true
}
pub fn set_replacement(&mut self, replacement: impl Into<Arc<str>>) -> bool {
let replacement = replacement.into();
if replacement.len() > MAX_FIND_REPLACEMENT_BYTES || self.replacement == replacement {
return false;
}
self.replacement = replacement;
true
}
pub fn set_options(&mut self, options: FindOptions, text: &str) -> bool {
if self.options == options {
return false;
}
self.options = options;
self.refresh(text);
true
}
pub fn refresh(&mut self, text: &str) -> bool {
let anchor = self.current_match().map(|range| range.start);
let previous = std::mem::take(&mut self.matches);
self.truncated = false;
if !self.query.is_empty() {
let mut cursor = 0_usize;
while let Some(found) = next_match(text, &self.query, self.options, cursor) {
cursor = found.end.max(found.start + 1);
self.matches.push(found);
if self.matches.len() == MAX_FIND_MATCHES {
self.truncated = true;
break;
}
}
}
self.current = anchor.and_then(|anchor| {
self.matches
.iter()
.position(|range| range.start >= anchor)
.or(if self.matches.is_empty() {
None
} else {
Some(0)
})
});
if self.current.is_none() && !self.matches.is_empty() {
self.current = Some(0);
}
self.matches != previous
}
pub fn find_next(&mut self, text: &str) -> Option<Range<usize>> {
self.refresh(text);
if self.matches.is_empty() {
self.current = None;
return None;
}
self.current = Some(match self.current {
Some(index) if index + 1 < self.matches.len() => index + 1,
Some(_) => 0,
None => 0,
});
self.current_match()
}
pub fn find_previous(&mut self, text: &str) -> Option<Range<usize>> {
self.refresh(text);
if self.matches.is_empty() {
self.current = None;
return None;
}
self.current = Some(match self.current {
Some(0) | None => self.matches.len() - 1,
Some(index) => index - 1,
});
self.current_match()
}
pub fn replace_current(&mut self, text: &str) -> Option<(String, Range<usize>)> {
let range = self.current_match()?;
if range.end > text.len() {
return None;
}
let mut replaced = String::with_capacity(text.len() + self.replacement.len());
replaced.push_str(&text[..range.start]);
replaced.push_str(&self.replacement);
replaced.push_str(&text[range.end..]);
let inserted = range.start..range.start + self.replacement.len();
self.refresh(&replaced);
Some((replaced, inserted))
}
pub fn replace_all(&self, text: &str) -> Option<String> {
if self.matches.is_empty() {
return None;
}
let mut replaced = String::with_capacity(text.len());
let mut cursor = 0_usize;
for range in &self.matches {
if range.start < cursor || range.end > text.len() {
continue;
}
replaced.push_str(&text[cursor..range.start]);
replaced.push_str(&self.replacement);
cursor = range.end;
}
replaced.push_str(&text[cursor..]);
Some(replaced)
}
pub fn highlights(&self) -> Vec<TextHighlight> {
let current = self.current_match();
self.matches
.iter()
.map(|range| {
let style = if Some(range) == current.as_ref() {
self.current_match_style.clone()
} else {
self.match_style.clone()
};
TextHighlight {
range: range.clone(),
style,
}
})
.collect()
}
pub fn count_label(&self) -> String {
if self.query.is_empty() {
return String::new();
}
if self.matches.is_empty() {
return "No results".to_owned();
}
let position = self.current.map_or(0, |index| index + 1);
let total = self.matches.len();
if self.truncated {
format!("{position} of {total}+")
} else {
format!("{position} of {total}")
}
}
}
fn next_match(text: &str, query: &str, options: FindOptions, from: usize) -> Option<Range<usize>> {
if query.is_empty() || from > text.len() {
return None;
}
let mut index = from;
while index <= text.len() {
if !text.is_char_boundary(index) {
index += 1;
continue;
}
if let Some(end) = match_at(text, index, query, options.case_sensitive)
&& (!options.whole_word || is_whole_word(text, index..end))
{
return Some(index..end);
}
if index == text.len() {
break;
}
index += 1;
}
None
}
fn match_at(text: &str, index: usize, query: &str, case_sensitive: bool) -> Option<usize> {
let mut haystack = text.get(index..)?.chars();
let mut end = index;
for expected in query.chars() {
let found = haystack.next()?;
if !characters_match(found, expected, case_sensitive) {
return None;
}
end += found.len_utf8();
}
Some(end)
}
fn characters_match(found: char, expected: char, case_sensitive: bool) -> bool {
if case_sensitive {
found == expected
} else {
found == expected || found.to_lowercase().eq(expected.to_lowercase())
}
}
fn is_whole_word(text: &str, range: Range<usize>) -> bool {
let before = text[..range.start].chars().next_back();
let after = text[range.end..].chars().next();
!before.is_some_and(is_word_character) && !after.is_some_and(is_word_character)
}
fn is_word_character(character: char) -> bool {
character.is_alphanumeric() || character == '_'
}
const FIND_BAR_QUERY_TAG: u64 = 0x6669_6e64_5f71_7565;
const FIND_BAR_REPLACE_TAG: u64 = 0x6669_6e64_5f72_706c;
const FIND_BAR_COUNT_TAG: u64 = 0x6669_6e64_5f63_6e74;
const FIND_BAR_NEXT_TAG: u64 = 0x6669_6e64_5f6e_7874;
const FIND_BAR_PREVIOUS_TAG: u64 = 0x6669_6e64_5f70_7276;
const FIND_BAR_REPLACE_BUTTON_TAG: u64 = 0x6669_6e64_5f72_6231;
const FIND_BAR_REPLACE_ALL_TAG: u64 = 0x6669_6e64_5f72_6261;
const FIND_BAR_CLOSE_TAG: u64 = 0x6669_6e64_5f63_6c73;
#[derive(Clone, Debug, Eq, PartialEq)]
#[must_use = "a FindBar descriptor has no effect until its parts are mounted"]
pub struct FindBar {
id: ElementId,
label: Arc<str>,
count: Arc<str>,
has_matches: bool,
can_replace: bool,
}
impl FindBar {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
label: Arc::from("Find"),
count: Arc::from(""),
has_matches: false,
can_replace: false,
}
}
pub fn state(mut self, state: &FindState) -> Self {
self.count = Arc::from(state.count_label());
self.has_matches = state.match_count() > 0;
self.can_replace = self.has_matches && !state.query().is_empty();
self
}
pub fn label(mut self, label: impl Into<Arc<str>>) -> Self {
self.label = label.into();
self
}
pub const fn id(&self) -> ElementId {
self.id
}
pub fn query_input_id(&self) -> ElementId {
derived_find_id(self.id, FIND_BAR_QUERY_TAG)
}
pub fn replace_input_id(&self) -> ElementId {
derived_find_id(self.id, FIND_BAR_REPLACE_TAG)
}
pub fn count_id(&self) -> ElementId {
derived_find_id(self.id, FIND_BAR_COUNT_TAG)
}
pub fn count_text(&self) -> &Arc<str> {
&self.count
}
pub fn root_with(&self, root: Element) -> Element {
root.id(self.id)
.key_context(FIND_BAR_KEY_CONTEXT)
.accessibility_role(AccessibilityRole::Group)
.accessibility_label(self.label.clone())
.accessibility_described_by(self.count_id())
}
pub fn root(&self) -> Element {
self.root_with(crate::div())
}
pub fn query_input_with(&self, input: Element) -> Element {
input
.id(self.query_input_id())
.accessibility_label(self.label.clone())
.accessibility_described_by(self.count_id())
}
pub fn query_input(&self) -> Element {
self.query_input_with(crate::text_input(""))
}
pub fn replace_input_with(&self, input: Element) -> Element {
input
.id(self.replace_input_id())
.accessibility_label("Replace with")
}
pub fn replace_input(&self) -> Element {
self.replace_input_with(crate::text_input(""))
}
pub fn count_with(&self, count: Element) -> Element {
count
.id(self.count_id())
.accessibility_role(AccessibilityRole::Label)
.accessibility_value(self.count.clone())
}
pub fn count(&self) -> Element {
self.count_with(crate::div())
}
pub fn next_with(&self, button: Element) -> Element {
self.command_part(button, FIND_BAR_NEXT_TAG, "Next match", !self.has_matches)
}
pub fn next(&self) -> Element {
self.next_with(crate::button())
}
pub fn previous_with(&self, button: Element) -> Element {
self.command_part(
button,
FIND_BAR_PREVIOUS_TAG,
"Previous match",
!self.has_matches,
)
}
pub fn previous(&self) -> Element {
self.previous_with(crate::button())
}
pub fn replace_with(&self, button: Element) -> Element {
self.command_part(
button,
FIND_BAR_REPLACE_BUTTON_TAG,
"Replace",
!self.can_replace,
)
}
pub fn replace(&self) -> Element {
self.replace_with(crate::button())
}
pub fn replace_all_with(&self, button: Element) -> Element {
self.command_part(
button,
FIND_BAR_REPLACE_ALL_TAG,
"Replace all",
!self.can_replace,
)
}
pub fn replace_all(&self) -> Element {
self.replace_all_with(crate::button())
}
pub fn close_with(&self, button: Element) -> Element {
self.command_part(button, FIND_BAR_CLOSE_TAG, "Close find bar", false)
}
pub fn close(&self) -> Element {
self.close_with(crate::button())
}
fn command_part(&self, button: Element, tag: u64, label: &str, disabled: bool) -> Element {
button
.id(derived_find_id(self.id, tag))
.accessibility_role(AccessibilityRole::Button)
.accessibility_label(label)
.disabled(disabled)
}
}
fn derived_find_id(parent: ElementId, tag: u64) -> ElementId {
let mut hash = parent.as_u64() ^ tag;
hash ^= hash >> 30;
hash = hash.wrapping_mul(0xbf58_476d_1ce4_e5b9);
hash ^= hash >> 27;
hash = hash.wrapping_mul(0x94d0_49bb_1331_11eb);
hash ^= hash >> 31;
if hash == parent.as_u64() || hash == u64::MAX {
hash ^= tag.rotate_left(17);
}
ElementId::new(hash)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{TextCheckingOverrides, element::InputConstraints, text_input::TextInputState};
#[test]
fn literal_matching_honors_case_and_word_boundaries() {
let text = "Cat cats CAT concat";
let mut find = FindState::new();
assert!(find.set_query("cat", text));
assert_eq!(find.match_count(), 4);
assert!(find.set_options(
FindOptions {
case_sensitive: true,
whole_word: false
},
text,
));
assert_eq!(find.matches(), &[4..7, 16..19]);
assert!(find.set_options(
FindOptions {
case_sensitive: false,
whole_word: true
},
text,
));
assert_eq!(find.matches(), &[0..3, 9..12]);
}
#[test]
fn navigation_wraps_in_both_directions() {
let text = "a b a b a";
let mut find = FindState::new();
assert!(find.set_query("a", text));
assert_eq!(find.current_match(), Some(0..1));
assert_eq!(find.find_next(text), Some(4..5));
assert_eq!(find.find_next(text), Some(8..9));
assert_eq!(find.find_next(text), Some(0..1));
assert_eq!(find.find_previous(text), Some(8..9));
assert_eq!(find.current_index(), Some(2));
assert_eq!(find.count_label(), "3 of 3");
let mut empty = FindState::new();
assert!(empty.set_query("zz", text));
assert_eq!(empty.find_next(text), None);
assert_eq!(empty.count_label(), "No results");
}
#[test]
fn matches_are_bounded_and_report_truncation() {
let text = "x".repeat(MAX_FIND_MATCHES + 32);
let mut find = FindState::new();
assert!(find.set_query("x", &text));
assert_eq!(find.match_count(), MAX_FIND_MATCHES);
assert!(find.is_truncated());
assert!(find.count_label().ends_with('+'));
let mut oversized = FindState::new();
assert!(!oversized.set_query("x".repeat(MAX_FIND_QUERY_BYTES + 1), &text));
assert!(oversized.query().is_empty());
assert!(!oversized.set_replacement("y".repeat(MAX_FIND_REPLACEMENT_BYTES + 1)));
}
#[test]
fn replacing_the_current_match_advances_without_losing_position() {
let text = "one two one";
let mut find = FindState::new();
assert!(find.set_query("one", text));
assert!(find.set_replacement("1"));
assert_eq!(find.find_next(text), Some(8..11));
let (replaced, range) = find.replace_current(text).expect("a current match exists");
assert_eq!(replaced, "one two 1");
assert_eq!(range, 8..9);
assert_eq!(find.match_count(), 1);
}
#[test]
fn replace_all_is_one_edit_and_one_undo_entry() {
let text = "red red red";
let mut find = FindState::new();
assert!(find.set_query("red", text));
assert!(find.set_replacement("blue"));
let replaced = find.replace_all(text).expect("matches exist");
assert_eq!(replaced, "blue blue blue");
let mut input = TextInputState::with_constraints(
text,
false,
InputConstraints {
max_length: None,
filter: None,
text_checking: TextCheckingOverrides::default(),
},
);
assert!(input.set_value(&replaced));
assert_eq!(input.text(), "blue blue blue");
assert!(input.undo());
assert_eq!(input.text(), "red red red");
assert!(!input.can_undo());
}
#[test]
fn highlights_distinguish_the_current_match() {
let text = "aa aa";
let mut find = FindState::new();
assert!(find.set_query("aa", text));
let highlights = find.highlights();
assert_eq!(highlights.len(), 2);
assert_eq!(highlights[0].range(), 0..2);
assert_ne!(highlights[0].style(), highlights[1].style());
find.find_next(text);
let highlights = find.highlights();
assert_eq!(highlights[1].style(), &find.current_match_style);
assert_eq!(highlights[0].style(), &find.match_style);
}
#[test]
fn find_bar_parts_are_stable_labelled_and_state_driven() {
let text = "alpha alpha";
let mut find = FindState::new();
assert!(find.set_query("alpha", text));
let bar = FindBar::new("editor-find").state(&find);
assert_eq!(bar.count_text().as_ref(), "1 of 2");
let root = bar.root_with(crate::div().w(320.0));
assert_eq!(root.explicit_id, Some(bar.id()));
assert!(root.key_context.is_some());
assert_eq!(root.accessibility.role, AccessibilityRole::Group);
assert_eq!(
root.accessibility.relations.described_by(),
Some(bar.count_id())
);
let count = bar.count_with(crate::text("1 of 2"));
assert_eq!(count.explicit_id, Some(bar.count_id()));
assert_eq!(count.accessibility.value.as_deref(), Some("1 of 2"));
let next = bar.next_with(crate::button());
assert_eq!(
next.explicit_id,
Some(derived_find_id(bar.id(), FIND_BAR_NEXT_TAG))
);
assert!(!next.accessibility.disabled);
let empty = FindBar::new("editor-find").state(&FindState::new());
assert!(empty.next_with(crate::button()).accessibility.disabled);
assert!(
empty
.replace_all_with(crate::button())
.accessibility
.disabled
);
let ids = [
bar.id(),
bar.query_input_id(),
bar.replace_input_id(),
bar.count_id(),
];
for (index, id) in ids.iter().enumerate() {
assert!(!ids[..index].contains(id));
}
}
#[test]
fn find_bar_key_bindings_cover_return_shift_return_and_escape() {
let bindings = find_bar_key_bindings();
assert_eq!(bindings.len(), 3);
assert!(
bindings
.iter()
.all(|binding| binding.context_predicate().is_some())
);
assert!(
bindings
.iter()
.any(|binding| binding.action().downcast_ref::<FindPrevious>().is_some())
);
}
}