From 3a6ae42eacabeef0332273db216bc287d4fff613 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 10 Jan 2021 14:24:01 +0300 Subject: Cleanup --- crates/ide/src/runnables.rs | 75 ++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 35 deletions(-) (limited to 'crates/ide/src/runnables.rs') diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs index 557563d7e..3a1e204db 100644 --- a/crates/ide/src/runnables.rs +++ b/crates/ide/src/runnables.rs @@ -96,21 +96,23 @@ impl Runnable { pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec { let sema = Semantics::new(db); let source_file = sema.parse(file_id); - source_file.syntax().descendants().filter_map(|i| runnable(&sema, i)).collect() -} - -pub(crate) fn runnable(sema: &Semantics, item: SyntaxNode) -> Option { - let runnable_item = match_ast! { - match (item.clone()) { - ast::Fn(func) => { - let def = sema.to_def(&func)?; - runnable_fn(sema, def) - }, - ast::Module(it) => runnable_mod(sema, it), - _ => None, - } - }; - runnable_item.or_else(|| runnable_doctest(sema, item)) + source_file + .syntax() + .descendants() + .filter_map(|item| { + let runnable = match_ast! { + match item { + ast::Fn(func) => { + let def = sema.to_def(&func)?; + runnable_fn(&sema, def) + }, + ast::Module(it) => runnable_mod(&sema, it), + _ => None, + } + }; + runnable.or_else(|| runnable_doctest(&sema, item)) + }) + .collect() } pub(crate) fn runnable_fn(sema: &Semantics, def: hir::Function) -> Option { @@ -145,6 +147,29 @@ pub(crate) fn runnable_fn(sema: &Semantics, def: hir::Function) -> Some(Runnable { nav, kind, cfg }) } +pub(crate) fn runnable_mod( + sema: &Semantics, + module: ast::Module, +) -> Option { + if !has_test_function_or_multiple_test_submodules(&module) { + return None; + } + let module_def = sema.to_def(&module)?; + + let path = module_def + .path_to_root(sema.db) + .into_iter() + .rev() + .filter_map(|it| it.name(sema.db)) + .join("::"); + + let def = sema.to_def(&module)?; + let attrs = def.attrs(sema.db); + let cfg = attrs.cfg(); + let nav = module_def.to_nav(sema.db); + Some(Runnable { nav, kind: RunnableKind::TestMod { path }, cfg }) +} + fn runnable_doctest(sema: &Semantics, item: SyntaxNode) -> Option { match_ast! { match item { @@ -253,26 +278,6 @@ fn has_runnable_doc_test(attrs: &hir::Attrs) -> bool { }) } -fn runnable_mod(sema: &Semantics, module: ast::Module) -> Option { - if !has_test_function_or_multiple_test_submodules(&module) { - return None; - } - let module_def = sema.to_def(&module)?; - - let path = module_def - .path_to_root(sema.db) - .into_iter() - .rev() - .filter_map(|it| it.name(sema.db)) - .join("::"); - - let def = sema.to_def(&module)?; - let attrs = def.attrs(sema.db); - let cfg = attrs.cfg(); - let nav = module_def.to_nav(sema.db); - Some(Runnable { nav, kind: RunnableKind::TestMod { path }, cfg }) -} - // We could create runnables for modules with number_of_test_submodules > 0, // but that bloats the runnables for no real benefit, since all tests can be run by the submodule already fn has_test_function_or_multiple_test_submodules(module: &ast::Module) -> bool { -- cgit v1.2.3