]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide/src/highlight_related.rs
Unnest ide::display::navigation_target module
[rust.git] / crates / ide / src / highlight_related.rs
index 67ad263fa238169d570941b2cc71e3b6c59f4997..357b0d2455cd02598557e62c51a4416d6eaf340c 100644 (file)
@@ -1,23 +1,28 @@
 use hir::Semantics;
 use ide_db::{
-    base_db::FilePosition,
-    defs::{Definition, NameClass, NameRefClass},
-    helpers::{for_each_break_expr, for_each_tail_expr, pick_best_token},
-    search::{FileReference, ReferenceAccess, SearchScope},
+    base_db::{FileId, FilePosition},
+    defs::Definition,
+    helpers::{for_each_break_expr, for_each_tail_expr, node_ext::walk_expr, pick_best_token},
+    search::{FileReference, ReferenceCategory, SearchScope},
     RootDatabase,
 };
 use rustc_hash::FxHashSet;
 use syntax::{
-    ast::{self, LoopBodyOwner},
-    match_ast, AstNode, SyntaxNode, SyntaxToken, TextRange, TextSize, T,
+    ast::{self, HasLoopBody},
+    match_ast, AstNode,
+    SyntaxKind::IDENT,
+    SyntaxNode, SyntaxToken, TextRange, T,
 };
 
-use crate::{display::TryToNav, references, NavigationTarget};
+use crate::{references, NavigationTarget, TryToNav};
 
 #[derive(PartialEq, Eq, Hash)]
 pub struct HighlightedRange {
     pub range: TextRange,
-    pub access: Option<ReferenceAccess>,
+    // FIXME: This needs to be more precise. Reference category makes sense only
+    // for references, but we also have defs. And things like exit points are
+    // neither.
+    pub category: Option<ReferenceCategory>,
 }
 
 #[derive(Default, Clone)]
