mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
internal: untangle usages of ReferenceCategory somewhat
Not everything that can be read or write is a reference, let's try to use more precise types.
This commit is contained in:
parent
46eb03d99a
commit
12103b16de
3 changed files with 41 additions and 37 deletions
|
@ -19,7 +19,10 @@ use crate::{display::TryToNav, references, NavigationTarget};
|
||||||
#[derive(PartialEq, Eq, Hash)]
|
#[derive(PartialEq, Eq, Hash)]
|
||||||
pub struct HighlightedRange {
|
pub struct HighlightedRange {
|
||||||
pub range: TextRange,
|
pub range: TextRange,
|
||||||
pub access: Option<ReferenceCategory>,
|
// FIXME: This needs to be more precise. Reference category makes sense only
|
||||||
|
// for references, but we also have defs. And things like exit points are
|
||||||
|
// neither.
|
||||||
|
pub category: Option<ReferenceCategory>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Clone)]
|
#[derive(Default, Clone)]
|
||||||
|
@ -87,7 +90,10 @@ fn highlight_references(
|
||||||
.remove(&file_id)
|
.remove(&file_id)
|
||||||
})
|
})
|
||||||
.flatten()
|
.flatten()
|
||||||
.map(|FileReference { category: access, range, .. }| HighlightedRange { range, access });
|
.map(|FileReference { category: access, range, .. }| HighlightedRange {
|
||||||
|
range,
|
||||||
|
category: access,
|
||||||
|
});
|
||||||
|
|
||||||
let declarations = defs.iter().flat_map(|def| {
|
let declarations = defs.iter().flat_map(|def| {
|
||||||
match def {
|
match def {
|
||||||
|
@ -99,8 +105,12 @@ fn highlight_references(
|
||||||
.filter(|decl| decl.file_id == file_id)
|
.filter(|decl| decl.file_id == file_id)
|
||||||
.and_then(|decl| {
|
.and_then(|decl| {
|
||||||
let range = decl.focus_range?;
|
let range = decl.focus_range?;
|
||||||
let access = references::decl_access(&def, syntax, range);
|
let category = if references::decl_mutability(&def, syntax, range) {
|
||||||
Some(HighlightedRange { range, access })
|
Some(ReferenceCategory::Write)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
Some(HighlightedRange { range, category })
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -125,18 +135,20 @@ fn highlight_exit_points(
|
||||||
walk_expr(&body, &mut |expr| match expr {
|
walk_expr(&body, &mut |expr| match expr {
|
||||||
ast::Expr::ReturnExpr(expr) => {
|
ast::Expr::ReturnExpr(expr) => {
|
||||||
if let Some(token) = expr.return_token() {
|
if let Some(token) = expr.return_token() {
|
||||||
highlights.push(HighlightedRange { access: None, range: token.text_range() });
|
highlights.push(HighlightedRange { category: None, range: token.text_range() });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast::Expr::TryExpr(try_) => {
|
ast::Expr::TryExpr(try_) => {
|
||||||
if let Some(token) = try_.question_mark_token() {
|
if let Some(token) = try_.question_mark_token() {
|
||||||
highlights.push(HighlightedRange { access: None, range: token.text_range() });
|
highlights.push(HighlightedRange { category: None, range: token.text_range() });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast::Expr::MethodCallExpr(_) | ast::Expr::CallExpr(_) | ast::Expr::MacroCall(_) => {
|
ast::Expr::MethodCallExpr(_) | ast::Expr::CallExpr(_) | ast::Expr::MacroCall(_) => {
|
||||||
if sema.type_of_expr(&expr).map_or(false, |ty| ty.original.is_never()) {
|
if sema.type_of_expr(&expr).map_or(false, |ty| ty.original.is_never()) {
|
||||||
highlights
|
highlights.push(HighlightedRange {
|
||||||
.push(HighlightedRange { access: None, range: expr.syntax().text_range() });
|
category: None,
|
||||||
|
range: expr.syntax().text_range(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
|
@ -154,7 +166,7 @@ fn highlight_exit_points(
|
||||||
.map_or_else(|| tail.syntax().text_range(), |tok| tok.text_range()),
|
.map_or_else(|| tail.syntax().text_range(), |tok| tok.text_range()),
|
||||||
_ => tail.syntax().text_range(),
|
_ => tail.syntax().text_range(),
|
||||||
};
|
};
|
||||||
highlights.push(HighlightedRange { access: None, range })
|
highlights.push(HighlightedRange { category: None, range })
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Some(highlights)
|
Some(highlights)
|
||||||
|
@ -187,13 +199,13 @@ fn highlight_break_points(token: SyntaxToken) -> Option<Vec<HighlightedRange>> {
|
||||||
token.map(|tok| tok.text_range()),
|
token.map(|tok| tok.text_range()),
|
||||||
label.as_ref().map(|it| it.syntax().text_range()),
|
label.as_ref().map(|it| it.syntax().text_range()),
|
||||||
);
|
);
|
||||||
highlights.extend(range.map(|range| HighlightedRange { access: None, range }));
|
highlights.extend(range.map(|range| HighlightedRange { category: None, range }));
|
||||||
for_each_break_expr(label, body, &mut |break_| {
|
for_each_break_expr(label, body, &mut |break_| {
|
||||||
let range = cover_range(
|
let range = cover_range(
|
||||||
break_.break_token().map(|it| it.text_range()),
|
break_.break_token().map(|it| it.text_range()),
|
||||||
break_.lifetime().map(|it| it.syntax().text_range()),
|
break_.lifetime().map(|it| it.syntax().text_range()),
|
||||||
);
|
);
|
||||||
highlights.extend(range.map(|range| HighlightedRange { access: None, range }));
|
highlights.extend(range.map(|range| HighlightedRange { category: None, range }));
|
||||||
});
|
});
|
||||||
Some(highlights)
|
Some(highlights)
|
||||||
}
|
}
|
||||||
|
@ -241,13 +253,13 @@ fn highlight_yield_points(token: SyntaxToken) -> Option<Vec<HighlightedRange>> {
|
||||||
body: Option<ast::Expr>,
|
body: Option<ast::Expr>,
|
||||||
) -> Option<Vec<HighlightedRange>> {
|
) -> Option<Vec<HighlightedRange>> {
|
||||||
let mut highlights =
|
let mut highlights =
|
||||||
vec![HighlightedRange { access: None, range: async_token?.text_range() }];
|
vec![HighlightedRange { category: None, range: async_token?.text_range() }];
|
||||||
if let Some(body) = body {
|
if let Some(body) = body {
|
||||||
walk_expr(&body, &mut |expr| {
|
walk_expr(&body, &mut |expr| {
|
||||||
if let ast::Expr::AwaitExpr(expr) = expr {
|
if let ast::Expr::AwaitExpr(expr) = expr {
|
||||||
if let Some(token) = expr.await_token() {
|
if let Some(token) = expr.await_token() {
|
||||||
highlights
|
highlights
|
||||||
.push(HighlightedRange { access: None, range: token.text_range() });
|
.push(HighlightedRange { category: None, range: token.text_range() });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -353,7 +365,7 @@ mod tests {
|
||||||
.map(|hl| {
|
.map(|hl| {
|
||||||
(
|
(
|
||||||
hl.range,
|
hl.range,
|
||||||
hl.access.map(|it| {
|
hl.category.map(|it| {
|
||||||
match it {
|
match it {
|
||||||
ReferenceCategory::Read => "read",
|
ReferenceCategory::Read => "read",
|
||||||
ReferenceCategory::Write => "write",
|
ReferenceCategory::Write => "write",
|
||||||
|
|
|
@ -37,7 +37,7 @@ pub struct ReferenceSearchResult {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Declaration {
|
pub struct Declaration {
|
||||||
pub nav: NavigationTarget,
|
pub nav: NavigationTarget,
|
||||||
pub access: Option<ReferenceCategory>,
|
pub is_mut: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Feature: Find All References
|
// Feature: Find All References
|
||||||
|
@ -88,7 +88,7 @@ pub(crate) fn find_all_refs(
|
||||||
.map(|nav| {
|
.map(|nav| {
|
||||||
let decl_range = nav.focus_or_full_range();
|
let decl_range = nav.focus_or_full_range();
|
||||||
Declaration {
|
Declaration {
|
||||||
access: decl_access(&def, sema.parse(nav.file_id).syntax(), decl_range),
|
is_mut: decl_mutability(&def, sema.parse(nav.file_id).syntax(), decl_range),
|
||||||
nav,
|
nav,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -145,28 +145,20 @@ pub(crate) fn find_defs<'a>(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn decl_access(
|
pub(crate) fn decl_mutability(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> bool {
|
||||||
def: &Definition,
|
|
||||||
syntax: &SyntaxNode,
|
|
||||||
range: TextRange,
|
|
||||||
) -> Option<ReferenceCategory> {
|
|
||||||
match def {
|
match def {
|
||||||
Definition::Local(_) | Definition::Field(_) => {}
|
Definition::Local(_) | Definition::Field(_) => {}
|
||||||
_ => return None,
|
_ => return false,
|
||||||
};
|
};
|
||||||
|
|
||||||
let stmt = find_node_at_offset::<ast::LetStmt>(syntax, range.start())?;
|
match find_node_at_offset::<ast::LetStmt>(syntax, range.start()) {
|
||||||
if stmt.initializer().is_some() {
|
Some(stmt) if stmt.initializer().is_some() => match stmt.pat() {
|
||||||
let pat = stmt.pat()?;
|
Some(ast::Pat::IdentPat(it)) => it.mut_token().is_some(),
|
||||||
if let ast::Pat::IdentPat(it) = pat {
|
_ => false,
|
||||||
if it.mut_token().is_some() {
|
},
|
||||||
return Some(ReferenceCategory::Write);
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Filter out all non-literal usages for adt-defs
|
/// Filter out all non-literal usages for adt-defs
|
||||||
fn retain_adt_literal_usages(
|
fn retain_adt_literal_usages(
|
||||||
|
@ -283,7 +275,7 @@ fn is_lit_name_ref(name_ref: &ast::NameRef) -> bool {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
use ide_db::base_db::FileId;
|
use ide_db::{base_db::FileId, search::ReferenceCategory};
|
||||||
use stdx::format_to;
|
use stdx::format_to;
|
||||||
|
|
||||||
use crate::{fixture, SearchScope};
|
use crate::{fixture, SearchScope};
|
||||||
|
@ -1095,8 +1087,8 @@ impl Foo {
|
||||||
|
|
||||||
if let Some(decl) = refs.declaration {
|
if let Some(decl) = refs.declaration {
|
||||||
format_to!(actual, "{}", decl.nav.debug_render());
|
format_to!(actual, "{}", decl.nav.debug_render());
|
||||||
if let Some(access) = decl.access {
|
if decl.is_mut {
|
||||||
format_to!(actual, " {:?}", access)
|
format_to!(actual, " {:?}", ReferenceCategory::Write)
|
||||||
}
|
}
|
||||||
actual += "\n\n";
|
actual += "\n\n";
|
||||||
}
|
}
|
||||||
|
|
|
@ -1182,9 +1182,9 @@ pub(crate) fn handle_document_highlight(
|
||||||
};
|
};
|
||||||
let res = refs
|
let res = refs
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|ide::HighlightedRange { range, access }| lsp_types::DocumentHighlight {
|
.map(|ide::HighlightedRange { range, category }| lsp_types::DocumentHighlight {
|
||||||
range: to_proto::range(&line_index, range),
|
range: to_proto::range(&line_index, range),
|
||||||
kind: access.map(to_proto::document_highlight_kind),
|
kind: category.map(to_proto::document_highlight_kind),
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
Ok(Some(res))
|
Ok(Some(res))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue