mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Add anchor for intra-doc links to associated items
This commit is contained in:
parent
2fa819c9d3
commit
15207a0474
@ -379,13 +379,15 @@ fn rewrite_intra_doc_link(
|
|||||||
let resolved = resolve_doc_path_for_def(db, def, link, ns)?;
|
let resolved = resolve_doc_path_for_def(db, def, link, ns)?;
|
||||||
let mut url = get_doc_base_urls(db, resolved, None, None).0?;
|
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) {
|
if let Some(path) = mod_path_of_def(db, resolved) {
|
||||||
url = url.join(&path).ok()?;
|
url = url.join(&path).ok()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let frag = anchor.or(frag.as_deref());
|
||||||
|
|
||||||
url = url.join(&file).ok()?;
|
url = url.join(&file).ok()?;
|
||||||
url.set_fragment(anchor);
|
url.set_fragment(frag);
|
||||||
|
|
||||||
Some((url.into(), strip_prefixes_suffixes(title).to_owned()))
|
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())
|
format!("fn.{}.html", f.name(db).as_str())
|
||||||
}
|
}
|
||||||
Definition::Variant(ev) => {
|
Definition::Variant(ev) => {
|
||||||
format!(
|
let def = Definition::Adt(ev.parent_enum(db).into());
|
||||||
"enum.{}.html#variant.{}",
|
let (_, file, _) = filename_and_frag_for_def(db, def)?;
|
||||||
ev.parent_enum(db).name(db).as_str(),
|
return Some((def, file, Some(format!("variant.{}", ev.name(db).as_str()))));
|
||||||
ev.name(db).as_str()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
Definition::Const(c) => {
|
Definition::Const(c) => {
|
||||||
format!("const.{}.html", c.name(db)?.as_str())
|
format!("const.{}.html", c.name(db)?.as_str())
|
||||||
|
@ -686,3 +686,95 @@ fn rewrite_intra_doc_link_with_anchor() {
|
|||||||
expect"],
|
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…
x
Reference in New Issue
Block a user