@@ -40,15 +45,16 @@ pub struct HighlightRelatedConfig {
 pub(crate) fn highlight_related(
     sema: &Semantics<RootDatabase>,
     config: HighlightRelatedConfig,
-    position: FilePosition,
+    FilePosition { offset, file_id }: FilePosition,
 ) -> Option<Vec<HighlightedRange>> {
     let _p = profile::span("highlight_related");
-    let syntax = sema.parse(position.file_id).syntax().clone();
+    let syntax = sema.parse(file_id).syntax().clone();
 
-    let token = pick_best_token(syntax.token_at_offset(position.offset), |kind| match kind {
-        T![?] => 3, // prefer `?` when the cursor is sandwiched like in `await$0?`
-        T![->] => 2,
-        kind if kind.is_keyword() => 1,
+    let token = pick_best_token(syntax.token_at_offset(offset), |kind| match kind {
+        T![?] => 4, // prefer `?` when the cursor is sandwiched like in `await$0?`
+        T![->] => 3,
+        kind if kind.is_keyword() => 2,
+        IDENT => 1,
         _ => 0,
     })?;
 
@@ -62,20 +68,21 @@ pub(crate) fn highlight_related(
             highlight_break_points(token)
         }
         T![break] | T![loop] | T![while] if config.break_points => highlight_break_points(token),
-        _ if config.references => highlight_references(sema, &syntax, position),
+        _ if config.references => highlight_references(sema, &syntax, token, file_id),
         _ => None,
     }
 }
 
 fn highlight_references(
     sema: &Semantics<RootDatabase>,
-    syntax: &SyntaxNode,
-    FilePosition { offset, file_id }: FilePosition,
+    node: &SyntaxNode,
+    token: SyntaxToken,
+    file_id: FileId,
 ) -> Option<Vec<HighlightedRange>> {
-    let defs = find_defs(sema, syntax, offset);
+    let defs = find_defs(sema, token.clone());
     let usages = defs
         .iter()
-        .flat_map(|&d| {
+        .filter_map(|&d| {
             d.usages(sema)
                 .set_scope(Some(SearchScope::single_file(file_id)))
                 .include_self_refs()
@@ -84,11 +91,14 @@ fn highlight_references(
                 .remove(&file_id)
         })
         .flatten()
-        .map(|FileReference { access, range, .. }| HighlightedRange { range, access });
+        .map(|FileReference { category: access, range, .. }| HighlightedRange {
+            range,
+            category: access,
+        });
 
     let declarations = defs.iter().flat_map(|def| {
         match def {
-            &Definition::ModuleDef(hir::ModuleDef::Module(module)) => {
+            &Definition::Module(module) => {
                 Some(NavigationTarget::from_module_to_decl(sema.db, module))
             }
             def => def.try_to_nav(sema.db),
@@ -96,8 +106,9 @@ fn highlight_references(
         .filter(|decl| decl.file_id == file_id)
         .and_then(|decl| {
             let range = decl.focus_range?;
-            let access = references::decl_access(&def, syntax, range);
-            Some(HighlightedRange { range, access })
+            let category =
+                references::decl_mutability(&def, node, range).then(|| ReferenceCategory::Write);
+            Some(HighlightedRange { range, category })
         })
     });
 
@@ -119,21 +130,23 @@ fn hl(
     ) -> Option<Vec<HighlightedRange>> {
         let mut highlights = Vec::new();
         let body = body?;
-        body.walk(&mut |expr| match expr {
+        walk_expr(&body, &mut |expr| match expr {
             ast::Expr::ReturnExpr(expr) => {
                 if let Some(token) = expr.return_token() {
-                    highlights.push(HighlightedRange { access: None, range: token.text_range() });
+                    highlights.push(HighlightedRange { category: None, range: token.text_range() });
                 }
             }
             ast::Expr::TryExpr(try_) => {
                 if let Some(token) = try_.question_mark_token() {
-                    highlights.push(HighlightedRange { access: None, range: token.text_range() });
+                    highlights.push(HighlightedRange { category: None, range: token.text_range() });
                 }
             }
             ast::Expr::MethodCallExpr(_) | ast::Expr::CallExpr(_) | ast::Expr::MacroCall(_) => {
                 if sema.type_of_expr(&expr).map_or(false, |ty| ty.original.is_never()) {
-                    highlights
-                        .push(HighlightedRange { access: None, range: expr.syntax().text_range() });
+                    highlights.push(HighlightedRange {
+                        category: None,
+                        range: expr.syntax().text_range(),
+                    });
                 }
             }
             _ => (),
@@ -151,7 +164,7 @@ fn hl(
                         .map_or_else(|| tail.syntax().text_range(), |tok| tok.text_range()),
                     _ => tail.syntax().text_range(),
                 };
-                highlights.push(HighlightedRange { access: None, range })
+                highlights.push(HighlightedRange { category: None, range })
             });
         }
         Some(highlights)
@@ -161,8 +174,8 @@ fn hl(
             match anc {
                 ast::Fn(fn_) => hl(sema, fn_.body().map(ast::Expr::BlockExpr)),
                 ast::ClosureExpr(closure) => hl(sema, closure.body()),
-                ast::EffectExpr(effect) => if matches!(effect.effect(), ast::Effect::Async(_) | ast::Effect::Try(_)| ast::Effect::Const(_)) {
-                    hl(sema, effect.block_expr().map(ast::Expr::BlockExpr))
+                ast::BlockExpr(block_expr) => if matches!(block_expr.modifier(), Some(ast::BlockModifier::Async(_) | ast::BlockModifier::Try(_)| ast::BlockModifier::Const(_))) {
+                    hl(sema, Some(block_expr.into()))
                 } else {
                     continue;
                 },
@@ -177,20 +190,20 @@ fn highlight_break_points(token: SyntaxToken) -> Option<Vec<HighlightedRange>> {
     fn hl(
         token: Option<SyntaxToken>,
         label: Option<ast::Label>,
-        body: Option<ast::BlockExpr>,
+        body: Option<ast::StmtList>,
     ) -> Option<Vec<HighlightedRange>> {
         let mut highlights = Vec::new();
         let range = cover_range(
             token.map(|tok| tok.text_range()),
             label.as_ref().map(|it| it.syntax().text_range()),
         );
-        highlights.extend(range.map(|range| HighlightedRange { access: None, range }));
+        highlights.extend(range.map(|range| HighlightedRange { category: None, range }));
         for_each_break_expr(label, body, &mut |break_| {
             let range = cover_range(
                 break_.break_token().map(|it| it.text_range()),
                 break_.lifetime().map(|it| it.syntax().text_range()),
             );
-            highlights.extend(range.map(|range| HighlightedRange { access: None, range }));
+            highlights.extend(range.map(|range| HighlightedRange { category: None, range }));
         });
         Some(highlights)
     }
@@ -201,7 +214,7 @@ fn hl(
             ast::LoopExpr(l) => l.label().and_then(|it| it.lifetime()),
             ast::ForExpr(f) => f.label().and_then(|it| it.lifetime()),
             ast::WhileExpr(w) => w.label().and_then(|it| it.lifetime()),
-            ast::EffectExpr(b) => Some(b.label().and_then(|it| it.lifetime())?),
+            ast::BlockExpr(b) => Some(b.label().and_then(|it| it.lifetime())?),
             _ => return None,
         }
     };
@@ -215,16 +228,16 @@ fn hl(
     for anc in token.ancestors().flat_map(ast::Expr::cast) {
         return match anc {
             ast::Expr::LoopExpr(l) if label_matches(l.label()) => {
-                hl(l.loop_token(), l.label(), l.loop_body())
+                hl(l.loop_token(), l.label(), l.loop_body().and_then(|it| it.stmt_list()))
             }
             ast::Expr::ForExpr(f) if label_matches(f.label()) => {
-                hl(f.for_token(), f.label(), f.loop_body())
+                hl(f.for_token(), f.label(), f.loop_body().and_then(|it| it.stmt_list()))
             }
             ast::Expr::WhileExpr(w) if label_matches(w.label()) => {
-                hl(w.while_token(), w.label(), w.loop_body())
+                hl(w.while_token(), w.label(), w.loop_body().and_then(|it| it.stmt_list()))
             }
-            ast::Expr::EffectExpr(e) if e.label().is_some() && label_matches(e.label()) => {
-                hl(None, e.label(), e.block_expr())
+            ast::Expr::BlockExpr(e) if e.label().is_some() && label_matches(e.label()) => {
+                hl(None, e.label(), e.stmt_list())
             }
             _ => continue,
         };
@@ -238,13 +251,13 @@ fn hl(
         body: Option<ast::Expr>,
     ) -> Option<Vec<HighlightedRange>> {
         let mut highlights =
-            vec![HighlightedRange { access: None, range: async_token?.text_range() }];
+            vec![HighlightedRange { category: None, range: async_token?.text_range() }];
         if let Some(body) = body {
-            body.walk(&mut |expr| {
+            walk_expr(&body, &mut |expr| {
                 if let ast::Expr::AwaitExpr(expr) = expr {
                     if let Some(token) = expr.await_token() {
                         highlights
-                            .push(HighlightedRange { access: None, range: token.text_range() });
+                            .push(HighlightedRange { category: None, range: token.text_range() });
                     }
                 }
             });
@@ -255,7 +268,12 @@ fn hl(
         return match_ast! {
             match anc {
                 ast::Fn(fn_) => hl(fn_.async_token(), fn_.body().map(ast::Expr::BlockExpr)),
-                ast::EffectExpr(effect) => hl(effect.async_token(), effect.block_expr().map(ast::Expr::BlockExpr)),
+                ast::BlockExpr(block_expr) => {
+                    if block_expr.async_token().is_none() {
+                        continue;
+                    }
+                    hl(block_expr.async_token(), Some(block_expr.into()))
+                },
                 ast::ClosureExpr(closure) => hl(closure.async_token(), closure.body()),
                 _ => continue,
             }
@@ -273,43 +291,10 @@ fn cover_range(r0: Option<TextRange>, r1: Option<TextRange>) -> Option<TextRange
     }
 }
 
-fn find_defs(
-    sema: &Semantics<RootDatabase>,
-    syntax: &SyntaxNode,
-    offset: TextSize,
-) -> FxHashSet<Definition> {
-    sema.find_nodes_at_offset_with_descend(syntax, offset)
-        .flat_map(|name_like| {
-            Some(match name_like {
-                ast::NameLike::NameRef(name_ref) => {
-                    match NameRefClass::classify(sema, &name_ref)? {
-                        NameRefClass::Definition(def) => vec![def],
-                        NameRefClass::FieldShorthand { local_ref, field_ref } => {
-                            vec![Definition::Local(local_ref), Definition::Field(field_ref)]
-                        }
-                    }
-                }
-                ast::NameLike::Name(name) => match NameClass::classify(sema, &name)? {
-                    NameClass::Definition(it) | NameClass::ConstReference(it) => vec![it],
-                    NameClass::PatFieldShorthand { local_def, field_ref } => {
-                        vec![Definition::Local(local_def), Definition::Field(field_ref)]
-                    }
-                },
-                ast::NameLike::Lifetime(lifetime) => {
-                    NameRefClass::classify_lifetime(sema, &lifetime)
-                        .and_then(|class| match class {
-                            NameRefClass::Definition(it) => Some(it),
-                            _ => None,
-                        })
-                        .or_else(|| {
-                            NameClass::classify_lifetime(sema, &lifetime)
-                                .and_then(NameClass::defined)
-                        })
-                        .map(|it| vec![it])?
-                }
-            })
-        })
-        .flatten()
+fn find_defs(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> FxHashSet<Definition> {
+    sema.descend_into_macros(token)
+        .into_iter()
+        .flat_map(|token| Definition::from_token(sema, &token))
         .collect()
 }
 
@@ -333,7 +318,7 @@ fn check(ra_fixture: &str) {
     fn check_with_config(ra_fixture: &str, config: HighlightRelatedConfig) {
         let (analysis, pos, annotations) = fixture::annotations(ra_fixture);
 
-        let hls = analysis.highlight_related(config, pos).unwrap().unwrap_or(Vec::default());
+        let hls = analysis.highlight_related(config, pos).unwrap().unwrap_or_default();
 
         let mut expected = annotations
             .into_iter()
@@ -345,10 +330,10 @@ fn check_with_config(ra_fixture: &str, config: HighlightRelatedConfig) {
             .map(|hl| {
                 (
                     hl.range,
-                    hl.access.map(|it| {
+                    hl.category.map(|it| {
                         match it {
-                            ReferenceAccess::Read => "read",
-                            ReferenceAccess::Write => "write",
+                            ReferenceCategory::Read => "read",
+                            ReferenceCategory::Write => "write",
                         }
                         .to_string()
                     }),
@@ -410,6 +395,22 @@ fn foo() {
         );
     }
 
+    #[test]
+    fn test_hl_local_in_attr() {
+        check(
+            r#"
+//- proc_macros: identity
+#[proc_macros::identity]
+fn foo() {
+    let mut bar = 3;
+         // ^^^ write
+    bar$0;
+ // ^^^ read
+}
+"#,
+        );
+    }
+
     #[test]
     fn test_multi_macro_usage() {
         check(