diff --git a/crates/ide/src/highlight_related.rs b/crates/ide/src/highlight_related.rs index d2c6c3c9dc..c0a4080ded 100644 --- a/crates/ide/src/highlight_related.rs +++ b/crates/ide/src/highlight_related.rs @@ -6,15 +6,11 @@ use ide_db::{ search::{FileReference, ReferenceAccess, SearchScope}, RootDatabase, }; -use syntax::{ - ast, match_ast, AstNode, - SyntaxKind::{ASYNC_KW, AWAIT_KW, QUESTION, RETURN_KW, THIN_ARROW}, - SyntaxNode, SyntaxToken, TextRange, WalkEvent, -}; +use syntax::{ast, match_ast, AstNode, SyntaxNode, SyntaxToken, TextRange, WalkEvent, T}; use crate::{display::TryToNav, references, NavigationTarget}; -pub struct DocumentHighlight { +pub struct HighlightedRange { pub range: TextRange, pub access: Option, } @@ -28,19 +24,19 @@ pub struct DocumentHighlight { pub(crate) fn highlight_related( sema: &Semantics, position: FilePosition, -) -> Option> { - let _p = profile::span("document_highlight"); +) -> Option> { + let _p = profile::span("highlight_related"); let syntax = sema.parse(position.file_id).syntax().clone(); 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, + T![?] => 2, // prefer `?` when the cursor is sandwiched like `await$0?` + T![await] | T![async] | T![->] | T![return] => 1, _ => 0, })?; match token.kind() { - QUESTION | RETURN_KW | THIN_ARROW => highlight_exit_points(sema, token), - AWAIT_KW | ASYNC_KW => highlight_yield_points(token), + T![?] | T![return] | T![->] => highlight_exit_points(sema, token), + T![await] | T![async] => highlight_yield_points(token), _ => highlight_references(sema, &syntax, position), } } @@ -49,7 +45,7 @@ fn highlight_references( sema: &Semantics, syntax: &SyntaxNode, FilePosition { offset, file_id }: FilePosition, -) -> Option> { +) -> Option> { let def = references::find_def(sema, syntax, offset)?; let usages = def.usages(sema).set_scope(Some(SearchScope::single_file(file_id))).all(); @@ -63,7 +59,7 @@ fn highlight_references( .and_then(|decl| { let range = decl.focus_range?; let access = references::decl_access(&def, syntax, range); - Some(DocumentHighlight { range, access }) + Some(HighlightedRange { range, access }) }); let file_refs = usages.references.get(&file_id).map_or(&[][..], Vec::as_slice); @@ -72,7 +68,7 @@ fn highlight_references( res.extend( file_refs .iter() - .map(|&FileReference { access, range, .. }| DocumentHighlight { range, access }), + .map(|&FileReference { access, range, .. }| HighlightedRange { range, access }), ); Some(res) } @@ -80,24 +76,24 @@ fn highlight_references( fn highlight_exit_points( sema: &Semantics, token: SyntaxToken, -) -> Option> { +) -> Option> { fn hl( sema: &Semantics, body: Option, - ) -> Option> { + ) -> Option> { let mut highlights = Vec::new(); let body = body?; walk(&body, |node| { match_ast! { match node { ast::ReturnExpr(expr) => if let Some(token) = expr.return_token() { - highlights.push(DocumentHighlight { + highlights.push(HighlightedRange { access: None, range: token.text_range(), }); }, ast::TryExpr(try_) => if let Some(token) = try_.question_mark_token() { - highlights.push(DocumentHighlight { + highlights.push(HighlightedRange { access: None, range: token.text_range(), }); @@ -105,7 +101,7 @@ fn highlight_exit_points( ast::Expr(expr) => match expr { ast::Expr::MethodCallExpr(_) | ast::Expr::CallExpr(_) | ast::Expr::MacroCall(_) => { if sema.type_of_expr(&expr).map_or(false, |ty| ty.is_never()) { - highlights.push(DocumentHighlight { + highlights.push(HighlightedRange { access: None, range: expr.syntax().text_range(), }); @@ -128,7 +124,7 @@ fn highlight_exit_points( e => Some(e), }; if let Some(tail) = tail { - highlights.push(DocumentHighlight { access: None, range: tail.syntax().text_range() }); + highlights.push(HighlightedRange { access: None, range: tail.syntax().text_range() }); } Some(highlights) } @@ -149,19 +145,19 @@ fn highlight_exit_points( None } -fn highlight_yield_points(token: SyntaxToken) -> Option> { +fn highlight_yield_points(token: SyntaxToken) -> Option> { fn hl( async_token: Option, body: Option, - ) -> Option> { + ) -> Option> { let mut highlights = Vec::new(); - highlights.push(DocumentHighlight { access: None, range: async_token?.text_range() }); + highlights.push(HighlightedRange { access: None, range: async_token?.text_range() }); if let Some(body) = body { walk(&body, |node| { match_ast! { match node { ast::AwaitExpr(expr) => if let Some(token) = expr.await_token() { - highlights.push(DocumentHighlight { + highlights.push(HighlightedRange { access: None, range: token.text_range(), }); diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index bb10ce5e8a..dc1448f636 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -76,7 +76,7 @@ pub use crate::{ expand_macro::ExpandedMacro, file_structure::{StructureNode, StructureNodeKind}, folding_ranges::{Fold, FoldKind}, - highlight_related::DocumentHighlight, + highlight_related::HighlightedRange, hover::{HoverAction, HoverConfig, HoverDocFormat, HoverGotoTypeData, HoverResult}, inlay_hints::{InlayHint, InlayHintsConfig, InlayKind}, markup::Markup, @@ -489,7 +489,7 @@ impl Analysis { pub fn highlight_related( &self, position: FilePosition, - ) -> Cancellable>> { + ) -> Cancellable>> { self.with_db(|db| highlight_related::highlight_related(&Semantics::new(db), position)) } diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 66f07ec6c4..3e6ab70e46 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -1174,7 +1174,7 @@ pub(crate) fn handle_document_highlight( }; let res = refs .into_iter() - .map(|ide::DocumentHighlight { range, access }| lsp_types::DocumentHighlight { + .map(|ide::HighlightedRange { range, access }| lsp_types::DocumentHighlight { range: to_proto::range(&line_index, range), kind: access.map(to_proto::document_highlight_kind), })