From 14b66bb4589569895d35742e995d1d1670b912cc Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 23 Jun 2021 14:56:40 +0200 Subject: [PATCH] Rename 'document_highlight' to 'highlight_related' --- ...t_highlight.rs => highlight_references.rs} | 52 +++++++++++++++---- crates/ide/src/lib.rs | 8 +-- crates/ide/src/references.rs | 8 +-- crates/rust-analyzer/src/handlers.rs | 2 +- 4 files changed, 51 insertions(+), 19 deletions(-) rename crates/ide/src/{document_highlight.rs => highlight_references.rs} (65%) diff --git a/crates/ide/src/document_highlight.rs b/crates/ide/src/highlight_references.rs similarity index 65% rename from crates/ide/src/document_highlight.rs rename to crates/ide/src/highlight_references.rs index ad202cf2705..78e5a9f04e0 100644 --- a/crates/ide/src/document_highlight.rs +++ b/crates/ide/src/highlight_references.rs @@ -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, } -// 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, position: FilePosition, ) -> Option> { 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> { + None +} + +fn highlight_yield_points() -> Option> { + None +} + +fn highlight_references( + sema: &Semantics, + syntax: &SyntaxNode, + FilePosition { offset, file_id }: FilePosition, +) -> Option> { + 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() diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 9661b8d4ad1..5e3420c0461 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -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>> { - 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. diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index 4aed2b526bd..c0ede0b8812 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs @@ -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, syntax: &SyntaxNode, - position: FilePosition, + offset: TextSize, ) -> Option { - 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) } diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index d83ae25e3e2..66f07ec6c4f 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -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, };