mirror of
https://github.com/helix-editor/helix.git
synced 2025-01-18 21:17:08 +04:00
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`
This commit is contained in:
parent
e4a9bec562
commit
70e4cdbd8e
@ -111,7 +111,8 @@ ### Selection manipulation
|
||||
| `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` |
|
||||
|
@ -522,7 +522,14 @@ fn normalize(mut self) -> Self {
|
||||
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];
|
||||
|
||||
|
@ -275,6 +275,7 @@ pub fn doc(&self) -> &str {
|
||||
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();
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user