Debian Codegraph @

helix-term/tests/test/commands/movement.rs

raw symbols SCIP
1 use super::*;
2
3 #[tokio::test(flavor = "multi_thread")]
4 async fn test_move_parent_node_end() -> anyhow::Result<()> {
5 let tests = vec![
6 // single cursor stays single cursor, first goes to end of current
7 // node, then parent
8 (
9 indoc! {r##"
10 fn foo() {
11 let result = if true {
12 "yes"
13 } else {
14 "no#["|]#
15 }
16 }
17 "##},
18 "<A-e>",
19 indoc! {"\
20 fn foo() {
21 let result = if true {
22 \"yes\"
23 } else {
24 \"no\"#[\n|]#
25 }
26 }
27 "},
28 ),
29 (
30 indoc! {"\
31 fn foo() {
32 let result = if true {
33 \"yes\"
34 } else {
35 \"no\"#[\n|]#
36 }
37 }
38 "},
39 "<A-e>",
40 indoc! {"\
41 fn foo() {
42 let result = if true {
43 \"yes\"
44 } else {
45 \"no\"
46 }#[\n|]#
47 }
48 "},
49 ),
50 // select mode extends
51 (
52 indoc! {r##"
53 fn foo() {
54 let result = if true {
55 "yes"
56 } else {
57 #["no"|]#
58 }
59 }
60 "##},
61 "v<A-e><A-e>",
62 indoc! {"\
63 fn foo() {
64 let result = if true {
65 \"yes\"
66 } else {
67 #[\"no\"
68 }\n|]#
69 }
70 "},
71 ),
72 ];
73
74 for test in tests {
75 test_with_config(AppBuilder::new().with_file("foo.rs", None), test).await?;
76 }
77
78 Ok(())
79 }
80
81 #[tokio::test(flavor = "multi_thread")]
82 async fn test_move_parent_node_start() -> anyhow::Result<()> {
83 let tests = vec![
84 // single cursor stays single cursor, first goes to end of current
85 // node, then parent
86 (
87 indoc! {r##"
88 fn foo() {
89 let result = if true {
90 "yes"
91 } else {
92 "no#["|]#
93 }
94 }
95 "##},
96 "<A-b>",
97 indoc! {"\
98 fn foo() {
99 let result = if true {
100 \"yes\"
101 } else {
102 #[\"|]#no\"
103 }
104 }
105 "},
106 ),
107 (
108 indoc! {"\
109 fn foo() {
110 let result = if true {
111 \"yes\"
112 } else {
113 \"no\"#[\n|]#
114 }
115 }
116 "},
117 "<A-b>",
118 indoc! {"\
119 fn foo() {
120 let result = if true {
121 \"yes\"
122 } else #[{|]#
123 \"no\"
124 }
125 }
126 "},
127 ),
128 (
129 indoc! {"\
130 fn foo() {
131 let result = if true {
132 \"yes\"
133 } else #[{|]#
134 \"no\"
135 }
136 }
137 "},
138 "<A-b>",
139 indoc! {"\
140 fn foo() {
141 let result = if true {
142 \"yes\"
143 } #[e|]#lse {
144 \"no\"
145 }
146 }
147 "},
148 ),
149 // select mode extends
150 (
151 indoc! {r##"
152 fn foo() {
153 let result = if true {
154 "yes"
155 } else {
156 #["no"|]#
157 }
158 }
159 "##},
160 "v<A-b><A-b>",
161 indoc! {"\
162 fn foo() {
163 let result = if true {
164 \"yes\"
165 } else #[|{
166 ]#\"no\"
167 }
168 }
169 "},
170 ),
171 (
172 indoc! {r##"
173 fn foo() {
174 let result = if true {
175 "yes"
176 } else {
177 #["no"|]#
178 }
179 }
180 "##},
181 "v<A-b><A-b><A-b>",
182 indoc! {"\
183 fn foo() {
184 let result = if true {
185 \"yes\"
186 } #[|else {
187 ]#\"no\"
188 }
189 }
190 "},
191 ),
192 ];
193
194 for test in tests {
195 test_with_config(AppBuilder::new().with_file("foo.rs", None), test).await?;
196 }
197
198 Ok(())
199 }
200
201 #[tokio::test(flavor = "multi_thread")]
202 async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
203 let tests = vec![
204 // single cursor stays single cursor, first goes to end of current
205 // node, then parent
206 (
207 indoc! {r##"
208 fn foo() {
209 let result = if true {
210 "yes"
211 } else {
212 "no#["|]#
213 }
214 }
215 "##},
216 "i<tab>",
217 indoc! {"\
218 fn foo() {
219 let result = if true {
220 \"yes\"
221 } else {
222 \"no\"#[|\n]#
223 }
224 }
225 "},
226 ),
227 (
228 indoc! {"\
229 fn foo() {
230 let result = if true {
231 \"yes\"
232 } else {
233 \"no\"#[\n|]#
234 }
235 }
236 "},
237 "i<tab>",
238 indoc! {"\
239 fn foo() {
240 let result = if true {
241 \"yes\"
242 } else {
243 \"no\"
244 }#[|\n]#
245 }
246 "},
247 ),
248 // appending to the end of a line should still look at the current
249 // line, not the next one
250 (
251 indoc! {"\
252 fn foo() {
253 let result = if true {
254 \"yes\"
255 } else {
256 \"no#[\"|]#
257 }
258 }
259 "},
260 "a<tab>",
261 indoc! {"\
262 fn foo() {
263 let result = if true {
264 \"yes\"
265 } else {
266 \"no\"
267 }#[\n|]#
268 }
269 "},
270 ),
271 // before cursor is all whitespace, so insert tab
272 (
273 indoc! {"\
274 fn foo() {
275 let result = if true {
276 \"yes\"
277 } else {
278 #[\"no\"|]#
279 }
280 }
281 "},
282 "i<tab>",
283 indoc! {"\
284 fn foo() {
285 let result = if true {
286 \"yes\"
287 } else {
288 #[|\"no\"]#
289 }
290 }
291 "},
292 ),
293 // if selection spans multiple lines, it should still only look at the
294 // line on which the head is
295 (
296 indoc! {"\
297 fn foo() {
298 let result = if true {
299 #[\"yes\"
300 } else {
301 \"no\"|]#
302 }
303 }
304 "},
305 "a<tab>",
306 indoc! {"\
307 fn foo() {
308 let result = if true {
309 \"yes\"
310 } else {
311 \"no\"
312 }#[\n|]#
313 }
314 "},
315 ),
316 (
317 indoc! {"\
318 fn foo() {
319 let result = if true {
320 #[\"yes\"
321 } else {
322 \"no\"|]#
323 }
324 }
325 "},
326 "i<tab>",
327 indoc! {"\
328 fn foo() {
329 let result = if true {
330 #[|\"yes\"
331 } else {
332 \"no\"]#
333 }
334 }
335 "},
336 ),
337 (
338 indoc! {"\
339 fn foo() {
340 #[l|]#et result = if true {
341 #(\"yes\"
342 } else {
343 \"no\"|)#
344 }
345 }
346 "},
347 "i<tab>",
348 indoc! {"\
349 fn foo() {
350 #[|l]#et result = if true {
351 #(|\"yes\"
352 } else {
353 \"no\")#
354 }
355 }
356 "},
357 ),
358 (
359 indoc! {"\
360 fn foo() {
361 let result = if true {
362 \"yes\"#[\n|]#
363 } else {
364 \"no\"#(\n|)#
365 }
366 }
367 "},
368 "i<tab>",
369 indoc! {"\
370 fn foo() {
371 let result = if true {
372 \"yes\"
373 }#[| ]#else {
374 \"no\"
375 }#(|\n)#
376 }
377 "},
378 ),
379 (
380 indoc! {"\
381 fn foo() {
382 let result = if true {
383 #[\"yes\"|]#
384 } else {
385 #(\"no\"|)#
386 }
387 }
388 "},
389 "i<tab>",
390 indoc! {"\
391 fn foo() {
392 let result = if true {
393 #[|\"yes\"]#
394 } else {
395 #(|\"no\")#
396 }
397 }
398 "},
399 ),
400 // if any cursors are not preceded by all whitespace, then do the
401 // smart_tab action
402 (
403 indoc! {"\
404 fn foo() {
405 let result = if true {
406 #[\"yes\"\n|]#
407 } else {
408 \"no#(\"\n|)#
409 }
410 }
411 "},
412 "i<tab>",
413 indoc! {"\
414 fn foo() {
415 let result = if true {
416 \"yes\"
417 }#[| ]#else {
418 \"no\"
419 }#(|\n)#
420 }
421 "},
422 ),
423 // Ctrl-tab always inserts a tab
424 (
425 indoc! {"\
426 fn foo() {
427 let result = if true {
428 #[\"yes\"\n|]#
429 } else {
430 \"no#(\"\n|)#
431 }
432 }
433 "},
434 "i<S-tab>",
435 indoc! {"\
436 fn foo() {
437 let result = if true {
438 #[|\"yes\"\n]#
439 } else {
440 \"no #(|\"\n)#
441 }
442 }
443 "},
444 ),
445 ];
446
447 for test in tests {
448 test_with_config(AppBuilder::new().with_file("foo.rs", None), test).await?;
449 }
450
451 Ok(())
452 }
453
454 #[tokio::test(flavor = "multi_thread")]
455 async fn select_all_siblings() -> anyhow::Result<()> {
456 let tests = vec![
457 // basic tests
458 (
459 indoc! {r##"
460 let foo = bar(#[a|]#, b, c);
461 "##},
462 "<A-a>",
463 indoc! {r##"
464 let foo = bar(#[a|]#, #(b|)#, #(c|)#);
465 "##},
466 ),
467 (
468 indoc! {r##"
469 let a = [
470 #[1|]#,
471 2,
472 3,
473 4,
474 5,
475 ];
476 "##},
477 "<A-a>",
478 indoc! {r##"
479 let a = [
480 #[1|]#,
481 #(2|)#,
482 #(3|)#,
483 #(4|)#,
484 #(5|)#,
485 ];
486 "##},
487 ),
488 // direction is preserved
489 (
490 indoc! {r##"
491 let a = [
492 #[|1]#,
493 2,
494 3,
495 4,
496 5,
497 ];
498 "##},
499 "<A-a>",
500 indoc! {r##"
501 let a = [
502 #[|1]#,
503 #(|2)#,
504 #(|3)#,
505 #(|4)#,
506 #(|5)#,
507 ];
508 "##},
509 ),
510 // can't pick any more siblings - selection stays the same
511 (
512 indoc! {r##"
513 let a = [
514 #[1|]#,
515 #(2|)#,
516 #(3|)#,
517 #(4|)#,
518 #(5|)#,
519 ];
520 "##},
521 "<A-a>",
522 indoc! {r##"
523 let a = [
524 #[1|]#,
525 #(2|)#,
526 #(3|)#,
527 #(4|)#,
528 #(5|)#,
529 ];
530 "##},
531 ),
532 // each cursor does the sibling select independently
533 (
534 indoc! {r##"
535 let a = [
536 #[1|]#,
537 2,
538 3,
539 4,
540 5,
541 ];
542
543 let b = [
544 #("one"|)#,
545 "two",
546 "three",
547 "four",
548 "five",
549 ];
550 "##},
551 "<A-a>",
552 indoc! {r##"
553 let a = [
554 #[1|]#,
555 #(2|)#,
556 #(3|)#,
557 #(4|)#,
558 #(5|)#,
559 ];
560
561 let b = [
562 #("one"|)#,
563 #("two"|)#,
564 #("three"|)#,
565 #("four"|)#,
566 #("five"|)#,
567 ];
568 "##},
569 ),
570 // conflicting sibling selections get normalized. Here, the primary
571 // selection would choose every list item, but because the secondary
572 // range covers more than one item, the descendent is the entire list,
573 // which means the sibling is the assignment. The list item ranges just
574 // get normalized out since the list itself becomes selected.
575 (
576 indoc! {r##"
577 let a = [
578 #[1|]#,
579 2,
580 #(3,
581 4|)#,
582 5,
583 ];
584 "##},
585 "<A-a>",
586 indoc! {r##"
587 let #(a|)# = #[[
588 1,
589 2,
590 3,
591 4,
592 5,
593 ]|]#;
594 "##},
595 ),
596 ];
597
598 for test in tests {
599 test_with_config(AppBuilder::new().with_file("foo.rs", None), test).await?;
600 }
601
602 Ok(())
603 }
604
605 #[tokio::test(flavor = "multi_thread")]
606 async fn select_all_children() -> anyhow::Result<()> {
607 let tests = vec![
608 // basic tests
609 (
610 indoc! {r##"
611 let foo = bar#[(a, b, c)|]#;
612 "##},
613 "<A-I>",
614 indoc! {r##"
615 let foo = bar(#[a|]#, #(b|)#, #(c|)#);
616 "##},
617 ),
618 (
619 indoc! {r##"
620 let a = #[[
621 1,
622 2,
623 3,
624 4,
625 5,
626 ]|]#;
627 "##},
628 "<A-I>",
629 indoc! {r##"
630 let a = [
631 #[1|]#,
632 #(2|)#,
633 #(3|)#,
634 #(4|)#,
635 #(5|)#,
636 ];
637 "##},
638 ),
639 // direction is preserved
640 (
641 indoc! {r##"
642 let a = #[|[
643 1,
644 2,
645 3,
646 4,
647 5,
648 ]]#;
649 "##},
650 "<A-I>",
651 indoc! {r##"
652 let a = [
653 #[|1]#,
654 #(|2)#,
655 #(|3)#,
656 #(|4)#,
657 #(|5)#,
658 ];
659 "##},
660 ),
661 // can't pick any more children - selection stays the same
662 (
663 indoc! {r##"
664 let a = [
665 #[1|]#,
666 #(2|)#,
667 #(3|)#,
668 #(4|)#,
669 #(5|)#,
670 ];
671 "##},
672 "<A-I>",
673 indoc! {r##"
674 let a = [
675 #[1|]#,
676 #(2|)#,
677 #(3|)#,
678 #(4|)#,
679 #(5|)#,
680 ];
681 "##},
682 ),
683 // each cursor does the sibling select independently
684 (
685 indoc! {r##"
686 let a = #[|[
687 1,
688 2,
689 3,
690 4,
691 5,
692 ]]#;
693
694 let b = #([
695 "one",
696 "two",
697 "three",
698 "four",
699 "five",
700 ]|)#;
701 "##},
702 "<A-I>",
703 indoc! {r##"
704 let a = [
705 #[|1]#,
706 #(|2)#,
707 #(|3)#,
708 #(|4)#,
709 #(|5)#,
710 ];
711
712 let b = [
713 #("one"|)#,
714 #("two"|)#,
715 #("three"|)#,
716 #("four"|)#,
717 #("five"|)#,
718 ];
719 "##},
720 ),
721 ];
722
723 for test in tests {
724 test_with_config(AppBuilder::new().with_file("foo.rs", None), test).await?;
725 }
726
727 Ok(())
728 }
729
730 #[tokio::test(flavor = "multi_thread")]
731 async fn test_select_next_sibling() -> anyhow::Result<()> {
732 let tests = vec![
733 // basic test
734 (
735 indoc! {r##"
736 fn inc(x: usize) -> usize { x + 1 #[}|]#
737 fn dec(x: usize) -> usize { x - 1 }
738 fn ident(x: usize) -> usize { x }
739 "##},
740 "<A-n>",
741 indoc! {r##"
742 fn inc(x: usize) -> usize { x + 1 }
743 #[fn dec(x: usize) -> usize { x - 1 }|]#
744 fn ident(x: usize) -> usize { x }
745 "##},
746 ),
747 // direction is not preserved and is always forward.
748 (
749 indoc! {r##"
750 fn inc(x: usize) -> usize { x + 1 #[}|]#
751 fn dec(x: usize) -> usize { x - 1 }
752 fn ident(x: usize) -> usize { x }
753 "##},
754 "<A-n><A-;><A-n>",
755 indoc! {r##"
756 fn inc(x: usize) -> usize { x + 1 }
757 fn dec(x: usize) -> usize { x - 1 }
758 #[fn ident(x: usize) -> usize { x }|]#
759 "##},
760 ),
761 ];
762
763 for test in tests {
764 test_with_config(AppBuilder::new().with_file("foo.rs", None), test).await?;
765 }
766
767 Ok(())
768 }
769
770 #[tokio::test(flavor = "multi_thread")]
771 async fn test_select_prev_sibling() -> anyhow::Result<()> {
772 let tests = vec![
773 // basic test
774 (
775 indoc! {r##"
776 fn inc(x: usize) -> usize { x + 1 }
777 fn dec(x: usize) -> usize { x - 1 }
778 #[|f]#n ident(x: usize) -> usize { x }
779 "##},
780 "<A-p>",
781 indoc! {r##"
782 fn inc(x: usize) -> usize { x + 1 }
783 #[|fn dec(x: usize) -> usize { x - 1 }]#
784 fn ident(x: usize) -> usize { x }
785 "##},
786 ),
787 // direction is not preserved and is always backward.
788 (
789 indoc! {r##"
790 fn inc(x: usize) -> usize { x + 1 }
791 fn dec(x: usize) -> usize { x - 1 }
792 #[|f]#n ident(x: usize) -> usize { x }
793 "##},
794 "<A-p><A-;><A-p>",
795 indoc! {r##"
796 #[|fn inc(x: usize) -> usize { x + 1 }]#
797 fn dec(x: usize) -> usize { x - 1 }
798 fn ident(x: usize) -> usize { x }
799 "##},
800 ),
801 ];
802
803 for test in tests {
804 test_with_config(AppBuilder::new().with_file("foo.rs", None), test).await?;
805 }
806
807 Ok(())
808 }
809
810 #[tokio::test(flavor = "multi_thread")]
811 async fn match_bracket() -> anyhow::Result<()> {
812 let rust_tests = vec![
813 // fwd
814 (
815 indoc! {r##"
816 fn foo(x: usize) -> usize { #[x|]# + 1 }
817 "##},
818 "mm",
819 indoc! {r##"
820 fn foo(x: usize) -> usize { x + 1 #[}|]#
821 "##},
822 ),
823 // backward
824 (
825 indoc! {r##"
826 fn foo(x: usize) -> usize { #[x|]# + 1 }
827 "##},
828 "mmmm",
829 indoc! {r##"
830 fn foo(x: usize) -> usize #[{|]# x + 1 }
831 "##},
832 ),
833 // avoid false positive inside string literal
834 (
835 indoc! {r##"
836 fn foo() -> &'static str { "(hello#[ |]#world)" }
837 "##},
838 "mm",
839 indoc! {r##"
840 fn foo() -> &'static str { "(hello world)#["|]# }
841 "##},
842 ),
843 // make sure matching on quotes works
844 (
845 indoc! {r##"
846 fn foo() -> &'static str { "(hello#[ |]#world)" }
847 "##},
848 "mm",
849 indoc! {r##"
850 fn foo() -> &'static str { "(hello world)#["|]# }
851 "##},
852 ),
853 // .. on both ends
854 (
855 indoc! {r##"
856 fn foo() -> &'static str { "(hello#[ |]#world)" }
857 "##},
858 "mmmm",
859 indoc! {r##"
860 fn foo() -> &'static str { #["|]#(hello world)" }
861 "##},
862 ),
863 // match on siblings nodes
864 (
865 indoc! {r##"
866 fn foo(bar: Option<usize>) -> usize {
867 match bar {
868 Some(b#[a|]#r) => bar,
869 None => 42,
870 }
871 }
872 "##},
873 "mmmm",
874 indoc! {r##"
875 fn foo(bar: Option<usize>) -> usize {
876 match bar {
877 Some#[(|]#bar) => bar,
878 None => 42,
879 }
880 }
881 "##},
882 ),
883 // gracefully handle multiple sibling brackets (usally for errors/incomplete syntax trees)
884 // in the past we selected the first > instead of the second > here
885 (
886 indoc! {r##"
887 fn foo() {
888 foo::<b#[a|]#r<>>
889 }
890 "##},
891 "mm",
892 indoc! {r##"
893 fn foo() {
894 foo::<bar<>#[>|]#
895 }
896 "##},
897 ),
898 // named node with 2 or more children
899 (
900 indoc! {r##"
901 use a::#[{|]#
902 b::{c, d, e, f, g},
903 h, i, j, k, l, m, n,
904 };
905 "##},
906 "mm",
907 indoc! {r##"
908 use a::{
909 b::{c, d, e, f, g},
910 h, i, j, k, l, m, n,
911 #[}|]#;
912 "##},
913 ),
914 ];
915
916 let python_tests = vec![
917 // python quotes have a slightly more complex syntax tree
918 // that triggerd a bug in an old implementation so we test
919 // them here
920 (
921 indoc! {r##"
922 foo_python = "mm does not#[ |]#work on this string"
923 "##},
924 "mm",
925 indoc! {r##"
926 foo_python = "mm does not work on this string#["|]#
927 "##},
928 ),
929 (
930 indoc! {r##"
931 foo_python = "mm does not#[ |]#work on this string"
932 "##},
933 "mmmm",
934 indoc! {r##"
935 foo_python = #["|]#mm does not work on this string"
936 "##},
937 ),
938 ];
939
940 for test in rust_tests {
941 println!("{test:?}");
942 test_with_config(AppBuilder::new().with_file("foo.rs", None), test).await?;
943 }
944 for test in python_tests {
945 println!("{test:?}");
946 test_with_config(AppBuilder::new().with_file("foo.py", None), test).await?;
947 }
948
949 Ok(())
950 }