mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-15 01:00:02 +00:00
implement infinitive search iter
This commit is contained in:
parent
f444d9b115
commit
58140ab335
@ -48,12 +48,13 @@ impl Form {
|
||||
let navigation = navigation.clone();
|
||||
let subject = subject.clone();
|
||||
move |_| {
|
||||
navigation.update(find(
|
||||
let matches = find(
|
||||
&subject,
|
||||
input.entry.text().as_str(),
|
||||
match_case.is_active(),
|
||||
));
|
||||
input.update(navigation.is_match());
|
||||
);
|
||||
input.update(!matches.is_empty());
|
||||
navigation.update(matches);
|
||||
}
|
||||
});
|
||||
|
||||
@ -62,12 +63,9 @@ impl Form {
|
||||
let navigation = navigation.clone();
|
||||
let subject = subject.clone();
|
||||
move |this| {
|
||||
navigation.update(find(
|
||||
&subject,
|
||||
input.entry.text().as_str(),
|
||||
this.is_active(),
|
||||
));
|
||||
input.update(navigation.is_match());
|
||||
let matches = find(&subject, input.entry.text().as_str(), this.is_active());
|
||||
input.update(!matches.is_empty());
|
||||
navigation.update(matches);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -1,18 +1,17 @@
|
||||
mod back;
|
||||
mod forward;
|
||||
mod iter;
|
||||
|
||||
use back::Back;
|
||||
use forward::Forward;
|
||||
use iter::Iter;
|
||||
|
||||
use super::Subject;
|
||||
use gtk::{
|
||||
prelude::{BoxExt, TextBufferExt, TextViewExt},
|
||||
Box, Orientation, TextIter,
|
||||
};
|
||||
use std::{
|
||||
cell::{Cell, RefCell},
|
||||
rc::Rc,
|
||||
};
|
||||
use std::cell::RefCell;
|
||||
|
||||
const MARGIN: i32 = 6;
|
||||
|
||||
@ -20,8 +19,7 @@ pub struct Navigation {
|
||||
pub back: Back,
|
||||
pub forward: Forward,
|
||||
pub g_box: Box,
|
||||
index: Rc<Cell<usize>>,
|
||||
matches: Rc<RefCell<Vec<(TextIter, TextIter)>>>,
|
||||
iter: RefCell<Option<Iter>>,
|
||||
}
|
||||
|
||||
impl Navigation {
|
||||
@ -29,10 +27,6 @@ impl Navigation {
|
||||
|
||||
/// Create new `Self`
|
||||
pub fn new() -> Self {
|
||||
// Init shared matches holder
|
||||
let index = Rc::new(Cell::new(0));
|
||||
let matches = Rc::new(RefCell::new(Vec::new()));
|
||||
|
||||
// Init components
|
||||
let back = Back::new();
|
||||
let forward = Forward::new();
|
||||
@ -53,21 +47,16 @@ impl Navigation {
|
||||
back,
|
||||
forward,
|
||||
g_box,
|
||||
index,
|
||||
matches,
|
||||
iter: RefCell::new(None),
|
||||
}
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
pub fn update(&self, matches: Vec<(TextIter, TextIter)>) {
|
||||
// Update self
|
||||
self.matches.replace(matches);
|
||||
self.index.replace(0); // reset
|
||||
|
||||
// Update child components
|
||||
self.back.update(self.is_match());
|
||||
self.forward.update(self.is_match());
|
||||
self.back.update(!matches.is_empty());
|
||||
self.forward.update(!matches.is_empty());
|
||||
self.iter.replace(Some(Iter::new(matches)));
|
||||
}
|
||||
|
||||
pub fn back(&self, subject: &Subject) -> Option<(TextIter, TextIter)> {
|
||||
@ -79,23 +68,15 @@ impl Navigation {
|
||||
&buffer.end_iter(),
|
||||
);
|
||||
|
||||
let index = self.index.take();
|
||||
|
||||
match self.matches.borrow().get(back(index)) {
|
||||
match self.iter.borrow_mut().as_mut() {
|
||||
Some(iter) => match iter.back() {
|
||||
Some((start, end)) => {
|
||||
buffer.apply_tag(&subject.tag.current, start, end);
|
||||
self.index.replace(if index == 0 {
|
||||
len_to_index(self.matches.borrow().len())
|
||||
} else {
|
||||
index
|
||||
});
|
||||
Some((*start, *end))
|
||||
}
|
||||
None => {
|
||||
self.index
|
||||
.replace(len_to_index(self.matches.borrow().len())); // go last
|
||||
None
|
||||
buffer.apply_tag(&subject.tag.current, &start, &end);
|
||||
Some((start, end))
|
||||
}
|
||||
None => iter.reset(),
|
||||
},
|
||||
None => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -108,36 +89,15 @@ impl Navigation {
|
||||
&buffer.end_iter(),
|
||||
);
|
||||
|
||||
let index = self.index.take();
|
||||
let next = forward(index);
|
||||
match self.matches.borrow().get(next) {
|
||||
match self.iter.borrow_mut().as_mut() {
|
||||
Some(iter) => match iter.forward() {
|
||||
Some((start, end)) => {
|
||||
buffer.apply_tag(&subject.tag.current, start, end);
|
||||
self.index.replace(next);
|
||||
Some((*start, *end))
|
||||
buffer.apply_tag(&subject.tag.current, &start, &end);
|
||||
Some((start, end))
|
||||
}
|
||||
None => {
|
||||
self.index.replace(0);
|
||||
None
|
||||
None => iter.reset(),
|
||||
},
|
||||
None => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
pub fn is_match(&self) -> bool {
|
||||
!self.matches.borrow().is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
fn back(index: usize) -> usize {
|
||||
index - 1
|
||||
}
|
||||
|
||||
fn forward(index: usize) -> usize {
|
||||
index + 1
|
||||
}
|
||||
|
||||
fn len_to_index(len: usize) -> usize {
|
||||
len - 1
|
||||
}
|
||||
|
@ -0,0 +1,45 @@
|
||||
use gtk::TextIter;
|
||||
|
||||
pub struct Iter {
|
||||
value: Vec<(TextIter, TextIter)>,
|
||||
index: Option<usize>,
|
||||
}
|
||||
|
||||
impl Iter {
|
||||
pub fn new(value: Vec<(TextIter, TextIter)>) -> Self {
|
||||
Self { index: None, value }
|
||||
}
|
||||
|
||||
pub fn back(&mut self) -> Option<(TextIter, TextIter)> {
|
||||
self.index = match self.index {
|
||||
Some(index) => {
|
||||
if index > 0 {
|
||||
Some(index - 1)
|
||||
} else {
|
||||
Some(self.value.len())
|
||||
}
|
||||
}
|
||||
None => Some(self.value.len()), // init
|
||||
};
|
||||
self.value.get(self.index.unwrap_or_default()).copied()
|
||||
}
|
||||
|
||||
pub fn forward(&mut self) -> Option<(TextIter, TextIter)> {
|
||||
self.index = match self.index {
|
||||
Some(index) => {
|
||||
if index < self.value.len() {
|
||||
Some(index + 1)
|
||||
} else {
|
||||
Some(0)
|
||||
}
|
||||
}
|
||||
None => Some(0), // init
|
||||
};
|
||||
self.value.get(self.index.unwrap_or_default()).copied()
|
||||
}
|
||||
|
||||
pub fn reset(&mut self) -> Option<(TextIter, TextIter)> {
|
||||
self.index = None;
|
||||
self.forward()
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user