aboutsummaryrefslogtreecommitdiff
path: root/crates/ide
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide')
-rw-r--r--crates/ide/src/diagnostics.rs596
-rw-r--r--crates/ide/src/diagnostics/field_shorthand.rs13
-rw-r--r--crates/ide/src/diagnostics/fixes.rs296
-rw-r--r--crates/ide/src/diagnostics/fixes/change_case.rs155
-rw-r--r--crates/ide/src/diagnostics/fixes/create_field.rs156
-rw-r--r--crates/ide/src/diagnostics/fixes/fill_missing_fields.rs217
-rw-r--r--crates/ide/src/diagnostics/fixes/remove_semicolon.rs41
-rw-r--r--crates/ide/src/diagnostics/fixes/replace_with_find_map.rs84
-rw-r--r--crates/ide/src/diagnostics/fixes/unresolved_module.rs89
-rw-r--r--crates/ide/src/diagnostics/fixes/wrap_tail_expr.rs211
-rw-r--r--crates/ide/src/diagnostics/unlinked_file.rs54
-rw-r--r--crates/ide/src/doc_links.rs8
-rw-r--r--crates/ide/src/join_lines.rs2
-rw-r--r--crates/ide/src/lib.rs2
-rw-r--r--crates/ide/src/matching_brace.rs12
-rw-r--r--crates/ide/src/parent_module.rs6
-rw-r--r--crates/ide/src/references/rename.rs12
-rw-r--r--crates/ide/src/syntax_highlighting/highlight.rs11
-rw-r--r--crates/ide/src/syntax_highlighting/inject.rs2
-rw-r--r--crates/ide/src/syntax_highlighting/tags.rs4
-rw-r--r--crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html14
-rw-r--r--crates/ide/src/syntax_highlighting/test_data/highlighting.html15
-rw-r--r--crates/ide/src/syntax_highlighting/tests.rs27
23 files changed, 1139 insertions, 888 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs
index 273d8cfbb..27d347dbd 100644
--- a/crates/ide/src/diagnostics.rs
+++ b/crates/ide/src/diagnostics.rs
@@ -28,7 +28,7 @@ use unlinked_file::UnlinkedFile;
28 28
29use crate::{Assist, AssistId, AssistKind, FileId, Label, SourceChange}; 29use crate::{Assist, AssistId, AssistKind, FileId, Label, SourceChange};
30 30
31use self::fixes::DiagnosticWithFix; 31use self::fixes::DiagnosticWithFixes;
32 32
33#[derive(Debug)] 33#[derive(Debug)]
34pub struct Diagnostic { 34pub struct Diagnostic {
@@ -36,14 +36,14 @@ pub struct Diagnostic {
36 pub message: String, 36 pub message: String,
37 pub range: TextRange, 37 pub range: TextRange,
38 pub severity: Severity, 38 pub severity: Severity,
39 pub fix: Option<Assist>, 39 pub fixes: Option<Vec<Assist>>,
40 pub unused: bool, 40 pub unused: bool,
41 pub code: Option<DiagnosticCode>, 41 pub code: Option<DiagnosticCode>,
42} 42}
43 43
44impl Diagnostic { 44impl Diagnostic {
45 fn error(range: TextRange, message: String) -> Self { 45 fn error(range: TextRange, message: String) -> Self {
46 Self { message, range, severity: Severity::Error, fix: None, unused: false, code: None } 46 Self { message, range, severity: Severity::Error, fixes: None, unused: false, code: None }
47 } 47 }
48 48
49 fn hint(range: TextRange, message: String) -> Self { 49 fn hint(range: TextRange, message: String) -> Self {
@@ -51,14 +51,14 @@ impl Diagnostic {
51 message, 51 message,
52 range, 52 range,
53 severity: Severity::WeakWarning, 53 severity: Severity::WeakWarning,
54 fix: None, 54 fixes: None,
55 unused: false, 55 unused: false,
56 code: None, 56 code: None,
57 } 57 }
58 } 58 }
59 59
60 fn with_fix(self, fix: Option<Assist>) -> Self { 60 fn with_fixes(self, fixes: Option<Vec<Assist>>) -> Self {
61 Self { fix, ..self } 61 Self { fixes, ..self }
62 } 62 }
63 63
64 fn with_unused(self, unused: bool) -> Self { 64 fn with_unused(self, unused: bool) -> Self {
@@ -154,7 +154,7 @@ pub(crate) fn diagnostics(
154 // Override severity and mark as unused. 154 // Override severity and mark as unused.
155 res.borrow_mut().push( 155 res.borrow_mut().push(
156 Diagnostic::hint(range, d.message()) 156 Diagnostic::hint(range, d.message())
157 .with_fix(d.fix(&sema, resolve)) 157 .with_fixes(d.fixes(&sema, resolve))
158 .with_code(Some(d.code())), 158 .with_code(Some(d.code())),
159 ); 159 );
160 }) 160 })
@@ -210,23 +210,23 @@ pub(crate) fn diagnostics(
210 res.into_inner() 210 res.into_inner()
211} 211}
212 212
213fn diagnostic_with_fix<D: DiagnosticWithFix>( 213fn diagnostic_with_fix<D: DiagnosticWithFixes>(
214 d: &D, 214 d: &D,
215 sema: &Semantics<RootDatabase>, 215 sema: &Semantics<RootDatabase>,
216 resolve: &AssistResolveStrategy, 216 resolve: &AssistResolveStrategy,
217) -> Diagnostic { 217) -> Diagnostic {
218 Diagnostic::error(sema.diagnostics_display_range(d.display_source()).range, d.message()) 218 Diagnostic::error(sema.diagnostics_display_range(d.display_source()).range, d.message())
219 .with_fix(d.fix(&sema, resolve)) 219 .with_fixes(d.fixes(&sema, resolve))
220 .with_code(Some(d.code())) 220 .with_code(Some(d.code()))
221} 221}
222 222
223fn warning_with_fix<D: DiagnosticWithFix>( 223fn warning_with_fix<D: DiagnosticWithFixes>(
224 d: &D, 224 d: &D,
225 sema: &Semantics<RootDatabase>, 225 sema: &Semantics<RootDatabase>,
226 resolve: &AssistResolveStrategy, 226 resolve: &AssistResolveStrategy,
227) -> Diagnostic { 227) -> Diagnostic {
228 Diagnostic::hint(sema.diagnostics_display_range(d.display_source()).range, d.message()) 228 Diagnostic::hint(sema.diagnostics_display_range(d.display_source()).range, d.message())
229 .with_fix(d.fix(&sema, resolve)) 229 .with_fixes(d.fixes(&sema, resolve))
230 .with_code(Some(d.code())) 230 .with_code(Some(d.code()))
231} 231}
232 232
@@ -256,12 +256,12 @@ fn check_unnecessary_braces_in_use_statement(
256 256
257 acc.push( 257 acc.push(
258 Diagnostic::hint(use_range, "Unnecessary braces in use statement".to_string()) 258 Diagnostic::hint(use_range, "Unnecessary braces in use statement".to_string())
259 .with_fix(Some(fix( 259 .with_fixes(Some(vec![fix(
260 "remove_braces", 260 "remove_braces",
261 "Remove unnecessary braces", 261 "Remove unnecessary braces",
262 SourceChange::from_text_edit(file_id, edit), 262 SourceChange::from_text_edit(file_id, edit),
263 use_range, 263 use_range,
264 ))), 264 )])),
265 ); 265 );
266 } 266 }
267 267
@@ -309,9 +309,23 @@ mod tests {
309 /// Takes a multi-file input fixture with annotated cursor positions, 309 /// Takes a multi-file input fixture with annotated cursor positions,
310 /// and checks that: 310 /// and checks that:
311 /// * a diagnostic is produced 311 /// * a diagnostic is produced
312 /// * this diagnostic fix trigger range touches the input cursor position 312 /// * the first diagnostic fix trigger range touches the input cursor position
313 /// * that the contents of the file containing the cursor match `after` after the diagnostic fix is applied 313 /// * that the contents of the file containing the cursor match `after` after the diagnostic fix is applied
314 pub(crate) fn check_fix(ra_fixture_before: &str, ra_fixture_after: &str) { 314 pub(crate) fn check_fix(ra_fixture_before: &str, ra_fixture_after: &str) {
315 check_nth_fix(0, ra_fixture_before, ra_fixture_after);
316 }
317 /// Takes a multi-file input fixture with annotated cursor positions,
318 /// and checks that:
319 /// * a diagnostic is produced
320 /// * every diagnostic fixes trigger range touches the input cursor position
321 /// * that the contents of the file containing the cursor match `after` after each diagnostic fix is applied
322 pub(crate) fn check_fixes(ra_fixture_before: &str, ra_fixtures_after: Vec<&str>) {
323 for (i, ra_fixture_after) in ra_fixtures_after.iter().enumerate() {
324 check_nth_fix(i, ra_fixture_before, ra_fixture_after)
325 }
326 }
327
328 fn check_nth_fix(nth: usize, ra_fixture_before: &str, ra_fixture_after: &str) {
315 let after = trim_indent(ra_fixture_after); 329 let after = trim_indent(ra_fixture_after);
316 330
317 let (analysis, file_position) = fixture::position(ra_fixture_before); 331 let (analysis, file_position) = fixture::position(ra_fixture_before);
@@ -324,9 +338,9 @@ mod tests {
324 .unwrap() 338 .unwrap()
325 .pop() 339 .pop()
326 .unwrap(); 340 .unwrap();
327 let fix = diagnostic.fix.unwrap(); 341 let fix = &diagnostic.fixes.unwrap()[nth];
328 let actual = { 342 let actual = {
329 let source_change = fix.source_change.unwrap(); 343 let source_change = fix.source_change.as_ref().unwrap();
330 let file_id = *source_change.source_file_edits.keys().next().unwrap(); 344 let file_id = *source_change.source_file_edits.keys().next().unwrap();
331 let mut actual = analysis.file_text(file_id).unwrap().to_string(); 345 let mut actual = analysis.file_text(file_id).unwrap().to_string();
332 346
@@ -344,7 +358,6 @@ mod tests {
344 file_position.offset 358 file_position.offset
345 ); 359 );
346 } 360 }
347
348 /// Checks that there's a diagnostic *without* fix at `$0`. 361 /// Checks that there's a diagnostic *without* fix at `$0`.
349 fn check_no_fix(ra_fixture: &str) { 362 fn check_no_fix(ra_fixture: &str) {
350 let (analysis, file_position) = fixture::position(ra_fixture); 363 let (analysis, file_position) = fixture::position(ra_fixture);
@@ -357,7 +370,7 @@ mod tests {
357 .unwrap() 370 .unwrap()
358 .pop() 371 .pop()
359 .unwrap(); 372 .unwrap();
360 assert!(diagnostic.fix.is_none(), "got a fix when none was expected: {:?}", diagnostic); 373 assert!(diagnostic.fixes.is_none(), "got a fix when none was expected: {:?}", diagnostic);
361 } 374 }
362 375
363 /// Takes a multi-file input fixture with annotated cursor position and checks that no diagnostics 376 /// Takes a multi-file input fixture with annotated cursor position and checks that no diagnostics
@@ -375,7 +388,7 @@ mod tests {
375 assert_eq!(diagnostics.len(), 0, "unexpected diagnostics:\n{:#?}", diagnostics); 388 assert_eq!(diagnostics.len(), 0, "unexpected diagnostics:\n{:#?}", diagnostics);
376 } 389 }
377 390
378 fn check_expect(ra_fixture: &str, expect: Expect) { 391 pub(crate) fn check_expect(ra_fixture: &str, expect: Expect) {
379 let (analysis, file_id) = fixture::file(ra_fixture); 392 let (analysis, file_id) = fixture::file(ra_fixture);
380 let diagnostics = analysis 393 let diagnostics = analysis
381 .diagnostics(&DiagnosticsConfig::default(), AssistResolveStrategy::All, file_id) 394 .diagnostics(&DiagnosticsConfig::default(), AssistResolveStrategy::All, file_id)
@@ -384,374 +397,6 @@ mod tests {
384 } 397 }
385 398
386 #[test] 399 #[test]
387 fn test_wrap_return_type_option() {
388 check_fix(
389 r#"
390//- /main.rs crate:main deps:core
391use core::option::Option::{self, Some, None};
392
393fn div(x: i32, y: i32) -> Option<i32> {
394 if y == 0 {
395 return None;
396 }
397 x / y$0
398}
399//- /core/lib.rs crate:core
400pub mod result {
401 pub enum Result<T, E> { Ok(T), Err(E) }
402}
403pub mod option {
404 pub enum Option<T> { Some(T), None }
405}
406"#,
407 r#"
408use core::option::Option::{self, Some, None};
409
410fn div(x: i32, y: i32) -> Option<i32> {
411 if y == 0 {
412 return None;
413 }
414 Some(x / y)
415}
416"#,
417 );
418 }
419
420 #[test]
421 fn test_wrap_return_type() {
422 check_fix(
423 r#"
424//- /main.rs crate:main deps:core
425use core::result::Result::{self, Ok, Err};
426
427fn div(x: i32, y: i32) -> Result<i32, ()> {
428 if y == 0 {
429 return Err(());
430 }
431 x / y$0
432}
433//- /core/lib.rs crate:core
434pub mod result {
435 pub enum Result<T, E> { Ok(T), Err(E) }
436}
437pub mod option {
438 pub enum Option<T> { Some(T), None }
439}
440"#,
441 r#"
442use core::result::Result::{self, Ok, Err};
443
444fn div(x: i32, y: i32) -> Result<i32, ()> {
445 if y == 0 {
446 return Err(());
447 }
448 Ok(x / y)
449}
450"#,
451 );
452 }
453
454 #[test]
455 fn test_wrap_return_type_handles_generic_functions() {
456 check_fix(
457 r#"
458//- /main.rs crate:main deps:core
459use core::result::Result::{self, Ok, Err};
460
461fn div<T>(x: T) -> Result<T, i32> {
462 if x == 0 {
463 return Err(7);
464 }
465 $0x
466}
467//- /core/lib.rs crate:core
468pub mod result {
469 pub enum Result<T, E> { Ok(T), Err(E) }
470}
471pub mod option {
472 pub enum Option<T> { Some(T), None }
473}
474"#,
475 r#"
476use core::result::Result::{self, Ok, Err};
477
478fn div<T>(x: T) -> Result<T, i32> {
479 if x == 0 {
480 return Err(7);
481 }
482 Ok(x)
483}
484"#,
485 );
486 }
487
488 #[test]
489 fn test_wrap_return_type_handles_type_aliases() {
490 check_fix(
491 r#"
492//- /main.rs crate:main deps:core
493use core::result::Result::{self, Ok, Err};
494
495type MyResult<T> = Result<T, ()>;
496
497fn div(x: i32, y: i32) -> MyResult<i32> {
498 if y == 0 {
499 return Err(());
500 }
501 x $0/ y
502}
503//- /core/lib.rs crate:core
504pub mod result {
505 pub enum Result<T, E> { Ok(T), Err(E) }
506}
507pub mod option {
508 pub enum Option<T> { Some(T), None }
509}
510"#,
511 r#"
512use core::result::Result::{self, Ok, Err};
513
514type MyResult<T> = Result<T, ()>;
515
516fn div(x: i32, y: i32) -> MyResult<i32> {
517 if y == 0 {
518 return Err(());
519 }
520 Ok(x / y)
521}
522"#,
523 );
524 }
525
526 #[test]
527 fn test_wrap_return_type_not_applicable_when_expr_type_does_not_match_ok_type() {
528 check_no_diagnostics(
529 r#"
530//- /main.rs crate:main deps:core
531use core::result::Result::{self, Ok, Err};
532
533fn foo() -> Result<(), i32> { 0 }
534
535//- /core/lib.rs crate:core
536pub mod result {
537 pub enum Result<T, E> { Ok(T), Err(E) }
538}
539pub mod option {
540 pub enum Option<T> { Some(T), None }
541}
542"#,
543 );
544 }
545
546 #[test]
547 fn test_wrap_return_type_not_applicable_when_return_type_is_not_result_or_option() {
548 check_no_diagnostics(
549 r#"
550//- /main.rs crate:main deps:core
551use core::result::Result::{self, Ok, Err};
552
553enum SomeOtherEnum { Ok(i32), Err(String) }
554
555fn foo() -> SomeOtherEnum { 0 }
556
557//- /core/lib.rs crate:core
558pub mod result {
559 pub enum Result<T, E> { Ok(T), Err(E) }
560}
561pub mod option {
562 pub enum Option<T> { Some(T), None }
563}
564"#,
565 );
566 }
567
568 #[test]
569 fn test_fill_struct_fields_empty() {
570 check_fix(
571 r#"
572struct TestStruct { one: i32, two: i64 }
573
574fn test_fn() {
575 let s = TestStruct {$0};
576}
577"#,
578 r#"
579struct TestStruct { one: i32, two: i64 }
580
581fn test_fn() {
582 let s = TestStruct { one: (), two: ()};
583}
584"#,
585 );
586 }
587
588 #[test]
589 fn test_fill_struct_fields_self() {
590 check_fix(
591 r#"
592struct TestStruct { one: i32 }
593
594impl TestStruct {
595 fn test_fn() { let s = Self {$0}; }
596}
597"#,
598 r#"
599struct TestStruct { one: i32 }
600
601impl TestStruct {
602 fn test_fn() { let s = Self { one: ()}; }
603}
604"#,
605 );
606 }
607
608 #[test]
609 fn test_fill_struct_fields_enum() {
610 check_fix(
611 r#"
612enum Expr {
613 Bin { lhs: Box<Expr>, rhs: Box<Expr> }
614}
615
616impl Expr {
617 fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
618 Expr::Bin {$0 }
619 }
620}
621"#,
622 r#"
623enum Expr {
624 Bin { lhs: Box<Expr>, rhs: Box<Expr> }
625}
626
627impl Expr {
628 fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
629 Expr::Bin { lhs: (), rhs: () }
630 }
631}
632"#,
633 );
634 }
635
636 #[test]
637 fn test_fill_struct_fields_partial() {
638 check_fix(
639 r#"
640struct TestStruct { one: i32, two: i64 }
641
642fn test_fn() {
643 let s = TestStruct{ two: 2$0 };
644}
645"#,
646 r"
647struct TestStruct { one: i32, two: i64 }
648
649fn test_fn() {
650 let s = TestStruct{ two: 2, one: () };
651}
652",
653 );
654 }
655
656 #[test]
657 fn test_fill_struct_fields_raw_ident() {
658 check_fix(
659 r#"
660struct TestStruct { r#type: u8 }
661
662fn test_fn() {
663 TestStruct { $0 };
664}
665"#,
666 r"
667struct TestStruct { r#type: u8 }
668
669fn test_fn() {
670 TestStruct { r#type: () };
671}
672",
673 );
674 }
675
676 #[test]
677 fn test_fill_struct_fields_no_diagnostic() {
678 check_no_diagnostics(
679 r"
680 struct TestStruct { one: i32, two: i64 }
681
682 fn test_fn() {
683 let one = 1;
684 let s = TestStruct{ one, two: 2 };
685 }
686 ",
687 );
688 }
689
690 #[test]
691 fn test_fill_struct_fields_no_diagnostic_on_spread() {
692 check_no_diagnostics(
693 r"
694 struct TestStruct { one: i32, two: i64 }
695
696 fn test_fn() {
697 let one = 1;
698 let s = TestStruct{ ..a };
699 }
700 ",
701 );
702 }
703
704 #[test]
705 fn test_unresolved_module_diagnostic() {
706 check_expect(
707 r#"mod foo;"#,
708 expect![[r#"
709 [
710 Diagnostic {
711 message: "unresolved module",
712 range: 0..8,
713 severity: Error,
714 fix: Some(
715 Assist {
716 id: AssistId(
717 "create_module",
718 QuickFix,
719 ),
720 label: "Create module",
721 group: None,
722 target: 0..8,
723 source_change: Some(
724 SourceChange {
725 source_file_edits: {},
726 file_system_edits: [
727 CreateFile {
728 dst: AnchoredPathBuf {
729 anchor: FileId(
730 0,
731 ),
732 path: "foo.rs",
733 },
734 initial_contents: "",
735 },
736 ],
737 is_snippet: false,
738 },
739 ),
740 },
741 ),
742 unused: false,
743 code: Some(
744 DiagnosticCode(
745 "unresolved-module",
746 ),
747 ),
748 },
749 ]
750 "#]],
751 );
752 }
753
754 #[test]
755 fn test_unresolved_macro_range() { 400 fn test_unresolved_macro_range() {
756 check_expect( 401 check_expect(
757 r#"foo::bar!(92);"#, 402 r#"foo::bar!(92);"#,
@@ -761,7 +406,7 @@ fn test_fn() {
761 message: "unresolved macro `foo::bar!`", 406 message: "unresolved macro `foo::bar!`",
762 range: 5..8, 407 range: 5..8,
763 severity: Error, 408 severity: Error,
764 fix: None, 409 fixes: None,
765 unused: false, 410 unused: false,
766 code: Some( 411 code: Some(
767 DiagnosticCode( 412 DiagnosticCode(
@@ -792,7 +437,7 @@ fn main() {
792pub struct Foo { pub a: i32, pub b: i32 } 437pub struct Foo { pub a: i32, pub b: i32 }
793"#, 438"#,
794 r#" 439 r#"
795fn some(, b: ()) {} 440fn some(, b: () ) {}
796fn items() {} 441fn items() {}
797fn here() {} 442fn here() {}
798 443
@@ -891,53 +536,6 @@ mod a {
891 } 536 }
892 537
893 #[test] 538 #[test]
894 fn test_add_field_from_usage() {
895 check_fix(
896 r"
897fn main() {
898 Foo { bar: 3, baz$0: false};
899}
900struct Foo {
901 bar: i32
902}
903",
904 r"
905fn main() {
906 Foo { bar: 3, baz: false};
907}
908struct Foo {
909 bar: i32,
910 baz: bool
911}
912",
913 )
914 }
915
916 #[test]
917 fn test_add_field_in_other_file_from_usage() {
918 check_fix(
919 r#"
920//- /main.rs
921mod foo;
922
923fn main() {
924 foo::Foo { bar: 3, $0baz: false};
925}
926//- /foo.rs
927struct Foo {
928 bar: i32
929}
930"#,
931 r#"
932struct Foo {
933 bar: i32,
934 pub(crate) baz: bool
935}
936"#,
937 )
938 }
939
940 #[test]
941 fn test_disabled_diagnostics() { 539 fn test_disabled_diagnostics() {
942 let mut config = DiagnosticsConfig::default(); 540 let mut config = DiagnosticsConfig::default();
943 config.disabled.insert("unresolved-module".into()); 541 config.disabled.insert("unresolved-module".into());
@@ -955,134 +553,28 @@ struct Foo {
955 } 553 }
956 554
957 #[test] 555 #[test]
958 fn test_rename_incorrect_case() {
959 check_fix(
960 r#"
961pub struct test_struct$0 { one: i32 }
962
963pub fn some_fn(val: test_struct) -> test_struct {
964 test_struct { one: val.one + 1 }
965}
966"#,
967 r#"
968pub struct TestStruct { one: i32 }
969
970pub fn some_fn(val: TestStruct) -> TestStruct {
971 TestStruct { one: val.one + 1 }
972}
973"#,
974 );
975
976 check_fix(
977 r#"
978pub fn some_fn(NonSnakeCase$0: u8) -> u8 {
979 NonSnakeCase
980}
981"#,
982 r#"
983pub fn some_fn(non_snake_case: u8) -> u8 {
984 non_snake_case
985}
986"#,
987 );
988
989 check_fix(
990 r#"
991pub fn SomeFn$0(val: u8) -> u8 {
992 if val != 0 { SomeFn(val - 1) } else { val }
993}
994"#,
995 r#"
996pub fn some_fn(val: u8) -> u8 {
997 if val != 0 { some_fn(val - 1) } else { val }
998}
999"#,
1000 );
1001
1002 check_fix(
1003 r#"
1004fn some_fn() {
1005 let whatAWeird_Formatting$0 = 10;
1006 another_func(whatAWeird_Formatting);
1007}
1008"#,
1009 r#"
1010fn some_fn() {
1011 let what_a_weird_formatting = 10;
1012 another_func(what_a_weird_formatting);
1013}
1014"#,
1015 );
1016 }
1017
1018 #[test]
1019 fn test_uppercase_const_no_diagnostics() {
1020 check_no_diagnostics(
1021 r#"
1022fn foo() {
1023 const ANOTHER_ITEM$0: &str = "some_item";
1024}
1025"#,
1026 );
1027 }
1028
1029 #[test]
1030 fn test_rename_incorrect_case_struct_method() {
1031 check_fix(
1032 r#"
1033pub struct TestStruct;
1034
1035impl TestStruct {
1036 pub fn SomeFn$0() -> TestStruct {
1037 TestStruct
1038 }
1039}
1040"#,
1041 r#"
1042pub struct TestStruct;
1043
1044impl TestStruct {
1045 pub fn some_fn() -> TestStruct {
1046 TestStruct
1047 }
1048}
1049"#,
1050 );
1051 }
1052
1053 #[test]
1054 fn test_single_incorrect_case_diagnostic_in_function_name_issue_6970() {
1055 let input = r#"fn FOO$0() {}"#;
1056 let expected = r#"fn foo() {}"#;
1057
1058 let (analysis, file_position) = fixture::position(input);
1059 let diagnostics = analysis
1060 .diagnostics(
1061 &DiagnosticsConfig::default(),
1062 AssistResolveStrategy::All,
1063 file_position.file_id,
1064 )
1065 .unwrap();
1066 assert_eq!(diagnostics.len(), 1);
1067
1068 check_fix(input, expected);
1069 }
1070
1071 #[test]
1072 fn unlinked_file_prepend_first_item() { 556 fn unlinked_file_prepend_first_item() {
1073 cov_mark::check!(unlinked_file_prepend_before_first_item); 557 cov_mark::check!(unlinked_file_prepend_before_first_item);
1074 check_fix( 558 // Only tests the first one for `pub mod` since the rest are the same
559 check_fixes(
1075 r#" 560 r#"
1076//- /main.rs 561//- /main.rs
1077fn f() {} 562fn f() {}
1078//- /foo.rs 563//- /foo.rs
1079$0 564$0
1080"#, 565"#,
1081 r#" 566 vec![
567 r#"
1082mod foo; 568mod foo;
1083 569
1084fn f() {} 570fn f() {}
1085"#, 571"#,
572 r#"
573pub mod foo;
574
575fn f() {}
576"#,
577 ],
1086 ); 578 );
1087 } 579 }
1088 580
diff --git a/crates/ide/src/diagnostics/field_shorthand.rs b/crates/ide/src/diagnostics/field_shorthand.rs
index 2b1787f9b..01bd2dba6 100644
--- a/crates/ide/src/diagnostics/field_shorthand.rs
+++ b/crates/ide/src/diagnostics/field_shorthand.rs
@@ -46,14 +46,13 @@ fn check_expr_field_shorthand(
46 46
47 let field_range = record_field.syntax().text_range(); 47 let field_range = record_field.syntax().text_range();
48 acc.push( 48 acc.push(
49 Diagnostic::hint(field_range, "Shorthand struct initialization".to_string()).with_fix( 49 Diagnostic::hint(field_range, "Shorthand struct initialization".to_string())
50 Some(fix( 50 .with_fixes(Some(vec![fix(
51 "use_expr_field_shorthand", 51 "use_expr_field_shorthand",
52 "Use struct shorthand initialization", 52 "Use struct shorthand initialization",
53 SourceChange::from_text_edit(file_id, edit), 53 SourceChange::from_text_edit(file_id, edit),
54 field_range, 54 field_range,
55 )), 55 )])),
56 ),
57 ); 56 );
58 } 57 }
59} 58}
@@ -86,13 +85,13 @@ fn check_pat_field_shorthand(
86 let edit = edit_builder.finish(); 85 let edit = edit_builder.finish();
87 86
88 let field_range = record_pat_field.syntax().text_range(); 87 let field_range = record_pat_field.syntax().text_range();
89 acc.push(Diagnostic::hint(field_range, "Shorthand struct pattern".to_string()).with_fix( 88 acc.push(Diagnostic::hint(field_range, "Shorthand struct pattern".to_string()).with_fixes(
90 Some(fix( 89 Some(vec![fix(
91 "use_pat_field_shorthand", 90 "use_pat_field_shorthand",
92 "Use struct field shorthand", 91 "Use struct field shorthand",
93 SourceChange::from_text_edit(file_id, edit), 92 SourceChange::from_text_edit(file_id, edit),
94 field_range, 93 field_range,
95 )), 94 )]),
96 )); 95 ));
97 } 96 }
98} 97}
diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs
index 15821500f..258ac6974 100644
--- a/crates/ide/src/diagnostics/fixes.rs
+++ b/crates/ide/src/diagnostics/fixes.rs
@@ -1,297 +1,31 @@
1//! Provides a way to attach fixes to the diagnostics. 1//! Provides a way to attach fixes to the diagnostics.
2//! The same module also has all curret custom fixes for the diagnostics implemented. 2//! The same module also has all curret custom fixes for the diagnostics implemented.
3use hir::{ 3mod change_case;
4 db::AstDatabase, 4mod create_field;
5 diagnostics::{ 5mod fill_missing_fields;
6 Diagnostic, IncorrectCase, MissingFields, MissingOkOrSomeInTailExpr, NoSuchField, 6mod remove_semicolon;
7 RemoveThisSemicolon, ReplaceFilterMapNextWithFindMap, UnresolvedModule, 7mod replace_with_find_map;
8 }, 8mod unresolved_module;
9 HasSource, HirDisplay, InFile, Semantics, VariantDef, 9mod wrap_tail_expr;
10}; 10
11use hir::{diagnostics::Diagnostic, Semantics};
11use ide_assists::AssistResolveStrategy; 12use ide_assists::AssistResolveStrategy;
12use ide_db::{ 13use ide_db::RootDatabase;
13 base_db::{AnchoredPathBuf, FileId},
14 source_change::{FileSystemEdit, SourceChange},
15 RootDatabase,
16};
17use syntax::{
18 algo,
19 ast::{self, edit::IndentLevel, make, ArgListOwner},
20 AstNode, TextRange,
21};
22use text_edit::TextEdit;
23 14
24use crate::{ 15use crate::Assist;
25 diagnostics::{fix, unresolved_fix},
26 references::rename::rename_with_semantics,
27 Assist, FilePosition,
28};
29 16
30/// A [Diagnostic] that potentially has a fix available. 17/// A [Diagnostic] that potentially has some fixes available.
31/// 18///
32/// [Diagnostic]: hir::diagnostics::Diagnostic 19/// [Diagnostic]: hir::diagnostics::Diagnostic
33pub(crate) trait DiagnosticWithFix: Diagnostic { 20pub(crate) trait DiagnosticWithFixes: Diagnostic {
34 /// `resolve` determines if the diagnostic should fill in the `edit` field 21 /// `resolve` determines if the diagnostic should fill in the `edit` field
35 /// of the assist. 22 /// of the assist.
36 /// 23 ///
37 /// If `resolve` is false, the edit will be computed later, on demand, and 24 /// If `resolve` is false, the edit will be computed later, on demand, and
38 /// can be omitted. 25 /// can be omitted.
39 fn fix( 26 fn fixes(
40 &self, 27 &self,
41 sema: &Semantics<RootDatabase>, 28 sema: &Semantics<RootDatabase>,
42 _resolve: &AssistResolveStrategy, 29 _resolve: &AssistResolveStrategy,
43 ) -> Option<Assist>; 30 ) -> Option<Vec<Assist>>;
44}
45
46impl DiagnosticWithFix for UnresolvedModule {
47 fn fix(
48 &self,
49 sema: &Semantics<RootDatabase>,
50 _resolve: &AssistResolveStrategy,
51 ) -> Option<Assist> {
52 let root = sema.db.parse_or_expand(self.file)?;
53 let unresolved_module = self.decl.to_node(&root);
54 Some(fix(
55 "create_module",
56 "Create module",
57 FileSystemEdit::CreateFile {
58 dst: AnchoredPathBuf {
59 anchor: self.file.original_file(sema.db),
60 path: self.candidate.clone(),
61 },
62 initial_contents: "".to_string(),
63 }
64 .into(),
65 unresolved_module.syntax().text_range(),
66 ))
67 }
68}
69
70impl DiagnosticWithFix for NoSuchField {
71 fn fix(
72 &self,
73 sema: &Semantics<RootDatabase>,
74 _resolve: &AssistResolveStrategy,
75 ) -> Option<Assist> {
76 let root = sema.db.parse_or_expand(self.file)?;
77 missing_record_expr_field_fix(
78 &sema,
79 self.file.original_file(sema.db),
80 &self.field.to_node(&root),
81 )
82 }
83}
84
85impl DiagnosticWithFix for MissingFields {
86 fn fix(
87 &self,
88 sema: &Semantics<RootDatabase>,
89 _resolve: &AssistResolveStrategy,
90 ) -> Option<Assist> {
91 // Note that although we could add a diagnostics to
92 // fill the missing tuple field, e.g :
93 // `struct A(usize);`
94 // `let a = A { 0: () }`
95 // but it is uncommon usage and it should not be encouraged.
96 if self.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) {
97 return None;
98 }
99
100 let root = sema.db.parse_or_expand(self.file)?;
101 let field_list_parent = self.field_list_parent.to_node(&root);
102 let old_field_list = field_list_parent.record_expr_field_list()?;
103 let mut new_field_list = old_field_list.clone();
104 for f in self.missed_fields.iter() {
105 let field =
106 make::record_expr_field(make::name_ref(&f.to_string()), Some(make::expr_unit()));
107 new_field_list = new_field_list.append_field(&field);
108 }
109
110 let edit = {
111 let mut builder = TextEdit::builder();
112 algo::diff(&old_field_list.syntax(), &new_field_list.syntax())
113 .into_text_edit(&mut builder);
114 builder.finish()
115 };
116 Some(fix(
117 "fill_missing_fields",
118 "Fill struct fields",
119 SourceChange::from_text_edit(self.file.original_file(sema.db), edit),
120 sema.original_range(&field_list_parent.syntax()).range,
121 ))
122 }
123}
124
125impl DiagnosticWithFix for MissingOkOrSomeInTailExpr {
126 fn fix(
127 &self,
128 sema: &Semantics<RootDatabase>,
129 _resolve: &AssistResolveStrategy,
130 ) -> Option<Assist> {
131 let root = sema.db.parse_or_expand(self.file)?;
132 let tail_expr = self.expr.to_node(&root);
133 let tail_expr_range = tail_expr.syntax().text_range();
134 let replacement = format!("{}({})", self.required, tail_expr.syntax());
135 let edit = TextEdit::replace(tail_expr_range, replacement);
136 let source_change = SourceChange::from_text_edit(self.file.original_file(sema.db), edit);
137 let name = if self.required == "Ok" { "Wrap with Ok" } else { "Wrap with Some" };
138 Some(fix("wrap_tail_expr", name, source_change, tail_expr_range))
139 }
140}
141
142impl DiagnosticWithFix for RemoveThisSemicolon {
143 fn fix(
144 &self,
145 sema: &Semantics<RootDatabase>,
146 _resolve: &AssistResolveStrategy,
147 ) -> Option<Assist> {
148 let root = sema.db.parse_or_expand(self.file)?;
149
150 let semicolon = self
151 .expr
152 .to_node(&root)
153 .syntax()
154 .parent()
155 .and_then(ast::ExprStmt::cast)
156 .and_then(|expr| expr.semicolon_token())?
157 .text_range();
158
159 let edit = TextEdit::delete(semicolon);
160 let source_change = SourceChange::from_text_edit(self.file.original_file(sema.db), edit);
161
162 Some(fix("remove_semicolon", "Remove this semicolon", source_change, semicolon))
163 }
164}
165
166impl DiagnosticWithFix for IncorrectCase {
167 fn fix(
168 &self,
169 sema: &Semantics<RootDatabase>,
170 resolve: &AssistResolveStrategy,
171 ) -> Option<Assist> {
172 let root = sema.db.parse_or_expand(self.file)?;
173 let name_node = self.ident.to_node(&root);
174
175 let name_node = InFile::new(self.file, name_node.syntax());
176 let frange = name_node.original_file_range(sema.db);
177 let file_position = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
178
179 let label = format!("Rename to {}", self.suggested_text);
180 let mut res = unresolved_fix("change_case", &label, frange.range);
181 if resolve.should_resolve(&res.id) {
182 let source_change = rename_with_semantics(sema, file_position, &self.suggested_text);
183 res.source_change = Some(source_change.ok().unwrap_or_default());
184 }
185
186 Some(res)
187 }
188}
189
190impl DiagnosticWithFix for ReplaceFilterMapNextWithFindMap {
191 fn fix(
192 &self,
193 sema: &Semantics<RootDatabase>,
194 _resolve: &AssistResolveStrategy,
195 ) -> Option<Assist> {
196 let root = sema.db.parse_or_expand(self.file)?;
197 let next_expr = self.next_expr.to_node(&root);
198 let next_call = ast::MethodCallExpr::cast(next_expr.syntax().clone())?;
199
200 let filter_map_call = ast::MethodCallExpr::cast(next_call.receiver()?.syntax().clone())?;
201 let filter_map_name_range = filter_map_call.name_ref()?.ident_token()?.text_range();
202 let filter_map_args = filter_map_call.arg_list()?;
203
204 let range_to_replace =
205 TextRange::new(filter_map_name_range.start(), next_expr.syntax().text_range().end());
206 let replacement = format!("find_map{}", filter_map_args.syntax().text());
207 let trigger_range = next_expr.syntax().text_range();
208
209 let edit = TextEdit::replace(range_to_replace, replacement);
210
211 let source_change = SourceChange::from_text_edit(self.file.original_file(sema.db), edit);
212
213 Some(fix(
214 "replace_with_find_map",
215 "Replace filter_map(..).next() with find_map()",
216 source_change,
217 trigger_range,
218 ))
219 }
220}
221
222fn missing_record_expr_field_fix(
223 sema: &Semantics<RootDatabase>,
224 usage_file_id: FileId,
225 record_expr_field: &ast::RecordExprField,
226) -> Option<Assist> {
227 let record_lit = ast::RecordExpr::cast(record_expr_field.syntax().parent()?.parent()?)?;
228 let def_id = sema.resolve_variant(record_lit)?;
229 let module;
230 let def_file_id;
231 let record_fields = match def_id {
232 VariantDef::Struct(s) => {
233 module = s.module(sema.db);
234 let source = s.source(sema.db)?;
235 def_file_id = source.file_id;
236 let fields = source.value.field_list()?;
237 record_field_list(fields)?
238 }
239 VariantDef::Union(u) => {
240 module = u.module(sema.db);
241 let source = u.source(sema.db)?;
242 def_file_id = source.file_id;
243 source.value.record_field_list()?
244 }
245 VariantDef::Variant(e) => {
246 module = e.module(sema.db);
247 let source = e.source(sema.db)?;
248 def_file_id = source.file_id;
249 let fields = source.value.field_list()?;
250 record_field_list(fields)?
251 }
252 };
253 let def_file_id = def_file_id.original_file(sema.db);
254
255 let new_field_type = sema.type_of_expr(&record_expr_field.expr()?)?;
256 if new_field_type.is_unknown() {
257 return None;
258 }
259 let new_field = make::record_field(
260 None,
261 make::name(&record_expr_field.field_name()?.text()),
262 make::ty(&new_field_type.display_source_code(sema.db, module.into()).ok()?),
263 );
264
265 let last_field = record_fields.fields().last()?;
266 let last_field_syntax = last_field.syntax();
267 let indent = IndentLevel::from_node(last_field_syntax);
268
269 let mut new_field = new_field.to_string();
270 if usage_file_id != def_file_id {
271 new_field = format!("pub(crate) {}", new_field);
272 }
273 new_field = format!("\n{}{}", indent, new_field);
274
275 let needs_comma = !last_field_syntax.to_string().ends_with(',');
276 if needs_comma {
277 new_field = format!(",{}", new_field);
278 }
279
280 let source_change = SourceChange::from_text_edit(
281 def_file_id,
282 TextEdit::insert(last_field_syntax.text_range().end(), new_field),
283 );
284 return Some(fix(
285 "create_field",
286 "Create field",
287 source_change,
288 record_expr_field.syntax().text_range(),
289 ));
290
291 fn record_field_list(field_def_list: ast::FieldList) -> Option<ast::RecordFieldList> {
292 match field_def_list {
293 ast::FieldList::RecordFieldList(it) => Some(it),
294 ast::FieldList::TupleFieldList(_) => None,
295 }
296 }
297} 31}
diff --git a/crates/ide/src/diagnostics/fixes/change_case.rs b/crates/ide/src/diagnostics/fixes/change_case.rs
new file mode 100644
index 000000000..42be3375f
--- /dev/null
+++ b/crates/ide/src/diagnostics/fixes/change_case.rs
@@ -0,0 +1,155 @@
1use hir::{db::AstDatabase, diagnostics::IncorrectCase, InFile, Semantics};
2use ide_assists::{Assist, AssistResolveStrategy};
3use ide_db::{base_db::FilePosition, RootDatabase};
4use syntax::AstNode;
5
6use crate::{
7 diagnostics::{unresolved_fix, DiagnosticWithFixes},
8 references::rename::rename_with_semantics,
9};
10
11impl DiagnosticWithFixes for IncorrectCase {
12 fn fixes(
13 &self,
14 sema: &Semantics<RootDatabase>,
15 resolve: &AssistResolveStrategy,
16 ) -> Option<Vec<Assist>> {
17 let root = sema.db.parse_or_expand(self.file)?;
18 let name_node = self.ident.to_node(&root);
19
20 let name_node = InFile::new(self.file, name_node.syntax());
21 let frange = name_node.original_file_range(sema.db);
22 let file_position = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
23
24 let label = format!("Rename to {}", self.suggested_text);
25 let mut res = unresolved_fix("change_case", &label, frange.range);
26 if resolve.should_resolve(&res.id) {
27 let source_change = rename_with_semantics(sema, file_position, &self.suggested_text);
28 res.source_change = Some(source_change.ok().unwrap_or_default());
29 }
30
31 Some(vec![res])
32 }
33}
34
35#[cfg(test)]
36mod change_case {
37 use crate::{
38 diagnostics::tests::{check_fix, check_no_diagnostics},
39 fixture, AssistResolveStrategy, DiagnosticsConfig,
40 };
41
42 #[test]
43 fn test_rename_incorrect_case() {
44 check_fix(
45 r#"
46pub struct test_struct$0 { one: i32 }
47
48pub fn some_fn(val: test_struct) -> test_struct {
49 test_struct { one: val.one + 1 }
50}
51"#,
52 r#"
53pub struct TestStruct { one: i32 }
54
55pub fn some_fn(val: TestStruct) -> TestStruct {
56 TestStruct { one: val.one + 1 }
57}
58"#,
59 );
60
61 check_fix(
62 r#"
63pub fn some_fn(NonSnakeCase$0: u8) -> u8 {
64 NonSnakeCase
65}
66"#,
67 r#"
68pub fn some_fn(non_snake_case: u8) -> u8 {
69 non_snake_case
70}
71"#,
72 );
73
74 check_fix(
75 r#"
76pub fn SomeFn$0(val: u8) -> u8 {
77 if val != 0 { SomeFn(val - 1) } else { val }
78}
79"#,
80 r#"
81pub fn some_fn(val: u8) -> u8 {
82 if val != 0 { some_fn(val - 1) } else { val }
83}
84"#,
85 );
86
87 check_fix(
88 r#"
89fn some_fn() {
90 let whatAWeird_Formatting$0 = 10;
91 another_func(whatAWeird_Formatting);
92}
93"#,
94 r#"
95fn some_fn() {
96 let what_a_weird_formatting = 10;
97 another_func(what_a_weird_formatting);
98}
99"#,
100 );
101 }
102
103 #[test]
104 fn test_uppercase_const_no_diagnostics() {
105 check_no_diagnostics(
106 r#"
107fn foo() {
108 const ANOTHER_ITEM$0: &str = "some_item";
109}
110"#,
111 );
112 }
113
114 #[test]
115 fn test_rename_incorrect_case_struct_method() {
116 check_fix(
117 r#"
118pub struct TestStruct;
119
120impl TestStruct {
121 pub fn SomeFn$0() -> TestStruct {
122 TestStruct
123 }
124}
125"#,
126 r#"
127pub struct TestStruct;
128
129impl TestStruct {
130 pub fn some_fn() -> TestStruct {
131 TestStruct
132 }
133}
134"#,
135 );
136 }
137
138 #[test]
139 fn test_single_incorrect_case_diagnostic_in_function_name_issue_6970() {
140 let input = r#"fn FOO$0() {}"#;
141 let expected = r#"fn foo() {}"#;
142
143 let (analysis, file_position) = fixture::position(input);
144 let diagnostics = analysis
145 .diagnostics(
146 &DiagnosticsConfig::default(),
147 AssistResolveStrategy::All,
148 file_position.file_id,
149 )
150 .unwrap();
151 assert_eq!(diagnostics.len(), 1);
152
153 check_fix(input, expected);
154 }
155}
diff --git a/crates/ide/src/diagnostics/fixes/create_field.rs b/crates/ide/src/diagnostics/fixes/create_field.rs
new file mode 100644
index 000000000..a5f457dce
--- /dev/null
+++ b/crates/ide/src/diagnostics/fixes/create_field.rs
@@ -0,0 +1,156 @@
1use hir::{db::AstDatabase, diagnostics::NoSuchField, HasSource, HirDisplay, Semantics};
2use ide_db::{base_db::FileId, source_change::SourceChange, RootDatabase};
3use syntax::{
4 ast::{self, edit::IndentLevel, make},
5 AstNode,
6};
7use text_edit::TextEdit;
8
9use crate::{
10 diagnostics::{fix, DiagnosticWithFixes},
11 Assist, AssistResolveStrategy,
12};
13impl DiagnosticWithFixes for NoSuchField {
14 fn fixes(
15 &self,
16 sema: &Semantics<RootDatabase>,
17 _resolve: &AssistResolveStrategy,
18 ) -> Option<Vec<Assist>> {
19 let root = sema.db.parse_or_expand(self.file)?;
20 missing_record_expr_field_fixes(
21 &sema,
22 self.file.original_file(sema.db),
23 &self.field.to_node(&root),
24 )
25 }
26}
27
28fn missing_record_expr_field_fixes(
29 sema: &Semantics<RootDatabase>,
30 usage_file_id: FileId,
31 record_expr_field: &ast::RecordExprField,
32) -> Option<Vec<Assist>> {
33 let record_lit = ast::RecordExpr::cast(record_expr_field.syntax().parent()?.parent()?)?;
34 let def_id = sema.resolve_variant(record_lit)?;
35 let module;
36 let def_file_id;
37 let record_fields = match def_id {
38 hir::VariantDef::Struct(s) => {
39 module = s.module(sema.db);
40 let source = s.source(sema.db)?;
41 def_file_id = source.file_id;
42 let fields = source.value.field_list()?;
43 record_field_list(fields)?
44 }
45 hir::VariantDef::Union(u) => {
46 module = u.module(sema.db);
47 let source = u.source(sema.db)?;
48 def_file_id = source.file_id;
49 source.value.record_field_list()?
50 }
51 hir::VariantDef::Variant(e) => {
52 module = e.module(sema.db);
53 let source = e.source(sema.db)?;
54 def_file_id = source.file_id;
55 let fields = source.value.field_list()?;
56 record_field_list(fields)?
57 }
58 };
59 let def_file_id = def_file_id.original_file(sema.db);
60
61 let new_field_type = sema.type_of_expr(&record_expr_field.expr()?)?;
62 if new_field_type.is_unknown() {
63 return None;
64 }
65 let new_field = make::record_field(
66 None,
67 make::name(&record_expr_field.field_name()?.text()),
68 make::ty(&new_field_type.display_source_code(sema.db, module.into()).ok()?),
69 );
70
71 let last_field = record_fields.fields().last()?;
72 let last_field_syntax = last_field.syntax();
73 let indent = IndentLevel::from_node(last_field_syntax);
74
75 let mut new_field = new_field.to_string();
76 if usage_file_id != def_file_id {
77 new_field = format!("pub(crate) {}", new_field);
78 }
79 new_field = format!("\n{}{}", indent, new_field);
80
81 let needs_comma = !last_field_syntax.to_string().ends_with(',');
82 if needs_comma {
83 new_field = format!(",{}", new_field);
84 }
85
86 let source_change = SourceChange::from_text_edit(
87 def_file_id,
88 TextEdit::insert(last_field_syntax.text_range().end(), new_field),
89 );
90
91 return Some(vec![fix(
92 "create_field",
93 "Create field",
94 source_change,
95 record_expr_field.syntax().text_range(),
96 )]);
97
98 fn record_field_list(field_def_list: ast::FieldList) -> Option<ast::RecordFieldList> {
99 match field_def_list {
100 ast::FieldList::RecordFieldList(it) => Some(it),
101 ast::FieldList::TupleFieldList(_) => None,
102 }
103 }
104}
105
106#[cfg(test)]
107mod tests {
108 use crate::diagnostics::tests::check_fix;
109
110 #[test]
111 fn test_add_field_from_usage() {
112 check_fix(
113 r"
114fn main() {
115 Foo { bar: 3, baz$0: false};
116}
117struct Foo {
118 bar: i32
119}
120",
121 r"
122fn main() {
123 Foo { bar: 3, baz: false};
124}
125struct Foo {
126 bar: i32,
127 baz: bool
128}
129",
130 )
131 }
132
133 #[test]
134 fn test_add_field_in_other_file_from_usage() {
135 check_fix(
136 r#"
137//- /main.rs
138mod foo;
139
140fn main() {
141 foo::Foo { bar: 3, $0baz: false};
142}
143//- /foo.rs
144struct Foo {
145 bar: i32
146}
147"#,
148 r#"
149struct Foo {
150 bar: i32,
151 pub(crate) baz: bool
152}
153"#,
154 )
155 }
156}
diff --git a/crates/ide/src/diagnostics/fixes/fill_missing_fields.rs b/crates/ide/src/diagnostics/fixes/fill_missing_fields.rs
new file mode 100644
index 000000000..b5dd64c08
--- /dev/null
+++ b/crates/ide/src/diagnostics/fixes/fill_missing_fields.rs
@@ -0,0 +1,217 @@
1use hir::{db::AstDatabase, diagnostics::MissingFields, Semantics};
2use ide_assists::AssistResolveStrategy;
3use ide_db::{source_change::SourceChange, RootDatabase};
4use syntax::{algo, ast::make, AstNode};
5use text_edit::TextEdit;
6
7use crate::{
8 diagnostics::{fix, fixes::DiagnosticWithFixes},
9 Assist,
10};
11
12impl DiagnosticWithFixes for MissingFields {
13 fn fixes(
14 &self,
15 sema: &Semantics<RootDatabase>,
16 _resolve: &AssistResolveStrategy,
17 ) -> Option<Vec<Assist>> {
18 // Note that although we could add a diagnostics to
19 // fill the missing tuple field, e.g :
20 // `struct A(usize);`
21 // `let a = A { 0: () }`
22 // but it is uncommon usage and it should not be encouraged.
23 if self.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) {
24 return None;
25 }
26
27 let root = sema.db.parse_or_expand(self.file)?;
28 let field_list_parent = self.field_list_parent.to_node(&root);
29 let old_field_list = field_list_parent.record_expr_field_list()?;
30 let new_field_list = old_field_list.clone_for_update();
31 for f in self.missed_fields.iter() {
32 let field =
33 make::record_expr_field(make::name_ref(&f.to_string()), Some(make::expr_unit()))
34 .clone_for_update();
35 new_field_list.add_field(field);
36 }
37
38 let edit = {
39 let mut builder = TextEdit::builder();
40 algo::diff(&old_field_list.syntax(), &new_field_list.syntax())
41 .into_text_edit(&mut builder);
42 builder.finish()
43 };
44 Some(vec![fix(
45 "fill_missing_fields",
46 "Fill struct fields",
47 SourceChange::from_text_edit(self.file.original_file(sema.db), edit),
48 sema.original_range(&field_list_parent.syntax()).range,
49 )])
50 }
51}
52
53#[cfg(test)]
54mod tests {
55 use crate::diagnostics::tests::{check_fix, check_no_diagnostics};
56
57 #[test]
58 fn test_fill_struct_fields_empty() {
59 check_fix(
60 r#"
61struct TestStruct { one: i32, two: i64 }
62
63fn test_fn() {
64 let s = TestStruct {$0};
65}
66"#,
67 r#"
68struct TestStruct { one: i32, two: i64 }
69
70fn test_fn() {
71 let s = TestStruct { one: (), two: () };
72}
73"#,
74 );
75 }
76
77 #[test]
78 fn test_fill_struct_fields_self() {
79 check_fix(
80 r#"
81struct TestStruct { one: i32 }
82
83impl TestStruct {
84 fn test_fn() { let s = Self {$0}; }
85}
86"#,
87 r#"
88struct TestStruct { one: i32 }
89
90impl TestStruct {
91 fn test_fn() { let s = Self { one: () }; }
92}
93"#,
94 );
95 }
96
97 #[test]
98 fn test_fill_struct_fields_enum() {
99 check_fix(
100 r#"
101enum Expr {
102 Bin { lhs: Box<Expr>, rhs: Box<Expr> }
103}
104
105impl Expr {
106 fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
107 Expr::Bin {$0 }
108 }
109}
110"#,
111 r#"
112enum Expr {
113 Bin { lhs: Box<Expr>, rhs: Box<Expr> }
114}
115
116impl Expr {
117 fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
118 Expr::Bin { lhs: (), rhs: () }
119 }
120}
121"#,
122 );
123 }
124
125 #[test]
126 fn test_fill_struct_fields_partial() {
127 check_fix(
128 r#"
129struct TestStruct { one: i32, two: i64 }
130
131fn test_fn() {
132 let s = TestStruct{ two: 2$0 };
133}
134"#,
135 r"
136struct TestStruct { one: i32, two: i64 }
137
138fn test_fn() {
139 let s = TestStruct{ two: 2, one: () };
140}
141",
142 );
143 }
144
145 #[test]
146 fn test_fill_struct_fields_raw_ident() {
147 check_fix(
148 r#"
149struct TestStruct { r#type: u8 }
150
151fn test_fn() {
152 TestStruct { $0 };
153}
154"#,
155 r"
156struct TestStruct { r#type: u8 }
157
158fn test_fn() {
159 TestStruct { r#type: () };
160}
161",
162 );
163 }
164
165 #[test]
166 fn test_fill_struct_fields_no_diagnostic() {
167 check_no_diagnostics(
168 r#"
169struct TestStruct { one: i32, two: i64 }
170
171fn test_fn() {
172 let one = 1;
173 let s = TestStruct{ one, two: 2 };
174}
175 "#,
176 );
177 }
178
179 #[test]
180 fn test_fill_struct_fields_no_diagnostic_on_spread() {
181 check_no_diagnostics(
182 r#"
183struct TestStruct { one: i32, two: i64 }
184
185fn test_fn() {
186 let one = 1;
187 let s = TestStruct{ ..a };
188}
189"#,
190 );
191 }
192
193 #[test]
194 fn test_fill_struct_fields_blank_line() {
195 check_fix(
196 r#"
197struct S { a: (), b: () }
198
199fn f() {
200 S {
201 $0
202 };
203}
204"#,
205 r#"
206struct S { a: (), b: () }
207
208fn f() {
209 S {
210 a: (),
211 b: (),
212 };
213}
214"#,
215 );
216 }
217}
diff --git a/crates/ide/src/diagnostics/fixes/remove_semicolon.rs b/crates/ide/src/diagnostics/fixes/remove_semicolon.rs
new file mode 100644
index 000000000..f1724d479
--- /dev/null
+++ b/crates/ide/src/diagnostics/fixes/remove_semicolon.rs
@@ -0,0 +1,41 @@
1use hir::{db::AstDatabase, diagnostics::RemoveThisSemicolon, Semantics};
2use ide_assists::{Assist, AssistResolveStrategy};
3use ide_db::{source_change::SourceChange, RootDatabase};
4use syntax::{ast, AstNode};
5use text_edit::TextEdit;
6
7use crate::diagnostics::{fix, DiagnosticWithFixes};
8
9impl DiagnosticWithFixes for RemoveThisSemicolon {
10 fn fixes(
11 &self,
12 sema: &Semantics<RootDatabase>,
13 _resolve: &AssistResolveStrategy,
14 ) -> Option<Vec<Assist>> {
15 let root = sema.db.parse_or_expand(self.file)?;
16
17 let semicolon = self
18 .expr
19 .to_node(&root)
20 .syntax()
21 .parent()
22 .and_then(ast::ExprStmt::cast)
23 .and_then(|expr| expr.semicolon_token())?
24 .text_range();
25
26 let edit = TextEdit::delete(semicolon);
27 let source_change = SourceChange::from_text_edit(self.file.original_file(sema.db), edit);
28
29 Some(vec![fix("remove_semicolon", "Remove this semicolon", source_change, semicolon)])
30 }
31}
32
33#[cfg(test)]
34mod tests {
35 use crate::diagnostics::tests::check_fix;
36
37 #[test]
38 fn remove_semicolon() {
39 check_fix(r#"fn f() -> i32 { 92$0; }"#, r#"fn f() -> i32 { 92 }"#);
40 }
41}
diff --git a/crates/ide/src/diagnostics/fixes/replace_with_find_map.rs b/crates/ide/src/diagnostics/fixes/replace_with_find_map.rs
new file mode 100644
index 000000000..444bf563b
--- /dev/null
+++ b/crates/ide/src/diagnostics/fixes/replace_with_find_map.rs
@@ -0,0 +1,84 @@
1use hir::{db::AstDatabase, diagnostics::ReplaceFilterMapNextWithFindMap, Semantics};
2use ide_assists::{Assist, AssistResolveStrategy};
3use ide_db::{source_change::SourceChange, RootDatabase};
4use syntax::{
5 ast::{self, ArgListOwner},
6 AstNode, TextRange,
7};
8use text_edit::TextEdit;
9
10use crate::diagnostics::{fix, DiagnosticWithFixes};
11
12impl DiagnosticWithFixes for ReplaceFilterMapNextWithFindMap {
13 fn fixes(
14 &self,
15 sema: &Semantics<RootDatabase>,
16 _resolve: &AssistResolveStrategy,
17 ) -> Option<Vec<Assist>> {
18 let root = sema.db.parse_or_expand(self.file)?;
19 let next_expr = self.next_expr.to_node(&root);
20 let next_call = ast::MethodCallExpr::cast(next_expr.syntax().clone())?;
21
22 let filter_map_call = ast::MethodCallExpr::cast(next_call.receiver()?.syntax().clone())?;
23 let filter_map_name_range = filter_map_call.name_ref()?.ident_token()?.text_range();
24 let filter_map_args = filter_map_call.arg_list()?;
25
26 let range_to_replace =
27 TextRange::new(filter_map_name_range.start(), next_expr.syntax().text_range().end());
28 let replacement = format!("find_map{}", filter_map_args.syntax().text());
29 let trigger_range = next_expr.syntax().text_range();
30
31 let edit = TextEdit::replace(range_to_replace, replacement);
32
33 let source_change = SourceChange::from_text_edit(self.file.original_file(sema.db), edit);
34
35 Some(vec![fix(
36 "replace_with_find_map",
37 "Replace filter_map(..).next() with find_map()",
38 source_change,
39 trigger_range,
40 )])
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use crate::diagnostics::tests::check_fix;
47
48 #[test]
49 fn replace_with_wind_map() {
50 check_fix(
51 r#"
52//- /main.rs crate:main deps:core
53use core::iter::Iterator;
54use core::option::Option::{self, Some, None};
55fn foo() {
56 let m = [1, 2, 3].iter().$0filter_map(|x| if *x == 2 { Some (4) } else { None }).next();
57}
58//- /core/lib.rs crate:core
59pub mod option {
60 pub enum Option<T> { Some(T), None }
61}
62pub mod iter {
63 pub trait Iterator {
64 type Item;
65 fn filter_map<B, F>(self, f: F) -> FilterMap where F: FnMut(Self::Item) -> Option<B> { FilterMap }
66 fn next(&mut self) -> Option<Self::Item>;
67 }
68 pub struct FilterMap {}
69 impl Iterator for FilterMap {
70 type Item = i32;
71 fn next(&mut self) -> i32 { 7 }
72 }
73}
74"#,
75 r#"
76use core::iter::Iterator;
77use core::option::Option::{self, Some, None};
78fn foo() {
79 let m = [1, 2, 3].iter().find_map(|x| if *x == 2 { Some (4) } else { None });
80}
81"#,
82 )
83 }
84}
diff --git a/crates/ide/src/diagnostics/fixes/unresolved_module.rs b/crates/ide/src/diagnostics/fixes/unresolved_module.rs
new file mode 100644
index 000000000..b3d0283bb
--- /dev/null
+++ b/crates/ide/src/diagnostics/fixes/unresolved_module.rs
@@ -0,0 +1,89 @@
1use hir::{db::AstDatabase, diagnostics::UnresolvedModule, Semantics};
2use ide_assists::{Assist, AssistResolveStrategy};
3use ide_db::{base_db::AnchoredPathBuf, source_change::FileSystemEdit, RootDatabase};
4use syntax::AstNode;
5
6use crate::diagnostics::{fix, DiagnosticWithFixes};
7
8impl DiagnosticWithFixes for UnresolvedModule {
9 fn fixes(
10 &self,
11 sema: &Semantics<RootDatabase>,
12 _resolve: &AssistResolveStrategy,
13 ) -> Option<Vec<Assist>> {
14 let root = sema.db.parse_or_expand(self.file)?;
15 let unresolved_module = self.decl.to_node(&root);
16 Some(vec![fix(
17 "create_module",
18 "Create module",
19 FileSystemEdit::CreateFile {
20 dst: AnchoredPathBuf {
21 anchor: self.file.original_file(sema.db),
22 path: self.candidate.clone(),
23 },
24 initial_contents: "".to_string(),
25 }
26 .into(),
27 unresolved_module.syntax().text_range(),
28 )])
29 }
30}
31
32#[cfg(test)]
33mod tests {
34 use expect_test::expect;
35
36 use crate::diagnostics::tests::check_expect;
37
38 #[test]
39 fn test_unresolved_module_diagnostic() {
40 check_expect(
41 r#"mod foo;"#,
42 expect![[r#"
43 [
44 Diagnostic {
45 message: "unresolved module",
46 range: 0..8,
47 severity: Error,
48 fixes: Some(
49 [
50 Assist {
51 id: AssistId(
52 "create_module",
53 QuickFix,
54 ),
55 label: "Create module",
56 group: None,
57 target: 0..8,
58 source_change: Some(
59 SourceChange {
60 source_file_edits: {},
61 file_system_edits: [
62 CreateFile {
63 dst: AnchoredPathBuf {
64 anchor: FileId(
65 0,
66 ),
67 path: "foo.rs",
68 },
69 initial_contents: "",
70 },
71 ],
72 is_snippet: false,
73 },
74 ),
75 },
76 ],
77 ),
78 unused: false,
79 code: Some(
80 DiagnosticCode(
81 "unresolved-module",
82 ),
83 ),
84 },
85 ]
86 "#]],
87 );
88 }
89}
diff --git a/crates/ide/src/diagnostics/fixes/wrap_tail_expr.rs b/crates/ide/src/diagnostics/fixes/wrap_tail_expr.rs
new file mode 100644
index 000000000..715a403b9
--- /dev/null
+++ b/crates/ide/src/diagnostics/fixes/wrap_tail_expr.rs
@@ -0,0 +1,211 @@
1use hir::{db::AstDatabase, diagnostics::MissingOkOrSomeInTailExpr, Semantics};
2use ide_assists::{Assist, AssistResolveStrategy};
3use ide_db::{source_change::SourceChange, RootDatabase};
4use syntax::AstNode;
5use text_edit::TextEdit;
6
7use crate::diagnostics::{fix, DiagnosticWithFixes};
8
9impl DiagnosticWithFixes for MissingOkOrSomeInTailExpr {
10 fn fixes(
11 &self,
12 sema: &Semantics<RootDatabase>,
13 _resolve: &AssistResolveStrategy,
14 ) -> Option<Vec<Assist>> {
15 let root = sema.db.parse_or_expand(self.file)?;
16 let tail_expr = self.expr.to_node(&root);
17 let tail_expr_range = tail_expr.syntax().text_range();
18 let replacement = format!("{}({})", self.required, tail_expr.syntax());
19 let edit = TextEdit::replace(tail_expr_range, replacement);
20 let source_change = SourceChange::from_text_edit(self.file.original_file(sema.db), edit);
21 let name = if self.required == "Ok" { "Wrap with Ok" } else { "Wrap with Some" };
22 Some(vec![fix("wrap_tail_expr", name, source_change, tail_expr_range)])
23 }
24}
25
26#[cfg(test)]
27mod tests {
28 use crate::diagnostics::tests::{check_fix, check_no_diagnostics};
29
30 #[test]
31 fn test_wrap_return_type_option() {
32 check_fix(
33 r#"
34//- /main.rs crate:main deps:core
35use core::option::Option::{self, Some, None};
36
37fn div(x: i32, y: i32) -> Option<i32> {
38 if y == 0 {
39 return None;
40 }
41 x / y$0
42}
43//- /core/lib.rs crate:core
44pub mod result {
45 pub enum Result<T, E> { Ok(T), Err(E) }
46}
47pub mod option {
48 pub enum Option<T> { Some(T), None }
49}
50"#,
51 r#"
52use core::option::Option::{self, Some, None};
53
54fn div(x: i32, y: i32) -> Option<i32> {
55 if y == 0 {
56 return None;
57 }
58 Some(x / y)
59}
60"#,
61 );
62 }
63
64 #[test]
65 fn test_wrap_return_type() {
66 check_fix(
67 r#"
68//- /main.rs crate:main deps:core
69use core::result::Result::{self, Ok, Err};
70
71fn div(x: i32, y: i32) -> Result<i32, ()> {
72 if y == 0 {
73 return Err(());
74 }
75 x / y$0
76}
77//- /core/lib.rs crate:core
78pub mod result {
79 pub enum Result<T, E> { Ok(T), Err(E) }
80}
81pub mod option {
82 pub enum Option<T> { Some(T), None }
83}
84"#,
85 r#"
86use core::result::Result::{self, Ok, Err};
87
88fn div(x: i32, y: i32) -> Result<i32, ()> {
89 if y == 0 {
90 return Err(());
91 }
92 Ok(x / y)
93}
94"#,
95 );
96 }
97
98 #[test]
99 fn test_wrap_return_type_handles_generic_functions() {
100 check_fix(
101 r#"
102//- /main.rs crate:main deps:core
103use core::result::Result::{self, Ok, Err};
104
105fn div<T>(x: T) -> Result<T, i32> {
106 if x == 0 {
107 return Err(7);
108 }
109 $0x
110}
111//- /core/lib.rs crate:core
112pub mod result {
113 pub enum Result<T, E> { Ok(T), Err(E) }
114}
115pub mod option {
116 pub enum Option<T> { Some(T), None }
117}
118"#,
119 r#"
120use core::result::Result::{self, Ok, Err};
121
122fn div<T>(x: T) -> Result<T, i32> {
123 if x == 0 {
124 return Err(7);
125 }
126 Ok(x)
127}
128"#,
129 );
130 }
131
132 #[test]
133 fn test_wrap_return_type_handles_type_aliases() {
134 check_fix(
135 r#"
136//- /main.rs crate:main deps:core
137use core::result::Result::{self, Ok, Err};
138
139type MyResult<T> = Result<T, ()>;
140
141fn div(x: i32, y: i32) -> MyResult<i32> {
142 if y == 0 {
143 return Err(());
144 }
145 x $0/ y
146}
147//- /core/lib.rs crate:core
148pub mod result {
149 pub enum Result<T, E> { Ok(T), Err(E) }
150}
151pub mod option {
152 pub enum Option<T> { Some(T), None }
153}
154"#,
155 r#"
156use core::result::Result::{self, Ok, Err};
157
158type MyResult<T> = Result<T, ()>;
159
160fn div(x: i32, y: i32) -> MyResult<i32> {
161 if y == 0 {
162 return Err(());
163 }
164 Ok(x / y)
165}
166"#,
167 );
168 }
169
170 #[test]
171 fn test_wrap_return_type_not_applicable_when_expr_type_does_not_match_ok_type() {
172 check_no_diagnostics(
173 r#"
174//- /main.rs crate:main deps:core
175use core::result::Result::{self, Ok, Err};
176
177fn foo() -> Result<(), i32> { 0 }
178
179//- /core/lib.rs crate:core
180pub mod result {
181 pub enum Result<T, E> { Ok(T), Err(E) }
182}
183pub mod option {
184 pub enum Option<T> { Some(T), None }
185}
186"#,
187 );
188 }
189
190 #[test]
191 fn test_wrap_return_type_not_applicable_when_return_type_is_not_result_or_option() {
192 check_no_diagnostics(
193 r#"
194//- /main.rs crate:main deps:core
195use core::result::Result::{self, Ok, Err};
196
197enum SomeOtherEnum { Ok(i32), Err(String) }
198
199fn foo() -> SomeOtherEnum { 0 }
200
201//- /core/lib.rs crate:core
202pub mod result {
203 pub enum Result<T, E> { Ok(T), Err(E) }
204}
205pub mod option {
206 pub enum Option<T> { Some(T), None }
207}
208"#,
209 );
210 }
211}
diff --git a/crates/ide/src/diagnostics/unlinked_file.rs b/crates/ide/src/diagnostics/unlinked_file.rs
index 93fd25dea..51fe0f360 100644
--- a/crates/ide/src/diagnostics/unlinked_file.rs
+++ b/crates/ide/src/diagnostics/unlinked_file.rs
@@ -18,7 +18,7 @@ use syntax::{
18use text_edit::TextEdit; 18use text_edit::TextEdit;
19 19
20use crate::{ 20use crate::{
21 diagnostics::{fix, fixes::DiagnosticWithFix}, 21 diagnostics::{fix, fixes::DiagnosticWithFixes},
22 Assist, 22 Assist,
23}; 23};
24 24
@@ -50,13 +50,13 @@ impl Diagnostic for UnlinkedFile {
50 } 50 }
51} 51}
52 52
53impl DiagnosticWithFix for UnlinkedFile { 53impl DiagnosticWithFixes for UnlinkedFile {
54 fn fix( 54 fn fixes(
55 &self, 55 &self,
56 sema: &hir::Semantics<RootDatabase>, 56 sema: &hir::Semantics<RootDatabase>,
57 _resolve: &AssistResolveStrategy, 57 _resolve: &AssistResolveStrategy,
58 ) -> Option<Assist> { 58 ) -> Option<Vec<Assist>> {
59 // If there's an existing module that could add a `mod` item to include the unlinked file, 59 // If there's an existing module that could add `mod` or `pub mod` items to include the unlinked file,
60 // suggest that as a fix. 60 // suggest that as a fix.
61 61
62 let source_root = sema.db.source_root(sema.db.file_source_root(self.file_id)); 62 let source_root = sema.db.source_root(sema.db.file_source_root(self.file_id));
@@ -90,7 +90,7 @@ impl DiagnosticWithFix for UnlinkedFile {
90 } 90 }
91 91
92 if module.origin.file_id() == Some(*parent_id) { 92 if module.origin.file_id() == Some(*parent_id) {
93 return make_fix(sema.db, *parent_id, module_name, self.file_id); 93 return make_fixes(sema.db, *parent_id, module_name, self.file_id);
94 } 94 }
95 } 95 }
96 } 96 }
@@ -101,20 +101,23 @@ impl DiagnosticWithFix for UnlinkedFile {
101 } 101 }
102} 102}
103 103
104fn make_fix( 104fn make_fixes(
105 db: &RootDatabase, 105 db: &RootDatabase,
106 parent_file_id: FileId, 106 parent_file_id: FileId,
107 new_mod_name: &str, 107 new_mod_name: &str,
108 added_file_id: FileId, 108 added_file_id: FileId,
109) -> Option<Assist> { 109) -> Option<Vec<Assist>> {
110 fn is_outline_mod(item: &ast::Item) -> bool { 110 fn is_outline_mod(item: &ast::Item) -> bool {
111 matches!(item, ast::Item::Module(m) if m.item_list().is_none()) 111 matches!(item, ast::Item::Module(m) if m.item_list().is_none())
112 } 112 }
113 113
114 let mod_decl = format!("mod {};", new_mod_name); 114 let mod_decl = format!("mod {};", new_mod_name);
115 let pub_mod_decl = format!("pub mod {};", new_mod_name);
116
115 let ast: ast::SourceFile = db.parse(parent_file_id).tree(); 117 let ast: ast::SourceFile = db.parse(parent_file_id).tree();
116 118
117 let mut builder = TextEdit::builder(); 119 let mut mod_decl_builder = TextEdit::builder();
120 let mut pub_mod_decl_builder = TextEdit::builder();
118 121
119 // If there's an existing `mod m;` statement matching the new one, don't emit a fix (it's 122 // If there's an existing `mod m;` statement matching the new one, don't emit a fix (it's
120 // probably `#[cfg]`d out). 123 // probably `#[cfg]`d out).
@@ -138,30 +141,43 @@ fn make_fix(
138 { 141 {
139 Some(last) => { 142 Some(last) => {
140 cov_mark::hit!(unlinked_file_append_to_existing_mods); 143 cov_mark::hit!(unlinked_file_append_to_existing_mods);
141 builder.insert(last.syntax().text_range().end(), format!("\n{}", mod_decl)); 144 let offset = last.syntax().text_range().end();
145 mod_decl_builder.insert(offset, format!("\n{}", mod_decl));
146 pub_mod_decl_builder.insert(offset, format!("\n{}", pub_mod_decl));
142 } 147 }
143 None => { 148 None => {
144 // Prepend before the first item in the file. 149 // Prepend before the first item in the file.
145 match ast.items().next() { 150 match ast.items().next() {
146 Some(item) => { 151 Some(item) => {
147 cov_mark::hit!(unlinked_file_prepend_before_first_item); 152 cov_mark::hit!(unlinked_file_prepend_before_first_item);
148 builder.insert(item.syntax().text_range().start(), format!("{}\n\n", mod_decl)); 153 let offset = item.syntax().text_range().start();
154 mod_decl_builder.insert(offset, format!("{}\n\n", mod_decl));
155 pub_mod_decl_builder.insert(offset, format!("{}\n\n", pub_mod_decl));
149 } 156 }
150 None => { 157 None => {
151 // No items in the file, so just append at the end. 158 // No items in the file, so just append at the end.
152 cov_mark::hit!(unlinked_file_empty_file); 159 cov_mark::hit!(unlinked_file_empty_file);
153 builder.insert(ast.syntax().text_range().end(), format!("{}\n", mod_decl)); 160 let offset = ast.syntax().text_range().end();
161 mod_decl_builder.insert(offset, format!("{}\n", mod_decl));
162 pub_mod_decl_builder.insert(offset, format!("{}\n", pub_mod_decl));
154 } 163 }
155 } 164 }
156 } 165 }
157 } 166 }
158 167
159 let edit = builder.finish();
160 let trigger_range = db.parse(added_file_id).tree().syntax().text_range(); 168 let trigger_range = db.parse(added_file_id).tree().syntax().text_range();
161 Some(fix( 169 Some(vec![
162 "add_mod_declaration", 170 fix(
163 &format!("Insert `{}`", mod_decl), 171 "add_mod_declaration",
164 SourceChange::from_text_edit(parent_file_id, edit), 172 &format!("Insert `{}`", mod_decl),
165 trigger_range, 173 SourceChange::from_text_edit(parent_file_id, mod_decl_builder.finish()),
166 )) 174 trigger_range,
175 ),
176 fix(
177 "add_pub_mod_declaration",
178 &format!("Insert `{}`", pub_mod_decl),
179 SourceChange::from_text_edit(parent_file_id, pub_mod_decl_builder.finish()),
180 trigger_range,
181 ),
182 ])
167} 183}
diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs
index cb5a8e19a..320694a17 100644
--- a/crates/ide/src/doc_links.rs
+++ b/crates/ide/src/doc_links.rs
@@ -29,7 +29,8 @@ pub(crate) type DocumentationLink = String;
29/// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs) 29/// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs)
30pub(crate) fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) -> String { 30pub(crate) fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) -> String {
31 let mut cb = broken_link_clone_cb; 31 let mut cb = broken_link_clone_cb;
32 let doc = Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(&mut cb)); 32 let doc =
33 Parser::new_with_broken_link_callback(markdown, Options::ENABLE_TASKLISTS, Some(&mut cb));
33 34
34 let doc = map_links(doc, |target, title: &str| { 35 let doc = map_links(doc, |target, title: &str| {
35 // This check is imperfect, there's some overlap between valid intra-doc links 36 // This check is imperfect, there's some overlap between valid intra-doc links
@@ -64,8 +65,7 @@ pub(crate) fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Defi
64pub(crate) fn remove_links(markdown: &str) -> String { 65pub(crate) fn remove_links(markdown: &str) -> String {
65 let mut drop_link = false; 66 let mut drop_link = false;
66 67
67 let mut opts = Options::empty(); 68 let opts = Options::ENABLE_TASKLISTS | Options::ENABLE_FOOTNOTES;
68 opts.insert(Options::ENABLE_FOOTNOTES);
69 69
70 let mut cb = |_: BrokenLink| { 70 let mut cb = |_: BrokenLink| {
71 let empty = InlineStr::try_from("").unwrap(); 71 let empty = InlineStr::try_from("").unwrap();
@@ -123,7 +123,7 @@ pub(crate) fn extract_definitions_from_markdown(
123) -> Vec<(TextRange, String, Option<hir::Namespace>)> { 123) -> Vec<(TextRange, String, Option<hir::Namespace>)> {
124 Parser::new_with_broken_link_callback( 124 Parser::new_with_broken_link_callback(
125 markdown, 125 markdown,
126 Options::empty(), 126 Options::ENABLE_TASKLISTS,
127 Some(&mut broken_link_clone_cb), 127 Some(&mut broken_link_clone_cb),
128 ) 128 )
129 .into_offset_iter() 129 .into_offset_iter()
diff --git a/crates/ide/src/join_lines.rs b/crates/ide/src/join_lines.rs
index 61dcbb399..c67ccd1a9 100644
--- a/crates/ide/src/join_lines.rs
+++ b/crates/ide/src/join_lines.rs
@@ -4,7 +4,7 @@ use ide_assists::utils::extract_trivial_expression;
4use itertools::Itertools; 4use itertools::Itertools;
5use syntax::{ 5use syntax::{
6 algo::non_trivia_sibling, 6 algo::non_trivia_sibling,
7 ast::{self, AstNode, AstToken}, 7 ast::{self, AstNode, AstToken, IsString},
8 Direction, NodeOrToken, SourceFile, 8 Direction, NodeOrToken, SourceFile,
9 SyntaxKind::{self, USE_TREE, WHITESPACE}, 9 SyntaxKind::{self, USE_TREE, WHITESPACE},
10 SyntaxNode, SyntaxToken, TextRange, TextSize, T, 10 SyntaxNode, SyntaxToken, TextRange, TextSize, T,
diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs
index db08547d1..f4b90db3a 100644
--- a/crates/ide/src/lib.rs
+++ b/crates/ide/src/lib.rs
@@ -565,7 +565,7 @@ impl Analysis {
565 let diagnostic_assists = if include_fixes { 565 let diagnostic_assists = if include_fixes {
566 diagnostics::diagnostics(db, diagnostics_config, &resolve, frange.file_id) 566 diagnostics::diagnostics(db, diagnostics_config, &resolve, frange.file_id)
567 .into_iter() 567 .into_iter()
568 .filter_map(|it| it.fix) 568 .flat_map(|it| it.fixes.unwrap_or_default())
569 .filter(|it| it.target.intersect(frange.range).is_some()) 569 .filter(|it| it.target.intersect(frange.range).is_some())
570 .collect() 570 .collect()
571 } else { 571 } else {
diff --git a/crates/ide/src/matching_brace.rs b/crates/ide/src/matching_brace.rs
index 261dcc255..011c8cc55 100644
--- a/crates/ide/src/matching_brace.rs
+++ b/crates/ide/src/matching_brace.rs
@@ -19,14 +19,10 @@ use syntax::{
19pub(crate) fn matching_brace(file: &SourceFile, offset: TextSize) -> Option<TextSize> { 19pub(crate) fn matching_brace(file: &SourceFile, offset: TextSize) -> Option<TextSize> {
20 const BRACES: &[SyntaxKind] = 20 const BRACES: &[SyntaxKind] =
21 &[T!['{'], T!['}'], T!['['], T![']'], T!['('], T![')'], T![<], T![>], T![|], T![|]]; 21 &[T!['{'], T!['}'], T!['['], T![']'], T!['('], T![')'], T![<], T![>], T![|], T![|]];
22 let (brace_token, brace_idx) = file 22 let (brace_token, brace_idx) = file.syntax().token_at_offset(offset).find_map(|node| {
23 .syntax() 23 let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
24 .token_at_offset(offset) 24 Some((node, idx))
25 .filter_map(|node| { 25 })?;
26 let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
27 Some((node, idx))
28 })
29 .next()?;
30 let parent = brace_token.parent()?; 26 let parent = brace_token.parent()?;
31 if brace_token.kind() == T![|] && !ast::ParamList::can_cast(parent.kind()) { 27 if brace_token.kind() == T![|] && !ast::ParamList::can_cast(parent.kind()) {
32 cov_mark::hit!(pipes_not_braces); 28 cov_mark::hit!(pipes_not_braces);
diff --git a/crates/ide/src/parent_module.rs b/crates/ide/src/parent_module.rs
index 99365c8a7..9b1f48044 100644
--- a/crates/ide/src/parent_module.rs
+++ b/crates/ide/src/parent_module.rs
@@ -1,6 +1,8 @@
1use hir::Semantics; 1use hir::Semantics;
2use ide_db::base_db::{CrateId, FileId, FilePosition}; 2use ide_db::{
3use ide_db::RootDatabase; 3 base_db::{CrateId, FileId, FilePosition},
4 RootDatabase,
5};
4use itertools::Itertools; 6use itertools::Itertools;
5use syntax::{ 7use syntax::{
6 algo::find_node_at_offset, 8 algo::find_node_at_offset,
diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs
index 2bf953305..01fe3a1a1 100644
--- a/crates/ide/src/references/rename.rs
+++ b/crates/ide/src/references/rename.rs
@@ -55,12 +55,14 @@ pub(crate) fn prepare_rename(
55 match def { 55 match def {
56 Definition::SelfType(_) => bail!("Cannot rename `Self`"), 56 Definition::SelfType(_) => bail!("Cannot rename `Self`"),
57 Definition::ModuleDef(ModuleDef::BuiltinType(_)) => bail!("Cannot rename builtin type"), 57 Definition::ModuleDef(ModuleDef::BuiltinType(_)) => bail!("Cannot rename builtin type"),
58 _ => {} 58 Definition::ModuleDef(ModuleDef::Module(_)) => (),
59 _ => {
60 let nav = def
61 .try_to_nav(sema.db)
62 .ok_or_else(|| format_err!("No references found at position"))?;
63 nav.focus_range.ok_or_else(|| format_err!("No identifier available to rename"))?;
64 }
59 }; 65 };
60 let nav =
61 def.try_to_nav(sema.db).ok_or_else(|| format_err!("No references found at position"))?;
62 nav.focus_range.ok_or_else(|| format_err!("No identifier available to rename"))?;
63
64 let name_like = sema 66 let name_like = sema
65 .find_node_at_offset_with_descend(&syntax, position.offset) 67 .find_node_at_offset_with_descend(&syntax, position.offset)
66 .ok_or_else(|| format_err!("No references found at position"))?; 68 .ok_or_else(|| format_err!("No references found at position"))?;
diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs
index b586dcc17..baed8e217 100644
--- a/crates/ide/src/syntax_highlighting/highlight.rs
+++ b/crates/ide/src/syntax_highlighting/highlight.rs
@@ -227,8 +227,8 @@ pub(super) fn element(
227 k if k.is_keyword() => { 227 k if k.is_keyword() => {
228 let h = Highlight::new(HlTag::Keyword); 228 let h = Highlight::new(HlTag::Keyword);
229 match k { 229 match k {
230 T![await] 230 T![await] => h | HlMod::Async | HlMod::ControlFlow,
231 | T![break] 231 T![break]
232 | T![continue] 232 | T![continue]
233 | T![else] 233 | T![else]
234 | T![if] 234 | T![if]
@@ -255,6 +255,7 @@ pub(super) fn element(
255 }) 255 })
256 .map(|modifier| h | modifier) 256 .map(|modifier| h | modifier)
257 .unwrap_or(h), 257 .unwrap_or(h),
258 T![async] => h | HlMod::Async,
258 _ => h, 259 _ => h,
259 } 260 }
260 } 261 }
@@ -310,6 +311,9 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
310 if func.is_unsafe(db) { 311 if func.is_unsafe(db) {
311 h |= HlMod::Unsafe; 312 h |= HlMod::Unsafe;
312 } 313 }
314 if func.is_async(db) {
315 h |= HlMod::Async;
316 }
313 return h; 317 return h;
314 } 318 }
315 hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HlTag::Symbol(SymbolKind::Struct), 319 hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HlTag::Symbol(SymbolKind::Struct),
@@ -409,6 +413,9 @@ fn highlight_method_call(
409 if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) { 413 if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) {
410 h |= HlMod::Unsafe; 414 h |= HlMod::Unsafe;
411 } 415 }
416 if func.is_async(sema.db) {
417 h |= HlMod::Async;
418 }
412 if func.as_assoc_item(sema.db).and_then(|it| it.containing_trait(sema.db)).is_some() { 419 if func.as_assoc_item(sema.db).and_then(|it| it.containing_trait(sema.db)).is_some() {
413 h |= HlMod::Trait 420 h |= HlMod::Trait
414 } 421 }
diff --git a/crates/ide/src/syntax_highlighting/inject.rs b/crates/ide/src/syntax_highlighting/inject.rs
index bc221d599..4269d339e 100644
--- a/crates/ide/src/syntax_highlighting/inject.rs
+++ b/crates/ide/src/syntax_highlighting/inject.rs
@@ -6,7 +6,7 @@ use either::Either;
6use hir::{InFile, Semantics}; 6use hir::{InFile, Semantics};
7use ide_db::{call_info::ActiveParameter, helpers::rust_doc::is_rust_fence, SymbolKind}; 7use ide_db::{call_info::ActiveParameter, helpers::rust_doc::is_rust_fence, SymbolKind};
8use syntax::{ 8use syntax::{
9 ast::{self, AstNode}, 9 ast::{self, AstNode, IsString},
10 AstToken, NodeOrToken, SyntaxNode, SyntaxToken, TextRange, TextSize, 10 AstToken, NodeOrToken, SyntaxNode, SyntaxToken, TextRange, TextSize,
11}; 11};
12 12
diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs
index a304b3250..f4a2e7506 100644
--- a/crates/ide/src/syntax_highlighting/tags.rs
+++ b/crates/ide/src/syntax_highlighting/tags.rs
@@ -65,6 +65,8 @@ pub enum HlMod {
65 Static, 65 Static,
66 /// Used for items in traits and trait impls. 66 /// Used for items in traits and trait impls.
67 Trait, 67 Trait,
68 /// Used with keywords like `async` and `await`.
69 Async,
68 // Keep this last! 70 // Keep this last!
69 /// Used for unsafe functions, mutable statics, union accesses and unsafe operations. 71 /// Used for unsafe functions, mutable statics, union accesses and unsafe operations.
70 Unsafe, 72 Unsafe,
@@ -186,6 +188,7 @@ impl HlMod {
186 HlMod::Mutable, 188 HlMod::Mutable,
187 HlMod::Static, 189 HlMod::Static,
188 HlMod::Trait, 190 HlMod::Trait,
191 HlMod::Async,
189 HlMod::Unsafe, 192 HlMod::Unsafe,
190 ]; 193 ];
191 194
@@ -203,6 +206,7 @@ impl HlMod {
203 HlMod::Mutable => "mutable", 206 HlMod::Mutable => "mutable",
204 HlMod::Static => "static", 207 HlMod::Static => "static",
205 HlMod::Trait => "trait", 208 HlMod::Trait => "trait",
209 HlMod::Async => "async",
206 HlMod::Unsafe => "unsafe", 210 HlMod::Unsafe => "unsafe",
207 } 211 }
208 } 212 }
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html
index 8d83ba206..921a956e6 100644
--- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html
+++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html
@@ -37,13 +37,25 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
37 37
38.unresolved_reference { color: #FC5555; text-decoration: wavy underline; } 38.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
39</style> 39</style>
40<pre><code><span class="comment documentation">/// ```</span> 40<pre><code><span class="comment documentation">//! This is a module to test doc injection.</span>
41<span class="comment documentation">//! ```</span>
42<span class="comment documentation">//! </span><span class="keyword injected">fn</span><span class="none injected"> </span><span class="function declaration injected">test</span><span class="parenthesis injected">(</span><span class="parenthesis injected">)</span><span class="none injected"> </span><span class="brace injected">{</span><span class="brace injected">}</span>
43<span class="comment documentation">//! ```</span>
44
45<span class="comment documentation">/// ```</span>
41<span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="punctuation injected">_</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="string_literal injected">"early doctests should not go boom"</span><span class="semicolon injected">;</span> 46<span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="punctuation injected">_</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="string_literal injected">"early doctests should not go boom"</span><span class="semicolon injected">;</span>
42<span class="comment documentation">/// ```</span> 47<span class="comment documentation">/// ```</span>
43<span class="keyword">struct</span> <span class="struct declaration">Foo</span> <span class="brace">{</span> 48<span class="keyword">struct</span> <span class="struct declaration">Foo</span> <span class="brace">{</span>
44 <span class="field declaration">bar</span><span class="colon">:</span> <span class="builtin_type">bool</span><span class="comma">,</span> 49 <span class="field declaration">bar</span><span class="colon">:</span> <span class="builtin_type">bool</span><span class="comma">,</span>
45<span class="brace">}</span> 50<span class="brace">}</span>
46 51
52<span class="comment documentation">/// This is an impl with a code block.</span>
53<span class="comment documentation">///</span>
54<span class="comment documentation">/// ```</span>
55<span class="comment documentation">/// </span><span class="keyword injected">fn</span><span class="none injected"> </span><span class="function declaration injected">foo</span><span class="parenthesis injected">(</span><span class="parenthesis injected">)</span><span class="none injected"> </span><span class="brace injected">{</span>
56<span class="comment documentation">///</span>
57<span class="comment documentation">/// </span><span class="brace injected">}</span>
58<span class="comment documentation">/// ```</span>
47<span class="keyword">impl</span> <span class="struct">Foo</span> <span class="brace">{</span> 59<span class="keyword">impl</span> <span class="struct">Foo</span> <span class="brace">{</span>
48 <span class="comment documentation">/// ```</span> 60 <span class="comment documentation">/// ```</span>
49 <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="punctuation injected">_</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="string_literal injected">"Call me</span> 61 <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="punctuation injected">_</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="string_literal injected">"Call me</span>
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html
index df4192194..0d325f3f3 100644
--- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html
+++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html
@@ -66,11 +66,11 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
66 <span class="keyword">pub</span> <span class="field declaration">y</span><span class="colon">:</span> <span class="builtin_type">i32</span><span class="comma">,</span> 66 <span class="keyword">pub</span> <span class="field declaration">y</span><span class="colon">:</span> <span class="builtin_type">i32</span><span class="comma">,</span>
67<span class="brace">}</span> 67<span class="brace">}</span>
68 68
69<span class="keyword">trait</span> <span class="trait declaration">Bar</span> <span class="brace">{</span> 69<span class="keyword">trait</span> <span class="trait declaration">Bar</span> <span class="keyword">where</span> <span class="type_param">Self</span><span class="colon">:</span> <span class="brace">{</span>
70 <span class="keyword">fn</span> <span class="function associated declaration trait">bar</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="operator">-&gt;</span> <span class="builtin_type">i32</span><span class="semicolon">;</span> 70 <span class="keyword">fn</span> <span class="function associated declaration trait">bar</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="operator">-&gt;</span> <span class="builtin_type">i32</span><span class="semicolon">;</span>
71<span class="brace">}</span> 71<span class="brace">}</span>
72 72
73<span class="keyword">impl</span> <span class="trait">Bar</span> <span class="keyword">for</span> <span class="struct">Foo</span> <span class="brace">{</span> 73<span class="keyword">impl</span> <span class="trait">Bar</span> <span class="keyword">for</span> <span class="struct">Foo</span> <span class="keyword">where</span> <span class="self_type">Self</span><span class="colon">:</span> <span class="brace">{</span>
74 <span class="keyword">fn</span> <span class="function associated declaration trait">bar</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="operator">-&gt;</span> <span class="builtin_type">i32</span> <span class="brace">{</span> 74 <span class="keyword">fn</span> <span class="function associated declaration trait">bar</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="operator">-&gt;</span> <span class="builtin_type">i32</span> <span class="brace">{</span>
75 <span class="self_keyword">self</span><span class="operator">.</span><span class="field">x</span> 75 <span class="self_keyword">self</span><span class="operator">.</span><span class="field">x</span>
76 <span class="brace">}</span> 76 <span class="brace">}</span>
@@ -234,4 +234,15 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
234 <span class="variable declaration">Nope</span> <span class="operator">=&gt;</span> <span class="variable">Nope</span><span class="comma">,</span> 234 <span class="variable declaration">Nope</span> <span class="operator">=&gt;</span> <span class="variable">Nope</span><span class="comma">,</span>
235 <span class="brace">}</span> 235 <span class="brace">}</span>
236 <span class="brace">}</span> 236 <span class="brace">}</span>
237<span class="brace">}</span>
238
239<span class="keyword async">async</span> <span class="keyword">fn</span> <span class="function declaration async">learn_and_sing</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
240 <span class="keyword">let</span> <span class="variable declaration">song</span> <span class="operator">=</span> <span class="unresolved_reference">learn_song</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="operator">.</span><span class="keyword control async">await</span><span class="semicolon">;</span>
241 <span class="unresolved_reference">sing_song</span><span class="parenthesis">(</span><span class="variable consuming">song</span><span class="parenthesis">)</span><span class="operator">.</span><span class="keyword control async">await</span><span class="semicolon">;</span>
242<span class="brace">}</span>
243
244<span class="keyword async">async</span> <span class="keyword">fn</span> <span class="function declaration async">async_main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
245 <span class="keyword">let</span> <span class="variable declaration">f1</span> <span class="operator">=</span> <span class="function async">learn_and_sing</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
246 <span class="keyword">let</span> <span class="variable declaration">f2</span> <span class="operator">=</span> <span class="unresolved_reference">dance</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
247 futures::<span class="macro">join!</span><span class="parenthesis">(</span>f1<span class="comma">,</span> f2<span class="parenthesis">)</span><span class="semicolon">;</span>
237<span class="brace">}</span></code></pre> \ No newline at end of file 248<span class="brace">}</span></code></pre> \ No newline at end of file
diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs
index b6e952b08..8c8878d36 100644
--- a/crates/ide/src/syntax_highlighting/tests.rs
+++ b/crates/ide/src/syntax_highlighting/tests.rs
@@ -39,11 +39,11 @@ struct Foo {
39 pub y: i32, 39 pub y: i32,
40} 40}
41 41
42trait Bar { 42trait Bar where Self: {
43 fn bar(&self) -> i32; 43 fn bar(&self) -> i32;
44} 44}
45 45
46impl Bar for Foo { 46impl Bar for Foo where Self: {
47 fn bar(&self) -> i32 { 47 fn bar(&self) -> i32 {
48 self.x 48 self.x
49 } 49 }
@@ -208,6 +208,17 @@ impl<T> Option<T> {
208 } 208 }
209 } 209 }
210} 210}
211
212async fn learn_and_sing() {
213 let song = learn_song().await;
214 sing_song(song).await;
215}
216
217async fn async_main() {
218 let f1 = learn_and_sing();
219 let f2 = dance();
220 futures::join!(f1, f2);
221}
211"# 222"#
212 .trim(), 223 .trim(),
213 expect_file!["./test_data/highlighting.html"], 224 expect_file!["./test_data/highlighting.html"],
@@ -513,6 +524,11 @@ fn main() {
513fn test_highlight_doc_comment() { 524fn test_highlight_doc_comment() {
514 check_highlighting( 525 check_highlighting(
515 r#" 526 r#"
527//! This is a module to test doc injection.
528//! ```
529//! fn test() {}
530//! ```
531
516/// ``` 532/// ```
517/// let _ = "early doctests should not go boom"; 533/// let _ = "early doctests should not go boom";
518/// ``` 534/// ```
@@ -520,6 +536,13 @@ struct Foo {
520 bar: bool, 536 bar: bool,
521} 537}
522 538
539/// This is an impl with a code block.
540///
541/// ```
542/// fn foo() {
543///
544/// }
545/// ```
523impl Foo { 546impl Foo {
524 /// ``` 547 /// ```
525 /// let _ = "Call me 548 /// let _ = "Call me