From f2610cbd7e4bfa4834d70cd92c5c4331c99a8ce0 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 14 Mar 2021 20:16:02 +0100 Subject: Don't drop type params in doc-test paths --- crates/ide/src/runnables.rs | 55 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) (limited to 'crates/ide/src') diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs index faa91541e..27d35de5b 100644 --- a/crates/ide/src/runnables.rs +++ b/crates/ide/src/runnables.rs @@ -2,7 +2,7 @@ use std::fmt; use ast::NameOwner; use cfg::CfgExpr; -use hir::{AsAssocItem, HasAttrs, HasSource, Semantics}; +use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics}; use ide_assists::utils::test_related_attribute; use ide_db::{ base_db::{FilePosition, FileRange}, @@ -340,11 +340,21 @@ fn module_def_doctest(sema: &Semantics, def: hir::ModuleDef) -> Op // FIXME: this also looks very wrong if let Some(assoc_def) = assoc_def { if let hir::AssocItemContainer::Impl(imp) = assoc_def.container(sema.db) { - if let Some(adt) = imp.target_ty(sema.db).as_adt() { - let name = adt.name(sema.db).to_string(); + let ty = imp.target_ty(sema.db); + if let Some(adt) = ty.as_adt() { + let name = adt.name(sema.db); let idx = path.rfind(':').map_or(0, |idx| idx + 1); let (prefix, suffix) = path.split_at(idx); - return format!("{}{}::{}", prefix, name, suffix); + let mut ty_params = ty.type_parameters().peekable(); + let params = if ty_params.peek().is_some() { + format!( + "<{}>", + ty_params.format_with(", ", |ty, cb| cb(&ty.display(sema.db))) + ) + } else { + String::new() + }; + return format!("{}{}{}::{}", prefix, name, params, suffix); } } } @@ -1406,4 +1416,41 @@ mod tests { "#]], ); } + + #[test] + fn doc_test_type_params() { + check( + r#" +//- /lib.rs +$0 +struct Foo; + +impl Foo { + /// ```rust + /// ```` + fn t() {} +} +"#, + &[&DOCTEST], + expect![[r#" + [ + Runnable { + nav: NavigationTarget { + file_id: FileId( + 0, + ), + full_range: 47..85, + name: "t", + }, + kind: DocTest { + test_id: Path( + "Foo::t", + ), + }, + cfg: None, + }, + ] + "#]], + ); + } } -- cgit v1.2.3