Skip to content

Commit 5ff551d

Browse files
bors[bot]matklad
andauthored
Merge #10426
10426: internal: use naming that matches intended use-case r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents c1fa583 + 12103b1 commit 5ff551d

File tree

7 files changed

+97
-78
lines changed

7 files changed

+97
-78
lines changed

crates/ide/src/highlight_related.rs

Lines changed: 29 additions & 17 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,10 @@ 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+
// FIXME: This needs to be more precise. Reference category makes sense only
23+
// for references, but we also have defs. And things like exit points are
24+
// neither.
25+
pub category: Option<ReferenceCategory>,
2326
}
2427

2528
#[derive(Default, Clone)]
@@ -87,7 +90,10 @@ fn highlight_references(
8790
.remove(&file_id)
8891
})
8992
.flatten()
90-
.map(|FileReference { access, range, .. }| HighlightedRange { range, access });
93+
.map(|FileReference { category: access, range, .. }| HighlightedRange {
94+
range,
95+
category: access,
96+
});
9197

9298
let declarations = defs.iter().flat_map(|def| {
9399
match def {
@@ -99,8 +105,12 @@ fn highlight_references(
99105
.filter(|decl| decl.file_id == file_id)
100106
.and_then(|decl| {
101107
let range = decl.focus_range?;
102-
let access = references::decl_access(&def, syntax, range);
103-
Some(HighlightedRange { range, access })
108+
let category = if references::decl_mutability(&def, syntax, range) {
109+
Some(ReferenceCategory::Write)
110+
} else {
111+
None
112+
};
113+
Some(HighlightedRange { range, category })
104114
})
105115
});
106116

@@ -125,18 +135,20 @@ fn highlight_exit_points(
125135
walk_expr(&body, &mut |expr| match expr {
126136
ast::Expr::ReturnExpr(expr) => {
127137
if let Some(token) = expr.return_token() {
128-
highlights.push(HighlightedRange { access: None, range: token.text_range() });
138+
highlights.push(HighlightedRange { category: None, range: token.text_range() });
129139
}
130140
}
131141
ast::Expr::TryExpr(try_) => {
132142
if let Some(token) = try_.question_mark_token() {
133-
highlights.push(HighlightedRange { access: None, range: token.text_range() });
143+
highlights.push(HighlightedRange { category: None, range: token.text_range() });
134144
}
135145
}
136146
ast::Expr::MethodCallExpr(_) | ast::Expr::CallExpr(_) | ast::Expr::MacroCall(_) => {
137147
if sema.type_of_expr(&expr).map_or(false, |ty| ty.original.is_never()) {
138-
highlights
139-
.push(HighlightedRange { access: None, range: expr.syntax().text_range() });
148+
highlights.push(HighlightedRange {
149+
category: None,
150+
range: expr.syntax().text_range(),
151+
});
140152
}
141153
}
142154
_ => (),
@@ -154,7 +166,7 @@ fn highlight_exit_points(
154166
.map_or_else(|| tail.syntax().text_range(), |tok| tok.text_range()),
155167
_ => tail.syntax().text_range(),
156168
};
157-
highlights.push(HighlightedRange { access: None, range })
169+
highlights.push(HighlightedRange { category: None, range })
158170
});
159171
}
160172
Some(highlights)
@@ -187,13 +199,13 @@ fn highlight_break_points(token: SyntaxToken) -> Option<Vec<HighlightedRange>> {
187199
token.map(|tok| tok.text_range()),
188200
label.as_ref().map(|it| it.syntax().text_range()),
189201
);
190-
highlights.extend(range.map(|range| HighlightedRange { access: None, range }));
202+
highlights.extend(range.map(|range| HighlightedRange { category: None, range }));
191203
for_each_break_expr(label, body, &mut |break_| {
192204
let range = cover_range(
193205
break_.break_token().map(|it| it.text_range()),
194206
break_.lifetime().map(|it| it.syntax().text_range()),
195207
);
196-
highlights.extend(range.map(|range| HighlightedRange { access: None, range }));
208+
highlights.extend(range.map(|range| HighlightedRange { category: None, range }));
197209
});
198210
Some(highlights)
199211
}
@@ -241,13 +253,13 @@ fn highlight_yield_points(token: SyntaxToken) -> Option<Vec<HighlightedRange>> {
241253
body: Option<ast::Expr>,
242254
) -> Option<Vec<HighlightedRange>> {
243255
let mut highlights =
244-
vec![HighlightedRange { access: None, range: async_token?.text_range() }];
256+
vec![HighlightedRange { category: None, range: async_token?.text_range() }];
245257
if let Some(body) = body {
246258
walk_expr(&body, &mut |expr| {
247259
if let ast::Expr::AwaitExpr(expr) = expr {
248260
if let Some(token) = expr.await_token() {
249261
highlights
250-
.push(HighlightedRange { access: None, range: token.text_range() });
262+
.push(HighlightedRange { category: None, range: token.text_range() });
251263
}
252264
}
253265
});
@@ -353,10 +365,10 @@ mod tests {
353365
.map(|hl| {
354366
(
355367
hl.range,
356-
hl.access.map(|it| {
368+
hl.category.map(|it| {
357369
match it {
358-
ReferenceAccess::Read => "read",
359-
ReferenceAccess::Write => "write",
370+
ReferenceCategory::Read => "read",
371+
ReferenceCategory::Write => "write",
360372
}
361373
.to_string()
362374
}),

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: 16 additions & 24 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 is_mut: bool,
4141
}
4242

4343
// Feature: Find All References
@@ -88,7 +88,7 @@ pub(crate) fn find_all_refs(
8888
.map(|nav| {
8989
let decl_range = nav.focus_or_full_range();
9090
Declaration {
91-
access: decl_access(&def, sema.parse(nav.file_id).syntax(), decl_range),
91+
is_mut: decl_mutability(&def, sema.parse(nav.file_id).syntax(), decl_range),
9292
nav,
9393
}
9494
});
@@ -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
})
@@ -145,27 +145,19 @@ pub(crate) fn find_defs<'a>(
145145
})
146146
}
147147

148-
pub(crate) fn decl_access(
149-
def: &Definition,
150-
syntax: &SyntaxNode,
151-
range: TextRange,
152-
) -> Option<ReferenceAccess> {
148+
pub(crate) fn decl_mutability(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> bool {
153149
match def {
154150
Definition::Local(_) | Definition::Field(_) => {}
155-
_ => return None,
151+
_ => return false,
156152
};
157153

158-
let stmt = find_node_at_offset::<ast::LetStmt>(syntax, range.start())?;
159-
if stmt.initializer().is_some() {
160-
let pat = stmt.pat()?;
161-
if let ast::Pat::IdentPat(it) = pat {
162-
if it.mut_token().is_some() {
163-
return Some(ReferenceAccess::Write);
164-
}
165-
}
154+
match find_node_at_offset::<ast::LetStmt>(syntax, range.start()) {
155+
Some(stmt) if stmt.initializer().is_some() => match stmt.pat() {
156+
Some(ast::Pat::IdentPat(it)) => it.mut_token().is_some(),
157+
_ => false,
158+
},
159+
_ => false,
166160
}
167-
168-
None
169161
}
170162

171163
/// Filter out all non-literal usages for adt-defs
@@ -283,7 +275,7 @@ fn is_lit_name_ref(name_ref: &ast::NameRef) -> bool {
283275
#[cfg(test)]
284276
mod tests {
285277
use expect_test::{expect, Expect};
286-
use ide_db::base_db::FileId;
278+
use ide_db::{base_db::FileId, search::ReferenceCategory};
287279
use stdx::format_to;
288280

289281
use crate::{fixture, SearchScope};
@@ -1095,8 +1087,8 @@ impl Foo {
10951087

10961088
if let Some(decl) = refs.declaration {
10971089
format_to!(actual, "{}", decl.nav.debug_render());
1098-
if let Some(access) = decl.access {
1099-
format_to!(actual, " {:?}", access)
1090+
if decl.is_mut {
1091+
format_to!(actual, " {:?}", ReferenceCategory::Write)
11001092
}
11011093
actual += "\n\n";
11021094
}

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

0 commit comments

Comments
 (0)