@@ -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+
38003821fn keep_selections(cx: &mut Context) {
38013822 keep_or_remove_selections_impl(cx, false)
38023823}