3 base_db::{FileId, FilePosition},
5 helpers::{for_each_break_expr, for_each_tail_expr, node_ext::walk_expr, pick_best_token},
6 search::{FileReference, ReferenceCategory, SearchScope},
9 use rustc_hash::FxHashSet;
11 ast::{self, HasLoopBody},
14 SyntaxNode, SyntaxToken, TextRange, T,
17 use crate::{references, NavigationTarget, TryToNav};
19 #[derive(PartialEq, Eq, Hash)]
20 pub struct HighlightedRange {
22 // FIXME: This needs to be more precise. Reference category makes sense only
23 // for references, but we also have defs. And things like exit points are
25 pub category: Option<ReferenceCategory>,
28 #[derive(Default, Clone)]
29 pub struct HighlightRelatedConfig {
31 pub exit_points: bool,
32 pub break_points: bool,
33 pub yield_points: bool,
36 // Feature: Highlight Related
38 // Highlights constructs related to the thing under the cursor:
39 // - if on an identifier, highlights all references to that identifier in the current file
40 // - if on an `async` or `await token, highlights all yield points for that async context
41 // - if on a `return` or `fn` keyword, `?` character or `->` return type arrow, highlights all exit points for that context
42 // - if on a `break`, `loop`, `while` or `for` token, highlights all break points for that loop or block context
44 // Note: `?` and `->` do not currently trigger this behavior in the VSCode editor.
45 pub(crate) fn highlight_related(
46 sema: &Semantics<RootDatabase>,
47 config: HighlightRelatedConfig,
48 FilePosition { offset, file_id }: FilePosition,
49 ) -> Option<Vec<HighlightedRange>> {
50 let _p = profile::span("highlight_related");
51 let syntax = sema.parse(file_id).syntax().clone();
53 let token = pick_best_token(syntax.token_at_offset(offset), |kind| match kind {
54 T![?] => 4, // prefer `?` when the cursor is sandwiched like in `await$0?`
56 kind if kind.is_keyword() => 2,
62 T![?] if config.exit_points && token.parent().and_then(ast::TryExpr::cast).is_some() => {
63 highlight_exit_points(sema, token)
65 T![fn] | T![return] | T![->] if config.exit_points => highlight_exit_points(sema, token),
66 T![await] | T![async] if config.yield_points => highlight_yield_points(token),
67 T![for] if config.break_points && token.parent().and_then(ast::ForExpr::cast).is_some() => {
68 highlight_break_points(token)
70 T![break] | T![loop] | T![while] if config.break_points => highlight_break_points(token),
71 _ if config.references => highlight_references(sema, &syntax, token, file_id),
76 fn highlight_references(
77 sema: &Semantics<RootDatabase>,
81 ) -> Option<Vec<HighlightedRange>> {
82 let defs = find_defs(sema, token.clone());
87 .set_scope(Some(SearchScope::single_file(file_id)))
94 .map(|FileReference { category: access, range, .. }| HighlightedRange {
99 let declarations = defs.iter().flat_map(|def| {
101 &Definition::Module(module) => {
102 Some(NavigationTarget::from_module_to_decl(sema.db, module))
104 def => def.try_to_nav(sema.db),
106 .filter(|decl| decl.file_id == file_id)
108 let range = decl.focus_range?;
110 references::decl_mutability(&def, node, range).then(|| ReferenceCategory::Write);
111 Some(HighlightedRange { range, category })
115 let res: FxHashSet<_> = declarations.chain(usages).collect();
119 Some(res.into_iter().collect())
123 fn highlight_exit_points(
124 sema: &Semantics<RootDatabase>,
126 ) -> Option<Vec<HighlightedRange>> {
128 sema: &Semantics<RootDatabase>,
129 body: Option<ast::Expr>,
130 ) -> Option<Vec<HighlightedRange>> {
131 let mut highlights = Vec::new();
133 walk_expr(&body, &mut |expr| match expr {
134 ast::Expr::ReturnExpr(expr) => {
135 if let Some(token) = expr.return_token() {
136 highlights.push(HighlightedRange { category: None, range: token.text_range() });
139 ast::Expr::TryExpr(try_) => {
140 if let Some(token) = try_.question_mark_token() {
141 highlights.push(HighlightedRange { category: None, range: token.text_range() });
144 ast::Expr::MethodCallExpr(_) | ast::Expr::CallExpr(_) | ast::Expr::MacroCall(_) => {
145 if sema.type_of_expr(&expr).map_or(false, |ty| ty.original.is_never()) {
146 highlights.push(HighlightedRange {
148 range: expr.syntax().text_range(),
154 let tail = match body {
155 ast::Expr::BlockExpr(b) => b.tail_expr(),
159 if let Some(tail) = tail {
160 for_each_tail_expr(&tail, &mut |tail| {
161 let range = match tail {
162 ast::Expr::BreakExpr(b) => b
164 .map_or_else(|| tail.syntax().text_range(), |tok| tok.text_range()),
165 _ => tail.syntax().text_range(),
167 highlights.push(HighlightedRange { category: None, range })
172 for anc in token.ancestors() {
175 ast::Fn(fn_) => hl(sema, fn_.body().map(ast::Expr::BlockExpr)),
176 ast::ClosureExpr(closure) => hl(sema, closure.body()),
177 ast::BlockExpr(block_expr) => if matches!(block_expr.modifier(), Some(ast::BlockModifier::Async(_) | ast::BlockModifier::Try(_)| ast::BlockModifier::Const(_))) {
178 hl(sema, Some(block_expr.into()))
189 fn highlight_break_points(token: SyntaxToken) -> Option<Vec<HighlightedRange>> {
191 token: Option<SyntaxToken>,
192 label: Option<ast::Label>,
193 body: Option<ast::StmtList>,
194 ) -> Option<Vec<HighlightedRange>> {
195 let mut highlights = Vec::new();
196 let range = cover_range(
197 token.map(|tok| tok.text_range()),
198 label.as_ref().map(|it| it.syntax().text_range()),
200 highlights.extend(range.map(|range| HighlightedRange { category: None, range }));
201 for_each_break_expr(label, body, &mut |break_| {
202 let range = cover_range(
203 break_.break_token().map(|it| it.text_range()),
204 break_.lifetime().map(|it| it.syntax().text_range()),
206 highlights.extend(range.map(|range| HighlightedRange { category: None, range }));
210 let parent = token.parent()?;
211 let lbl = match_ast! {
213 ast::BreakExpr(b) => b.lifetime(),
214 ast::LoopExpr(l) => l.label().and_then(|it| it.lifetime()),
215 ast::ForExpr(f) => f.label().and_then(|it| it.lifetime()),
216 ast::WhileExpr(w) => w.label().and_then(|it| it.lifetime()),
217 ast::BlockExpr(b) => Some(b.label().and_then(|it| it.lifetime())?),
221 let lbl = lbl.as_ref();
222 let label_matches = |def_lbl: Option<ast::Label>| match lbl {
224 Some(lbl.text()) == def_lbl.and_then(|it| it.lifetime()).as_ref().map(|it| it.text())
228 for anc in token.ancestors().flat_map(ast::Expr::cast) {
230 ast::Expr::LoopExpr(l) if label_matches(l.label()) => {
231 hl(l.loop_token(), l.label(), l.loop_body().and_then(|it| it.stmt_list()))
233 ast::Expr::ForExpr(f) if label_matches(f.label()) => {
234 hl(f.for_token(), f.label(), f.loop_body().and_then(|it| it.stmt_list()))
236 ast::Expr::WhileExpr(w) if label_matches(w.label()) => {
237 hl(w.while_token(), w.label(), w.loop_body().and_then(|it| it.stmt_list()))
239 ast::Expr::BlockExpr(e) if e.label().is_some() && label_matches(e.label()) => {
240 hl(None, e.label(), e.stmt_list())
248 fn highlight_yield_points(token: SyntaxToken) -> Option<Vec<HighlightedRange>> {
250 async_token: Option<SyntaxToken>,
251 body: Option<ast::Expr>,
252 ) -> Option<Vec<HighlightedRange>> {
254 vec![HighlightedRange { category: None, range: async_token?.text_range() }];
255 if let Some(body) = body {
256 walk_expr(&body, &mut |expr| {
257 if let ast::Expr::AwaitExpr(expr) = expr {
258 if let Some(token) = expr.await_token() {
260 .push(HighlightedRange { category: None, range: token.text_range() });
267 for anc in token.ancestors() {
270 ast::Fn(fn_) => hl(fn_.async_token(), fn_.body().map(ast::Expr::BlockExpr)),
271 ast::BlockExpr(block_expr) => {
272 if block_expr.async_token().is_none() {
275 hl(block_expr.async_token(), Some(block_expr.into()))
277 ast::ClosureExpr(closure) => hl(closure.async_token(), closure.body()),
285 fn cover_range(r0: Option<TextRange>, r1: Option<TextRange>) -> Option<TextRange> {
287 (Some(r0), Some(r1)) => Some(r0.cover(r1)),
288 (Some(range), None) => Some(range),
289 (None, Some(range)) => Some(range),
290 (None, None) => None,
294 fn find_defs(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> FxHashSet<Definition> {
295 sema.descend_into_macros(token)
297 .flat_map(|token| Definition::from_token(sema, &token))
307 fn check(ra_fixture: &str) {
308 let config = HighlightRelatedConfig {
315 check_with_config(ra_fixture, config);
318 fn check_with_config(ra_fixture: &str, config: HighlightRelatedConfig) {
319 let (analysis, pos, annotations) = fixture::annotations(ra_fixture);
321 let hls = analysis.highlight_related(config, pos).unwrap().unwrap_or_default();
323 let mut expected = annotations
325 .map(|(r, access)| (r.range, (!access.is_empty()).then(|| access)))
326 .collect::<Vec<_>>();
333 hl.category.map(|it| {
335 ReferenceCategory::Read => "read",
336 ReferenceCategory::Write => "write",
342 .collect::<Vec<_>>();
343 actual.sort_by_key(|(range, _)| range.start());
344 expected.sort_by_key(|(range, _)| range.start());
346 assert_eq!(expected, actual);
350 fn test_hl_module() {
363 fn test_hl_self_in_crate_root() {
372 fn test_hl_self_in_module() {
399 fn test_hl_local_in_attr() {
402 //- proc_macros: identity
403 #[proc_macros::identity]
415 fn test_multi_macro_usage() {
420 fn $ident() -> $ident { loop {} }
428 let bar: bar = bar();
438 fn $ident() -> $ident { loop {} }
446 let bar: bar$0 = bar();
454 fn test_hl_yield_points() {
465 (async { 0.await }).await
473 fn test_hl_yield_points2() {
476 pub async$0 fn foo() {
484 (async { 0.await }).await
492 fn test_hl_yield_nested_fn() {
511 fn test_hl_yield_nested_async_blocks() {
528 fn test_hl_exit_points() {
547 fn test_hl_exit_points2() {
566 fn test_hl_exit_points3() {
585 fn test_hl_prefer_ref_over_tail_exit() {
604 fn test_hl_never_call_is_exit_point() {
609 fn never(self) -> ! { loop {} }
614 fn never() -> ! { loop {} }
619 // FIXME sema doesn't give us types for macrocalls
632 fn test_hl_inner_tail_exit_points() {
673 fn test_hl_inner_tail_exit_points_labeled_block() {
694 fn test_hl_break_loop() {
722 fn test_hl_break_loop2() {
749 fn test_hl_break_for() {
753 'outer: for _ in () {
757 'inner: for _ in () {
759 'innermost: for _ in () {
777 fn test_hl_break_while() {
787 'innermost: while true {
805 fn test_hl_break_labeled_block() {
833 fn test_hl_break_unlabeled_loop() {
848 fn test_hl_break_unlabeled_block_in_loop() {
865 fn test_hl_field_shorthand() {
868 struct Struct { field: u32 }
870 fn function(field: u32) {
880 fn test_hl_disabled_ref_local() {
881 let config = HighlightRelatedConfig {
894 check_with_config(ra_fixture, config);
898 fn test_hl_disabled_ref_local_preserved_break() {
899 let config = HighlightRelatedConfig {
916 check_with_config(ra_fixture, config.clone());
930 check_with_config(ra_fixture, config);
934 fn test_hl_disabled_ref_local_preserved_yield() {
935 let config = HighlightRelatedConfig {
950 check_with_config(ra_fixture, config.clone());
962 check_with_config(ra_fixture, config);
966 fn test_hl_disabled_ref_local_preserved_exit() {
967 let config = HighlightRelatedConfig {
986 check_with_config(ra_fixture, config.clone());
1002 check_with_config(ra_fixture, config);
1006 fn test_hl_disabled_break() {
1007 let config = HighlightRelatedConfig {
1009 break_points: false,
1014 let ra_fixture = r#"
1021 check_with_config(ra_fixture, config);
1025 fn test_hl_disabled_yield() {
1026 let config = HighlightRelatedConfig {
1030 yield_points: false,
1033 let ra_fixture = r#"
1038 check_with_config(ra_fixture, config);
1042 fn test_hl_disabled_exit() {
1043 let config = HighlightRelatedConfig {
1050 let ra_fixture = r#"
1059 check_with_config(ra_fixture, config);