From 7339066a17dc034d7b11f10beca7488c756f46ef Mon Sep 17 00:00:00 2001 From: Matthew Bourke Date: Tue, 16 Apr 2024 00:40:00 +1000 Subject: [PATCH 1/3] Enable traversing multiple buffers at once --- helix-term/src/commands.rs | 18 +++++++++--------- helix-term/src/commands/typed.rs | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 6cda93dcb73c..b9de365556d3 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -799,28 +799,28 @@ fn goto_line_start(cx: &mut Context) { } fn goto_next_buffer(cx: &mut Context) { - goto_buffer(cx.editor, Direction::Forward); + goto_buffer(cx.editor, Direction::Forward, cx.count()); } fn goto_previous_buffer(cx: &mut Context) { - goto_buffer(cx.editor, Direction::Backward); + goto_buffer(cx.editor, Direction::Backward, cx.count()); } -fn goto_buffer(editor: &mut Editor, direction: Direction) { +fn goto_buffer(editor: &mut Editor, direction: Direction, count: usize) { let current = view!(editor).doc; let id = match direction { Direction::Forward => { let iter = editor.documents.keys(); - let mut iter = iter.skip_while(|id| *id != ¤t); - iter.next(); // skip current item - iter.next().or_else(|| editor.documents.keys().next()) + // skip 'count' times past current buffer + let mut iter = iter.cycle().skip_while(|id| *id != ¤t).skip(count); + iter.next() } Direction::Backward => { let iter = editor.documents.keys(); - let mut iter = iter.rev().skip_while(|id| *id != ¤t); - iter.next(); // skip current item - iter.next().or_else(|| editor.documents.keys().next_back()) + // skip 'count' times past current buffer + let mut iter = iter.rev().cycle().skip_while(|id| *id != ¤t).skip(count); + iter.next() } } .unwrap(); diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 5d7057da6b39..acbd8a8c3d34 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -309,7 +309,7 @@ fn buffer_next( return Ok(()); } - goto_buffer(cx.editor, Direction::Forward); + goto_buffer(cx.editor, Direction::Forward, 1); Ok(()) } @@ -322,7 +322,7 @@ fn buffer_previous( return Ok(()); } - goto_buffer(cx.editor, Direction::Backward); + goto_buffer(cx.editor, Direction::Backward, 1); Ok(()) } From bdda34df1ad1468300d9ec32e0c34563d19552bd Mon Sep 17 00:00:00 2001 From: Matthew Bourke Date: Tue, 16 Apr 2024 22:37:41 +1000 Subject: [PATCH 2/3] run cargo fmt --- helix-term/src/commands.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index b9de365556d3..c4ba9863f067 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -819,7 +819,11 @@ fn goto_buffer(editor: &mut Editor, direction: Direction, count: usize) { Direction::Backward => { let iter = editor.documents.keys(); // skip 'count' times past current buffer - let mut iter = iter.rev().cycle().skip_while(|id| *id != ¤t).skip(count); + let mut iter = iter + .rev() + .cycle() + .skip_while(|id| *id != ¤t) + .skip(count); iter.next() } } From 50ec798fd9d94c7decfa6a5f2242f81724c715a9 Mon Sep 17 00:00:00 2001 From: Matthew Bourke Date: Tue, 16 Apr 2024 23:34:23 +1000 Subject: [PATCH 3/3] simplify iterator call --- helix-term/src/commands.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index c4ba9863f067..8f7675bc576c 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -813,18 +813,15 @@ fn goto_buffer(editor: &mut Editor, direction: Direction, count: usize) { Direction::Forward => { let iter = editor.documents.keys(); // skip 'count' times past current buffer - let mut iter = iter.cycle().skip_while(|id| *id != ¤t).skip(count); - iter.next() + iter.cycle().skip_while(|id| *id != ¤t).nth(count) } Direction::Backward => { let iter = editor.documents.keys(); // skip 'count' times past current buffer - let mut iter = iter - .rev() + iter.rev() .cycle() .skip_while(|id| *id != ¤t) - .skip(count); - iter.next() + .nth(count) } } .unwrap();