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.
This commit is contained in:
Michael Davis 2022-11-16 18:59:59 -06:00 committed by GitHub
parent 6eec14ee37
commit b474ee1843
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 14 deletions

View File

@ -1243,16 +1243,7 @@ pub fn focus(&mut self, view_id: ViewId) {
}
pub fn focus_next(&mut self) {
let prev_id = self.tree.focus;
self.tree.focus_next();
let id = self.tree.focus;
// if leaving the view: mode should reset and the cursor should be
// within view
if prev_id != id {
self.mode = Mode::Normal;
self.ensure_cursor_in_view(id);
}
self.focus(self.tree.next());
}
pub fn focus_direction(&mut self, direction: tree::Direction) {

View File

@ -219,7 +219,7 @@ pub fn remove(&mut self, index: ViewId) {
if self.focus == index {
// focus on something else
self.focus_next();
self.focus = self.next();
}
stack.push(index);
@ -521,7 +521,7 @@ fn find_child(&self, id: ViewId, children: &[ViewId], direction: Direction) -> O
Some(child_id)
}
pub fn focus_next(&mut self) {
pub fn next(&self) -> ViewId {
// This function is very dumb, but that's because we don't store any parent links.
// (we'd be able to go parent.next_sibling() recursively until we find something)
// For now that's okay though, since it's unlikely you'll be able to open a large enough
@ -532,11 +532,11 @@ pub fn focus_next(&mut self) {
.skip_while(|&(id, _view)| id != self.focus)
.skip(1); // Skip focused value
if let Some((id, _)) = views.next() {
self.focus = id;
id
} else {
// extremely crude, take the first item again
let (key, _) = self.traverse().next().unwrap();
self.focus = key;
key
}
}