Skip to content

Commit 1ed1c14

Browse files
bors[bot]matklad
andauthored
Merge #7229
7229: Cleanup r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 6a0a47d + 3a6ae42 commit 1ed1c14

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed

crates/ide/src/hover.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
doc_links::{remove_links, rewrite_links},
1818
markdown_remove::remove_markdown,
1919
markup::Markup,
20-
runnables::{runnable, runnable_fn},
20+
runnables::{runnable_fn, runnable_mod},
2121
FileId, FilePosition, NavigationTarget, RangeInfo, Runnable,
2222
};
2323

@@ -192,7 +192,7 @@ fn runnable_action(
192192
Definition::ModuleDef(it) => match it {
193193
ModuleDef::Module(it) => match it.definition_source(sema.db).value {
194194
ModuleSource::Module(it) => {
195-
runnable(&sema, it.syntax().clone()).map(|it| HoverAction::Runnable(it))
195+
runnable_mod(&sema, it).map(|it| HoverAction::Runnable(it))
196196
}
197197
_ => None,
198198
},

crates/ide/src/runnables.rs

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,23 @@ impl Runnable {
9696
pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
9797
let sema = Semantics::new(db);
9898
let source_file = sema.parse(file_id);
99-
source_file.syntax().descendants().filter_map(|i| runnable(&sema, i)).collect()
100-
}
101-
102-
pub(crate) fn runnable(sema: &Semantics<RootDatabase>, item: SyntaxNode) -> Option<Runnable> {
103-
let runnable_item = match_ast! {
104-
match (item.clone()) {
105-
ast::Fn(func) => {
106-
let def = sema.to_def(&func)?;
107-
runnable_fn(sema, def)
108-
},
109-
ast::Module(it) => runnable_mod(sema, it),
110-
_ => None,
111-
}
112-
};
113-
runnable_item.or_else(|| runnable_doctest(sema, item))
99+
source_file
100+
.syntax()
101+
.descendants()
102+
.filter_map(|item| {
103+
let runnable = match_ast! {
104+
match item {
105+
ast::Fn(func) => {
106+
let def = sema.to_def(&func)?;
107+
runnable_fn(&sema, def)
108+
},
109+
ast::Module(it) => runnable_mod(&sema, it),
110+
_ => None,
111+
}
112+
};
113+
runnable.or_else(|| runnable_doctest(&sema, item))
114+
})
115+
.collect()
114116
}
115117

116118
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) ->
145147
Some(Runnable { nav, kind, cfg })
146148
}
147149

150+
pub(crate) fn runnable_mod(
151+
sema: &Semantics<RootDatabase>,
152+
module: ast::Module,
153+
) -> Option<Runnable> {
154+
if !has_test_function_or_multiple_test_submodules(&module) {
155+
return None;
156+
}
157+
let module_def = sema.to_def(&module)?;
158+
159+
let path = module_def
160+
.path_to_root(sema.db)
161+
.into_iter()
162+
.rev()
163+
.filter_map(|it| it.name(sema.db))
164+
.join("::");
165+
166+
let def = sema.to_def(&module)?;
167+
let attrs = def.attrs(sema.db);
168+
let cfg = attrs.cfg();
169+
let nav = module_def.to_nav(sema.db);
170+
Some(Runnable { nav, kind: RunnableKind::TestMod { path }, cfg })
171+
}
172+
148173
fn runnable_doctest(sema: &Semantics<RootDatabase>, item: SyntaxNode) -> Option<Runnable> {
149174
match_ast! {
150175
match item {
@@ -253,26 +278,6 @@ fn has_runnable_doc_test(attrs: &hir::Attrs) -> bool {
253278
})
254279
}
255280

256-
fn runnable_mod(sema: &Semantics<RootDatabase>, module: ast::Module) -> Option<Runnable> {
257-
if !has_test_function_or_multiple_test_submodules(&module) {
258-
return None;
259-
}
260-
let module_def = sema.to_def(&module)?;
261-
262-
let path = module_def
263-
.path_to_root(sema.db)
264-
.into_iter()
265-
.rev()
266-
.filter_map(|it| it.name(sema.db))
267-
.join("::");
268-
269-
let def = sema.to_def(&module)?;
270-
let attrs = def.attrs(sema.db);
271-
let cfg = attrs.cfg();
272-
let nav = module_def.to_nav(sema.db);
273-
Some(Runnable { nav, kind: RunnableKind::TestMod { path }, cfg })
274-
}
275-
276281
// We could create runnables for modules with number_of_test_submodules > 0,
277282
// but that bloats the runnables for no real benefit, since all tests can be run by the submodule already
278283
fn has_test_function_or_multiple_test_submodules(module: &ast::Module) -> bool {

0 commit comments

Comments
 (0)