Struct SearchState

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§

§

impl SearchState

Updates search results and active-match selection.

pub fn new(query: SearchQuery) -> SearchState

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());

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());

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);

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));

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));

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));

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());

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§

§

impl Clone for SearchState

§

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
§

impl Debug for SearchState

§

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

Formats the value using the given formatter. Read more
§

impl Default for SearchState

Creates an empty search state with an empty query.

§

fn default() -> SearchState

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

§

impl PartialEq for SearchState

§

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.
§

impl Eq for SearchState

§

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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

§

impl<A> IntoClickAction<A> for A
where A: Clone + 'static,

§

fn into_click_action(self) -> ClickAction<A>

Implements the into_click_action helper used by this module.

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> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

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

§

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

§

impl<T> Props for T
where T: Clone + 'static,