Struct SearchState

Source
pub struct SearchState {
    pub query: SearchQuery,
    pub matches: Vec<SearchMatch>,
    pub active_index: Option<usize>,
    /* private fields */
}
Expand description

Mutable search results, selection, and cache state.

The cache key contains only the query and DocumentVersion. Callers must change the version whenever the searched text changes or explicitly call SearchState::invalidate_cache.

§Examples

use ailloli_ui_editor::{DocumentVersion, SearchQuery, SearchState};

let mut state = SearchState::new(SearchQuery::new("go"));
assert!(state.refresh("go stop go", DocumentVersion(1)));
assert_eq!(state.matches.len(), 2);
assert_eq!(state.active_index, Some(0));

Fields§

§query: SearchQuery

Query used by the next refresh.

§matches: Vec<SearchMatch>

Ordered, non-overlapping matches from the last refresh.

§active_index: Option<usize>

Selected match index, or None when no match is selected.

Implementations§

Source§

impl SearchState

Updates search results and active-match selection.

Source

pub fn new(query: SearchQuery) -> Self

Creates an unrefreshed state for query.

§Examples
use ailloli_ui_editor::{SearchQuery, SearchState};

let state = SearchState::new(SearchQuery::new("needle"));
assert_eq!(state.query.text, "needle");
assert!(state.matches.is_empty() && state.active_index.is_none());
Source

pub fn set_query(&mut self, query: SearchQuery)

Replaces the query and clears cached results when it differs.

Supplying an equal query is a no-op and preserves both matches and the active index.

§Examples
use ailloli_ui_editor::{DocumentVersion, SearchQuery, SearchState};

let mut state = SearchState::new(SearchQuery::new("old"));
state.refresh("old", DocumentVersion(1));
state.set_query(SearchQuery::new("new"));
assert!(state.matches.is_empty() && state.active_index.is_none());
Source

pub fn refresh(&mut self, text: &str, version: DocumentVersion) -> bool

Recomputes results unless the query/version cache key is unchanged.

Returns true after recomputation and false for a cache hit. An existing active index is clamped to the new final match; non-empty fresh results otherwise select index zero.

§Examples
use ailloli_ui_editor::{DocumentVersion, SearchQuery, SearchState};

let mut state = SearchState::new(SearchQuery::new("a"));
assert!(state.refresh("a a", DocumentVersion(4)));
assert!(!state.refresh("different text", DocumentVersion(4)));
assert_eq!(state.matches.len(), 2);
Source

pub fn next_match(&mut self) -> Option<&SearchMatch>

Selects and returns the following match, wrapping at the end.

Because a successful refresh selects index zero, the first call after a multi-match refresh advances to index one. With no results, this returns None and clears the active index.

§Examples
use ailloli_ui_editor::{DocumentVersion, SearchQuery, SearchState};

let mut state = SearchState::new(SearchQuery::new("x"));
state.refresh("x x", DocumentVersion(1));
assert_eq!(state.next_match().map(|m| m.range.clone()), Some(2..3));
assert_eq!(state.next_match().map(|m| m.range.clone()), Some(0..1));
Source

pub fn previous_match(&mut self) -> Option<&SearchMatch>

Selects and returns the preceding match, wrapping at the start.

With no results, this returns None and clears the active index.

§Examples
use ailloli_ui_editor::{DocumentVersion, SearchQuery, SearchState};

let mut state = SearchState::new(SearchQuery::new("x"));
state.refresh("x x", DocumentVersion(1));
assert_eq!(state.previous_match().map(|m| m.range.clone()), Some(2..3));
Source

pub fn set_active_index(&mut self, active_index: Option<usize>)

Selects an in-range match index, otherwise clears the selection.

§Examples
use ailloli_ui_editor::{DocumentVersion, SearchQuery, SearchState};

let mut state = SearchState::new(SearchQuery::new("x"));
state.refresh("x x", DocumentVersion(1));
state.set_active_index(Some(9));
assert_eq!(state.active_index, None);
state.set_active_index(Some(1));
assert_eq!(state.active_index, Some(1));
Source

pub fn clear(&mut self)

Restores the default empty query, results, selection, and cache.

§Examples
use ailloli_ui_editor::{DocumentVersion, SearchQuery, SearchState};

let mut state = SearchState::new(SearchQuery::new("x"));
state.refresh("x", DocumentVersion(1));
state.clear();
assert!(state.query.text.is_empty() && state.matches.is_empty());
Source

pub fn invalidate_cache(&mut self)

Forces the next SearchState::refresh to recompute.

Existing results and selection remain available until that refresh.

§Examples
use ailloli_ui_editor::{DocumentVersion, SearchQuery, SearchState};

let mut state = SearchState::new(SearchQuery::new("x"));
state.refresh("x", DocumentVersion(1));
state.invalidate_cache();
assert!(state.refresh("x x", DocumentVersion(1)));
assert_eq!(state.matches.len(), 2);

Trait Implementations§

Source§

impl Clone for SearchState

Source§

fn clone(&self) -> SearchState

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SearchState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SearchState

Creates an empty search state with an empty query.

Source§

fn default() -> Self

Creates an empty, unrefreshed state with the default empty query.

Source§

impl PartialEq for SearchState

Source§

fn eq(&self, other: &SearchState) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for SearchState

Source§

impl StructuralPartialEq for SearchState

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Brush for T
where T: Clone + PartialEq + Default + Debug,

§

impl<T> ErasedDestructor for T
where T: 'static,