Debian Codegraph @

helix-term/tests/test/commands.rs

raw symbols SCIP
1 use helix_term::application::Application;
2
3 use super::*;
4
5 mod insert;
6 mod movement;
7 mod reverse_selection_contents;
8 mod rotate_selection_contents;
9 mod write;
10
11 #[tokio::test(flavor = "multi_thread")]
12 async fn search_selection_detect_word_boundaries_at_eof() -> anyhow::Result<()> {
13 // <https://github.com/helix-editor/helix/issues/12609>
14 test((
15 indoc! {"\
16 #[o|]#ne
17 two
18 three"},
19 "gej*h",
20 indoc! {"\
21 one
22 two
23 three#[
24 |]#"},
25 ))
26 .await?;
27
28 Ok(())
29 }
30
31 #[tokio::test(flavor = "multi_thread")]
32 async fn test_selection_duplication() -> anyhow::Result<()> {
33 // Forward
34 test((
35 indoc! {"\
36 #[lo|]#rem
37 ipsum
38 dolor
39 "},
40 "CC",
41 indoc! {"\
42 #(lo|)#rem
43 #(ip|)#sum
44 #[do|]#lor
45 "},
46 ))
47 .await?;
48
49 // Backward
50 test((
51 indoc! {"\
52 #[|lo]#rem
53 ipsum
54 dolor
55 "},
56 "CC",
57 indoc! {"\
58 #(|lo)#rem
59 #(|ip)#sum
60 #[|do]#lor
61 "},
62 ))
63 .await?;
64
65 // Copy the selection to previous line, skipping the first line in the file
66 test((
67 indoc! {"\
68 test
69 #[testitem|]#
70 "},
71 "<A-C>",
72 indoc! {"\
73 test
74 #[testitem|]#
75 "},
76 ))
77 .await?;
78
79 // Copy the selection to previous line, including the first line in the file
80 test((
81 indoc! {"\
82 test
83 #[test|]#
84 "},
85 "<A-C>",
86 indoc! {"\
87 #[test|]#
88 #(test|)#
89 "},
90 ))
91 .await?;
92
93 // Copy the selection to next line, skipping the last line in the file
94 test((
95 indoc! {"\
96 #[testitem|]#
97 test
98 "},
99 "C",
100 indoc! {"\
101 #[testitem|]#
102 test
103 "},
104 ))
105 .await?;
106
107 // Copy the selection to next line, including the last line in the file
108 test((
109 indoc! {"\
110 #[test|]#
111 test
112 "},
113 "C",
114 indoc! {"\
115 #(test|)#
116 #[test|]#
117 "},
118 ))
119 .await?;
120 Ok(())
121 }
122
123 #[tokio::test(flavor = "multi_thread")]
124 async fn test_goto_file_impl() -> anyhow::Result<()> {
125 let file = tempfile::NamedTempFile::new()?;
126
127 fn match_paths(app: &Application, matches: Vec<&str>) -> usize {
128 app.editor
129 .documents()
130 .filter_map(|d| d.path()?.file_name())
131 .filter(|n| matches.iter().any(|m| *m == n.to_string_lossy()))
132 .count()
133 }
134
135 // Single selection
136 test_key_sequence(
137 &mut AppBuilder::new().with_file(file.path(), None).build()?,
138 Some("ione.js<esc>%gf"),
139 Some(&|app| {
140 assert_eq!(1, match_paths(app, vec!["one.js"]));
141 }),
142 false,
143 )
144 .await?;
145
146 // Multiple selection
147 test_key_sequence(
148 &mut AppBuilder::new().with_file(file.path(), None).build()?,
149 Some("ione.js<ret>two.js<esc>%<A-s>gf"),
150 Some(&|app| {
151 assert_eq!(2, match_paths(app, vec!["one.js", "two.js"]));
152 }),
153 false,
154 )
155 .await?;
156
157 // Cursor on first quote
158 test_key_sequence(
159 &mut AppBuilder::new().with_file(file.path(), None).build()?,
160 Some("iimport 'one.js'<esc>B;gf"),
161 Some(&|app| {
162 assert_eq!(1, match_paths(app, vec!["one.js"]));
163 }),
164 false,
165 )
166 .await?;
167
168 // Cursor on last quote
169 test_key_sequence(
170 &mut AppBuilder::new().with_file(file.path(), None).build()?,
171 Some("iimport 'one.js'<esc>bgf"),
172 Some(&|app| {
173 assert_eq!(1, match_paths(app, vec!["one.js"]));
174 }),
175 false,
176 )
177 .await?;
178
179 // ';' is behind the path
180 test_key_sequence(
181 &mut AppBuilder::new().with_file(file.path(), None).build()?,
182 Some("iimport 'one.js';<esc>B;gf"),
183 Some(&|app| {
184 assert_eq!(1, match_paths(app, vec!["one.js"]));
185 }),
186 false,
187 )
188 .await?;
189
190 // allow numeric values in path
191 test_key_sequence(
192 &mut AppBuilder::new().with_file(file.path(), None).build()?,
193 Some("iimport 'one123.js'<esc>B;gf"),
194 Some(&|app| {
195 assert_eq!(1, match_paths(app, vec!["one123.js"]));
196 }),
197 false,
198 )
199 .await?;
200
201 Ok(())
202 }
203
204 #[tokio::test(flavor = "multi_thread")]
205 async fn test_multi_selection_paste() -> anyhow::Result<()> {
206 test((
207 indoc! {"\
208 #[|lorem]#
209 #(|ipsum)#
210 #(|dolor)#
211 "},
212 "yp",
213 indoc! {"\
214 lorem#[|lorem]#
215 ipsum#(|ipsum)#
216 dolor#(|dolor)#
217 "},
218 ))
219 .await?;
220
221 Ok(())
222 }
223
224 #[tokio::test(flavor = "multi_thread")]
225 async fn test_multi_selection_shell_commands() -> anyhow::Result<()> {
226 // pipe
227 test((
228 indoc! {"\
229 #[|lorem]#
230 #(|ipsum)#
231 #(|dolor)#
232 "},
233 "|echo foo<ret>",
234 indoc! {"\
235 #[|foo]#
236 #(|foo)#
237 #(|foo)#"
238 },
239 ))
240 .await?;
241
242 // insert-output
243 test((
244 indoc! {"\
245 #[|lorem]#
246 #(|ipsum)#
247 #(|dolor)#
248 "},
249 "!echo foo<ret>",
250 indoc! {"\
251 #[|foo]#lorem
252 #(|foo)#ipsum
253 #(|foo)#dolor
254 "},
255 ))
256 .await?;
257
258 // append-output
259 test((
260 indoc! {"\
261 #[|lorem]#
262 #(|ipsum)#
263 #(|dolor)#
264 "},
265 "<A-!>echo foo<ret>",
266 indoc! {"\
267 lorem#[|foo]#
268 ipsum#(|foo)#
269 dolor#(|foo)#
270 "},
271 ))
272 .await?;
273
274 Ok(())
275 }
276
277 #[tokio::test(flavor = "multi_thread")]
278 async fn test_undo_redo() -> anyhow::Result<()> {
279 // A jumplist selection is created at a point which is undone.
280 //
281 // * 2[<space> Add two newlines at line start. We're now on line 3.
282 // * <C-s> Save the selection on line 3 in the jumplist.
283 // * u Undo the two newlines. We're now on line 1.
284 // * <C-o><C-i> Jump forward an back again in the jumplist. This would panic
285 // if the jumplist were not being updated correctly.
286 test((
287 "#[|]#",
288 "2[<space><C-s>u<C-o><C-i>",
289 "#[|]#",
290 LineFeedHandling::AsIs,
291 ))
292 .await?;
293
294 // A jumplist selection is passed through an edit and then an undo and then a redo.
295 //
296 // * [<space> Add a newline at line start. We're now on line 2.
297 // * <C-s> Save the selection on line 2 in the jumplist.
298 // * kd Delete line 1. The jumplist selection should be adjusted to the new line 1.
299 // * uU Undo and redo the `kd` edit.
300 // * <C-o> Jump back in the jumplist. This would panic if the jumplist were not being
301 // updated correctly.
302 // * <C-i> Jump forward to line 1.
303 test((
304 "#[|]#",
305 "[<space><C-s>kduU<C-o><C-i>",
306 "#[|]#",
307 LineFeedHandling::AsIs,
308 ))
309 .await?;
310
311 // In this case we 'redo' manually to ensure that the transactions are composing correctly.
312 test((
313 "#[|]#",
314 "[<space>u[<space>u",
315 "#[|]#",
316 LineFeedHandling::AsIs,
317 ))
318 .await?;
319
320 Ok(())
321 }
322
323 #[tokio::test(flavor = "multi_thread")]
324 async fn test_extend_line() -> anyhow::Result<()> {
325 // extend with line selected then count
326 test((
327 indoc! {"\
328 #[l|]#orem
329 ipsum
330 dolor
331
332 "},
333 "x2x",
334 indoc! {"\
335 #[lorem
336 ipsum
337 dolor\n|]#
338
339 "},
340 ))
341 .await?;
342
343 // extend with count on partial selection
344 test((
345 indoc! {"\
346 #[l|]#orem
347 ipsum
348
349 "},
350 "2x",
351 indoc! {"\
352 #[lorem
353 ipsum\n|]#
354
355 "},
356 ))
357 .await?;
358
359 Ok(())
360 }
361
362 #[tokio::test(flavor = "multi_thread")]
363 async fn test_character_info() -> anyhow::Result<()> {
364 // UTF-8, single byte
365 test_key_sequence(
366 &mut helpers::AppBuilder::new().build()?,
367 Some("ih<esc>h:char<ret>"),
368 Some(&|app| {
369 assert_eq!(
370 r#""h" (U+0068) Dec 104 Hex 68"#,
371 app.editor.get_status().unwrap().0
372 );
373 }),
374 false,
375 )
376 .await?;
377
378 // UTF-8, multi-byte
379 test_key_sequence(
380 &mut helpers::AppBuilder::new().build()?,
381 Some("ië<esc>h:char<ret>"),
382 Some(&|app| {
383 assert_eq!(
384 r#""ë" (U+0065 U+0308) Hex 65 + cc 88"#,
385 app.editor.get_status().unwrap().0
386 );
387 }),
388 false,
389 )
390 .await?;
391
392 // Multiple characters displayed as one, escaped characters
393 test_key_sequence(
394 &mut helpers::AppBuilder::new().build()?,
395 Some(":line<minus>ending crlf<ret>:char<ret>"),
396 Some(&|app| {
397 assert_eq!(
398 r#""\r\n" (U+000d U+000a) Hex 0d + 0a"#,
399 app.editor.get_status().unwrap().0
400 );
401 }),
402 false,
403 )
404 .await?;
405
406 // Non-UTF-8
407 test_key_sequence(
408 &mut helpers::AppBuilder::new().build()?,
409 Some(":encoding ascii<ret>ih<esc>h:char<ret>"),
410 Some(&|app| {
411 assert_eq!(r#""h" Dec 104 Hex 68"#, app.editor.get_status().unwrap().0);
412 }),
413 false,
414 )
415 .await?;
416
417 Ok(())
418 }
419
420 #[tokio::test(flavor = "multi_thread")]
421 async fn test_delete_char_backward() -> anyhow::Result<()> {
422 // don't panic when deleting overlapping ranges
423 test(("#(x|)# #[x|]#", "c<space><backspace><esc>", "#[\n|]#")).await?;
424 test((
425 "#( |)##( |)#a#( |)#axx#[x|]#a",
426 "li<backspace><esc>",
427 "#(a|)##(|a)#xx#[|a]#",
428 ))
429 .await?;
430
431 Ok(())
432 }
433
434 // Cursor behavior is different when the text is created in the buffer vs loaded from a file.
435 // This test will not work for reproducing the crash or verifying the result after the fix.
436 // // #[tokio::test(flavor = "multi_thread")]
437 // async fn test_try_restore_indent() -> anyhow::Result<()> {
438 // test((" #[ |]#foo\na#( |)#bar\n", "o<C-u><esc>", " foo\n#[\n|]#a bar\n#(\n|)#")).await?;
439 // Ok(())
440 // }
441
442 #[tokio::test(flavor = "multi_thread")]
443 async fn test_try_restore_indent() -> anyhow::Result<()> {
444 // Bug: 15228 try_restore_indent uses primary cursor position for all selections,
445 // causing invalid range errors when multiple cursors are on different lines
446 let file = temp_file_with_contents(" foo\na bar\n")?;
447 test_key_sequence(
448 &mut AppBuilder::new().with_file(file.path(), None).build()?,
449 Some("jl<A-C>o<C-u><esc>"),
450 None,
451 false,
452 )
453 .await?;
454
455 Ok(())
456 }
457
458 #[tokio::test(flavor = "multi_thread")]
459 async fn test_delete_word_backward() -> anyhow::Result<()> {
460 // don't panic when deleting overlapping ranges
461 test(("fo#[o|]#ba#(r|)#", "a<C-w><esc>", "#[\n|]#")).await?;
462 Ok(())
463 }
464
465 #[tokio::test(flavor = "multi_thread")]
466 async fn test_delete_word_forward() -> anyhow::Result<()> {
467 // don't panic when deleting overlapping ranges
468 test(("fo#[o|]#b#(|ar)#", "i<A-d><esc>", "fo#[\n|]#")).await?;
469 Ok(())
470 }
471
472 #[tokio::test(flavor = "multi_thread")]
473 async fn test_delete_char_forward() -> anyhow::Result<()> {
474 test((
475 indoc! {"\
476 #[abc|]#def
477 #(abc|)#ef
478 #(abc|)#f
479 #(abc|)#
480 "},
481 "a<del><esc>",
482 indoc! {"\
483 #[abc|]#ef
484 #(abc|)#f
485 #(abc|)#
486 #(abc|)#
487 "},
488 ))
489 .await?;
490
491 Ok(())
492 }
493
494 #[tokio::test(flavor = "multi_thread")]
495 async fn test_insert_with_indent() -> anyhow::Result<()> {
496 const INPUT: &str = indoc! { "
497 #[f|]#n foo() {
498 if let Some(_) = None {
499
500 }
501
502 }
503
504 fn bar() {
505
506 }
507 "
508 };
509
510 // insert_at_line_start
511 test((
512 INPUT,
513 ":lang rust<ret>%<A-s>I",
514 indoc! { "
515 #[f|]#n foo() {
516 #(i|)#f let Some(_) = None {
517 #(\n|)#
518 #(}|)#
519 #( |)#
520 #(}|)#
521 #(\n|)#
522 #(f|)#n bar() {
523 #(\n|)#
524 #(}|)#
525 "
526 },
527 ))
528 .await?;
529
530 // insert_at_line_end
531 test((
532 INPUT,
533 ":lang rust<ret>%<A-s>A",
534 indoc! { "
535 fn foo() {#[\n|]#
536 if let Some(_) = None {#(\n|)#
537 #(\n|)#
538 }#(\n|)#
539 #(\n|)#
540 }#(\n|)#
541 #(\n|)#
542 fn bar() {#(\n|)#
543 #(\n|)#
544 }#(\n|)#
545 "
546 },
547 ))
548 .await?;
549
550 Ok(())
551 }
552
553 #[tokio::test(flavor = "multi_thread")]
554 async fn test_join_selections() -> anyhow::Result<()> {
555 // normal join
556 test((
557 indoc! {"\
558 #[a|]#bc
559 def
560 "},
561 "J",
562 indoc! {"\
563 #[a|]#bc def
564 "},
565 ))
566 .await?;
567
568 // join with empty line
569 test((
570 indoc! {"\
571 #[a|]#bc
572
573 def
574 "},
575 "JJ",
576 indoc! {"\
577 #[a|]#bc def
578 "},
579 ))
580 .await?;
581
582 // join with additional space in non-empty line
583 test((
584 indoc! {"\
585 #[a|]#bc
586
587 def
588 "},
589 "JJ",
590 indoc! {"\
591 #[a|]#bc def
592 "},
593 ))
594 .await?;
595
596 Ok(())
597 }
598
599 #[tokio::test(flavor = "multi_thread")]
600 async fn test_join_selections_space() -> anyhow::Result<()> {
601 // join with empty lines panic
602 test((
603 indoc! {"\
604 #[a
605
606 b
607
608 c
609
610 d
611
612 e|]#
613 "},
614 "<A-J>",
615 indoc! {"\
616 a#[ |]#b#( |)#c#( |)#d#( |)#e
617 "},
618 ))
619 .await?;
620
621 // normal join
622 test((
623 indoc! {"\
624 #[a|]#bc
625 def
626 "},
627 "<A-J>",
628 indoc! {"\
629 abc#[ |]#def
630 "},
631 ))
632 .await?;
633
634 // join with empty line
635 test((
636 indoc! {"\
637 #[a|]#bc
638
639 def
640 "},
641 "<A-J>",
642 indoc! {"\
643 #[a|]#bc
644 def
645 "},
646 ))
647 .await?;
648
649 // join with additional space in non-empty line
650 test((
651 indoc! {"\
652 #[a|]#bc
653
654 def
655 "},
656 "<A-J><A-J>",
657 indoc! {"\
658 abc#[ |]#def
659 "},
660 ))
661 .await?;
662
663 // join with retained trailing spaces
664 test((
665 indoc! {"\
666 #[aaa
667
668 bb
669
670 c |]#
671 "},
672 "<A-J>",
673 indoc! {"\
674 aaa #[ |]#bb #( |)#c
675 "},
676 ))
677 .await?;
678
679 Ok(())
680 }
681
682 #[tokio::test(flavor = "multi_thread")]
683 async fn test_join_selections_comment() -> anyhow::Result<()> {
684 test((
685 indoc! {"\
686 /// #[a|]#bc
687 /// def
688 "},
689 ":lang rust<ret>J",
690 indoc! {"\
691 /// #[a|]#bc def
692 "},
693 ))
694 .await?;
695
696 // Only join if the comment token matches the previous line.
697 test((
698 indoc! {"\
699 #[| // a
700 // b
701 /// c
702 /// d
703 e
704 /// f
705 // g]#
706 "},
707 ":lang rust<ret>J",
708 indoc! {"\
709 #[| // a b /// c d e f // g]#
710 "},
711 ))
712 .await?;
713
714 test((
715 "#[|\t// Join comments
716 \t// with indent]#",
717 ":lang go<ret>J",
718 "#[|\t// Join comments with indent]#",
719 ))
720 .await?;
721
722 Ok(())
723 }
724
725 #[tokio::test(flavor = "multi_thread")]
726 async fn test_toggle_comments_inside_comment_injection() -> anyhow::Result<()> {
727 // A `//` line comment's text is injected as the `comment` language, which has no
728 // comment-tokens of its own. With the cursor inside the comment, toggling must
729 // resolve tokens from the enclosing language and un-comment the line,
730 // not fall back to the hardcoded default `#`.
731 test((
732 indoc! {"\
733 // #[a|]#bc
734 "},
735 ":lang rust<ret><C-c>",
736 indoc! {"\
737 #[a|]#bc
738 "},
739 ))
740 .await?;
741
742 // A `///` doc comment's text is injected as markdown (no line comment token of
743 // its own). Toggling must strip the whole `///` marker via Rust's tokens rather
744 // than insert a markdown `<!-- -->` inside or leave a stray `/`.
745 test((
746 indoc! {"\
747 /// #[a|]#bc
748 "},
749 ":lang rust<ret><C-c>",
750 indoc! {"\
751 #[a|]#bc
752 "},
753 ))
754 .await?;
755
756 // Likewise for the `//!` inner doc comment marker.
757 test((
758 indoc! {"\
759 //! #[a|]#bc
760 "},
761 ":lang rust<ret><C-c>",
762 indoc! {"\
763 #[a|]#bc
764 "},
765 ))
766 .await?;
767
768 // Commenting a normal code line still uses the top-level language's token
769 // (no injection layer at the cursor), no regression for the common case.
770 test((
771 indoc! {"\
772 #[l|]#et x = 5;
773 "},
774 ":lang rust<ret><C-c>",
775 indoc! {"\
776 // #[l|]#et x = 5;
777 "},
778 ))
779 .await?;
780
781 Ok(())
782 }
783
784 #[tokio::test(flavor = "multi_thread")]
785 async fn test_read_file() -> anyhow::Result<()> {
786 let mut file = tempfile::NamedTempFile::new()?;
787 let contents_to_read = "some contents";
788 let output_file = helpers::temp_file_with_contents(contents_to_read)?;
789
790 test_key_sequence(
791 &mut helpers::AppBuilder::new()
792 .with_file(file.path(), None)
793 .build()?,
794 Some(&format!(":r {:?}<ret><esc>:w<ret>", output_file.path())),
795 Some(&|app| {
796 assert!(!app.editor.is_err(), "error: {:?}", app.editor.get_status());
797 }),
798 false,
799 )
800 .await?;
801
802 let expected_contents = LineFeedHandling::Native.apply(contents_to_read);
803 helpers::assert_file_has_content(&mut file, &expected_contents)?;
804
805 Ok(())
806 }
807
808 #[tokio::test(flavor = "multi_thread")]
809 async fn surround_delete() -> anyhow::Result<()> {
810 // Test `surround_delete` when head < anchor
811 test(("(#[| ]#)", "mdm", "#[| ]#")).await?;
812 test(("(#[| ]#)", "md(", "#[| ]#")).await?;
813
814 Ok(())
815 }
816
817 #[tokio::test(flavor = "multi_thread")]
818 async fn surround_replace_ts() -> anyhow::Result<()> {
819 const INPUT: &str = r#"\
820 fn foo() {
821 if let Some(_) = None {
822 testing!("f#[|o]#o)");
823 }
824 }
825 "#;
826 test((
827 INPUT,
828 ":lang rust<ret>mrm'",
829 r#"\
830 fn foo() {
831 if let Some(_) = None {
832 testing!('f#[|o]#o)');
833 }
834 }
835 "#,
836 ))
837 .await?;
838
839 test((
840 INPUT,
841 ":lang rust<ret>3mrm[",
842 r#"\
843 fn foo() {
844 if let Some(_) = None [
845 testing!("f#[|o]#o)");
846 ]
847 }
848 "#,
849 ))
850 .await?;
851
852 test((
853 INPUT,
854 ":lang rust<ret>2mrm{",
855 r#"\
856 fn foo() {
857 if let Some(_) = None {
858 testing!{"f#[|o]#o)"};
859 }
860 }
861 "#,
862 ))
863 .await?;
864
865 test((
866 indoc! {"\
867 #[a
868 b
869 c
870 d
871 e|]#
872 f
873 "},
874 "s\\n<ret>r,",
875 "a#[,|]#b#(,|)#c#(,|)#d#(,|)#e\nf\n",
876 ))
877 .await?;
878
879 Ok(())
880 }
881
882 #[tokio::test(flavor = "multi_thread")]
883 async fn macro_play_within_macro_record() -> anyhow::Result<()> {
884 // <https://github.com/helix-editor/helix/issues/12697>
885 //
886 // * `"aQihello<esc>Q` record a macro to register 'a' which inserts "hello"
887 // * `Q"aq<space>world<esc>Q` record a macro to the default macro register which plays the
888 // macro in register 'a' and then inserts " world"
889 // * `%d` clear the buffer
890 // * `q` replay the macro in the default macro register
891 // * `i<ret>` add a newline at the end
892 //
893 // The inner macro in register 'a' should replay within the outer macro exactly once to insert
894 // "hello world".
895 test((
896 indoc! {"\
897 #[|]#
898 "},
899 r#""aQihello<esc>QQ"aqi<space>world<esc>Q%dqi<ret>"#,
900 indoc! {"\
901 hello world
902 #[|]#"},
903 ))
904 .await?;
905
906 Ok(())
907 }
908
909 #[tokio::test(flavor = "multi_thread")]
910 async fn global_search_with_multibyte_chars() -> anyhow::Result<()> {
911 // Assert that `helix_term::commands::global_search` handles multibyte characters correctly.
912 test((
913 indoc! {"\
914 // Hello world!
915 // #[|
916 ]#
917 "},
918 // start global search
919 " /«十分に長い マルチバイトキャラクター列» で検索<ret><esc>",
920 indoc! {"\
921 // Hello world!
922 // #[|
923 ]#
924 "},
925 ))
926 .await?;
927
928 Ok(())
929 }
930
931 #[tokio::test(flavor = "multi_thread")]
932 async fn align_selections_with_varying_columns() -> anyhow::Result<()> {
933 test((
934 indoc! {r"
935 #[|]#I I II I
936 IIIIIIIII
937 IIIII
938 IIIIIIIII
939 "},
940 r"%sI<ret>&gg",
941 indoc! {r"
942 #[I|]# I II I
943 I I II IIIII
944 I I II I
945 I I II IIIII
946 "},
947 ))
948 .await?;
949
950 Ok(())
951 }