Skip to content

Commit 66bbba9

Browse files
authored
Select inserted space after join (#3549)
* Select inserted space after join * Split join_selections with space selection to A-J Kakoune does that too and some users may still want to retain their selections. * Update join_selections docs
1 parent 4a3b776 commit 66bbba9

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

book/src/keymap.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
| `X` | Extend selection to line bounds (line-wise selection) | `extend_to_line_bounds` |
130130
| `Alt-x` | Shrink selection to line bounds (line-wise selection) | `shrink_to_line_bounds` |
131131
| `J` | Join lines inside selection | `join_selections` |
132+
| `A-J` | Join lines inside selection and select space | `join_selections_space` |
132133
| `K` | Keep selections matching the regex | `keep_selections` |
133134
| `Alt-K` | Remove selections matching the regex | `remove_selections` |
134135
| `Ctrl-c` | Comment/uncomment the selections | `toggle_comments` |

helix-term/src/commands.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ impl MappableCommand {
346346
unindent, "Unindent selection",
347347
format_selections, "Format selection",
348348
join_selections, "Join lines inside selection",
349+
join_selections_space, "Join lines inside selection and select spaces",
349350
keep_selections, "Keep selections matching regex",
350351
remove_selections, "Remove selections matching regex",
351352
align_selections, "Align selections in column",
@@ -3731,7 +3732,7 @@ fn format_selections(cx: &mut Context) {
37313732
}
37323733
}
37333734

3734-
fn join_selections(cx: &mut Context) {
3735+
fn join_selections_inner(cx: &mut Context, select_space: bool) {
37353736
use movement::skip_while;
37363737
let (view, doc) = current!(cx.editor);
37373738
let text = doc.text();
@@ -3766,9 +3767,21 @@ fn join_selections(cx: &mut Context) {
37663767
// TODO: joining multiple empty lines should be replaced by a single space.
37673768
// need to merge change ranges that touch
37683769

3769-
let transaction = Transaction::change(doc.text(), changes.into_iter());
3770-
// TODO: select inserted spaces
3771-
// .with_selection(selection);
3770+
// select inserted spaces
3771+
let transaction = if select_space {
3772+
let ranges: SmallVec<_> = changes
3773+
.iter()
3774+
.scan(0, |offset, change| {
3775+
let range = Range::point(change.0 - *offset);
3776+
*offset += change.1 - change.0 - 1; // -1 because cursor is 0-sized
3777+
Some(range)
3778+
})
3779+
.collect();
3780+
let selection = Selection::new(ranges, 0);
3781+
Transaction::change(doc.text(), changes.into_iter()).with_selection(selection)
3782+
} else {
3783+
Transaction::change(doc.text(), changes.into_iter())
3784+
};
37723785

37733786
doc.apply(&transaction, view.id);
37743787
}
@@ -3797,6 +3810,14 @@ fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) {
37973810
)
37983811
}
37993812

3813+
fn join_selections(cx: &mut Context) {
3814+
join_selections_inner(cx, false)
3815+
}
3816+
3817+
fn join_selections_space(cx: &mut Context) {
3818+
join_selections_inner(cx, true)
3819+
}
3820+
38003821
fn keep_selections(cx: &mut Context) {
38013822
keep_or_remove_selections_impl(cx, false)
38023823
}

helix-term/src/keymap/default.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
144144
"<" => unindent,
145145
"=" => format_selections,
146146
"J" => join_selections,
147+
"A-J" => join_selections_space,
147148
"K" => keep_selections,
148149
"A-K" => remove_selections,
149150

0 commit comments

Comments
 (0)