Skip to content

Commit 3afb212

Browse files
ChrHornwes adams
authored andcommitted
When view closes, focus on previous view (#4539)
1 parent 322e957 commit 3afb212

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

helix-view/src/tree.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl Tree {
219219

220220
if self.focus == index {
221221
// focus on something else
222-
self.focus = self.next();
222+
self.focus = self.prev();
223223
}
224224

225225
stack.push(index);
@@ -521,6 +521,26 @@ impl Tree {
521521
Some(child_id)
522522
}
523523

524+
pub fn prev(&self) -> ViewId {
525+
// This function is very dumb, but that's because we don't store any parent links.
526+
// (we'd be able to go parent.next_sibling() recursively until we find something)
527+
// For now that's okay though, since it's unlikely you'll be able to open a large enough
528+
// number of splits to notice.
529+
530+
let mut views = self
531+
.traverse()
532+
.rev()
533+
.skip_while(|&(id, _view)| id != self.focus)
534+
.skip(1); // Skip focused value
535+
if let Some((id, _)) = views.next() {
536+
id
537+
} else {
538+
// extremely crude, take the first item again
539+
let (key, _) = self.traverse().rev().next().unwrap();
540+
key
541+
}
542+
}
543+
524544
pub fn next(&self) -> ViewId {
525545
// This function is very dumb, but that's because we don't store any parent links.
526546
// (we'd be able to go parent.next_sibling() recursively until we find something)
@@ -661,6 +681,23 @@ impl<'a> Iterator for Traverse<'a> {
661681
}
662682
}
663683

684+
impl<'a> DoubleEndedIterator for Traverse<'a> {
685+
fn next_back(&mut self) -> Option<Self::Item> {
686+
loop {
687+
let key = self.stack.pop()?;
688+
689+
let node = &self.tree.nodes[key];
690+
691+
match &node.content {
692+
Content::View(view) => return Some((key, view)),
693+
Content::Container(container) => {
694+
self.stack.extend(container.children.iter());
695+
}
696+
}
697+
}
698+
}
699+
}
700+
664701
#[cfg(test)]
665702
mod test {
666703
use super::*;

0 commit comments

Comments
 (0)