Skip to content

Commit 46eb03d

Browse files
committed
internal: use naming that matches intended use-case
1 parent 59c758d commit 46eb03d

File tree

6 files changed

+61
-46
lines changed

6 files changed

+61
-46
lines changed

crates/ide/src/highlight_related.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use ide_db::{
33
base_db::FilePosition,
44
defs::{Definition, NameClass, NameRefClass},
55
helpers::{for_each_break_expr, for_each_tail_expr, node_ext::walk_expr, pick_best_token},
6-
search::{FileReference, ReferenceAccess, SearchScope},
6+
search::{FileReference, ReferenceCategory, SearchScope},
77
RootDatabase,
88
};
99
use rustc_hash::FxHashSet;
@@ -19,7 +19,7 @@ use crate::{display::TryToNav, references, NavigationTarget};
1919
#[derive(PartialEq, Eq, Hash)]
2020
pub struct HighlightedRange {
2121
pub range: TextRange,
22-
pub access: Option<ReferenceAccess>,
22+
pub access: Option<ReferenceCategory>,
2323
}
2424

2525
#[derive(Default, Clone)]
@@ -87,7 +87,7 @@ fn highlight_references(
8787
.remove(&file_id)
8888
})
8989
.flatten()
90-
.map(|FileReference { access, range, .. }| HighlightedRange { range, access });
90+
.map(|FileReference { category: access, range, .. }| HighlightedRange { range, access });
9191

9292
let declarations = defs.iter().flat_map(|def| {
9393
match def {
@@ -355,8 +355,8 @@ mod tests {
355355
hl.range,
356356
hl.access.map(|it| {
357357
match it {
358-
ReferenceAccess::Read => "read",
359-
ReferenceAccess::Write => "write",
358+
ReferenceCategory::Read => "read",
359+
ReferenceCategory::Write => "write",
360360
}
361361
.to_string()
362362
}),

crates/ide/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub use ide_db::{
108108
call_info::CallInfo,
109109
label::Label,
110110
line_index::{LineCol, LineColUtf16, LineIndex},
111-
search::{ReferenceAccess, SearchScope},
111+
search::{ReferenceCategory, SearchScope},
112112
source_change::{FileSystemEdit, SourceChange},
113113
symbol_index::Query,
114114
RootDatabase, SymbolKind,

crates/ide/src/references.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use hir::{PathResolution, Semantics};
1616
use ide_db::{
1717
base_db::FileId,
1818
defs::{Definition, NameClass, NameRefClass},
19-
search::{ReferenceAccess, SearchScope, UsageSearchResult},
19+
search::{ReferenceCategory, SearchScope, UsageSearchResult},
2020
RootDatabase,
2121
};
2222
use rustc_hash::FxHashMap;
@@ -31,13 +31,13 @@ use crate::{display::TryToNav, FilePosition, NavigationTarget};
3131
#[derive(Debug, Clone)]
3232
pub struct ReferenceSearchResult {
3333
pub declaration: Option<Declaration>,
34-
pub references: FxHashMap<FileId, Vec<(TextRange, Option<ReferenceAccess>)>>,
34+
pub references: FxHashMap<FileId, Vec<(TextRange, Option<ReferenceCategory>)>>,
3535
}
3636

3737
#[derive(Debug, Clone)]
3838
pub struct Declaration {
3939
pub nav: NavigationTarget,
40-
pub access: Option<ReferenceAccess>,
40+
pub access: Option<ReferenceCategory>,
4141
}
4242

4343
// Feature: Find All References
@@ -102,7 +102,7 @@ pub(crate) fn find_all_refs(
102102
(
103103
file_id,
104104
refs.into_iter()
105-
.map(|file_ref| (file_ref.range, file_ref.access))
105+
.map(|file_ref| (file_ref.range, file_ref.category))
106106
.collect(),
107107
)
108108
})
@@ -149,7 +149,7 @@ pub(crate) fn decl_access(
149149
def: &Definition,
150150
syntax: &SyntaxNode,
151151
range: TextRange,
152-
) -> Option<ReferenceAccess> {
152+
) -> Option<ReferenceCategory> {
153153
match def {
154154
Definition::Local(_) | Definition::Field(_) => {}
155155
_ => return None,
@@ -160,7 +160,7 @@ pub(crate) fn decl_access(
160160
let pat = stmt.pat()?;
161161
if let ast::Pat::IdentPat(it) = pat {
162162
if it.mut_token().is_some() {
163-
return Some(ReferenceAccess::Write);
163+
return Some(ReferenceCategory::Write);
164164
}
165165
}
166166
}

crates/ide_assists/src/handlers/extract_function.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use hir::{HirDisplay, InFile, Local, Semantics, TypeInfo};
66
use ide_db::{
77
defs::{Definition, NameRefClass},
88
helpers::node_ext::{preorder_expr, walk_expr, walk_pat, walk_patterns_in_expr},
9-
search::{FileReference, ReferenceAccess, SearchScope},
9+
search::{FileReference, ReferenceCategory, SearchScope},
1010
RootDatabase,
1111
};
1212
use itertools::Itertools;
@@ -877,7 +877,7 @@ fn reference_is_exclusive(
877877
ctx: &AssistContext,
878878
) -> bool {
879879
// we directly modify variable with set: `n = 0`, `n += 1`
880-
if reference.access == Some(ReferenceAccess::Write) {
880+
if reference.category == Some(ReferenceCategory::Write) {
881881
return true;
882882
}
883883

crates/ide_db/src/search.rs

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,18 @@ impl IntoIterator for UsageSearchResult {
5858
pub 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
}

crates/rust-analyzer/src/to_proto.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use ide::{
99
Annotation, AnnotationKind, Assist, AssistKind, CallInfo, Cancellable, CompletionItem,
1010
CompletionItemKind, CompletionRelevance, Documentation, FileId, FileRange, FileSystemEdit,
1111
Fold, FoldKind, Highlight, HlMod, HlOperator, HlPunct, HlRange, HlTag, Indel, InlayHint,
12-
InlayKind, Markup, NavigationTarget, ReferenceAccess, RenameError, Runnable, Severity,
12+
InlayKind, Markup, NavigationTarget, ReferenceCategory, RenameError, Runnable, Severity,
1313
SourceChange, StructureNodeKind, SymbolKind, TextEdit, TextRange, TextSize,
1414
};
1515
use itertools::Itertools;
@@ -75,11 +75,11 @@ pub(crate) fn structure_node_kind(kind: StructureNodeKind) -> lsp_types::SymbolK
7575
}
7676

7777
pub(crate) fn document_highlight_kind(
78-
reference_access: ReferenceAccess,
78+
category: ReferenceCategory,
7979
) -> lsp_types::DocumentHighlightKind {
80-
match reference_access {
81-
ReferenceAccess::Read => lsp_types::DocumentHighlightKind::Read,
82-
ReferenceAccess::Write => lsp_types::DocumentHighlightKind::Write,
80+
match category {
81+
ReferenceCategory::Read => lsp_types::DocumentHighlightKind::Read,
82+
ReferenceCategory::Write => lsp_types::DocumentHighlightKind::Write,
8383
}
8484
}
8585

0 commit comments

Comments
 (0)