diff options
author | Blaž Hrastnik | 2021-11-04 04:55:45 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-11-04 05:03:03 +0000 |
commit | 7b65a6d687bbf4d12de020a7785082277804bbd3 (patch) | |
tree | 2573582ae120ec6c1da4a4ccf7cb2b6705fead2e | |
parent | 78c68fae91579ccda6f65e55f79316b01c5b654a (diff) |
Rewrite goto_buffer
-rw-r--r-- | helix-term/src/commands.rs | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index c1891baf..f16afdfe 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -530,22 +530,28 @@ fn goto_previous_buffer(cx: &mut Context) { } fn goto_buffer(cx: &mut Context, direction: Direction) { - let buf_cur = current!(cx.editor).1.id(); + let current = view!(cx.editor).doc; - if let Some(pos) = cx.editor.documents.iter().position(|(id, _)| id == buf_cur) { - let goto_id = if direction == Direction::Forward { - if pos < cx.editor.documents.iter().count() - 1 { - cx.editor.documents.iter().nth(pos + 1).unwrap().0 - } else { - cx.editor.documents.iter().next().unwrap().0 - } - } else if pos > 0 { - cx.editor.documents.iter().nth(pos - 1).unwrap().0 - } else { - cx.editor.documents.iter().last().unwrap().0 - }; - cx.editor.switch(goto_id, Action::Replace); + let id = match direction { + Direction::Forward => { + let iter = cx.editor.documents.keys(); + let mut iter = iter.skip_while(|id| *id != ¤t); + iter.next(); // skip current item + iter.next().or_else(|| cx.editor.documents.keys().next()) + } + Direction::Backward => { + let iter = cx.editor.documents.keys(); + let mut iter = iter.rev().skip_while(|id| *id != ¤t); + iter.next(); // skip current item + iter.next() + .or_else(|| cx.editor.documents.keys().rev().next()) + } } + .unwrap(); + + let id = *id; + + cx.editor.switch(id, Action::Replace); } fn extend_to_line_start(cx: &mut Context) { |