From 8b02bf2ea8c07926697ad8e1e01d77596a540d59 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Mon, 7 Feb 2022 23:37:09 +0800 Subject: Add (prev) paragraph motion Also improved testing facility. Fix #1580 --- helix-term/src/commands.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'helix-term/src/commands.rs') diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 29648039..beb564ad 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -209,6 +209,8 @@ impl MappableCommand { move_next_long_word_start, "Move to beginning of next long word", move_prev_long_word_start, "Move to beginning of previous long word", move_next_long_word_end, "Move to end of next long word", + move_prev_para, "Move to previous paragraph", + move_next_para, "Move to next paragraph", extend_next_word_start, "Extend to beginning of next word", extend_prev_word_start, "Extend to beginning of previous word", extend_next_long_word_start, "Extend to beginning of next long word", @@ -902,6 +904,34 @@ fn move_next_long_word_end(cx: &mut Context) { move_word_impl(cx, movement::move_next_long_word_end) } +fn move_para_impl(cx: &mut Context, move_fn: F) +where + F: Fn(RopeSlice, Range, usize, Movement) -> Range, +{ + let count = cx.count(); + let (view, doc) = current!(cx.editor); + let text = doc.text().slice(..); + let behavior = if doc.mode == Mode::Select { + Movement::Extend + } else { + Movement::Move + }; + + let selection = doc + .selection(view.id) + .clone() + .transform(|range| move_fn(text, range, count, behavior)); + doc.set_selection(view.id, selection); +} + +fn move_prev_para(cx: &mut Context) { + move_para_impl(cx, movement::move_prev_para) +} + +fn move_next_para(cx: &mut Context) { + move_para_impl(cx, movement::move_next_para) +} + fn goto_file_start(cx: &mut Context) { if cx.count.is_some() { goto_line(cx); -- cgit v1.2.3-70-g09d2