on master help sign in
Factor out common code for focusing the next view (#4607)

There is some common code between Editor::focus_next and Editor::focus that can be eliminated by refactoring Tree::focus_next into a function that only returns the next ViewId.

commitb474ee1843d5cb7cb5291bee4166490a223e5aac authorMichael Davis <mcarsondavis@gmail.com> dateWed 16 Nov 2022, 18:59 (4 years ago) parent6eec14e
2 files changed+5−14
helix-view/src/editor.rs+1−10 helix-view/src/tree.rs+4−4
helix-view/src/editor.rs+1−10
@@ -1243,16 +1243,7 @@ impl Editor {
12431243 }
12441244
12451245 pub fn focus_next(&mut self) {
1246− let prev_id = self.tree.focus;
1247− self.tree.focus_next();
1248− let id = self.tree.focus;
1249−
1250− // if leaving the view: mode should reset and the cursor should be
1251− // within view
1252− if prev_id != id {
1253− self.mode = Mode::Normal;
1254− self.ensure_cursor_in_view(id);
1255− }
1246+ self.focus(self.tree.next());
12561247 }
12571248
12581249 pub fn focus_direction(&mut self, direction: tree::Direction) {
helix-view/src/tree.rs+4−4
@@ -219,7 +219,7 @@ impl Tree {
219219
220220 if self.focus == index {
221221 // focus on something else
222− self.focus_next();
222+ self.focus = self.next();
223223 }
224224
225225 stack.push(index);
@@ -521,7 +521,7 @@ impl Tree {
521521 Some(child_id)
522522 }
523523
524− pub fn focus_next(&mut self) {
524+ pub fn next(&self) -> ViewId {
525525 // This function is very dumb, but that's because we don't store any parent links.
526526 // (we'd be able to go parent.next_sibling() recursively until we find something)
527527 // For now that's okay though, since it's unlikely you'll be able to open a large enough
@@ -532,11 +532,11 @@ impl Tree {
532532 .skip_while(|&(id, _view)| id != self.focus)
533533 .skip(1); // Skip focused value
534534 if let Some((id, _)) = views.next() {
535− self.focus = id;
535+ id
536536 } else {
537537 // extremely crude, take the first item again
538538 let (key, _) = self.traverse().next().unwrap();
539− self.focus = key;
539+ key
540540 }
541541 }
542542