Skip to content

Commit bd8b58d

Browse files
authored
Merge pull request rust-lang#19246 from ncrothers/add-anchor-for-associated-items
Add anchor for intra-doc links to associated items
2 parents 13eb834 + af0c8b7 commit bd8b58d

File tree

2 files changed

+99
-7
lines changed

2 files changed

+99
-7
lines changed

src/tools/rust-analyzer/crates/ide/src/doc_links.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,15 @@ fn rewrite_intra_doc_link(
379379
let resolved = resolve_doc_path_for_def(db, def, link, ns)?;
380380
let mut url = get_doc_base_urls(db, resolved, None, None).0?;
381381

382-
let (_, file, _) = filename_and_frag_for_def(db, resolved)?;
382+
let (_, file, frag) = filename_and_frag_for_def(db, resolved)?;
383383
if let Some(path) = mod_path_of_def(db, resolved) {
384384
url = url.join(&path).ok()?;
385385
}
386386

387+
let frag = anchor.or(frag.as_deref());
388+
387389
url = url.join(&file).ok()?;
388-
url.set_fragment(anchor);
390+
url.set_fragment(frag);
389391

390392
Some((url.into(), strip_prefixes_suffixes(title).to_owned()))
391393
}
@@ -621,11 +623,9 @@ fn filename_and_frag_for_def(
621623
format!("fn.{}.html", f.name(db).as_str())
622624
}
623625
Definition::Variant(ev) => {
624-
format!(
625-
"enum.{}.html#variant.{}",
626-
ev.parent_enum(db).name(db).as_str(),
627-
ev.name(db).as_str()
628-
)
626+
let def = Definition::Adt(ev.parent_enum(db).into());
627+
let (_, file, _) = filename_and_frag_for_def(db, def)?;
628+
return Some((def, file, Some(format!("variant.{}", ev.name(db).as_str()))));
629629
}
630630
Definition::Const(c) => {
631631
format!("const.{}.html", c.name(db)?.as_str())

src/tools/rust-analyzer/crates/ide/src/doc_links/tests.rs

+92
Original file line numberDiff line numberDiff line change
@@ -686,3 +686,95 @@ fn rewrite_intra_doc_link_with_anchor() {
686686
expect!["[PartialEq#derivable](https://doc.rust-lang.org/stable/core/cmp/trait.PartialEq.html#derivable)"],
687687
);
688688
}
689+
690+
#[test]
691+
fn rewrite_intra_doc_link_to_associated_item() {
692+
check_rewrite(
693+
r#"
694+
//- /main.rs crate:foo
695+
/// [Foo::bar]
696+
pub struct $0Foo;
697+
698+
impl Foo {
699+
fn bar() {}
700+
}
701+
"#,
702+
expect![[r#"[Foo::bar](https://docs.rs/foo/*/foo/struct.Foo.html#method.bar)"#]],
703+
);
704+
check_rewrite(
705+
r#"
706+
//- /main.rs crate:foo
707+
/// [Foo::bar]
708+
pub struct $0Foo {
709+
bar: ()
710+
}
711+
"#,
712+
expect![[r#"[Foo::bar](https://docs.rs/foo/*/foo/struct.Foo.html#structfield.bar)"#]],
713+
);
714+
check_rewrite(
715+
r#"
716+
//- /main.rs crate:foo
717+
/// [Foo::Bar]
718+
pub enum $0Foo {
719+
Bar
720+
}
721+
"#,
722+
expect![[r#"[Foo::Bar](https://docs.rs/foo/*/foo/enum.Foo.html#variant.Bar)"#]],
723+
);
724+
check_rewrite(
725+
r#"
726+
//- /main.rs crate:foo
727+
/// [Foo::BAR]
728+
pub struct $0Foo;
729+
730+
impl Foo {
731+
const BAR: () = ();
732+
}
733+
"#,
734+
expect![[
735+
r#"[Foo::BAR](https://docs.rs/foo/*/foo/struct.Foo.html#associatedconstant.BAR)"#
736+
]],
737+
);
738+
check_rewrite(
739+
r#"
740+
//- /main.rs crate:foo
741+
/// [Foo::bar]
742+
pub trait $0Foo {
743+
fn bar();
744+
}
745+
"#,
746+
expect![[r#"[Foo::bar](https://docs.rs/foo/*/foo/trait.Foo.html#tymethod.bar)"#]],
747+
);
748+
check_rewrite(
749+
r#"
750+
//- /main.rs crate:foo
751+
/// [Foo::Bar]
752+
pub trait $0Foo {
753+
type Bar;
754+
}
755+
"#,
756+
expect![[r#"[Foo::Bar](https://docs.rs/foo/*/foo/trait.Foo.html#associatedtype.Bar)"#]],
757+
);
758+
check_rewrite(
759+
r#"
760+
//- /main.rs crate:foo
761+
/// [Foo::bar#anchor]
762+
pub struct $0Foo {
763+
bar: (),
764+
}
765+
"#,
766+
expect![[r#"[Foo::bar#anchor](https://docs.rs/foo/*/foo/struct.Foo.html#anchor)"#]],
767+
);
768+
check_rewrite(
769+
r#"
770+
//- /main.rs crate:foo
771+
/// [method](Foo::bar)
772+
pub struct $0Foo;
773+
774+
impl Foo {
775+
fn bar() {}
776+
}
777+
"#,
778+
expect![[r#"[method](https://docs.rs/foo/*/foo/struct.Foo.html#method.bar)"#]],
779+
);
780+
}

0 commit comments

Comments
 (0)