Skip to content

Commit 131849f

Browse files
bors[bot]bnjjj
andauthored
Merge #4491
4491: fix doctest inside impl block r=matklad a=bnjjj close #4449 Co-authored-by: Benjamin Coenen <[email protected]> Co-authored-by: Coenen Benjamin <[email protected]>
2 parents f6e70e7 + 01688f8 commit 131849f

File tree

1 file changed

+65
-5
lines changed

1 file changed

+65
-5
lines changed

crates/ra_ide/src/runnables.rs

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! FIXME: write short doc here
22
3-
use hir::Semantics;
3+
use hir::{AsAssocItem, Semantics};
44
use itertools::Itertools;
55
use ra_ide_db::RootDatabase;
66
use ra_syntax::{
@@ -65,14 +65,36 @@ fn runnable_fn(sema: &Semantics<RootDatabase>, fn_def: ast::FnDef) -> Option<Run
6565
RunnableKind::Bin
6666
} else {
6767
let test_id = if let Some(module) = sema.to_def(&fn_def).map(|def| def.module(sema.db)) {
68-
let path = module
68+
let def = sema.to_def(&fn_def)?;
69+
let impl_trait_name =
70+
def.as_assoc_item(sema.db).and_then(|assoc_item| {
71+
match assoc_item.container(sema.db) {
72+
hir::AssocItemContainer::Trait(trait_item) => {
73+
Some(trait_item.name(sema.db).to_string())
74+
}
75+
hir::AssocItemContainer::ImplDef(impl_def) => impl_def
76+
.target_ty(sema.db)
77+
.as_adt()
78+
.map(|adt| adt.name(sema.db).to_string()),
79+
}
80+
});
81+
82+
let path_iter = module
6983
.path_to_root(sema.db)
7084
.into_iter()
7185
.rev()
7286
.filter_map(|it| it.name(sema.db))
73-
.map(|name| name.to_string())
74-
.chain(std::iter::once(name_string))
75-
.join("::");
87+
.map(|name| name.to_string());
88+
89+
let path = if let Some(impl_trait_name) = impl_trait_name {
90+
path_iter
91+
.chain(std::iter::once(impl_trait_name))
92+
.chain(std::iter::once(name_string))
93+
.join("::")
94+
} else {
95+
path_iter.chain(std::iter::once(name_string)).join("::")
96+
};
97+
7698
TestId::Path(path)
7799
} else {
78100
TestId::Name(name_string)
@@ -237,6 +259,44 @@ mod tests {
237259
);
238260
}
239261

262+
#[test]
263+
fn test_runnables_doc_test_in_impl() {
264+
let (analysis, pos) = analysis_and_position(
265+
r#"
266+
//- /lib.rs
267+
<|> //empty
268+
fn main() {}
269+
270+
struct Data;
271+
impl Data {
272+
/// ```
273+
/// let x = 5;
274+
/// ```
275+
fn foo() {}
276+
}
277+
"#,
278+
);
279+
let runnables = analysis.runnables(pos.file_id).unwrap();
280+
assert_debug_snapshot!(&runnables,
281+
@r###"
282+
[
283+
Runnable {
284+
range: 1..21,
285+
kind: Bin,
286+
},
287+
Runnable {
288+
range: 51..105,
289+
kind: DocTest {
290+
test_id: Path(
291+
"Data::foo",
292+
),
293+
},
294+
},
295+
]
296+
"###
297+
);
298+
}
299+
240300
#[test]
241301
fn test_runnables_module() {
242302
let (analysis, pos) = analysis_and_position(

0 commit comments

Comments
 (0)