on master help sign in
Select inserted space after join (#3549)
66bbba9Ivan Tham
book/src/keymap.md+1
@@ -129,6 +129,7 @@
129| `X` | Extend selection to line bounds (line-wise selection) | `extend_to_line_bounds` |
129| `X` | Extend selection to line bounds (line-wise selection) | `extend_to_line_bounds` |
130| `Alt-x` | Shrink selection to line bounds (line-wise selection) | `shrink_to_line_bounds` |
130| `Alt-x` | Shrink selection to line bounds (line-wise selection) | `shrink_to_line_bounds` |
131| `J` | Join lines inside selection | `join_selections` |
131| `J` | Join lines inside selection | `join_selections` |
132+| `A-J` | Join lines inside selection and select space | `join_selections_space` |
132| `K` | Keep selections matching the regex | `keep_selections` |
133| `K` | Keep selections matching the regex | `keep_selections` |
133| `Alt-K` | Remove selections matching the regex | `remove_selections` |
134| `Alt-K` | Remove selections matching the regex | `remove_selections` |
134| `Ctrl-c` | Comment/uncomment the selections | `toggle_comments` |
135| `Ctrl-c` | Comment/uncomment the selections | `toggle_comments` |
helix-term/src/commands.rs+25−4
@@ -346,6 +346,7 @@ impl MappableCommand {
346 unindent, "Unindent selection",
346 unindent, "Unindent selection",
347 format_selections, "Format selection",
347 format_selections, "Format selection",
348 join_selections, "Join lines inside selection",
348 join_selections, "Join lines inside selection",
349+ join_selections_space, "Join lines inside selection and select spaces",
349 keep_selections, "Keep selections matching regex",
350 keep_selections, "Keep selections matching regex",
350 remove_selections, "Remove selections matching regex",
351 remove_selections, "Remove selections matching regex",
351 align_selections, "Align selections in column",
352 align_selections, "Align selections in column",
@@ -3731,7 +3732,7 @@ fn format_selections(cx: &mut Context) {
3731 }
3732 }
3732}
3733}
3733
3734
3734−fn join_selections(cx: &mut Context) {
3735+fn join_selections_inner(cx: &mut Context, select_space: bool) {
3735 use movement::skip_while;
3736 use movement::skip_while;
3736 let (view, doc) = current!(cx.editor);
3737 let (view, doc) = current!(cx.editor);
3737 let text = doc.text();
3738 let text = doc.text();
@@ -3766,9 +3767,21 @@ fn join_selections(cx: &mut Context) {
3766 // TODO: joining multiple empty lines should be replaced by a single space.
3767 // TODO: joining multiple empty lines should be replaced by a single space.
3767 // need to merge change ranges that touch
3768 // need to merge change ranges that touch
3768
3769
3769− let transaction = Transaction::change(doc.text(), changes.into_iter());
3770− // TODO: select inserted spaces
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);
3771− // .with_selection(selection);
3781+ Transaction::change(doc.text(), changes.into_iter()).with_selection(selection)
3782+ } else {
3783+ Transaction::change(doc.text(), changes.into_iter())
3784+ };
3772
3785
3773 doc.apply(&transaction, view.id);
3786 doc.apply(&transaction, view.id);
3774}
3787}
@@ -3797,6 +3810,14 @@ fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) {
3797 )
3810 )
3798}
3811}
3799
3812
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+
3800fn keep_selections(cx: &mut Context) {
3821fn keep_selections(cx: &mut Context) {
3801 keep_or_remove_selections_impl(cx, false)
3822 keep_or_remove_selections_impl(cx, false)
3802}
3823}
helix-term/src/keymap/default.rs+1
@@ -144,6 +144,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
144 "<" => unindent,
144 "<" => unindent,
145 "=" => format_selections,
145 "=" => format_selections,
146 "J" => join_selections,
146 "J" => join_selections,
147+ "A-J" => join_selections_space,
147 "K" => keep_selections,
148 "K" => keep_selections,
148 "A-K" => remove_selections,
149 "A-K" => remove_selections,
149
150