From 755c933df322cadb0fbd2e477616ff800521c3f7 Mon Sep 17 00:00:00 2001 From: wes adams Date: Tue, 15 Nov 2022 15:43:19 -0500 Subject: [PATCH] When buffer closes, focus on parent buffer --- helix-view/src/tree.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/helix-view/src/tree.rs b/helix-view/src/tree.rs index 6174021cb000c..7c8c2cc71cab6 100644 --- a/helix-view/src/tree.rs +++ b/helix-view/src/tree.rs @@ -21,6 +21,7 @@ pub struct Tree { pub struct Node { parent: ViewId, content: Content, + spawned_by: ViewId, } #[derive(Debug)] @@ -30,17 +31,19 @@ pub enum Content { } impl Node { - pub fn container(layout: Layout) -> Self { + pub fn container(layout: Layout, focus: ViewId) -> Self { Self { parent: ViewId::default(), content: Content::Container(Box::new(Container::new(layout))), + spawned_by: focus, } } - pub fn view(view: View) -> Self { + pub fn view(view: View, focus: ViewId) -> Self { Self { parent: ViewId::default(), content: Content::View(Box::new(view)), + spawned_by: focus, } } } @@ -85,10 +88,11 @@ impl Default for Container { impl Tree { pub fn new(area: Rect) -> Self { - let root = Node::container(Layout::Vertical); + let root = Node::container(Layout::Vertical, ViewId::default()); let mut nodes = HopSlotMap::with_key(); let root = nodes.insert(root); + //nodes[root].spawned_by = root; // root is it's own parent nodes[root].parent = root; @@ -106,7 +110,7 @@ impl Tree { pub fn insert(&mut self, view: View) -> ViewId { let focus = self.focus; let parent = self.nodes[focus].parent; - let mut node = Node::view(view); + let mut node = Node::view(view, focus); node.parent = parent; let node = self.nodes.insert(node); self.get_mut(node).id = node; @@ -145,7 +149,7 @@ impl Tree { let focus = self.focus; let parent = self.nodes[focus].parent; - let node = Node::view(view); + let node = Node::view(view, focus); let node = self.nodes.insert(node); self.get_mut(node).id = node; @@ -171,7 +175,7 @@ impl Tree { container.children.insert(pos, node); self.nodes[node].parent = parent; } else { - let mut split = Node::container(layout); + let mut split = Node::container(layout, focus); split.parent = parent; let split = self.nodes.insert(split); @@ -219,7 +223,13 @@ impl Tree { if self.focus == index { // focus on something else - self.focus_next(); + if self.contains(self.nodes[index].spawned_by) + && self.nodes[index].spawned_by != self.root + { + self.focus = self.nodes[index].spawned_by; + } else { + self.focus_next(); + } } stack.push(index);