Add anchor for intra-doc links to associated items
This commit is contained in:
parent
239a6864c7
commit
af0c8b7f25
2 changed files with 99 additions and 7 deletions
|
@ -379,13 +379,15 @@ fn rewrite_intra_doc_link(
|
|||
let resolved = resolve_doc_path_for_def(db, def, link, ns)?;
|
||||
let mut url = get_doc_base_urls(db, resolved, None, None).0?;
|
||||
|
||||
let (_, file, _) = filename_and_frag_for_def(db, resolved)?;
|
||||
let (_, file, frag) = filename_and_frag_for_def(db, resolved)?;
|
||||
if let Some(path) = mod_path_of_def(db, resolved) {
|
||||
url = url.join(&path).ok()?;
|
||||
}
|
||||
|
||||
let frag = anchor.or(frag.as_deref());
|
||||
|
||||
url = url.join(&file).ok()?;
|
||||
url.set_fragment(anchor);
|
||||
url.set_fragment(frag);
|
||||
|
||||
Some((url.into(), strip_prefixes_suffixes(title).to_owned()))
|
||||
}
|
||||
|
@ -621,11 +623,9 @@ fn filename_and_frag_for_def(
|
|||
format!("fn.{}.html", f.name(db).as_str())
|
||||
}
|
||||
Definition::Variant(ev) => {
|
||||
format!(
|
||||
"enum.{}.html#variant.{}",
|
||||
ev.parent_enum(db).name(db).as_str(),
|
||||
ev.name(db).as_str()
|
||||
)
|
||||
let def = Definition::Adt(ev.parent_enum(db).into());
|
||||
let (_, file, _) = filename_and_frag_for_def(db, def)?;
|
||||
return Some((def, file, Some(format!("variant.{}", ev.name(db).as_str()))));
|
||||
}
|
||||
Definition::Const(c) => {
|
||||
format!("const.{}.html", c.name(db)?.as_str())
|
||||
|
|
|
@ -686,3 +686,95 @@ fn rewrite_intra_doc_link_with_anchor() {
|
|||
expect"],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rewrite_intra_doc_link_to_associated_item() {
|
||||
check_rewrite(
|
||||
r#"
|
||||
//- /main.rs crate:foo
|
||||
/// [Foo::bar]
|
||||
pub struct $0Foo;
|
||||
|
||||
impl Foo {
|
||||
fn bar() {}
|
||||
}
|
||||
"#,
|
||||
expect"#]],
|
||||
);
|
||||
check_rewrite(
|
||||
r#"
|
||||
//- /main.rs crate:foo
|
||||
/// [Foo::bar]
|
||||
pub struct $0Foo {
|
||||
bar: ()
|
||||
}
|
||||
"#,
|
||||
expect"#]],
|
||||
);
|
||||
check_rewrite(
|
||||
r#"
|
||||
//- /main.rs crate:foo
|
||||
/// [Foo::Bar]
|
||||
pub enum $0Foo {
|
||||
Bar
|
||||
}
|
||||
"#,
|
||||
expect"#]],
|
||||
);
|
||||
check_rewrite(
|
||||
r#"
|
||||
//- /main.rs crate:foo
|
||||
/// [Foo::BAR]
|
||||
pub struct $0Foo;
|
||||
|
||||
impl Foo {
|
||||
const BAR: () = ();
|
||||
}
|
||||
"#,
|
||||
expect"#
|
||||
]],
|
||||
);
|
||||
check_rewrite(
|
||||
r#"
|
||||
//- /main.rs crate:foo
|
||||
/// [Foo::bar]
|
||||
pub trait $0Foo {
|
||||
fn bar();
|
||||
}
|
||||
"#,
|
||||
expect"#]],
|
||||
);
|
||||
check_rewrite(
|
||||
r#"
|
||||
//- /main.rs crate:foo
|
||||
/// [Foo::Bar]
|
||||
pub trait $0Foo {
|
||||
type Bar;
|
||||
}
|
||||
"#,
|
||||
expect"#]],
|
||||
);
|
||||
check_rewrite(
|
||||
r#"
|
||||
//- /main.rs crate:foo
|
||||
/// [Foo::bar#anchor]
|
||||
pub struct $0Foo {
|
||||
bar: (),
|
||||
}
|
||||
"#,
|
||||
expect"#]],
|
||||
);
|
||||
check_rewrite(
|
||||
r#"
|
||||
//- /main.rs crate:foo
|
||||
/// [method](Foo::bar)
|
||||
pub struct $0Foo;
|
||||
|
||||
impl Foo {
|
||||
fn bar() {}
|
||||
}
|
||||
"#,
|
||||
expect"#]],
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue