From 70e4cdbd8e3f0b2a1c5bd2e1cb1c1ec537bdca04 Mon Sep 17 00:00:00 2001 From: Szabin Date: Sat, 20 May 2023 01:31:39 +0200 Subject: Add command to merge non-consecutive ranges (#7053) * Add command for merging non-consecutive ranges * Add `merge_selections` command to book * Simplify `merge_ranges` Heeded the advice of @the-mikedavis to stop iterating over all ranges and simply merge the first and the last range, as the invariants of `Selection` guarantee that the list of ranges is always sorted and never empty. * Clarify doc comment of `merge_ranges`--- helix-term/src/commands.rs | 7 +++++++ helix-term/src/keymap/default.rs | 1 + 2 files changed, 8 insertions(+) (limited to 'helix-term') diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 9859f64b..116c6378 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -275,6 +275,7 @@ impl MappableCommand { select_regex, "Select all regex matches inside selections", split_selection, "Split selections on regex matches", split_selection_on_newline, "Split selection on newlines", + merge_selections, "Merge selections", merge_consecutive_selections, "Merge consecutive selections", search, "Search for regex pattern", rsearch, "Reverse search for regex pattern", @@ -1737,6 +1738,12 @@ fn split_selection_on_newline(cx: &mut Context) { doc.set_selection(view.id, selection); } +fn merge_selections(cx: &mut Context) { + let (view, doc) = current!(cx.editor); + let selection = doc.selection(view.id).clone().merge_ranges(); + doc.set_selection(view.id, selection); +} + fn merge_consecutive_selections(cx: &mut Context) { let (view, doc) = current!(cx.editor); let selection = doc.selection(view.id).clone().merge_consecutive_ranges(); diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index 9bd00280..d80ab1ff 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -79,6 +79,7 @@ pub fn default() -> HashMap { "s" => select_regex, "A-s" => split_selection_on_newline, + "A-minus" => merge_selections, "A-_" => merge_consecutive_selections, "S" => split_selection, ";" => collapse_selection, -- cgit v1.2.3-70-g09d2