Debian Codegraph @

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

raw symbols SCIP
1 use super::*;
2
3 #[tokio::test(flavor = "multi_thread")]
4 async fn change_line_above_comment() -> anyhow::Result<()> {
5 // <https://github.com/helix-editor/helix/issues/12570>
6 test((
7 indoc! {"\
8 #[fn main() {}
9 |]#// a comment
10 "},
11 ":lang rust<ret>c",
12 indoc! {"\
13 #[
14 |]#// a comment
15 "},
16 ))
17 .await?;
18
19 Ok(())
20 }
21
22 #[tokio::test(flavor = "multi_thread")]
23 async fn insert_newline_many_selections() -> anyhow::Result<()> {
24 test((
25 indoc! {"\
26 #(|o)#ne
27 #(|t)#wo
28 #[|t]#hree
29 "},
30 "i<ret>",
31 indoc! {"\
32 \n#(|o)#ne
33
34 #(|t)#wo
35
36 #[|t]#hree
37 "},
38 ))
39 .await?;
40
41 // In this case the global offset that adjusts selections for inserted and deleted text
42 // should become negative because more text is deleted than is inserted.
43 test((
44 indoc! {"\
45 #[|🏴‍☠️]# #(|🏴‍☠️)# #(|🏴‍☠️)#
46 #(|🏴‍☠️)# #(|🏴‍☠️)# #(|🏴‍☠️)#
47 "},
48 "i<ret>",
49 indoc! {"\
50 \n#[|🏴‍☠️]#
51 #(|🏴‍☠️)#
52 #(|🏴‍☠️)#
53
54 #(|🏴‍☠️)#
55 #(|🏴‍☠️)#
56 #(|🏴‍☠️)#
57 "},
58 ))
59 .await?;
60
61 // <https://github.com/helix-editor/helix/issues/12495>
62 test((
63 indoc! {"\
64 id #(|1)#,Item #(|1)#,cost #(|1)#,location #(|1)#
65 id #(|2)#,Item #(|2)#,cost #(|2)#,location #(|2)#
66 id #(|1)##(|0)#,Item #(|1)##(|0)#,cost #(|1)##(|0)#,location #(|1)##[|0]#"},
67 "i<ret>",
68 indoc! {"\
69 id
70 #(|1)#,Item
71 #(|1)#,cost
72 #(|1)#,location
73 #(|1)#
74 id
75 #(|2)#,Item
76 #(|2)#,cost
77 #(|2)#,location
78 #(|2)#
79 id
80 #(|1)#
81 #(|0)#,Item
82 #(|1)#
83 #(|0)#,cost
84 #(|1)#
85 #(|0)#,location
86 #(|1)#
87 #[|0]#"},
88 ))
89 .await?;
90
91 // <https://github.com/helix-editor/helix/issues/12461>
92 test((
93 indoc! {"\
94 real R〉 #(||)# 〈real R〉 @ 〈real R〉
95 #(||)# 〈real R〉 + 〈ureal R〉 i #(||)# 〈real R〉 - 〈ureal R〉 i
96 #(||)# 〈real R〉 + i #(||)# 〈real R〉 - i #(||)# 〈real R〉 〈infnan〉 i
97 #(||)# + 〈ureal R〉 i #(||)# - 〈ureal R〉 i
98 #(||)# 〈infnan〉 i #(||)# + i #[||]# - i"},
99 "i<ret>",
100 indoc! {"\
101 real R〉
102 #(||)# 〈real R〉 @ 〈real R〉
103
104 #(||)# 〈real R〉 + 〈ureal R〉 i
105 #(||)# 〈real R〉 - 〈ureal R〉 i
106
107 #(||)# 〈real R〉 + i
108 #(||)# 〈real R〉 - i
109 #(||)# 〈real R〉 〈infnan〉 i
110
111 #(||)# + 〈ureal R〉 i
112 #(||)# - 〈ureal R〉 i
113
114 #(||)# 〈infnan〉 i
115 #(||)# + i
116 #[||]# - i"},
117 ))
118 .await?;
119
120 Ok(())
121 }
122
123 #[tokio::test(flavor = "multi_thread")]
124 async fn insert_newline_trim_trailing_whitespace() -> anyhow::Result<()> {
125 // Trailing whitespace is trimmed.
126 test((
127 indoc! {"\
128 hello·······#[|
129 ]#world
130 "}
131 .replace('·', " "),
132 "i<ret>",
133 indoc! {"\
134 hello
135 #[|
136 ]#world
137 "}
138 .replace('·', " "),
139 ))
140 .await?;
141
142 // Whitespace that would become trailing is trimmed too.
143 test((
144 indoc! {"\
145 hello········#[|w]#orld
146 "}
147 .replace('·', " "),
148 "i<ret>",
149 indoc! {"\
150 hello
151 #[|w]#orld
152 "}
153 .replace('·', " "),
154 ))
155 .await?;
156
157 // Only whitespace before the cursor is trimmed.
158 test((
159 indoc! {"\
160 hello········#[|·]#····world
161 "}
162 .replace('·', " "),
163 "i<ret>",
164 indoc! {"\
165 hello
166 #[|·]#····world
167 "}
168 .replace('·', " "),
169 ))
170 .await?;
171
172 Ok(())
173 }
174
175 #[tokio::test(flavor = "multi_thread")]
176 async fn insert_newline_trim_whitespace_to_previous_selection() -> anyhow::Result<()> {
177 test((
178 indoc! {"\"#[a|]# #(a|)# #(a|)#\""},
179 "c<ret>",
180 indoc! {"\"\n#[\n|]##(\n|)##(\"|)#"},
181 ))
182 .await?;
183
184 Ok(())
185 }
186
187 #[tokio::test(flavor = "multi_thread")]
188 async fn insert_newline_continue_line_comment() -> anyhow::Result<()> {
189 // `insert_newline` continues a single line comment
190 test((
191 indoc! {"\
192 // Hello world!#[|
193 ]#
194 "},
195 ":lang rust<ret>i<ret>",
196 indoc! {"\
197 // Hello world!
198 // #[|
199 ]#
200 "},
201 ))
202 .await?;
203
204 // Continuation should use the enclosing layer's comment tokens if an injected layer
205 // doesn't define comment token.
206 test((
207 indoc! {"\
208 // Hello world!#[|
209 ]#
210 "},
211 ":lang go<ret>i<ret>",
212 indoc! {"\
213 // Hello world!
214 // #[|
215 ]#
216 "},
217 ))
218 .await?;
219
220 test((
221 indoc! {"\
222 //go:generate echo hello#[|
223 ]#
224 "},
225 ":lang go<ret>i<ret>",
226 indoc! {"\
227 //go:generate echo hello
228 // #[|
229 ]#
230 "},
231 ))
232 .await?;
233
234 // The comment is not continued if the cursor is before the comment token. (Note that we
235 // are entering insert-mode with `I`.)
236 test((
237 indoc! {"\
238 // Hello world!#[|
239 ]#
240 "},
241 ":lang rust<ret>I<ret>",
242 indoc! {"\
243 \n#[/|]#/ Hello world!
244 "},
245 ))
246 .await?;
247
248 // `insert_newline` again clears the whitespace on the first continued comment and continues
249 // the comment again.
250 test((
251 indoc! {"\
252 // Hello world!
253 // #[|
254 ]#
255 "},
256 ":lang rust<ret>i<ret>",
257 indoc! {"\
258 // Hello world!
259 //
260 // #[|
261 ]#
262 "},
263 ))
264 .await?;
265
266 // Line comment continuation and trailing whitespace is also trimmed when using
267 // `insert_newline` in the middle of a comment.
268 test((
269 indoc! {"\
270 //·hello····#[|·]#····world
271 "}
272 .replace('·', " "),
273 ":lang rust<ret>i<ret>",
274 indoc! {"\
275 //·hello
276 //·#[|·]#····world
277 "}
278 .replace('·', " "),
279 ))
280 .await?;
281
282 // Comment continuation should work on multiple selections.
283 // <https://github.com/helix-editor/helix/issues/12539>
284 test((
285 indoc! {"\
286 ///·Docs#[|·]#
287 pub·struct·A;
288
289 ///·Docs#(|·)#
290 pub·struct·B;
291 "}
292 .replace('·', " "),
293 ":lang rust<ret>i<ret><ret>",
294 indoc! {"\
295 ///·Docs
296 ///
297 ///·#[|·]#
298 pub·struct·A;
299
300 ///·Docs
301 ///
302 ///·#(|·)#
303 pub·struct·B;
304 "}
305 .replace('·', " "),
306 ))
307 .await?;
308
309 Ok(())
310 }
311
312 /// NOTE: Language is set to markdown to check if the indentation is correct for the new line
313 #[tokio::test(flavor = "multi_thread")]
314 async fn test_open_above() -> anyhow::Result<()> {
315 // `O` is pressed in the first line
316 test((
317 indoc! {"Helix #[is|]# cool"},
318 ":lang markdown<ret>O",
319 indoc! {"\
320 #[\n|]#
321 Helix is cool
322 "},
323 ))
324 .await?;
325
326 // `O` is pressed in the first line, but the current line has some indentation
327 test((
328 indoc! {"\
329 ··This line has 2 spaces in front of it#[\n|]#
330 "}
331 .replace('·', " "),
332 ":lang markdown<ret>Oa",
333 indoc! {"\
334 ··a#[\n|]#
335 ··This line has 2 spaces in front of it
336 "}
337 .replace('·', " "),
338 ))
339 .await?;
340
341 // `O` is pressed but *not* in the first line
342 test((
343 indoc! {"\
344 I use
345 b#[t|]#w.
346 "},
347 ":lang markdown<ret>Oarch",
348 indoc! {"\
349 I use
350 arch#[\n|]#
351 btw.
352 "},
353 ))
354 .await?;
355
356 // `O` is pressed but *not* in the first line and the line has some indentation
357 test((
358 indoc! {"\
359 I use
360 ····b#[t|]#w.
361 "}
362 .replace("·", " "),
363 ":lang markdown<ret>Ohelix",
364 indoc! {"\
365 I use
366 ····helix#[\n|]#
367 ····btw.
368 "}
369 .replace("·", " "),
370 ))
371 .await?;
372
373 Ok(())
374 }
375
376 #[tokio::test(flavor = "multi_thread")]
377 async fn test_open_above_with_multiple_cursors() -> anyhow::Result<()> {
378 // the primary cursor is also in the top line
379 test((
380 indoc! {"#[H|]#elix
381 #(i|)#s
382 #(c|)#ool"},
383 "O",
384 indoc! {
385 "#[\n|]#
386 Helix
387 #(\n|)#
388 is
389 #(\n|)#
390 cool
391 "
392 },
393 ))
394 .await?;
395
396 // now with some additional indentation
397 test((
398 indoc! {"····#[H|]#elix
399 ····#(i|)#s
400 ····#(c|)#ool"}
401 .replace("·", " "),
402 ":indent-style 4<ret>O",
403 indoc! {
404 "····#[\n|]#
405 ····Helix
406 ····#(\n|)#
407 ····is
408 ····#(\n|)#
409 ····cool
410 "
411 }
412 .replace("·", " "),
413 ))
414 .await?;
415
416 // the first line is within a comment, the second not.
417 // However, if we open above, the first newly added line should start within a comment
418 // while the other should be a normal line
419 test((
420 indoc! {"fn main() {
421 // #[VIP|]# comment
422 l#(e|)#t yes = false;
423 }"},
424 ":lang rust<ret>O",
425 indoc! {"fn main() {
426 // #[\n|]#
427 // VIP comment
428 #(\n|)#
429 let yes = false;
430 }"},
431 ))
432 .await?;
433
434 Ok(())
435 }
436
437 #[tokio::test(flavor = "multi_thread")]
438 async fn test_open_below_with_multiple_cursors() -> anyhow::Result<()> {
439 // the primary cursor is also in the top line
440 test((
441 indoc! {"#[H|]#elix
442 #(i|)#s
443 #(c|)#ool"},
444 "o",
445 indoc! {"Helix
446 #[\n|]#
447 is
448 #(\n|)#
449 cool
450 #(\n|)#
451 "
452 },
453 ))
454 .await?;
455
456 // now with some additional indentation
457 test((
458 indoc! {"····#[H|]#elix
459 ····#(i|)#s
460 ····#(c|)#ool"}
461 .replace("·", " "),
462 ":indent-style 4<ret>o",
463 indoc! {
464 "····Helix
465 ····#[\n|]#
466 ····is
467 ····#(\n|)#
468 ····cool
469 ····#(\n|)#
470 "
471 }
472 .replace("·", " "),
473 ))
474 .await?;
475
476 // the first line is within a comment, the second not.
477 // However, if we open below, the first newly added line should start within a comment
478 // while the other should be a normal line
479 test((
480 indoc! {"fn main() {
481 // #[VIP|]# comment
482 l#(e|)#t yes = false;
483 }"},
484 ":lang rust<ret>o",
485 indoc! {"fn main() {
486 // VIP comment
487 // #[\n|]#
488 let yes = false;
489 #(\n|)#
490 }"},
491 ))
492 .await?;
493
494 // Open below should continue comment even if language has `comment` grammar
495 // injection.
496 test((
497 indoc! {"\
498 package main
499
500 // VIP#[|]# comment
501 func main() {}
502 "},
503 ":lang go<ret>o",
504 indoc! {"\
505 package main
506
507 // VIP comment
508 // #[\n|]#
509 func main() {}
510 "},
511 ))
512 .await?;
513
514 Ok(())
515 }
516
517 /// NOTE: To make the `open_above` comment-aware, we're setting the language for each test to rust.
518 #[tokio::test(flavor = "multi_thread")]
519 async fn test_open_above_with_comments() -> anyhow::Result<()> {
520 // `O` is pressed in the first line inside a line comment
521 test((
522 indoc! {"// a commen#[t|]#"},
523 ":lang rust<ret>O",
524 indoc! {"\
525 // #[\n|]#
526 // a comment
527 "},
528 ))
529 .await?;
530
531 // `O` is pressed in the first line inside a line comment, but with indentation
532 test((
533 indoc! {"····// a comm#[e|]#nt"}.replace("·", " "),
534 ":lang rust<ret>O",
535 indoc! {"\
536 ····// #[\n|]#
537 ····// a comment
538 "}
539 .replace("·", " "),
540 ))
541 .await?;
542
543 // `O` is pressed but not in the first line but inside a line comment
544 test((
545 indoc! {"\
546 fn main() { }
547 // yeetus deletus#[\n|]#
548 "},
549 ":lang rust<ret>O",
550 indoc! {"\
551 fn main() { }
552 // #[\n|]#
553 // yeetus deletus
554 "},
555 ))
556 .await?;
557
558 // `O` is pressed but not in the first line but inside a line comment and with indentation
559 test((
560 indoc! {"\
561 fn main() { }
562 ····// yeetus deletus#[\n|]#
563 "}
564 .replace("·", " "),
565 ":lang rust<ret>O",
566 indoc! {"\
567 fn main() { }
568 ····// #[\n|]#
569 ····// yeetus deletus
570 "}
571 .replace("·", " "),
572 ))
573 .await?;
574
575 Ok(())
576 }
577
578 #[tokio::test(flavor = "multi_thread")]
579 async fn try_restore_indent() -> anyhow::Result<()> {
580 // Assert that `helix_view::editor::try_restore_indent` handles line endings correctly
581 // endings.
582 test((
583 indoc! {"\
584 if true #[|{]#
585 }
586 "},
587 // `try_restore_indent` should remove the indentation when adding a blank line.
588 ":lang rust<ret>o<esc>",
589 indoc! {"\
590 if true {
591 #[
592 |]#}
593 "},
594 ))
595 .await?;
596
597 Ok(())
598 }
599
600 // Tests being able to jump in insert mode, then undo the write performed by the jump
601 // https://github.com/helix-editor/helix/issues/13480
602 #[tokio::test(flavor = "multi_thread")]
603 async fn test_jump_undo_redo() -> anyhow::Result<()> {
604 use helix_core::hashmap;
605 use helix_term::keymap;
606 use helix_view::document::Mode;
607
608 let mut config = Config::default();
609 config.keys.insert(
610 Mode::Insert,
611 keymap!({"Insert Mode"
612 "C-i" => goto_file_start,
613 "C-o" => goto_file_end,
614 }),
615 );
616
617 // Undo
618 test_with_config(
619 AppBuilder::new().with_config(config.clone()),
620 ("#[|]#", "iworld<C-i>Hello, <esc>u", "#[w|]#orld"),
621 )
622 .await?;
623
624 // Redo
625 test_with_config(
626 AppBuilder::new().with_config(config),
627 (
628 "#[|]#",
629 "iworld<C-i>Hello, <esc>ui<C-o><esc>U",
630 "Hello, #[w|]#orld",
631 ),
632 )
633 .await?;
634 Ok(())
635 }
636
637 #[tokio::test(flavor = "multi_thread")]
638 async fn test_indent_with_spaces() -> anyhow::Result<()> {
639 let tests = vec![
640 // at start of line
641 (
642 indoc! {"\
643 SELECT *
644 #[|FROM table]#
645 #(|WHERE condition)#
646 "},
647 "i<tab>",
648 indoc! {"\
649 SELECT *
650 #[|FROM table]#
651 #(|WHERE condition)#
652 "},
653 ),
654 // in the middle of line
655 (
656 indoc! {"\
657 SELECT #[*|]#
658 FROM #(table|)#
659 WHERE #(condition|)#
660 "},
661 "i<S-tab>",
662 indoc! {"\
663 SELECT #[|*]#
664 FROM #(|table)#
665 WHERE #(|condition)#
666 "},
667 ),
668 // indentation in normal mode
669 (
670 indoc! {"\
671 -- comment
672 #[|SELECT *
673 FROM table
674 WHERE condition]#
675 "},
676 "<gt>",
677 indoc! {"\
678 -- comment
679 #[|SELECT *
680 FROM table
681 WHERE condition]#
682 "},
683 ),
684 ];
685
686 for test in tests {
687 test_with_config(AppBuilder::new().with_file("foo.rs", None), test).await?;
688 }
689
690 Ok(())
691 }