1
- use std:: iter:: once;
1
+ use std:: { iter:: once, mem } ;
2
2
3
3
use hir:: Semantics ;
4
4
use ide_db:: { base_db:: FileRange , RootDatabase } ;
@@ -102,7 +102,7 @@ fn move_in_direction(
102
102
ast:: GenericArgList ( it) => swap_sibling_in_list( node, it. generic_args( ) , range, direction) ,
103
103
ast:: VariantList ( it) => swap_sibling_in_list( node, it. variants( ) , range, direction) ,
104
104
ast:: TypeBoundList ( it) => swap_sibling_in_list( node, it. bounds( ) , range, direction) ,
105
- _ => Some ( replace_nodes( node, & match direction {
105
+ _ => Some ( replace_nodes( range , node, & match direction {
106
106
Direction :: Up => node. prev_sibling( ) ,
107
107
Direction :: Down => node. next_sibling( ) ,
108
108
} ?) )
@@ -125,7 +125,7 @@ fn swap_sibling_in_list<A: AstNode + Clone, I: Iterator<Item = A>>(
125
125
. next ( ) ;
126
126
127
127
if let Some ( ( l, r) ) = list_lookup {
128
- Some ( replace_nodes ( l. syntax ( ) , r. syntax ( ) ) )
128
+ Some ( replace_nodes ( range , l. syntax ( ) , r. syntax ( ) ) )
129
129
} else {
130
130
// Cursor is beyond any movable list item (for example, on curly brace in enum).
131
131
// It's not necessary, that parent of list is movable (arg list's parent is not, for example),
@@ -134,11 +134,38 @@ fn swap_sibling_in_list<A: AstNode + Clone, I: Iterator<Item = A>>(
134
134
}
135
135
}
136
136
137
- fn replace_nodes ( first : & SyntaxNode , second : & SyntaxNode ) -> TextEdit {
137
+ fn replace_nodes < ' a > (
138
+ range : TextRange ,
139
+ mut first : & ' a SyntaxNode ,
140
+ mut second : & ' a SyntaxNode ,
141
+ ) -> TextEdit {
142
+ let cursor_offset = if range. is_empty ( ) {
143
+ // FIXME: `applySnippetTextEdits` does not support non-empty selection ranges
144
+ if first. text_range ( ) . contains_range ( range) {
145
+ Some ( range. start ( ) - first. text_range ( ) . start ( ) )
146
+ } else if second. text_range ( ) . contains_range ( range) {
147
+ mem:: swap ( & mut first, & mut second) ;
148
+ Some ( range. start ( ) - first. text_range ( ) . start ( ) )
149
+ } else {
150
+ None
151
+ }
152
+ } else {
153
+ None
154
+ } ;
155
+
156
+ let first_with_cursor = match cursor_offset {
157
+ Some ( offset) => {
158
+ let mut item_text = first. text ( ) . to_string ( ) ;
159
+ item_text. insert_str ( offset. into ( ) , "$0" ) ;
160
+ item_text
161
+ }
162
+ None => first. text ( ) . to_string ( ) ,
163
+ } ;
164
+
138
165
let mut edit = TextEditBuilder :: default ( ) ;
139
166
140
167
algo:: diff ( first, second) . into_text_edit ( & mut edit) ;
141
- algo :: diff ( second, first ) . into_text_edit ( & mut edit ) ;
168
+ edit . replace ( second. text_range ( ) , first_with_cursor ) ;
142
169
143
170
edit. finish ( )
144
171
}
@@ -188,7 +215,7 @@ fn main() {
188
215
expect ! [ [ r#"
189
216
fn main() {
190
217
match true {
191
- false => {
218
+ false =>$0 {
192
219
println!("Test");
193
220
},
194
221
true => {
@@ -222,7 +249,7 @@ fn main() {
222
249
false => {
223
250
println!("Test");
224
251
},
225
- true => {
252
+ true =>$0 {
226
253
println!("Hello, world");
227
254
}
228
255
};
@@ -274,7 +301,7 @@ fn main() {
274
301
"# ,
275
302
expect ! [ [ r#"
276
303
fn main() {
277
- let test2 = 456;
304
+ let test2$0 = 456;
278
305
let test = 123;
279
306
}
280
307
"# ] ] ,
@@ -293,7 +320,7 @@ fn main() {
293
320
"# ,
294
321
expect ! [ [ r#"
295
322
fn main() {
296
- println!("All I want to say is...");
323
+ println!("All I want to say is...");$0
297
324
println!("Hello, world");
298
325
}
299
326
"# ] ] ,
@@ -313,7 +340,7 @@ fn main() {
313
340
fn main() {
314
341
if true {
315
342
println!("Test");
316
- }
343
+ }$0
317
344
318
345
println!("Hello, world");
319
346
}
@@ -334,7 +361,7 @@ fn main() {
334
361
fn main() {
335
362
for i in 0..10 {
336
363
println!("Test");
337
- }
364
+ }$0
338
365
339
366
println!("Hello, world");
340
367
}
@@ -355,7 +382,7 @@ fn main() {
355
382
fn main() {
356
383
loop {
357
384
println!("Test");
358
- }
385
+ }$0
359
386
360
387
println!("Hello, world");
361
388
}
@@ -376,7 +403,7 @@ fn main() {
376
403
fn main() {
377
404
while true {
378
405
println!("Test");
379
- }
406
+ }$0
380
407
381
408
println!("Hello, world");
382
409
}
@@ -393,7 +420,7 @@ fn main() {
393
420
"# ,
394
421
expect ! [ [ r#"
395
422
fn main() {
396
- return 123;
423
+ return 123;$0
397
424
398
425
println!("Hello, world");
399
426
}
@@ -430,7 +457,7 @@ fn main() {}
430
457
fn foo() {}$0$0
431
458
"# ,
432
459
expect ! [ [ r#"
433
- fn foo() {}
460
+ fn foo() {}$0
434
461
435
462
fn main() {}
436
463
"# ] ] ,
@@ -451,7 +478,7 @@ impl Wow for Yay $0$0{}
451
478
expect ! [ [ r#"
452
479
struct Yay;
453
480
454
- impl Wow for Yay {}
481
+ impl Wow for Yay $0 {}
455
482
456
483
trait Wow {}
457
484
"# ] ] ,
@@ -467,7 +494,7 @@ use std::vec::Vec;
467
494
use std::collections::HashMap$0$0;
468
495
"# ,
469
496
expect ! [ [ r#"
470
- use std::collections::HashMap;
497
+ use std::collections::HashMap$0 ;
471
498
use std::vec::Vec;
472
499
"# ] ] ,
473
500
Direction :: Up ,
@@ -502,7 +529,7 @@ fn main() {
502
529
}
503
530
504
531
#[ test]
505
- fn test_moves_param_up ( ) {
532
+ fn test_moves_param ( ) {
506
533
check (
507
534
r#"
508
535
fn test(one: i32, two$0$0: u32) {}
@@ -512,14 +539,23 @@ fn main() {
512
539
}
513
540
"# ,
514
541
expect ! [ [ r#"
515
- fn test(two: u32, one: i32) {}
542
+ fn test(two$0 : u32, one: i32) {}
516
543
517
544
fn main() {
518
545
test(123, 456);
519
546
}
520
547
"# ] ] ,
521
548
Direction :: Up ,
522
549
) ;
550
+ check (
551
+ r#"
552
+ fn f($0$0arg: u8, arg2: u16) {}
553
+ "# ,
554
+ expect ! [ [ r#"
555
+ fn f(arg2: u16, $0arg: u8) {}
556
+ "# ] ] ,
557
+ Direction :: Down ,
558
+ ) ;
523
559
}
524
560
525
561
#[ test]
@@ -536,7 +572,7 @@ fn main() {
536
572
fn test(one: i32, two: u32) {}
537
573
538
574
fn main() {
539
- test(456, 123);
575
+ test(456$0 , 123);
540
576
}
541
577
"# ] ] ,
542
578
Direction :: Up ,
@@ -557,7 +593,7 @@ fn main() {
557
593
fn test(one: i32, two: u32) {}
558
594
559
595
fn main() {
560
- test(456, 123);
596
+ test(456, 123$0 );
561
597
}
562
598
"# ] ] ,
563
599
Direction :: Down ,
@@ -594,7 +630,7 @@ struct Test<A, B$0$0>(A, B);
594
630
fn main() {}
595
631
"# ,
596
632
expect ! [ [ r#"
597
- struct Test<B, A>(A, B);
633
+ struct Test<B$0 , A>(A, B);
598
634
599
635
fn main() {}
600
636
"# ] ] ,
@@ -616,7 +652,7 @@ fn main() {
616
652
struct Test<A, B>(A, B);
617
653
618
654
fn main() {
619
- let t = Test::<&str, i32>(123, "yay");
655
+ let t = Test::<&str$0 , i32>(123, "yay");
620
656
}
621
657
"# ] ] ,
622
658
Direction :: Up ,
@@ -636,7 +672,7 @@ fn main() {}
636
672
"# ,
637
673
expect ! [ [ r#"
638
674
enum Hello {
639
- Two,
675
+ Two$0 ,
640
676
One
641
677
}
642
678
@@ -663,7 +699,7 @@ trait One {}
663
699
664
700
trait Two {}
665
701
666
- fn test<T: Two + One>(t: T) {}
702
+ fn test<T: Two$0 + One>(t: T) {}
667
703
668
704
fn main() {}
669
705
"# ] ] ,
@@ -709,7 +745,7 @@ trait Yay {
709
745
impl Yay for Test {
710
746
type One = i32;
711
747
712
- fn inner() {
748
+ fn inner() {$0
713
749
println!("Mmmm");
714
750
}
715
751
@@ -736,7 +772,7 @@ fn test() {
736
772
"# ,
737
773
expect ! [ [ r#"
738
774
fn test() {
739
- mod hi {
775
+ mod hi {$0
740
776
fn inner() {}
741
777
}
742
778
@@ -764,7 +800,7 @@ fn main() {}
764
800
expect ! [ [ r#"
765
801
fn main() {}
766
802
767
- #[derive(Debug)]
803
+ $0 #[derive(Debug)]
768
804
enum FooBar {
769
805
Foo,
770
806
Bar,
@@ -784,7 +820,7 @@ fn main() {}
784
820
expect ! [ [ r#"
785
821
fn main() {}
786
822
787
- enum FooBar {
823
+ $0enum FooBar {
788
824
Foo,
789
825
Bar,
790
826
}
@@ -804,7 +840,7 @@ fn main() {}
804
840
expect ! [ [ r#"
805
841
struct Test;
806
842
807
- impl SomeTrait for Test {}
843
+ $0impl SomeTrait for Test {}
808
844
809
845
trait SomeTrait {}
810
846
@@ -831,7 +867,7 @@ fn main() {}
831
867
enum FooBar {
832
868
Foo,
833
869
Bar,
834
- }
870
+ }$0
835
871
"# ] ] ,
836
872
Direction :: Down ,
837
873
) ;
@@ -848,7 +884,7 @@ fn main() {}
848
884
expect ! [ [ r#"
849
885
struct Test;
850
886
851
- impl SomeTrait for Test {}
887
+ impl SomeTrait for Test {}$0
852
888
853
889
trait SomeTrait {}
854
890
0 commit comments