diff --git a/core/src/manager/finder.rs b/core/src/manager/finder.rs index 57470fac1..b8fdec8e1 100644 --- a/core/src/manager/finder.rs +++ b/core/src/manager/finder.rs @@ -17,60 +17,24 @@ impl Finder { Ok(Self { query: Regex::new(s)?, matched: Default::default(), version: 0 }) } - pub(super) fn arrow(&self, files: &Files, cursor: usize, prev: bool) -> Option { - if prev { - for i in 1..files.len() { - let index = (cursor + files.len() - i) % files.len(); - if files[index].name().is_some_and(|name| self.matches(name)) { - return Some((index as isize) - (cursor as isize)); - } + pub(super) fn prev(&self, files: &Files, cursor: usize, include: bool) -> Option { + for i in include as usize ^ 1..files.len() { + let idx = (cursor + files.len() - i) % files.len(); + if files[idx].name().is_some_and(|n| self.matches(n)) { + return Some(idx as isize - cursor as isize); } - None - } else { - for i in 1..files.len() { - let index = (cursor + i) % files.len(); - if files[index].name().is_some_and(|name| self.matches(name)) { - return Some((index as isize) - (cursor as isize)); - } - } - None } + None } - pub(super) fn ring(&self, files: &Files, cursor: usize, prev: bool) -> Option { - if prev { - files - .iter() - .take(cursor + 1) - .rev() - .enumerate() - .find(|(_, f)| f.name().map_or(false, |n| self.matches(n))) - .map(|(i, _)| -(i as isize)) - .or_else(|| { - files - .iter() - .skip(cursor + 1) - .enumerate() - .find(|(_, f)| f.name().map_or(false, |n| self.matches(n))) - .map(|(i, _)| i as isize + 1) - }) - } else { - files - .iter() - .skip(cursor) - .enumerate() - .find(|(_, f)| f.name().map_or(false, |n| self.matches(n))) - .map(|(i, _)| i as isize) - .or_else(|| { - files - .iter() - .take(cursor) - .rev() - .enumerate() - .find(|(_, f)| f.name().map_or(false, |n| self.matches(n))) - .map(|(i, _)| -(i as isize) - 1) - }) + pub(super) fn next(&self, files: &Files, cursor: usize, include: bool) -> Option { + for i in include as usize ^ 1..files.len() { + let idx = (cursor + i) % files.len(); + if files[idx].name().is_some_and(|n| self.matches(n)) { + return Some(idx as isize - cursor as isize); + } } + None } pub(super) fn catchup(&mut self, files: &Files) -> bool { diff --git a/core/src/manager/tab.rs b/core/src/manager/tab.rs index 295e8c65e..7506314f1 100644 --- a/core/src/manager/tab.rs +++ b/core/src/manager/tab.rs @@ -243,7 +243,13 @@ impl Tab { return false; }; - if let Some(step) = finder.ring(&self.current.files, self.current.cursor(), prev) { + let step = if prev { + finder.prev(&self.current.files, self.current.cursor(), true) + } else { + finder.next(&self.current.files, self.current.cursor(), true) + }; + + if let Some(step) = step { self.arrow(step.into()); } @@ -274,12 +280,14 @@ impl Tab { return false; }; - let mut b = finder.catchup(&self.current.files); - if let Some(step) = finder.arrow(&self.current.files, self.current.cursor(), prev) { - b |= self.arrow(step.into()); - } + let b = finder.catchup(&self.current.files); + let step = if prev { + finder.prev(&self.current.files, self.current.cursor(), false) + } else { + finder.next(&self.current.files, self.current.cursor(), false) + }; - b + b || step.is_some_and(|s| self.arrow(s.into())) } pub fn search(&mut self, grep: bool) -> bool {