Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to merge non-consecutive ranges #7053

Merged
merged 4 commits into from May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion book/src/keymap.md
Expand Up @@ -111,7 +111,8 @@
| `s` | Select all regex matches inside selections | `select_regex` |
| `S` | Split selection into sub selections on regex matches | `split_selection` |
| `Alt-s` | Split selection on newlines | `split_selection_on_newline` |
| `Alt-_ ` | Merge consecutive selections | `merge_consecutive_selections` |
| `Alt-minus` | Merge selections | `merge_selections` |
| `Alt-_` | Merge consecutive selections | `merge_consecutive_selections` |
| `&` | Align selection in columns | `align_selections` |
| `_` | Trim whitespace from the selection | `trim_selections` |
| `;` | Collapse selection onto a single cursor | `collapse_selection` |
Expand Down
9 changes: 8 additions & 1 deletion helix-core/src/selection.rs
Expand Up @@ -522,7 +522,14 @@ impl Selection {
self
}

// Merges all ranges that are consecutive
/// Replaces ranges with one spanning from first to last range.
pub fn merge_ranges(self) -> Self {
let first = self.ranges.first().unwrap();
let last = self.ranges.last().unwrap();
Selection::new(smallvec![first.merge(*last)], 0)
}

/// Merges all ranges that are consecutive.
pub fn merge_consecutive_ranges(mut self) -> Self {
let mut primary = self.ranges[self.primary_index];

Expand Down
7 changes: 7 additions & 0 deletions helix-term/src/commands.rs
Expand Up @@ -274,6 +274,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",
Expand Down Expand Up @@ -1740,6 +1741,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();
Expand Down
1 change: 1 addition & 0 deletions helix-term/src/keymap/default.rs
Expand Up @@ -79,6 +79,7 @@ pub fn default() -> HashMap<Mode, Keymap> {

"s" => select_regex,
"A-s" => split_selection_on_newline,
"A-minus" => merge_selections,
"A-_" => merge_consecutive_selections,
"S" => split_selection,
";" => collapse_selection,
Expand Down