Skip to content

Commit 113beab

Browse files
committed
Cleanup runnables canonical path impl
1 parent 97d63d6 commit 113beab

File tree

2 files changed

+49
-51
lines changed

2 files changed

+49
-51
lines changed

crates/hir/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,12 @@ impl ModuleDef {
295295
}
296296

297297
pub fn canonical_path(&self, db: &dyn HirDatabase) -> Option<String> {
298-
let mut segments = vec![self.name(db)?.to_string()];
298+
let mut segments = vec![self.name(db)?];
299299
for m in self.module(db)?.path_to_root(db) {
300-
segments.extend(m.name(db).map(|it| it.to_string()))
300+
segments.extend(m.name(db))
301301
}
302302
segments.reverse();
303-
Some(segments.join("::"))
303+
Some(segments.into_iter().join("::"))
304304
}
305305

306306
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {

crates/ide/src/runnables.rs

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
157157
hir::ModuleDef::Function(it) => runnable_fn(&sema, it),
158158
_ => None,
159159
};
160-
add_opt(runnable.or_else(|| module_def_doctest(&sema, def)), Some(def));
160+
add_opt(runnable.or_else(|| module_def_doctest(sema.db, def)), Some(def));
161161
}
162162
Either::Right(impl_) => {
163163
add_opt(runnable_impl(&sema, &impl_), None);
@@ -168,9 +168,9 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
168168
(
169169
match assoc {
170170
hir::AssocItem::Function(it) => runnable_fn(&sema, it)
171-
.or_else(|| module_def_doctest(&sema, it.into())),
172-
hir::AssocItem::Const(it) => module_def_doctest(&sema, it.into()),
173-
hir::AssocItem::TypeAlias(it) => module_def_doctest(&sema, it.into()),
171+
.or_else(|| module_def_doctest(sema.db, it.into())),
172+
hir::AssocItem::Const(it) => module_def_doctest(sema.db, it.into()),
173+
hir::AssocItem::TypeAlias(it) => module_def_doctest(sema.db, it.into()),
174174
},
175175
assoc,
176176
)
@@ -382,61 +382,59 @@ fn runnable_mod_outline_definition(
382382
}
383383
}
384384

385-
fn module_def_doctest(sema: &Semantics<RootDatabase>, def: hir::ModuleDef) -> Option<Runnable> {
385+
fn module_def_doctest(db: &RootDatabase, def: hir::ModuleDef) -> Option<Runnable> {
386386
let attrs = match def {
387-
hir::ModuleDef::Module(it) => it.attrs(sema.db),
388-
hir::ModuleDef::Function(it) => it.attrs(sema.db),
389-
hir::ModuleDef::Adt(it) => it.attrs(sema.db),
390-
hir::ModuleDef::Variant(it) => it.attrs(sema.db),
391-
hir::ModuleDef::Const(it) => it.attrs(sema.db),
392-
hir::ModuleDef::Static(it) => it.attrs(sema.db),
393-
hir::ModuleDef::Trait(it) => it.attrs(sema.db),
394-
hir::ModuleDef::TypeAlias(it) => it.attrs(sema.db),
387+
hir::ModuleDef::Module(it) => it.attrs(db),
388+
hir::ModuleDef::Function(it) => it.attrs(db),
389+
hir::ModuleDef::Adt(it) => it.attrs(db),
390+
hir::ModuleDef::Variant(it) => it.attrs(db),
391+
hir::ModuleDef::Const(it) => it.attrs(db),
392+
hir::ModuleDef::Static(it) => it.attrs(db),
393+
hir::ModuleDef::Trait(it) => it.attrs(db),
394+
hir::ModuleDef::TypeAlias(it) => it.attrs(db),
395395
hir::ModuleDef::BuiltinType(_) => return None,
396396
};
397397
if !has_runnable_doc_test(&attrs) {
398398
return None;
399399
}
400-
let def_name = def.name(sema.db).map(|it| it.to_string());
401-
let test_id = def
402-
.canonical_path(sema.db)
403-
// This probably belongs to canonical path?
404-
.map(|path| {
405-
let assoc_def = match def {
406-
hir::ModuleDef::Function(it) => it.as_assoc_item(sema.db),
407-
hir::ModuleDef::Const(it) => it.as_assoc_item(sema.db),
408-
hir::ModuleDef::TypeAlias(it) => it.as_assoc_item(sema.db),
409-
_ => None,
410-
};
411-
// FIXME: this also looks very wrong
412-
if let Some(assoc_def) = assoc_def {
413-
if let hir::AssocItemContainer::Impl(imp) = assoc_def.container(sema.db) {
414-
let ty = imp.self_ty(sema.db);
415-
if let Some(adt) = ty.as_adt() {
416-
let name = adt.name(sema.db);
417-
let idx = path.rfind(':').map_or(0, |idx| idx + 1);
418-
let (prefix, suffix) = path.split_at(idx);
419-
let mut ty_args = ty.type_arguments().peekable();
420-
let params = if ty_args.peek().is_some() {
421-
format!(
422-
"<{}>",
423-
ty_args.format_with(", ", |ty, cb| cb(&ty.display(sema.db)))
424-
)
425-
} else {
426-
String::new()
427-
};
428-
return format!("{}{}{}::{}", prefix, name, params, suffix);
400+
let def_name = def.name(db)?;
401+
let path = (|| {
402+
let mut path = String::new();
403+
def.module(db)?
404+
.path_to_root(db)
405+
.into_iter()
406+
.rev()
407+
.flat_map(|it| it.name(db))
408+
.for_each(|name| format_to!(path, "{}::", name));
409+
// This probably belongs to canonical_path?
410+
if let Some(assoc_item) = def.as_assoc_item(db) {
411+
if let hir::AssocItemContainer::Impl(imp) = assoc_item.container(db) {
412+
let ty = imp.self_ty(db);
413+
if let Some(adt) = ty.as_adt() {
414+
let name = adt.name(db);
415+
let mut ty_args = ty.type_arguments().peekable();
416+
format_to!(path, "{}", name);
417+
if ty_args.peek().is_some() {
418+
format_to!(
419+
path,
420+
"<{}>",
421+
ty_args.format_with(", ", |ty, cb| cb(&ty.display(db)))
422+
);
429423
}
424+
format_to!(path, "::{}", def_name);
425+
return Some(path);
430426
}
431427
}
432-
path
433-
})
434-
.map(TestId::Path)
435-
.or_else(|| def_name.clone().map(TestId::Name))?;
428+
}
429+
format_to!(path, "{}", def_name);
430+
Some(path)
431+
})();
432+
433+
let test_id = path.map_or_else(|| TestId::Name(def_name.to_string()), TestId::Path);
436434

437435
let mut nav = match def {
438-
hir::ModuleDef::Module(def) => NavigationTarget::from_module_to_decl(sema.db, def),
439-
def => def.try_to_nav(sema.db)?,
436+
hir::ModuleDef::Module(def) => NavigationTarget::from_module_to_decl(db, def),
437+
def => def.try_to_nav(db)?,
440438
};
441439
nav.focus_range = None;
442440
nav.description = None;

0 commit comments

Comments
 (0)