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 // The comment is not continued if the cursor is before the comment token. (Note that we
205 // are entering insert-mode with `I`.)
206 test((
207 indoc! {"\
208 // Hello world!#[|
209 ]#
210 "},
211 ":lang rust<ret>I<ret>",
212 indoc! {"\
213 \n#[/|]#/ Hello world!
214 "},
215 ))
216 .await?;
217
218 // `insert_newline` again clears the whitespace on the first continued comment and continues
219 // the comment again.
220 test((
221 indoc! {"\
222 // Hello world!
223 // #[|
224 ]#
225 "},
226 ":lang rust<ret>i<ret>",
227 indoc! {"\
228 // Hello world!
229 //
230 // #[|
231 ]#
232 "},
233 ))
234 .await?;
235
236 // Line comment continuation and trailing whitespace is also trimmed when using
237 // `insert_newline` in the middle of a comment.
238 test((
239 indoc! {"\
240 //·hello····#[|·]#····world
241 "}
242 .replace('·', " "),
243 ":lang rust<ret>i<ret>",
244 indoc! {"\
245 //·hello
246 //·#[|·]#····world
247 "}
248 .replace('·', " "),
249 ))
250 .await?;
251
252 // Comment continuation should work on multiple selections.
253 // <https://github.com/helix-editor/helix/issues/12539>
254 test((
255 indoc! {"\
256 ///·Docs#[|·]#
257 pub·struct·A;
258
259 ///·Docs#(|·)#
260 pub·struct·B;
261 "}
262 .replace('·', " "),
263 ":lang rust<ret>i<ret><ret>",
264 indoc! {"\
265 ///·Docs
266 ///
267 ///·#[|·]#
268 pub·struct·A;
269
270 ///·Docs
271 ///
272 ///·#(|·)#
273 pub·struct·B;
274 "}
275 .replace('·', " "),
276 ))
277 .await?;
278
279 Ok(())
280 }
281
282 /// NOTE: Language is set to markdown to check if the indentation is correct for the new line
283 #[tokio::test(flavor = "multi_thread")]
284 async fn test_open_above() -> anyhow::Result<()> {
285 // `O` is pressed in the first line
286 test((
287 indoc! {"Helix #[is|]# cool"},
288 ":lang markdown<ret>O",
289 indoc! {"\
290 #[\n|]#
291 Helix is cool
292 "},
293 ))
294 .await?;
295
296 // `O` is pressed in the first line, but the current line has some indentation
297 test((
298 indoc! {"\
299 ··This line has 2 spaces in front of it#[\n|]#
300 "}
301 .replace('·', " "),
302 ":lang markdown<ret>Oa",
303 indoc! {"\
304 ··a#[\n|]#
305 ··This line has 2 spaces in front of it
306 "}
307 .replace('·', " "),
308 ))
309 .await?;
310
311 // `O` is pressed but *not* in the first line
312 test((
313 indoc! {"\
314 I use
315 b#[t|]#w.
316 "},
317 ":lang markdown<ret>Oarch",
318 indoc! {"\
319 I use
320 arch#[\n|]#
321 btw.
322 "},
323 ))
324 .await?;
325
326 // `O` is pressed but *not* in the first line and the line has some indentation
327 test((
328 indoc! {"\
329 I use
330 ····b#[t|]#w.
331 "}
332 .replace("·", " "),
333 ":lang markdown<ret>Ohelix",
334 indoc! {"\
335 I use
336 ····helix#[\n|]#
337 ····btw.
338 "}
339 .replace("·", " "),
340 ))
341 .await?;
342
343 Ok(())
344 }
345
346 #[tokio::test(flavor = "multi_thread")]
347 async fn test_open_above_with_multiple_cursors() -> anyhow::Result<()> {
348 // the primary cursor is also in the top line
349 test((
350 indoc! {"#[H|]#elix
351 #(i|)#s
352 #(c|)#ool"},
353 "O",
354 indoc! {
355 "#[\n|]#
356 Helix
357 #(\n|)#
358 is
359 #(\n|)#
360 cool
361 "
362 },
363 ))
364 .await?;
365
366 // now with some additional indentation
367 test((
368 indoc! {"····#[H|]#elix
369 ····#(i|)#s
370 ····#(c|)#ool"}
371 .replace("·", " "),
372 ":indent-style 4<ret>O",
373 indoc! {
374 "····#[\n|]#
375 ····Helix
376 ····#(\n|)#
377 ····is
378 ····#(\n|)#
379 ····cool
380 "
381 }
382 .replace("·", " "),
383 ))
384 .await?;
385
386 // the first line is within a comment, the second not.
387 // However, if we open above, the first newly added line should start within a comment
388 // while the other should be a normal line
389 test((
390 indoc! {"fn main() {
391 // #[VIP|]# comment
392 l#(e|)#t yes = false;
393 }"},
394 ":lang rust<ret>O",
395 indoc! {"fn main() {
396 // #[\n|]#
397 // VIP comment
398 #(\n|)#
399 let yes = false;
400 }"},
401 ))
402 .await?;
403
404 Ok(())
405 }
406
407 #[tokio::test(flavor = "multi_thread")]
408 async fn test_open_below_with_multiple_cursors() -> anyhow::Result<()> {
409 // the primary cursor is also in the top line
410 test((
411 indoc! {"#[H|]#elix
412 #(i|)#s
413 #(c|)#ool"},
414 "o",
415 indoc! {"Helix
416 #[\n|]#
417 is
418 #(\n|)#
419 cool
420 #(\n|)#
421 "
422 },
423 ))
424 .await?;
425
426 // now with some additional indentation
427 test((
428 indoc! {"····#[H|]#elix
429 ····#(i|)#s
430 ····#(c|)#ool"}
431 .replace("·", " "),
432 ":indent-style 4<ret>o",
433 indoc! {
434 "····Helix
435 ····#[\n|]#
436 ····is
437 ····#(\n|)#
438 ····cool
439 ····#(\n|)#
440 "
441 }
442 .replace("·", " "),
443 ))
444 .await?;
445
446 // the first line is within a comment, the second not.
447 // However, if we open below, the first newly added line should start within a comment
448 // while the other should be a normal line
449 test((
450 indoc! {"fn main() {
451 // #[VIP|]# comment
452 l#(e|)#t yes = false;
453 }"},
454 ":lang rust<ret>o",
455 indoc! {"fn main() {
456 // VIP comment
457 // #[\n|]#
458 let yes = false;
459 #(\n|)#
460 }"},
461 ))
462 .await?;
463
464 Ok(())
465 }
466
467 /// NOTE: To make the `open_above` comment-aware, we're setting the language for each test to rust.
468 #[tokio::test(flavor = "multi_thread")]
469 async fn test_open_above_with_comments() -> anyhow::Result<()> {
470 // `O` is pressed in the first line inside a line comment
471 test((
472 indoc! {"// a commen#[t|]#"},
473 ":lang rust<ret>O",
474 indoc! {"\
475 // #[\n|]#
476 // a comment
477 "},
478 ))
479 .await?;
480
481 // `O` is pressed in the first line inside a line comment, but with indentation
482 test((
483 indoc! {"····// a comm#[e|]#nt"}.replace("·", " "),
484 ":lang rust<ret>O",
485 indoc! {"\
486 ····// #[\n|]#
487 ····// a comment
488 "}
489 .replace("·", " "),
490 ))
491 .await?;
492
493 // `O` is pressed but not in the first line but inside a line comment
494 test((
495 indoc! {"\
496 fn main() { }
497 // yeetus deletus#[\n|]#
498 "},
499 ":lang rust<ret>O",
500 indoc! {"\
501 fn main() { }
502 // #[\n|]#
503 // yeetus deletus
504 "},
505 ))
506 .await?;
507
508 // `O` is pressed but not in the first line but inside a line comment and with indentation
509 test((
510 indoc! {"\
511 fn main() { }
512 ····// yeetus deletus#[\n|]#
513 "}
514 .replace("·", " "),
515 ":lang rust<ret>O",
516 indoc! {"\
517 fn main() { }
518 ····// #[\n|]#
519 ····// yeetus deletus
520 "}
521 .replace("·", " "),
522 ))
523 .await?;
524
525 Ok(())
526 }
527
528 #[tokio::test(flavor = "multi_thread")]
529 async fn try_restore_indent() -> anyhow::Result<()> {
530 // Assert that `helix_view::editor::try_restore_indent` handles line endings correctly
531 // endings.
532 test((
533 indoc! {"\
534 if true #[|{]#
535 }
536 "},
537 // `try_restore_indent` should remove the indentation when adding a blank line.
538 ":lang rust<ret>o<esc>",
539 indoc! {"\
540 if true {
541 #[
542 |]#}
543 "},
544 ))
545 .await?;
546
547 Ok(())
548 }
549
550 // Tests being able to jump in insert mode, then undo the write performed by the jump
551 // https://github.com/helix-editor/helix/issues/13480
552 #[tokio::test(flavor = "multi_thread")]
553 async fn test_jump_undo_redo() -> anyhow::Result<()> {
554 use helix_core::hashmap;
555 use helix_term::keymap;
556 use helix_view::document::Mode;
557
558 let mut config = Config::default();
559 config.keys.insert(
560 Mode::Insert,
561 keymap!({"Insert Mode"
562 "C-i" => goto_file_start,
563 "C-o" => goto_file_end,
564 }),
565 );
566
567 // Undo
568 test_with_config(
569 AppBuilder::new().with_config(config.clone()),
570 ("#[|]#", "iworld<C-i>Hello, <esc>u", "#[w|]#orld"),
571 )
572 .await?;
573
574 // Redo
575 test_with_config(
576 AppBuilder::new().with_config(config),
577 (
578 "#[|]#",
579 "iworld<C-i>Hello, <esc>ui<C-o><esc>U",
580 "Hello, #[w|]#orld",
581 ),
582 )
583 .await?;
584 Ok(())
585 }
586
587 #[tokio::test(flavor = "multi_thread")]
588 async fn test_indent_with_spaces() -> anyhow::Result<()> {
589 let tests = vec![
590 // at start of line
591 (
592 indoc! {"\
593 SELECT *
594 #[|FROM table]#
595 #(|WHERE condition)#
596 "},
597 "i<tab>",
598 indoc! {"\
599 SELECT *
600 #[|FROM table]#
601 #(|WHERE condition)#
602 "},
603 ),
604 // in the middle of line
605 (
606 indoc! {"\
607 SELECT #[*|]#
608 FROM #(table|)#
609 WHERE #(condition|)#
610 "},
611 "i<S-tab>",
612 indoc! {"\
613 SELECT #[|*]#
614 FROM #(|table)#
615 WHERE #(|condition)#
616 "},
617 ),
618 // indentation in normal mode
619 (
620 indoc! {"\
621 -- comment
622 #[|SELECT *
623 FROM table
624 WHERE condition]#
625 "},
626 "<gt>",
627 indoc! {"\
628 -- comment
629 #[|SELECT *
630 FROM table
631 WHERE condition]#
632 "},
633 ),
634 ];
635
636 for test in tests {
637 test_with_config(AppBuilder::new().with_file("foo.rs", None), test).await?;
638 }
639
640 Ok(())
641 }