2021-06-22 17:39:59 +00:00
|
|
|
use hir::Semantics;
|
|
|
|
use ide_db::{
|
|
|
|
base_db::FilePosition,
|
|
|
|
defs::Definition,
|
2021-06-23 12:56:40 +00:00
|
|
|
helpers::pick_best_token,
|
2021-06-22 17:39:59 +00:00
|
|
|
search::{FileReference, ReferenceAccess, SearchScope},
|
|
|
|
RootDatabase,
|
|
|
|
};
|
2021-06-23 12:56:40 +00:00
|
|
|
use syntax::{
|
|
|
|
AstNode,
|
|
|
|
SyntaxKind::{ASYNC_KW, AWAIT_KW, QUESTION, RETURN_KW, THIN_ARROW},
|
|
|
|
SyntaxNode, TextRange,
|
|
|
|
};
|
2021-06-22 17:39:59 +00:00
|
|
|
|
|
|
|
use crate::{display::TryToNav, references, NavigationTarget};
|
|
|
|
|
|
|
|
pub struct DocumentHighlight {
|
|
|
|
pub range: TextRange,
|
|
|
|
pub access: Option<ReferenceAccess>,
|
|
|
|
}
|
|
|
|
|
2021-06-23 12:56:40 +00:00
|
|
|
// Feature: Highlight related
|
2021-06-22 17:39:59 +00:00
|
|
|
//
|
2021-06-23 12:56:40 +00:00
|
|
|
// Highlights exit points, yield points or the definition and all references of the item at the cursor location in the current file.
|
|
|
|
pub(crate) fn highlight_related(
|
2021-06-22 17:39:59 +00:00
|
|
|
sema: &Semantics<RootDatabase>,
|
|
|
|
position: FilePosition,
|
|
|
|
) -> Option<Vec<DocumentHighlight>> {
|
|
|
|
let _p = profile::span("document_highlight");
|
|
|
|
let syntax = sema.parse(position.file_id).syntax().clone();
|
2021-06-23 12:56:40 +00:00
|
|
|
|
|
|
|
let token = pick_best_token(syntax.token_at_offset(position.offset), |kind| match kind {
|
|
|
|
QUESTION => 2, // prefer `?` when the cursor is sandwiched like `await$0?`
|
|
|
|
AWAIT_KW | ASYNC_KW | THIN_ARROW | RETURN_KW => 1,
|
|
|
|
_ => 0,
|
|
|
|
})?;
|
|
|
|
|
|
|
|
match token.kind() {
|
|
|
|
QUESTION | RETURN_KW | THIN_ARROW => highlight_exit_points(),
|
|
|
|
AWAIT_KW | ASYNC_KW => highlight_yield_points(),
|
|
|
|
_ => highlight_references(sema, &syntax, position),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn highlight_exit_points() -> Option<Vec<DocumentHighlight>> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn highlight_yield_points() -> Option<Vec<DocumentHighlight>> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn highlight_references(
|
|
|
|
sema: &Semantics<RootDatabase>,
|
|
|
|
syntax: &SyntaxNode,
|
|
|
|
FilePosition { offset, file_id }: FilePosition,
|
|
|
|
) -> Option<Vec<DocumentHighlight>> {
|
|
|
|
let def = references::find_def(sema, syntax, offset)?;
|
|
|
|
let usages = def.usages(sema).set_scope(Some(SearchScope::single_file(file_id))).all();
|
2021-06-22 17:39:59 +00:00
|
|
|
|
|
|
|
let declaration = match def {
|
|
|
|
Definition::ModuleDef(hir::ModuleDef::Module(module)) => {
|
|
|
|
Some(NavigationTarget::from_module_to_decl(sema.db, module))
|
|
|
|
}
|
|
|
|
def => def.try_to_nav(sema.db),
|
|
|
|
}
|
2021-06-23 12:56:40 +00:00
|
|
|
.filter(|decl| decl.file_id == file_id)
|
2021-06-22 17:39:59 +00:00
|
|
|
.and_then(|decl| {
|
|
|
|
let range = decl.focus_range?;
|
2021-06-23 12:56:40 +00:00
|
|
|
let access = references::decl_access(&def, syntax, range);
|
2021-06-22 17:39:59 +00:00
|
|
|
Some(DocumentHighlight { range, access })
|
|
|
|
});
|
|
|
|
|
2021-06-23 12:56:40 +00:00
|
|
|
let file_refs = usages.references.get(&file_id).map_or(&[][..], Vec::as_slice);
|
2021-06-22 17:39:59 +00:00
|
|
|
let mut res = Vec::with_capacity(file_refs.len() + 1);
|
|
|
|
res.extend(declaration);
|
|
|
|
res.extend(
|
|
|
|
file_refs
|
|
|
|
.iter()
|
|
|
|
.map(|&FileReference { access, range, .. }| DocumentHighlight { range, access }),
|
|
|
|
);
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::fixture;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
fn check(ra_fixture: &str) {
|
|
|
|
let (analysis, pos, annotations) = fixture::annotations(ra_fixture);
|
2021-06-23 12:56:40 +00:00
|
|
|
let hls = analysis.highlight_related(pos).unwrap().unwrap();
|
2021-06-22 17:39:59 +00:00
|
|
|
|
|
|
|
let mut expected = annotations
|
|
|
|
.into_iter()
|
|
|
|
.map(|(r, access)| (r.range, (!access.is_empty()).then(|| access)))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let mut actual = hls
|
|
|
|
.into_iter()
|
|
|
|
.map(|hl| {
|
|
|
|
(
|
|
|
|
hl.range,
|
|
|
|
hl.access.map(|it| {
|
|
|
|
match it {
|
|
|
|
ReferenceAccess::Read => "read",
|
|
|
|
ReferenceAccess::Write => "write",
|
|
|
|
}
|
|
|
|
.to_string()
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
actual.sort_by_key(|(range, _)| range.start());
|
|
|
|
expected.sort_by_key(|(range, _)| range.start());
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hl_module() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo$0;
|
|
|
|
// ^^^
|
|
|
|
//- /foo.rs
|
|
|
|
struct Foo;
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hl_self_in_crate_root() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
use self$0;
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hl_self_in_module() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
|
|
|
//- /foo.rs
|
|
|
|
use self$0;
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hl_local() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
fn foo() {
|
|
|
|
let mut bar = 3;
|
|
|
|
// ^^^ write
|
|
|
|
bar$0;
|
|
|
|
// ^^^ read
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|