mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-15 01:00:02 +00:00
use cycle iter
This commit is contained in:
parent
093ee87b1f
commit
8cf8d2b298
@ -1,17 +1,15 @@
|
||||
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::RefCell;
|
||||
use std::{cell::RefCell, iter::Cycle, vec::IntoIter};
|
||||
|
||||
const MARGIN: i32 = 6;
|
||||
|
||||
@ -19,7 +17,7 @@ pub struct Navigation {
|
||||
pub back: Back,
|
||||
pub forward: Forward,
|
||||
pub g_box: Box,
|
||||
iter: RefCell<Option<Iter>>,
|
||||
iter: RefCell<Option<Cycle<IntoIter<(TextIter, TextIter)>>>>,
|
||||
}
|
||||
|
||||
impl Navigation {
|
||||
@ -56,7 +54,7 @@ impl Navigation {
|
||||
pub fn update(&self, matches: Vec<(TextIter, TextIter)>) {
|
||||
self.back.update(!matches.is_empty());
|
||||
self.forward.update(!matches.is_empty());
|
||||
self.iter.replace(Some(Iter::new(matches)));
|
||||
self.iter.replace(Some(matches.into_iter().cycle()));
|
||||
}
|
||||
|
||||
pub fn back(&self, subject: &Subject) -> Option<(TextIter, TextIter)> {
|
||||
@ -69,15 +67,15 @@ impl Navigation {
|
||||
);
|
||||
|
||||
match self.iter.borrow_mut().as_mut() {
|
||||
Some(iter) => match iter.back() {
|
||||
Some(iter) => match iter.next() {
|
||||
Some((start, end)) => {
|
||||
buffer.apply_tag(&subject.tag.current, &start, &end);
|
||||
Some((start, end))
|
||||
}
|
||||
None => iter.reset(),
|
||||
None => None,
|
||||
},
|
||||
None => todo!(), // unexpected
|
||||
}
|
||||
} // @TODO reverse
|
||||
}
|
||||
|
||||
pub fn forward(&self, subject: &Subject) -> Option<(TextIter, TextIter)> {
|
||||
@ -90,12 +88,12 @@ impl Navigation {
|
||||
);
|
||||
|
||||
match self.iter.borrow_mut().as_mut() {
|
||||
Some(iter) => match iter.forward() {
|
||||
Some(iter) => match iter.next() {
|
||||
Some((start, end)) => {
|
||||
buffer.apply_tag(&subject.tag.current, &start, &end);
|
||||
Some((start, end))
|
||||
}
|
||||
None => iter.reset(),
|
||||
None => None,
|
||||
},
|
||||
None => todo!(), // unexpected
|
||||
}
|
||||
|
@ -1,45 +0,0 @@
|
||||
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