7229: Cleanup r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2021-01-10 11:24:52 +00:00 committed by GitHub
commit 1ed1c14b2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 37 deletions

View file

@ -17,7 +17,7 @@ use crate::{
doc_links::{remove_links, rewrite_links}, doc_links::{remove_links, rewrite_links},
markdown_remove::remove_markdown, markdown_remove::remove_markdown,
markup::Markup, markup::Markup,
runnables::{runnable, runnable_fn}, runnables::{runnable_fn, runnable_mod},
FileId, FilePosition, NavigationTarget, RangeInfo, Runnable, FileId, FilePosition, NavigationTarget, RangeInfo, Runnable,
}; };
@ -192,7 +192,7 @@ fn runnable_action(
Definition::ModuleDef(it) => match it { Definition::ModuleDef(it) => match it {
ModuleDef::Module(it) => match it.definition_source(sema.db).value { ModuleDef::Module(it) => match it.definition_source(sema.db).value {
ModuleSource::Module(it) => { ModuleSource::Module(it) => {
runnable(&sema, it.syntax().clone()).map(|it| HoverAction::Runnable(it)) runnable_mod(&sema, it).map(|it| HoverAction::Runnable(it))
} }
_ => None, _ => None,
}, },

View file

@ -96,21 +96,23 @@ impl Runnable {
pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> { pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
let sema = Semantics::new(db); let sema = Semantics::new(db);
let source_file = sema.parse(file_id); let source_file = sema.parse(file_id);
source_file.syntax().descendants().filter_map(|i| runnable(&sema, i)).collect() source_file
} .syntax()
.descendants()
pub(crate) fn runnable(sema: &Semantics<RootDatabase>, item: SyntaxNode) -> Option<Runnable> { .filter_map(|item| {
let runnable_item = match_ast! { let runnable = match_ast! {
match (item.clone()) { match item {
ast::Fn(func) => { ast::Fn(func) => {
let def = sema.to_def(&func)?; let def = sema.to_def(&func)?;
runnable_fn(sema, def) runnable_fn(&sema, def)
}, },
ast::Module(it) => runnable_mod(sema, it), ast::Module(it) => runnable_mod(&sema, it),
_ => None, _ => None,
} }
}; };
runnable_item.or_else(|| runnable_doctest(sema, item)) runnable.or_else(|| runnable_doctest(&sema, item))
})
.collect()
} }
pub(crate) fn runnable_fn(sema: &Semantics<RootDatabase>, def: hir::Function) -> Option<Runnable> { pub(crate) fn runnable_fn(sema: &Semantics<RootDatabase>, def: hir::Function) -> Option<Runnable> {
@ -145,6 +147,29 @@ pub(crate) fn runnable_fn(sema: &Semantics<RootDatabase>, def: hir::Function) ->
Some(Runnable { nav, kind, cfg }) Some(Runnable { nav, kind, cfg })
} }
pub(crate) fn runnable_mod(
sema: &Semantics<RootDatabase>,
module: ast::Module,
) -> Option<Runnable> {
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<RootDatabase>, item: SyntaxNode) -> Option<Runnable> { fn runnable_doctest(sema: &Semantics<RootDatabase>, item: SyntaxNode) -> Option<Runnable> {
match_ast! { match_ast! {
match item { match item {
@ -253,26 +278,6 @@ fn has_runnable_doc_test(attrs: &hir::Attrs) -> bool {
}) })
} }
fn runnable_mod(sema: &Semantics<RootDatabase>, module: ast::Module) -> Option<Runnable> {
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, // 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 // 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 { fn has_test_function_or_multiple_test_submodules(module: &ast::Module) -> bool {