mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
Tweaks
This commit is contained in:
parent
d993f329a0
commit
6c89d86ade
1 changed files with 35 additions and 13 deletions
|
@ -232,22 +232,26 @@ fn access_mode(kind: NameKind, name_ref: &ast::NameRef) -> Option<ReferenceAcces
|
||||||
match_ast! {
|
match_ast! {
|
||||||
match (node) {
|
match (node) {
|
||||||
ast::BinExpr(expr) => {
|
ast::BinExpr(expr) => {
|
||||||
match expr.op_kind() {
|
if expr.op_kind()?.is_assignment() {
|
||||||
Some(kind) if kind.is_assignment() => {
|
// If the variable or field ends on the LHS's end then it's a Write (covers fields and locals).
|
||||||
|
// FIXME: This is not terribly accurate.
|
||||||
if let Some(lhs) = expr.lhs() {
|
if let Some(lhs) = expr.lhs() {
|
||||||
if lhs.syntax().text_range() == name_ref.syntax().text_range() {
|
if lhs.syntax().text_range().end() == name_ref.syntax().text_range().end() {
|
||||||
return Some(ReferenceAccess::Write);
|
return Some(ReferenceAccess::Write);
|
||||||
}
|
} else if name_ref.syntax().text_range().is_subrange(&lhs.syntax().text_range()) {
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(rhs) = expr.rhs() {
|
|
||||||
if rhs.syntax().text_range().is_subrange(&name_ref.syntax().text_range()) {
|
|
||||||
return Some(ReferenceAccess::Read);
|
return Some(ReferenceAccess::Read);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
_ => { return Some(ReferenceAccess::Read) },
|
// If the variable is on the RHS then it's a Read.
|
||||||
|
if let Some(rhs) = expr.rhs() {
|
||||||
|
if name_ref.syntax().text_range().is_subrange(&rhs.syntax().text_range()) {
|
||||||
|
return Some(ReferenceAccess::Read);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cannot determine access
|
||||||
None
|
None
|
||||||
},
|
},
|
||||||
_ => {None}
|
_ => {None}
|
||||||
|
@ -565,7 +569,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_basic_highlight_read() {
|
fn test_basic_highlight_read_write() {
|
||||||
let code = r#"
|
let code = r#"
|
||||||
fn foo() {
|
fn foo() {
|
||||||
let i<|> = 0;
|
let i<|> = 0;
|
||||||
|
@ -578,6 +582,24 @@ mod tests {
|
||||||
assert_eq!(refs.references[1].access, Some(ReferenceAccess::Read));
|
assert_eq!(refs.references[1].access, Some(ReferenceAccess::Read));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_basic_highlight_field_read_write() {
|
||||||
|
let code = r#"
|
||||||
|
struct S {
|
||||||
|
f: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo() {
|
||||||
|
let mut s = S{f: 0};
|
||||||
|
s.f<|> = 0;
|
||||||
|
}"#;
|
||||||
|
|
||||||
|
let refs = get_all_refs(code);
|
||||||
|
assert_eq!(refs.len(), 3);
|
||||||
|
//assert_eq!(refs.references[0].access, Some(ReferenceAccess::Write));
|
||||||
|
assert_eq!(refs.references[1].access, Some(ReferenceAccess::Write));
|
||||||
|
}
|
||||||
|
|
||||||
fn get_all_refs(text: &str) -> ReferenceSearchResult {
|
fn get_all_refs(text: &str) -> ReferenceSearchResult {
|
||||||
let (analysis, position) = single_file_with_position(text);
|
let (analysis, position) = single_file_with_position(text);
|
||||||
analysis.find_all_refs(position, None).unwrap().unwrap()
|
analysis.find_all_refs(position, None).unwrap().unwrap()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue