Skip to content

Commit 4387449

Browse files
pickfireShekhinah Memmel
authored andcommitted
Select inserted space after join (helix-editor#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 d024465 commit 4387449

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",
@@ -3771,7 +3772,7 @@ fn format_selections(cx: &mut Context) {
37713772
}
37723773
}
37733774

3774-
fn join_selections(cx: &mut Context) {
3775+
fn join_selections_inner(cx: &mut Context, select_space: bool) {
37753776
use movement::skip_while;
37763777
let (view, doc) = current!(cx.editor);
37773778
let text = doc.text();
@@ -3806,9 +3807,21 @@ fn join_selections(cx: &mut Context) {
38063807
// TODO: joining multiple empty lines should be replaced by a single space.
38073808
// need to merge change ranges that touch
38083809

3809-
let transaction = Transaction::change(doc.text(), changes.into_iter());
3810-
// TODO: select inserted spaces
3811-
// .with_selection(selection);
3810+
// select inserted spaces
3811+
let transaction = if select_space {
3812+
let ranges: SmallVec<_> = changes
3813+
.iter()
3814+
.scan(0, |offset, change| {
3815+
let range = Range::point(change.0 - *offset);
3816+
*offset += change.1 - change.0 - 1; // -1 because cursor is 0-sized
3817+
Some(range)
3818+
})
3819+
.collect();
3820+
let selection = Selection::new(ranges, 0);
3821+
Transaction::change(doc.text(), changes.into_iter()).with_selection(selection)
3822+
} else {
3823+
Transaction::change(doc.text(), changes.into_iter())
3824+
};
38123825

38133826
doc.apply(&transaction, view.id);
38143827
}
@@ -3837,6 +3850,14 @@ fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) {
38373850
)
38383851
}
38393852

3853+
fn join_selections(cx: &mut Context) {
3854+
join_selections_inner(cx, false)
3855+
}
3856+
3857+
fn join_selections_space(cx: &mut Context) {
3858+
join_selections_inner(cx, true)
3859+
}
3860+
38403861
fn keep_selections(cx: &mut Context) {
38413862
keep_or_remove_selections_impl(cx, false)
38423863
}

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)