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/src') 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