2020use hir:: { PathResolution , Semantics } ;
2121use ide_db:: {
2222 FileId , RootDatabase ,
23+ base_db:: SourceDatabase ,
2324 defs:: { Definition , NameClass , NameRefClass } ,
2425 helpers:: pick_best_token,
2526 ra_fixture:: { RaFixtureConfig , UpmapFromRaFixture } ,
@@ -93,7 +94,6 @@ pub struct FindAllRefsConfig<'a> {
9394 pub ra_fixture : RaFixtureConfig < ' a > ,
9495 pub exclude_imports : bool ,
9596 pub exclude_tests : bool ,
96- pub exclude_library_refs : bool ,
9797}
9898
9999/// Find all references to the item at the given position.
@@ -128,6 +128,7 @@ pub(crate) fn find_all_refs(
128128) -> Option < Vec < ReferenceSearchResult > > {
129129 let _p = tracing:: info_span!( "find_all_refs" ) . entered ( ) ;
130130 let syntax = sema. parse_guess_edition ( position. file_id ) . syntax ( ) . clone ( ) ;
131+ let exclude_library_refs = !is_library_file ( sema. db , position. file_id ) ;
131132 let make_searcher = |literal_search : bool | {
132133 move |def : Definition | {
133134 let mut excluded_categories = ReferenceCategory :: empty ( ) ;
@@ -141,7 +142,7 @@ pub(crate) fn find_all_refs(
141142 . usages ( sema)
142143 . set_scope ( config. search_scope . as_ref ( ) )
143144 . set_excluded_categories ( excluded_categories)
144- . set_exclude_library_files ( config . exclude_library_refs )
145+ . set_exclude_library_files ( exclude_library_refs)
145146 . include_self_refs ( )
146147 . all ( ) ;
147148 if literal_search {
@@ -182,9 +183,7 @@ pub(crate) fn find_all_refs(
182183 nav,
183184 }
184185 } )
185- . filter ( |decl| {
186- !( config. exclude_library_refs && is_library_file ( sema. db , decl. nav . file_id ) )
187- } ) ;
186+ . filter ( |decl| !( exclude_library_refs && is_library_file ( sema. db , decl. nav . file_id ) ) ) ;
188187 ReferenceSearchResult { declaration, references }
189188 }
190189 } ;
@@ -505,7 +504,6 @@ fn test() {
505504 test_func();
506505}
507506"# ,
508- false ,
509507 false ,
510508 false ,
511509 expect ! [ [ r#"
@@ -529,7 +527,6 @@ fn test() {
529527 test_func();
530528}
531529"# ,
532- false ,
533530 false ,
534531 false ,
535532 expect ! [ [ r#"
@@ -555,7 +552,6 @@ fn test() {
555552"# ,
556553 false ,
557554 true ,
558- false ,
559555 expect ! [ [ r#"
560556 test_func Function FileId(0) 0..17 3..12
561557
@@ -585,7 +581,6 @@ pub fn also_calls_foo() {
585581"# ,
586582 false ,
587583 false ,
588- true ,
589584 expect ! [ [ r#"
590585 FileId(0) 9..12 import
591586 FileId(0) 31..34
@@ -602,7 +597,6 @@ fn main() {
602597"# ,
603598 false ,
604599 false ,
605- true ,
606600 expect ! [ [ r#"
607601 FileId(0) 46..50
608602 "# ] ] ,
@@ -627,7 +621,36 @@ pub fn also_calls_foo() {
627621"# ,
628622 false ,
629623 false ,
630- true ,
624+ expect ! [ [ r#"
625+ foo Function FileId(1) 0..15 7..10
626+
627+ FileId(0) 9..12 import
628+ FileId(0) 31..34
629+ FileId(1) 47..50
630+ "# ] ] ,
631+ ) ;
632+ }
633+
634+ #[ test]
635+ fn find_refs_from_library_source_keeps_library_refs ( ) {
636+ check_with_filters (
637+ r#"
638+ //- /main.rs crate:main deps:dep
639+ use dep::foo;
640+
641+ fn main() {
642+ foo();
643+ }
644+
645+ //- /dep/lib.rs crate:dep new_source_root:library
646+ pub fn foo$0() {}
647+
648+ pub fn also_calls_foo() {
649+ foo();
650+ }
651+ "# ,
652+ false ,
653+ false ,
631654 expect ! [ [ r#"
632655 foo Function FileId(1) 0..15 7..10
633656
@@ -1682,40 +1705,31 @@ fn main() {
16821705 }
16831706
16841707 fn check ( #[ rust_analyzer:: rust_fixture] ra_fixture : & str , expect : Expect ) {
1685- check_with_filters ( ra_fixture, false , false , false , expect)
1708+ check_with_filters ( ra_fixture, false , false , expect)
16861709 }
16871710
16881711 fn check_with_filters (
16891712 #[ rust_analyzer:: rust_fixture] ra_fixture : & str ,
16901713 exclude_imports : bool ,
16911714 exclude_tests : bool ,
1692- exclude_library_refs : bool ,
16931715 expect : Expect ,
16941716 ) {
1695- check_with_scope_and_filters (
1696- ra_fixture,
1697- None ,
1698- exclude_imports,
1699- exclude_tests,
1700- exclude_library_refs,
1701- expect,
1702- )
1717+ check_with_scope_and_filters ( ra_fixture, None , exclude_imports, exclude_tests, expect)
17031718 }
17041719
17051720 fn check_with_scope (
17061721 #[ rust_analyzer:: rust_fixture] ra_fixture : & str ,
17071722 search_scope : Option < & mut dyn FnMut ( & RootDatabase ) -> SearchScope > ,
17081723 expect : Expect ,
17091724 ) {
1710- check_with_scope_and_filters ( ra_fixture, search_scope, false , false , false , expect)
1725+ check_with_scope_and_filters ( ra_fixture, search_scope, false , false , expect)
17111726 }
17121727
17131728 fn check_with_scope_and_filters (
17141729 #[ rust_analyzer:: rust_fixture] ra_fixture : & str ,
17151730 search_scope : Option < & mut dyn FnMut ( & RootDatabase ) -> SearchScope > ,
17161731 exclude_imports : bool ,
17171732 exclude_tests : bool ,
1718- exclude_library_refs : bool ,
17191733 expect : Expect ,
17201734 ) {
17211735 let ( analysis, pos) = fixture:: position ( ra_fixture) ;
@@ -1724,10 +1738,6 @@ fn main() {
17241738 ra_fixture : RaFixtureConfig :: default ( ) ,
17251739 exclude_imports,
17261740 exclude_tests,
1727- exclude_library_refs,
1728- exclude_imports : false ,
1729- exclude_tests : false ,
1730- exclude_library_refs : false ,
17311741 } ;
17321742 let refs = analysis. find_all_refs ( pos, & config) . unwrap ( ) . unwrap ( ) ;
17331743
@@ -2234,8 +2244,6 @@ use proc_macros::identity;
22342244fn func() {}
22352245"# ,
22362246 expect ! [ [ r#"
2237- identity Attribute FileId(1) 1..107 32..40
2238-
22392247 FileId(0) 17..25 import
22402248 FileId(0) 43..51
22412249 "# ] ] ,
@@ -2265,8 +2273,6 @@ use proc_macros::mirror;
22652273mirror$0! {}
22662274"# ,
22672275 expect ! [ [ r#"
2268- mirror ProcMacro FileId(1) 1..77 22..28
2269-
22702276 FileId(0) 17..23 import
22712277 FileId(0) 26..32
22722278 "# ] ] ,
@@ -2285,8 +2291,6 @@ use proc_macros::DeriveIdentity;
22852291struct Foo;
22862292"# ,
22872293 expect ! [ [ r#"
2288- derive_identity Derive FileId(2) 1..107 45..60
2289-
22902294 FileId(0) 17..31 import
22912295 FileId(0) 56..70
22922296 "# ] ] ,
0 commit comments