mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-04 02:54:00 +00:00
Rename 'document_highlight' to 'highlight_related'
This commit is contained in:
parent
b26a8ecca1
commit
14b66bb458
@ -2,10 +2,15 @@ use hir::Semantics;
|
||||
use ide_db::{
|
||||
base_db::FilePosition,
|
||||
defs::Definition,
|
||||
helpers::pick_best_token,
|
||||
search::{FileReference, ReferenceAccess, SearchScope},
|
||||
RootDatabase,
|
||||
};
|
||||
use syntax::{AstNode, TextRange};
|
||||
use syntax::{
|
||||
AstNode,
|
||||
SyntaxKind::{ASYNC_KW, AWAIT_KW, QUESTION, RETURN_KW, THIN_ARROW},
|
||||
SyntaxNode, TextRange,
|
||||
};
|
||||
|
||||
use crate::{display::TryToNav, references, NavigationTarget};
|
||||
|
||||
@ -14,17 +19,44 @@ pub struct DocumentHighlight {
|
||||
pub access: Option<ReferenceAccess>,
|
||||
}
|
||||
|
||||
// Feature: Document highlight
|
||||
// Feature: Highlight related
|
||||
//
|
||||
// Highlights the definition and its all references of the item at the cursor location in the current file.
|
||||
pub(crate) fn document_highlight(
|
||||
// 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(
|
||||
sema: &Semantics<RootDatabase>,
|
||||
position: FilePosition,
|
||||
) -> Option<Vec<DocumentHighlight>> {
|
||||
let _p = profile::span("document_highlight");
|
||||
let syntax = sema.parse(position.file_id).syntax().clone();
|
||||
let def = references::find_def(sema, &syntax, position)?;
|
||||
let usages = def.usages(sema).set_scope(Some(SearchScope::single_file(position.file_id))).all();
|
||||
|
||||
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();
|
||||
|
||||
let declaration = match def {
|
||||
Definition::ModuleDef(hir::ModuleDef::Module(module)) => {
|
||||
@ -32,14 +64,14 @@ pub(crate) fn document_highlight(
|
||||
}
|
||||
def => def.try_to_nav(sema.db),
|
||||
}
|
||||
.filter(|decl| decl.file_id == position.file_id)
|
||||
.filter(|decl| decl.file_id == file_id)
|
||||
.and_then(|decl| {
|
||||
let range = decl.focus_range?;
|
||||
let access = references::decl_access(&def, &syntax, range);
|
||||
let access = references::decl_access(&def, syntax, range);
|
||||
Some(DocumentHighlight { range, access })
|
||||
});
|
||||
|
||||
let file_refs = usages.references.get(&position.file_id).map_or(&[][..], Vec::as_slice);
|
||||
let file_refs = usages.references.get(&file_id).map_or(&[][..], Vec::as_slice);
|
||||
let mut res = Vec::with_capacity(file_refs.len() + 1);
|
||||
res.extend(declaration);
|
||||
res.extend(
|
||||
@ -58,7 +90,7 @@ mod tests {
|
||||
|
||||
fn check(ra_fixture: &str) {
|
||||
let (analysis, pos, annotations) = fixture::annotations(ra_fixture);
|
||||
let hls = analysis.highlight_document(pos).unwrap().unwrap();
|
||||
let hls = analysis.highlight_related(pos).unwrap().unwrap();
|
||||
|
||||
let mut expected = annotations
|
||||
.into_iter()
|
@ -25,7 +25,7 @@ mod display;
|
||||
mod annotations;
|
||||
mod call_hierarchy;
|
||||
mod doc_links;
|
||||
mod document_highlight;
|
||||
mod highlight_references;
|
||||
mod expand_macro;
|
||||
mod extend_selection;
|
||||
mod file_structure;
|
||||
@ -73,10 +73,10 @@ pub use crate::{
|
||||
annotations::{Annotation, AnnotationConfig, AnnotationKind},
|
||||
call_hierarchy::CallItem,
|
||||
display::navigation_target::NavigationTarget,
|
||||
document_highlight::DocumentHighlight,
|
||||
expand_macro::ExpandedMacro,
|
||||
file_structure::{StructureNode, StructureNodeKind},
|
||||
folding_ranges::{Fold, FoldKind},
|
||||
highlight_references::DocumentHighlight,
|
||||
hover::{HoverAction, HoverConfig, HoverDocFormat, HoverGotoTypeData, HoverResult},
|
||||
inlay_hints::{InlayHint, InlayHintsConfig, InlayKind},
|
||||
markup::Markup,
|
||||
@ -486,11 +486,11 @@ impl Analysis {
|
||||
}
|
||||
|
||||
/// Computes all ranges to highlight for a given item in a file.
|
||||
pub fn highlight_document(
|
||||
pub fn highlight_related(
|
||||
&self,
|
||||
position: FilePosition,
|
||||
) -> Cancellable<Option<Vec<DocumentHighlight>>> {
|
||||
self.with_db(|db| document_highlight::document_highlight(&Semantics::new(db), position))
|
||||
self.with_db(|db| highlight_references::highlight_related(&Semantics::new(db), position))
|
||||
}
|
||||
|
||||
/// Computes syntax highlighting for the given file range.
|
||||
|
@ -20,7 +20,7 @@ use rustc_hash::FxHashMap;
|
||||
use syntax::{
|
||||
algo::find_node_at_offset,
|
||||
ast::{self, NameOwner},
|
||||
match_ast, AstNode, SyntaxNode, TextRange, T,
|
||||
match_ast, AstNode, SyntaxNode, TextRange, TextSize, T,
|
||||
};
|
||||
|
||||
use crate::{display::TryToNav, FilePosition, NavigationTarget};
|
||||
@ -60,7 +60,7 @@ pub(crate) fn find_all_refs(
|
||||
if let Some(name) = get_name_of_item_declaration(&syntax, position) {
|
||||
(NameClass::classify(sema, &name)?.referenced_or_defined(sema.db), true)
|
||||
} else {
|
||||
(find_def(sema, &syntax, position)?, false)
|
||||
(find_def(sema, &syntax, position.offset)?, false)
|
||||
};
|
||||
|
||||
let mut usages = def.usages(sema).set_scope(search_scope).include_self_refs().all();
|
||||
@ -113,9 +113,9 @@ pub(crate) fn find_all_refs(
|
||||
pub(crate) fn find_def(
|
||||
sema: &Semantics<RootDatabase>,
|
||||
syntax: &SyntaxNode,
|
||||
position: FilePosition,
|
||||
offset: TextSize,
|
||||
) -> Option<Definition> {
|
||||
let def = match sema.find_node_at_offset_with_descend(syntax, position.offset)? {
|
||||
let def = match sema.find_node_at_offset_with_descend(syntax, offset)? {
|
||||
ast::NameLike::NameRef(name_ref) => {
|
||||
NameRefClass::classify(sema, &name_ref)?.referenced(sema.db)
|
||||
}
|
||||
|
@ -1168,7 +1168,7 @@ pub(crate) fn handle_document_highlight(
|
||||
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
|
||||
let line_index = snap.file_line_index(position.file_id)?;
|
||||
|
||||
let refs = match snap.analysis.highlight_document(position)? {
|
||||
let refs = match snap.analysis.highlight_related(position)? {
|
||||
None => return Ok(None),
|
||||
Some(refs) => refs,
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user