@@ -58,13 +58,18 @@ impl IntoIterator for UsageSearchResult {
58
58
pub struct FileReference {
59
59
pub range : TextRange ,
60
60
pub name : ast:: NameLike ,
61
- pub access : Option < ReferenceAccess > ,
61
+ pub category : Option < ReferenceCategory > ,
62
62
}
63
63
64
64
#[ 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
67
68
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
68
73
}
69
74
70
75
/// Generally, `search_scope` returns files that might contain references for the element.
@@ -472,7 +477,7 @@ impl<'a> FindUsages<'a> {
472
477
let reference = FileReference {
473
478
range,
474
479
name : ast:: NameLike :: NameRef ( name_ref. clone ( ) ) ,
475
- access : None ,
480
+ category : None ,
476
481
} ;
477
482
sink ( file_id, reference)
478
483
}
@@ -491,7 +496,7 @@ impl<'a> FindUsages<'a> {
491
496
let reference = FileReference {
492
497
range,
493
498
name : ast:: NameLike :: NameRef ( name_ref. clone ( ) ) ,
494
- access : None ,
499
+ category : None ,
495
500
} ;
496
501
sink ( file_id, reference)
497
502
}
@@ -510,7 +515,7 @@ impl<'a> FindUsages<'a> {
510
515
let reference = FileReference {
511
516
range,
512
517
name : ast:: NameLike :: Lifetime ( lifetime. clone ( ) ) ,
513
- access : None ,
518
+ category : None ,
514
519
} ;
515
520
sink ( file_id, reference)
516
521
}
@@ -529,7 +534,7 @@ impl<'a> FindUsages<'a> {
529
534
let reference = FileReference {
530
535
range,
531
536
name : ast:: NameLike :: NameRef ( name_ref. clone ( ) ) ,
532
- access : reference_access ( & def, name_ref) ,
537
+ category : ReferenceCategory :: new ( & def, name_ref) ,
533
538
} ;
534
539
sink ( file_id, reference)
535
540
}
@@ -539,7 +544,7 @@ impl<'a> FindUsages<'a> {
539
544
let reference = FileReference {
540
545
range,
541
546
name : ast:: NameLike :: NameRef ( name_ref. clone ( ) ) ,
542
- access : reference_access ( & def, name_ref) ,
547
+ category : ReferenceCategory :: new ( & def, name_ref) ,
543
548
} ;
544
549
sink ( file_id, reference)
545
550
} else {
@@ -550,14 +555,19 @@ impl<'a> FindUsages<'a> {
550
555
let field = Definition :: Field ( field) ;
551
556
let FileRange { file_id, range } = self . sema . original_range ( name_ref. syntax ( ) ) ;
552
557
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
+ }
554
561
Definition :: Local ( l) if local == l => {
555
- reference_access ( & Definition :: Local ( local) , name_ref)
562
+ ReferenceCategory :: new ( & Definition :: Local ( local) , name_ref)
556
563
}
557
564
_ => return false ,
558
565
} ;
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
+ } ;
561
571
sink ( file_id, reference)
562
572
}
563
573
_ => false ,
@@ -580,14 +590,17 @@ impl<'a> FindUsages<'a> {
580
590
range,
581
591
name : ast:: NameLike :: Name ( name. clone ( ) ) ,
582
592
// FIXME: mutable patterns should have `Write` access
583
- access : Some ( ReferenceAccess :: Read ) ,
593
+ category : Some ( ReferenceCategory :: Read ) ,
584
594
} ;
585
595
sink ( file_id, reference)
586
596
}
587
597
Some ( NameClass :: ConstReference ( def) ) if self . def == def => {
588
598
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
+ } ;
591
604
sink ( file_id, reference)
592
605
}
593
606
// 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> {
611
624
let reference = FileReference {
612
625
range,
613
626
name : ast:: NameLike :: Name ( name. clone ( ) ) ,
614
- access : None ,
627
+ category : None ,
615
628
} ;
616
629
sink ( file_id, reference)
617
630
} )
@@ -642,32 +655,34 @@ fn def_to_ty(sema: &Semantics<RootDatabase>, def: &Definition) -> Option<hir::Ty
642
655
}
643
656
}
644
657
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
+ }
650
664
651
- let mode = name_ref . syntax ( ) . ancestors ( ) . find_map ( |node| {
665
+ let mode = r . syntax ( ) . ancestors ( ) . find_map ( |node| {
652
666
match_ast ! {
653
667
match ( node) {
654
668
ast:: BinExpr ( expr) => {
655
669
if matches!( expr. op_kind( ) ?, ast:: BinaryOp :: Assignment { .. } ) {
656
670
// If the variable or field ends on the LHS's end then it's a Write (covers fields and locals).
657
671
// FIXME: This is not terribly accurate.
658
672
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 ) ;
661
675
}
662
676
}
663
677
}
664
- Some ( ReferenceAccess :: Read )
678
+ Some ( ReferenceCategory :: Read )
665
679
} ,
666
680
_ => None
667
681
}
668
682
}
669
683
} ) ;
670
684
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
+ }
673
688
}
0 commit comments