@@ -58,13 +58,18 @@ impl IntoIterator for UsageSearchResult {
5858pub struct FileReference {
5959 pub range : TextRange ,
6060 pub name : ast:: NameLike ,
61- pub access : Option < ReferenceAccess > ,
61+ pub category : Option < ReferenceCategory > ,
6262}
6363
6464#[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
65- pub enum ReferenceAccess {
66- Read ,
65+ pub enum ReferenceCategory {
66+ // FIXME: Add this variant and delete the `retain_adt_literal_usages` function.
67+ // Create
6768 Write ,
69+ Read ,
70+ // FIXME: Some day should be able to search in doc comments. Would probably
71+ // need to switch from enum to bitflags then?
72+ // DocComment
6873}
6974
7075/// Generally, `search_scope` returns files that might contain references for the element.
@@ -472,7 +477,7 @@ impl<'a> FindUsages<'a> {
472477 let reference = FileReference {
473478 range,
474479 name : ast:: NameLike :: NameRef ( name_ref. clone ( ) ) ,
475- access : None ,
480+ category : None ,
476481 } ;
477482 sink ( file_id, reference)
478483 }
@@ -491,7 +496,7 @@ impl<'a> FindUsages<'a> {
491496 let reference = FileReference {
492497 range,
493498 name : ast:: NameLike :: NameRef ( name_ref. clone ( ) ) ,
494- access : None ,
499+ category : None ,
495500 } ;
496501 sink ( file_id, reference)
497502 }
@@ -510,7 +515,7 @@ impl<'a> FindUsages<'a> {
510515 let reference = FileReference {
511516 range,
512517 name : ast:: NameLike :: Lifetime ( lifetime. clone ( ) ) ,
513- access : None ,
518+ category : None ,
514519 } ;
515520 sink ( file_id, reference)
516521 }
@@ -529,7 +534,7 @@ impl<'a> FindUsages<'a> {
529534 let reference = FileReference {
530535 range,
531536 name : ast:: NameLike :: NameRef ( name_ref. clone ( ) ) ,
532- access : reference_access ( & def, name_ref) ,
537+ category : ReferenceCategory :: new ( & def, name_ref) ,
533538 } ;
534539 sink ( file_id, reference)
535540 }
@@ -539,7 +544,7 @@ impl<'a> FindUsages<'a> {
539544 let reference = FileReference {
540545 range,
541546 name : ast:: NameLike :: NameRef ( name_ref. clone ( ) ) ,
542- access : reference_access ( & def, name_ref) ,
547+ category : ReferenceCategory :: new ( & def, name_ref) ,
543548 } ;
544549 sink ( file_id, reference)
545550 } else {
@@ -550,14 +555,19 @@ impl<'a> FindUsages<'a> {
550555 let field = Definition :: Field ( field) ;
551556 let FileRange { file_id, range } = self . sema . original_range ( name_ref. syntax ( ) ) ;
552557 let access = match self . def {
553- Definition :: Field ( _) if field == self . def => reference_access ( & field, name_ref) ,
558+ Definition :: Field ( _) if field == self . def => {
559+ ReferenceCategory :: new ( & field, name_ref)
560+ }
554561 Definition :: Local ( l) if local == l => {
555- reference_access ( & Definition :: Local ( local) , name_ref)
562+ ReferenceCategory :: new ( & Definition :: Local ( local) , name_ref)
556563 }
557564 _ => return false ,
558565 } ;
559- let reference =
560- FileReference { range, name : ast:: NameLike :: NameRef ( name_ref. clone ( ) ) , access } ;
566+ let reference = FileReference {
567+ range,
568+ name : ast:: NameLike :: NameRef ( name_ref. clone ( ) ) ,
569+ category : access,
570+ } ;
561571 sink ( file_id, reference)
562572 }
563573 _ => false ,
@@ -580,14 +590,17 @@ impl<'a> FindUsages<'a> {
580590 range,
581591 name : ast:: NameLike :: Name ( name. clone ( ) ) ,
582592 // FIXME: mutable patterns should have `Write` access
583- access : Some ( ReferenceAccess :: Read ) ,
593+ category : Some ( ReferenceCategory :: Read ) ,
584594 } ;
585595 sink ( file_id, reference)
586596 }
587597 Some ( NameClass :: ConstReference ( def) ) if self . def == def => {
588598 let FileRange { file_id, range } = self . sema . original_range ( name. syntax ( ) ) ;
589- let reference =
590- FileReference { range, name : ast:: NameLike :: Name ( name. clone ( ) ) , access : None } ;
599+ let reference = FileReference {
600+ range,
601+ name : ast:: NameLike :: Name ( name. clone ( ) ) ,
602+ category : None ,
603+ } ;
591604 sink ( file_id, reference)
592605 }
593606 // Resolve trait impl function definitions to the trait definition's version if self.def is the trait definition's
@@ -611,7 +624,7 @@ impl<'a> FindUsages<'a> {
611624 let reference = FileReference {
612625 range,
613626 name : ast:: NameLike :: Name ( name. clone ( ) ) ,
614- access : None ,
627+ category : None ,
615628 } ;
616629 sink ( file_id, reference)
617630 } )
@@ -642,32 +655,34 @@ fn def_to_ty(sema: &Semantics<RootDatabase>, def: &Definition) -> Option<hir::Ty
642655 }
643656}
644657
645- fn reference_access ( def : & Definition , name_ref : & ast:: NameRef ) -> Option < ReferenceAccess > {
646- // Only Locals and Fields have accesses for now.
647- if !matches ! ( def, Definition :: Local ( _) | Definition :: Field ( _) ) {
648- return None ;
649- }
658+ impl ReferenceCategory {
659+ fn new ( def : & Definition , r : & ast:: NameRef ) -> Option < ReferenceCategory > {
660+ // Only Locals and Fields have accesses for now.
661+ if !matches ! ( def, Definition :: Local ( _) | Definition :: Field ( _) ) {
662+ return None ;
663+ }
650664
651- let mode = name_ref . syntax ( ) . ancestors ( ) . find_map ( |node| {
665+ let mode = r . syntax ( ) . ancestors ( ) . find_map ( |node| {
652666 match_ast ! {
653667 match ( node) {
654668 ast:: BinExpr ( expr) => {
655669 if matches!( expr. op_kind( ) ?, ast:: BinaryOp :: Assignment { .. } ) {
656670 // If the variable or field ends on the LHS's end then it's a Write (covers fields and locals).
657671 // FIXME: This is not terribly accurate.
658672 if let Some ( lhs) = expr. lhs( ) {
659- if lhs. syntax( ) . text_range( ) . end( ) == name_ref . syntax( ) . text_range( ) . end( ) {
660- return Some ( ReferenceAccess :: Write ) ;
673+ if lhs. syntax( ) . text_range( ) . end( ) == r . syntax( ) . text_range( ) . end( ) {
674+ return Some ( ReferenceCategory :: Write ) ;
661675 }
662676 }
663677 }
664- Some ( ReferenceAccess :: Read )
678+ Some ( ReferenceCategory :: Read )
665679 } ,
666680 _ => None
667681 }
668682 }
669683 } ) ;
670684
671- // Default Locals and Fields to read
672- mode. or ( Some ( ReferenceAccess :: Read ) )
685+ // Default Locals and Fields to read
686+ mode. or ( Some ( ReferenceCategory :: Read ) )
687+ }
673688}
0 commit comments