@@ -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" ,
@@ -3771,7 +3772,7 @@ fn format_selections(cx: &mut Context) {
3771
3772
}
3772
3773
}
3773
3774
3774
- fn join_selections ( cx : & mut Context ) {
3775
+ fn join_selections_inner ( cx : & mut Context , select_space : bool ) {
3775
3776
use movement:: skip_while;
3776
3777
let ( view, doc) = current ! ( cx. editor) ;
3777
3778
let text = doc. text ( ) ;
@@ -3806,9 +3807,21 @@ fn join_selections(cx: &mut Context) {
3806
3807
// TODO: joining multiple empty lines should be replaced by a single space.
3807
3808
// need to merge change ranges that touch
3808
3809
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
+ } ;
3812
3825
3813
3826
doc. apply ( & transaction, view. id ) ;
3814
3827
}
@@ -3837,6 +3850,14 @@ fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) {
3837
3850
)
3838
3851
}
3839
3852
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
+
3840
3861
fn keep_selections ( cx : & mut Context ) {
3841
3862
keep_or_remove_selections_impl ( cx, false )
3842
3863
}
0 commit comments