Skip to content

Commit

Permalink
Merge pull request rust-lang#19246 from ncrothers/add-anchor-for-asso…
Browse files Browse the repository at this point in the history
…ciated-items

Add anchor for intra-doc links to associated items
  • Loading branch information
Veykril authored Feb 28, 2025
2 parents 13eb834 + af0c8b7 commit bd8b58d
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/tools/rust-analyzer/crates/ide/src/doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand Down Expand Up @@ -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())
Expand Down
92 changes: 92 additions & 0 deletions src/tools/rust-analyzer/crates/ide/src/doc_links/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,3 +686,95 @@ fn rewrite_intra_doc_link_with_anchor() {
expect!["[PartialEq#derivable](https://doc.rust-lang.org/stable/core/cmp/trait.PartialEq.html#derivable)"],
);
}

#[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![[r#"[Foo::bar](https://docs.rs/foo/*/foo/struct.Foo.html#method.bar)"#]],
);
check_rewrite(
r#"
//- /main.rs crate:foo
/// [Foo::bar]
pub struct $0Foo {
bar: ()
}
"#,
expect![[r#"[Foo::bar](https://docs.rs/foo/*/foo/struct.Foo.html#structfield.bar)"#]],
);
check_rewrite(
r#"
//- /main.rs crate:foo
/// [Foo::Bar]
pub enum $0Foo {
Bar
}
"#,
expect![[r#"[Foo::Bar](https://docs.rs/foo/*/foo/enum.Foo.html#variant.Bar)"#]],
);
check_rewrite(
r#"
//- /main.rs crate:foo
/// [Foo::BAR]
pub struct $0Foo;
impl Foo {
const BAR: () = ();
}
"#,
expect![[
r#"[Foo::BAR](https://docs.rs/foo/*/foo/struct.Foo.html#associatedconstant.BAR)"#
]],
);
check_rewrite(
r#"
//- /main.rs crate:foo
/// [Foo::bar]
pub trait $0Foo {
fn bar();
}
"#,
expect![[r#"[Foo::bar](https://docs.rs/foo/*/foo/trait.Foo.html#tymethod.bar)"#]],
);
check_rewrite(
r#"
//- /main.rs crate:foo
/// [Foo::Bar]
pub trait $0Foo {
type Bar;
}
"#,
expect![[r#"[Foo::Bar](https://docs.rs/foo/*/foo/trait.Foo.html#associatedtype.Bar)"#]],
);
check_rewrite(
r#"
//- /main.rs crate:foo
/// [Foo::bar#anchor]
pub struct $0Foo {
bar: (),
}
"#,
expect![[r#"[Foo::bar#anchor](https://docs.rs/foo/*/foo/struct.Foo.html#anchor)"#]],
);
check_rewrite(
r#"
//- /main.rs crate:foo
/// [method](Foo::bar)
pub struct $0Foo;
impl Foo {
fn bar() {}
}
"#,
expect![[r#"[method](https://docs.rs/foo/*/foo/struct.Foo.html#method.bar)"#]],
);
}

0 comments on commit bd8b58d

Please sign in to comment.