]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide/src/runnables.rs
Unnest ide::display::navigation_target module
[rust.git] / crates / ide / src / runnables.rs
index b748d84ecc0100e216937ec3b16b1af46690aced..3078789d1236fe309b83813b7a3f9fce14f73059 100644 (file)
@@ -1,12 +1,12 @@
 use std::fmt;
 
-use ast::NameOwner;
+use ast::HasName;
 use cfg::CfgExpr;
-use either::Either;
 use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics};
 use ide_assists::utils::test_related_attribute;
 use ide_db::{
     base_db::{FilePosition, FileRange},
+    defs::Definition,
     helpers::visit_file_defs,
     search::SearchScope,
     RootDatabase, SymbolKind,
 use itertools::Itertools;
 use rustc_hash::{FxHashMap, FxHashSet};
 use stdx::{always, format_to};
-use syntax::ast::{self, AstNode, AttrsOwner};
+use syntax::{
+    ast::{self, AstNode, HasAttrs as _},
+    SmolStr, SyntaxNode,
+};
 
-use crate::{display::TryToNav, references, FileId, NavigationTarget};
+use crate::{references, FileId, NavigationTarget, ToNav, TryToNav};
 
 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
 pub struct Runnable {
@@ -28,7 +31,7 @@ pub struct Runnable {
 
 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
 pub enum TestId {
-    Name(String),
+    Name(SmolStr),
     Path(String),
 }
 
@@ -74,16 +77,6 @@ pub fn label(&self, target: Option<String>) -> String {
         }
     }
 
-    pub fn can_debug(&self) -> bool {
-        matches!(
-            &self.kind,
-            RunnableKind::TestMod { .. }
-                | RunnableKind::Test { .. }
-                | RunnableKind::Bench { .. }
-                | RunnableKind::Bin
-        )
-    }
-
     pub fn title(&self) -> String {
         let mut s = String::from("▶\u{fe0e} Run ");
         if self.use_name_in_title {
@@ -145,8 +138,8 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
         }) {
             if let Some(def) = def {
                 let file_id = match def {
-                    hir::ModuleDef::Module(it) => it.declaration_source(db).map(|src| src.file_id),
-                    hir::ModuleDef::Function(it) => it.source(db).map(|src| src.file_id),
+                    Definition::Module(it) => it.declaration_source(db).map(|src| src.file_id),
+                    Definition::Function(it) => it.source(db).map(|src| src.file_id),
                     _ => None,
                 };
                 if let Some(file_id) = file_id.filter(|file| file.call_node(db).is_some()) {
@@ -157,35 +150,32 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
             res.push(runnable);
         }
     };
-    visit_file_defs(&sema, file_id, &mut |def| match def {
-        Either::Left(def) => {
-            let runnable = match def {
-                hir::ModuleDef::Module(it) => runnable_mod(&sema, it),
-                hir::ModuleDef::Function(it) => runnable_fn(&sema, it),
-                _ => None,
-            };
-            add_opt(runnable.or_else(|| module_def_doctest(&sema, def)), Some(def));
-        }
-        Either::Right(impl_) => {
-            add_opt(runnable_impl(&sema, &impl_), None);
-            impl_
-                .items(db)
-                .into_iter()
-                .map(|assoc| {
-                    (
-                        match assoc {
-                            hir::AssocItem::Function(it) => runnable_fn(&sema, it)
-                                .or_else(|| module_def_doctest(&sema, it.into())),
-                            hir::AssocItem::Const(it) => module_def_doctest(&sema, it.into()),
-                            hir::AssocItem::TypeAlias(it) => module_def_doctest(&sema, it.into()),
-                        },
-                        assoc,
-                    )
-                })
-                .for_each(|(r, assoc)| add_opt(r, Some(assoc.into())));
+    visit_file_defs(&sema, file_id, &mut |def| {
+        let runnable = match def {
+            Definition::Module(it) => runnable_mod(&sema, it),
+            Definition::Function(it) => runnable_fn(&sema, it),
+            Definition::SelfType(impl_) => runnable_impl(&sema, &impl_),
+            _ => None,
+        };
+        add_opt(runnable.or_else(|| module_def_doctest(sema.db, def)), Some(def));
+        if let Definition::SelfType(impl_) = def {
+            impl_.items(db).into_iter().for_each(|assoc| {
+                let runnable = match assoc {
+                    hir::AssocItem::Function(it) => {
+                        runnable_fn(&sema, it).or_else(|| module_def_doctest(sema.db, it.into()))
+                    }
+                    hir::AssocItem::Const(it) => module_def_doctest(sema.db, it.into()),
+                    hir::AssocItem::TypeAlias(it) => module_def_doctest(sema.db, it.into()),
+                };
+                add_opt(runnable, Some(assoc.into()))
+            });
         }
     });
 
+    sema.to_module_defs(file_id)
+        .map(|it| runnable_mod_outline_definition(&sema, it))
+        .for_each(|it| add_opt(it, None));
+
     res.extend(in_macro_expansion.into_iter().flat_map(|(_, runnables)| {
         let use_name_in_title = runnables.len() != 1;
         runnables.into_iter().map(move |mut r| {
@@ -216,35 +206,43 @@ pub(crate) fn related_tests(
 ) -> Vec<Runnable> {
     let sema = Semantics::new(db);
     let mut res: FxHashSet<Runnable> = FxHashSet::default();
+    let syntax = sema.parse(position.file_id).syntax().clone();
 
-    find_related_tests(&sema, position, search_scope, &mut res);
+    find_related_tests(&sema, &syntax, position, search_scope, &mut res);
 
-    res.into_iter().collect_vec()
+    res.into_iter().collect()
 }
 
 fn find_related_tests(
     sema: &Semantics<RootDatabase>,
+    syntax: &SyntaxNode,
     position: FilePosition,
     search_scope: Option<SearchScope>,
     tests: &mut FxHashSet<Runnable>,
 ) {
-    if let Some(refs) = references::find_all_refs(sema, position, search_scope) {
-        for (file_id, refs) in refs.references {
-            let file = sema.parse(file_id);
-            let file = file.syntax();
-            let functions = refs.iter().filter_map(|(range, _)| {
-                let token = file.token_at_offset(range.start()).next()?;
-                let token = sema.descend_into_macros(token);
-                token.ancestors().find_map(ast::Fn::cast)
-            });
-
-            for fn_def in functions {
+    let defs = references::find_defs(sema, syntax, position.offset);
+    for def in defs {
+        let defs = def
+            .usages(sema)
+            .set_scope(search_scope.clone())
+            .all()
+            .references
+            .into_values()
+            .flatten();
+        for ref_ in defs {
+            let name_ref = match ref_.name {
+                ast::NameLike::NameRef(name_ref) => name_ref,
+                _ => continue,
+            };
+            if let Some(fn_def) =
+                sema.ancestors_with_macros(name_ref.syntax().clone()).find_map(ast::Fn::cast)
+            {
                 if let Some(runnable) = as_test_runnable(sema, &fn_def) {
                     // direct test
                     tests.insert(runnable);
                 } else if let Some(module) = parent_test_module(sema, &fn_def) {
                     // indirect test
-                    find_related_tests_in_module(sema, &fn_def, &module, tests);
+                    find_related_tests_in_module(sema, syntax, &fn_def, &module, tests);
                 }
             }
         }
@@ -253,23 +251,26 @@ fn find_related_tests(
 
 fn find_related_tests_in_module(
     sema: &Semantics<RootDatabase>,
+    syntax: &SyntaxNode,
     fn_def: &ast::Fn,
     parent_module: &hir::Module,
     tests: &mut FxHashSet<Runnable>,
 ) {
-    if let Some(fn_name) = fn_def.name() {
-        let mod_source = parent_module.definition_source(sema.db);
-        let range = match mod_source.value {
-            hir::ModuleSource::Module(m) => m.syntax().text_range(),
-            hir::ModuleSource::BlockExpr(b) => b.syntax().text_range(),
-            hir::ModuleSource::SourceFile(f) => f.syntax().text_range(),
-        };
+    let fn_name = match fn_def.name() {
+        Some(it) => it,
+        _ => return,
+    };
+    let mod_source = parent_module.definition_source(sema.db);
+    let range = match &mod_source.value {
+        hir::ModuleSource::Module(m) => m.syntax().text_range(),
+        hir::ModuleSource::BlockExpr(b) => b.syntax().text_range(),
+        hir::ModuleSource::SourceFile(f) => f.syntax().text_range(),
+    };
 
-        let file_id = mod_source.file_id.original_file(sema.db);
-        let mod_scope = SearchScope::file_range(FileRange { file_id, range });
-        let fn_pos = FilePosition { file_id, offset: fn_name.syntax().text_range().start() };
-        find_related_tests(sema, fn_pos, Some(mod_scope), tests)
-    }
+    let file_id = mod_source.file_id.original_file(sema.db);
+    let mod_scope = SearchScope::file_range(FileRange { file_id, range });
+    let fn_pos = FilePosition { file_id, offset: fn_name.syntax().text_range().start() };
+    find_related_tests(sema, syntax, fn_pos, Some(mod_scope), tests)
 }
 
 fn as_test_runnable(sema: &Semantics<RootDatabase>, fn_def: &ast::Fn) -> Option<Runnable> {
@@ -296,24 +297,26 @@ fn parent_test_module(sema: &Semantics<RootDatabase>, fn_def: &ast::Fn) -> Optio
 
 pub(crate) fn runnable_fn(sema: &Semantics<RootDatabase>, def: hir::Function) -> Option<Runnable> {
     let func = def.source(sema.db)?;
-    let name_string = def.name(sema.db).to_string();
+    let name = def.name(sema.db).to_smol_str();
 
     let root = def.module(sema.db).krate().root_module(sema.db);
 
-    let kind = if name_string == "main" && def.module(sema.db) == root {
+    let kind = if name == "main" && def.module(sema.db) == root {
         RunnableKind::Bin
     } else {
-        let canonical_path = {
-            let def: hir::ModuleDef = def.into();
-            def.canonical_path(sema.db)
+        let test_id = || {
+            let canonical_path = {
+                let def: hir::ModuleDef = def.into();
+                def.canonical_path(sema.db)
+            };
+            canonical_path.map(TestId::Path).unwrap_or(TestId::Name(name))
         };
-        let test_id = canonical_path.map(TestId::Path).unwrap_or(TestId::Name(name_string));
 
         if test_related_attribute(&func.value).is_some() {
             let attr = TestAttr::from_fn(&func.value);
-            RunnableKind::Test { test_id, attr }
+            RunnableKind::Test { test_id: test_id(), attr }
         } else if func.value.has_atom_attr("bench") {
-            RunnableKind::Bench { test_id }
+            RunnableKind::Bench { test_id: test_id() }
         } else {
             return None;
         }
@@ -321,7 +324,7 @@ pub(crate) fn runnable_fn(sema: &Semantics<RootDatabase>, def: hir::Function) ->
 
     let nav = NavigationTarget::from_named(
         sema.db,
-        func.as_ref().map(|it| it as &dyn ast::NameOwner),
+        func.as_ref().map(|it| it as &dyn ast::HasName),
         SymbolKind::Function,
     );
     let cfg = def.attrs(sema.db).cfg();
@@ -361,61 +364,82 @@ pub(crate) fn runnable_impl(sema: &Semantics<RootDatabase>, def: &hir::Impl) ->
     Some(Runnable { use_name_in_title: false, nav, kind: RunnableKind::DocTest { test_id }, cfg })
 }
 
-fn module_def_doctest(sema: &Semantics<RootDatabase>, def: hir::ModuleDef) -> Option<Runnable> {
+/// Creates a test mod runnable for outline modules at the top of their definition.
+fn runnable_mod_outline_definition(
+    sema: &Semantics<RootDatabase>,
+    def: hir::Module,
+) -> Option<Runnable> {
+    if !has_test_function_or_multiple_test_submodules(sema, &def) {
+        return None;
+    }
+    let path =
+        def.path_to_root(sema.db).into_iter().rev().filter_map(|it| it.name(sema.db)).join("::");
+
+    let attrs = def.attrs(sema.db);
+    let cfg = attrs.cfg();
+    match def.definition_source(sema.db).value {
+        hir::ModuleSource::SourceFile(_) => Some(Runnable {
+            use_name_in_title: false,
+            nav: def.to_nav(sema.db),
+            kind: RunnableKind::TestMod { path },
+            cfg,
+        }),
+        _ => None,
+    }
+}
+
+fn module_def_doctest(db: &RootDatabase, def: Definition) -> Option<Runnable> {
     let attrs = match def {
-        hir::ModuleDef::Module(it) => it.attrs(sema.db),
-        hir::ModuleDef::Function(it) => it.attrs(sema.db),
-        hir::ModuleDef::Adt(it) => it.attrs(sema.db),
-        hir::ModuleDef::Variant(it) => it.attrs(sema.db),
-        hir::ModuleDef::Const(it) => it.attrs(sema.db),
-        hir::ModuleDef::Static(it) => it.attrs(sema.db),
-        hir::ModuleDef::Trait(it) => it.attrs(sema.db),
-        hir::ModuleDef::TypeAlias(it) => it.attrs(sema.db),
-        hir::ModuleDef::BuiltinType(_) => return None,
+        Definition::Module(it) => it.attrs(db),
+        Definition::Function(it) => it.attrs(db),
+        Definition::Adt(it) => it.attrs(db),
+        Definition::Variant(it) => it.attrs(db),
+        Definition::Const(it) => it.attrs(db),
+        Definition::Static(it) => it.attrs(db),
+        Definition::Trait(it) => it.attrs(db),
+        Definition::TypeAlias(it) => it.attrs(db),
+        Definition::Macro(it) => it.attrs(db),
+        Definition::SelfType(it) => it.attrs(db),
+        _ => return None,
     };
     if !has_runnable_doc_test(&attrs) {
         return None;
     }
-    let def_name = def.name(sema.db).map(|it| it.to_string());
-    let test_id = def
-        .canonical_path(sema.db)
-        // This probably belongs to canonical path?
-        .map(|path| {
-            let assoc_def = match def {
-                hir::ModuleDef::Function(it) => it.as_assoc_item(sema.db),
-                hir::ModuleDef::Const(it) => it.as_assoc_item(sema.db),
-                hir::ModuleDef::TypeAlias(it) => it.as_assoc_item(sema.db),
-                _ => None,
-            };
-            // FIXME: this also looks very wrong
-            if let Some(assoc_def) = assoc_def {
-                if let hir::AssocItemContainer::Impl(imp) = assoc_def.container(sema.db) {
-                    let ty = imp.self_ty(sema.db);
-                    if let Some(adt) = ty.as_adt() {
-                        let name = adt.name(sema.db);
-                        let idx = path.rfind(':').map_or(0, |idx| idx + 1);
-                        let (prefix, suffix) = path.split_at(idx);
-                        let mut ty_args = ty.type_arguments().peekable();
-                        let params = if ty_args.peek().is_some() {
-                            format!(
-                                "<{}>",
-                                ty_args.format_with(", ", |ty, cb| cb(&ty.display(sema.db)))
-                            )
-                        } else {
-                            String::new()
-                        };
-                        return format!("{}{}{}::{}", prefix, name, params, suffix);
+    let def_name = def.name(db)?;
+    let path = (|| {
+        let mut path = String::new();
+        def.canonical_module_path(db)?
+            .flat_map(|it| it.name(db))
+            .for_each(|name| format_to!(path, "{}::", name));
+        // This probably belongs to canonical_path?
+        if let Some(assoc_item) = def.as_assoc_item(db) {
+            if let hir::AssocItemContainer::Impl(imp) = assoc_item.container(db) {
+                let ty = imp.self_ty(db);
+                if let Some(adt) = ty.as_adt() {
+                    let name = adt.name(db);
+                    let mut ty_args = ty.type_arguments().peekable();
+                    format_to!(path, "{}", name);
+                    if ty_args.peek().is_some() {
+                        format_to!(
+                            path,
+                            "<{}>",
+                            ty_args.format_with(", ", |ty, cb| cb(&ty.display(db)))
+                        );
                     }
+                    format_to!(path, "::{}", def_name);
+                    return Some(path);
                 }
             }
-            path
-        })
-        .map(TestId::Path)
-        .or_else(|| def_name.clone().map(TestId::Name))?;
+        }
+        format_to!(path, "{}", def_name);
+        Some(path)
+    })();
+
+    let test_id = path.map_or_else(|| TestId::Name(def_name.to_smol_str()), TestId::Path);
 
     let mut nav = match def {
-        hir::ModuleDef::Module(def) => NavigationTarget::from_module_to_decl(sema.db, def),
-        def => def.try_to_nav(sema.db)?,
+        Definition::Module(def) => NavigationTarget::from_module_to_decl(db, def),
+        def => def.try_to_nav(db)?,
     };
     nav.focus_range = None;
     nav.description = None;
@@ -551,7 +575,7 @@ mod not_a_root {
     fn main() {}
 }
 "#,
-            &[Bin, Test, Test, Bench],
+            &[Bin, Test, Test, Bench, TestMod],
             expect![[r#"
                 [
                     Runnable {
@@ -628,6 +652,21 @@ fn main() {}
                         },
                         cfg: None,
                     },
+                    Runnable {
+                        use_name_in_title: false,
+                        nav: NavigationTarget {
+                            file_id: FileId(
+                                0,
+                            ),
+                            full_range: 0..137,
+                            name: "",
+                            kind: Module,
+                        },
+                        kind: TestMod {
+                            path: "",
+                        },
+                        cfg: None,
+                    },
                 ]
             "#]],
         );
@@ -1153,7 +1192,7 @@ fn test_runnables_with_feature() {
 #[cfg(feature = "foo")]
 fn test_foo1() {}
 "#,
-            &[Test],
+            &[Test, TestMod],
             expect![[r#"
                 [
                     Runnable {
@@ -1184,6 +1223,21 @@ fn test_foo1() {}
                             ),
                         ),
                     },
+                    Runnable {
+                        use_name_in_title: false,
+                        nav: NavigationTarget {
+                            file_id: FileId(
+                                0,
+                            ),
+                            full_range: 0..51,
+                            name: "",
+                            kind: Module,
+                        },
+                        kind: TestMod {
+                            path: "",
+                        },
+                        cfg: None,
+                    },
                 ]
             "#]],
         );
@@ -1199,7 +1253,7 @@ fn test_runnables_with_features() {
 #[cfg(all(feature = "foo", feature = "bar"))]
 fn test_foo1() {}
 "#,
-            &[Test],
+            &[Test, TestMod],
             expect![[r#"
                 [
                     Runnable {
@@ -1240,6 +1294,21 @@ fn test_foo1() {}
                             ),
                         ),
                     },
+                    Runnable {
+                        use_name_in_title: false,
+                        nav: NavigationTarget {
+                            file_id: FileId(
+                                0,
+                            ),
+                            full_range: 0..73,
+                            name: "",
+                            kind: Module,
+                        },
+                        kind: TestMod {
+                            path: "",
+                        },
+                        cfg: None,
+                    },
                 ]
             "#]],
         );
@@ -1326,7 +1395,7 @@ mod tests {
 }
 gen2!();
 "#,
-            &[TestMod, TestMod, Test, Test],
+            &[TestMod, TestMod, TestMod, Test, Test],
             expect![[r#"
                 [
                     Runnable {
@@ -1346,6 +1415,21 @@ mod tests {
                         },
                         cfg: None,
                     },
+                    Runnable {
+                        use_name_in_title: false,
+                        nav: NavigationTarget {
+                            file_id: FileId(
+                                0,
+                            ),
+                            full_range: 0..237,
+                            name: "",
+                            kind: Module,
+                        },
+                        kind: TestMod {
+                            path: "",
+                        },
+                        cfg: None,
+                    },
                     Runnable {
                         use_name_in_title: true,
                         nav: NavigationTarget {
@@ -1370,7 +1454,6 @@ mod tests {
                                 0,
                             ),
                             full_range: 228..236,
-                            focus_range: 228..236,
                             name: "foo_test2",
                             kind: Function,
                         },
@@ -1391,7 +1474,6 @@ mod tests {
                                 0,
                             ),
                             full_range: 218..225,
-                            focus_range: 218..225,
                             name: "foo_test",
                             kind: Function,
                         },
@@ -1457,7 +1539,6 @@ fn foo2() {}
                                 0,
                             ),
                             full_range: 210..217,
-                            focus_range: 210..217,
                             name: "foo0",
                             kind: Function,
                         },
@@ -1478,7 +1559,6 @@ fn foo2() {}
                                 0,
                             ),
                             full_range: 210..217,
-                            focus_range: 210..217,
                             name: "foo1",
                             kind: Function,
                         },
@@ -1499,7 +1579,6 @@ fn foo2() {}
                                 0,
                             ),
                             full_range: 210..217,
-                            focus_range: 210..217,
                             name: "foo2",
                             kind: Function,
                         },
@@ -1589,7 +1668,7 @@ fn t0() {}
 #[test]
 fn t1() {}
 "#,
-            &[Test, Test],
+            &[Test, Test, TestMod],
             expect![[r#"
                 [
                     Runnable {
@@ -1634,6 +1713,103 @@ fn t1() {}
                         },
                         cfg: None,
                     },
+                    Runnable {
+                        use_name_in_title: false,
+                        nav: NavigationTarget {
+                            file_id: FileId(
+                                1,
+                            ),
+                            full_range: 0..39,
+                            name: "m",
+                            kind: Module,
+                        },
+                        kind: TestMod {
+                            path: "m",
+                        },
+                        cfg: None,
+                    },
+                ]
+            "#]],
+        );
+    }
+
+    #[test]
+    fn attributed_module() {
+        check(
+            r#"
+//- proc_macros: identity
+//- /lib.rs
+$0
+#[proc_macros::identity]
+mod module {
+    #[test]
+    fn t0() {}
+    #[test]
+    fn t1() {}
+}
+"#,
+            &[TestMod, Test, Test],
+            expect![[r#"
+                [
+                    Runnable {
+                        use_name_in_title: true,
+                        nav: NavigationTarget {
+                            file_id: FileId(
+                                0,
+                            ),
+                            full_range: 26..94,
+                            focus_range: 30..36,
+                            name: "module",
+                            kind: Module,
+                            description: "mod module",
+                        },
+                        kind: TestMod {
+                            path: "module",
+                        },
+                        cfg: None,
+                    },
+                    Runnable {
+                        use_name_in_title: true,
+                        nav: NavigationTarget {
+                            file_id: FileId(
+                                0,
+                            ),
+                            full_range: 43..65,
+                            focus_range: 58..60,
+                            name: "t0",
+                            kind: Function,
+                        },
+                        kind: Test {
+                            test_id: Path(
+                                "module::t0",
+                            ),
+                            attr: TestAttr {
+                                ignore: false,
+                            },
+                        },
+                        cfg: None,
+                    },
+                    Runnable {
+                        use_name_in_title: true,
+                        nav: NavigationTarget {
+                            file_id: FileId(
+                                0,
+                            ),
+                            full_range: 70..92,
+                            focus_range: 85..87,
+                            name: "t1",
+                            kind: Function,
+                        },
+                        kind: Test {
+                            test_id: Path(
+                                "module::t1",
+                            ),
+                            attr: TestAttr {
+                                ignore: false,
+                            },
+                        },
+                        cfg: None,
+                    },
                 ]
             "#]],
         );