@@ -346,6 +346,7 @@ impl MappableCommand {
346
346
unindent, "Unindent selection" ,
347
347
format_selections, "Format selection" ,
348
348
join_selections, "Join lines inside selection" ,
349
+ join_selections_space, "Join lines inside selection and select spaces" ,
349
350
keep_selections, "Keep selections matching regex" ,
350
351
remove_selections, "Remove selections matching regex" ,
351
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
3736
use movement:: skip_while;
3736
3737
let ( view, doc) = current ! ( cx. editor) ;
3737
3738
let text = doc. text ( ) ;
@@ -3766,9 +3767,21 @@ fn join_selections(cx: &mut Context) {
3766
3767
// TODO: joining multiple empty lines should be replaced by a single space.
3767
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
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
+ } ;
3772
3785
3773
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
+
3800
3821
fn keep_selections ( cx : & mut Context ) {
3801
3822
keep_or_remove_selections_impl ( cx, false )
3802
3823
}
0 commit comments