From 41f470fea84998af65292f3c297c3e2b1d897848 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 8 May 2021 22:34:55 +0200 Subject: Correctly support SelfType when searching for usages --- crates/ide/src/references.rs | 55 +++++++++++++++++++++++++++++++++++-- crates/ide/src/references/rename.rs | 17 ++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index 11ca7ec6b..1551cd2a8 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs @@ -65,7 +65,7 @@ pub(crate) fn find_all_refs( (find_def(&sema, &syntax, position)?, false) }; - let mut usages = def.usages(sema).set_scope(search_scope).all(); + let mut usages = def.usages(sema).set_scope(search_scope).include_self_refs().all(); if is_literal_search { // filter for constructor-literals let refs = usages.references.values_mut(); @@ -1166,18 +1166,67 @@ fn foo() -> usize { fn test_find_self_ty_in_trait_def() { check( r#" -trait Foo { +trait Foo where Self: { fn f() -> Self$0; } "#, expect![[r#" Self TypeParam FileId(0) 6..9 6..9 - FileId(0) 26..30 + FileId(0) 16..20 + FileId(0) 38..42 "#]], ); + // check( + // r#" + // trait Foo$0 where Self: { + // fn f() -> Self; + // } + // "#, + // expect![[r#" + // Foo Trait FileId(0) 0..45 6..9 + + // FileId(0) 16..20 + // FileId(0) 38..42 + // "#]], + // ); } + #[test] + fn test_self_ty() { + check( + r#" + struct $0Foo; + + impl Foo where Self: { + fn f() -> Self; + } + "#, + expect![[r#" + Foo Struct FileId(0) 0..11 7..10 + + FileId(0) 18..21 + FileId(0) 28..32 + FileId(0) 50..54 + "#]], + ); + check( + r#" +struct Foo; + +impl Foo where Self: { + fn f() -> Self$0; +} +"#, + expect![[r#" + impl Impl FileId(0) 13..57 18..21 + + FileId(0) 18..21 + FileId(0) 28..32 + FileId(0) 50..54 + "#]], + ); + } #[test] fn test_self_variant_with_payload() { check( diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index 175e7a31d..2bf953305 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs @@ -1888,4 +1888,21 @@ impl Foo { "error: Cannot rename `Self`", ); } + + #[test] + fn test_rename_ignores_self_ty() { + check( + "Fo0", + r#" +struct $0Foo; + +impl Foo where Self: {} +"#, + r#" +struct Fo0; + +impl Fo0 where Self: {} +"#, + ); + } } -- cgit v1.2.3 From 3a346412cf8ce2f04e123195452bf1b1c86211a7 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 8 May 2021 23:30:19 +0200 Subject: Don't handle Self as a usage for TraitDefs --- crates/ide/src/references.rs | 49 ++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 22 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index 1551cd2a8..ae492a264 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs @@ -1163,33 +1163,38 @@ fn foo() -> usize { } #[test] - fn test_find_self_ty_in_trait_def() { + fn test_trait() { check( r#" -trait Foo where Self: { - fn f() -> Self$0; +trait Foo$0 where Self: {} + +impl Foo for () {} +"#, + expect![[r#" + Foo Trait FileId(0) 0..24 6..9 + + FileId(0) 31..34 + "#]], + ); + } + + #[test] + fn test_trait_self() { + check( + r#" +trait Foo where Self$0 { + fn f() -> Self; } + +impl Foo for () {} "#, expect![[r#" Self TypeParam FileId(0) 6..9 6..9 FileId(0) 16..20 - FileId(0) 38..42 + FileId(0) 37..41 "#]], ); - // check( - // r#" - // trait Foo$0 where Self: { - // fn f() -> Self; - // } - // "#, - // expect![[r#" - // Foo Trait FileId(0) 0..45 6..9 - - // FileId(0) 16..20 - // FileId(0) 38..42 - // "#]], - // ); } #[test] @@ -1203,12 +1208,12 @@ trait Foo where Self: { } "#, expect![[r#" - Foo Struct FileId(0) 0..11 7..10 + Foo Struct FileId(0) 0..11 7..10 - FileId(0) 18..21 - FileId(0) 28..32 - FileId(0) 50..54 - "#]], + FileId(0) 18..21 + FileId(0) 28..32 + FileId(0) 50..54 + "#]], ); check( r#" -- cgit v1.2.3