diff options
| author | Nicholas Crothers <hawx523@gmail.com> | 2025-02-27 16:39:07 -0600 |
|---|---|---|
| committer | Nicholas Crothers <hawx523@gmail.com> | 2025-02-27 16:39:07 -0600 |
| commit | af0c8b7f25977a7ad2bfefafd005a5ff4bd50827 (patch) | |
| tree | cdd1972bc18d7f94f26f7c1ab613ce5fa579e0f4 /src | |
| parent | 239a6864c78995fc9f0a069d37eb493a54c0f2a3 (diff) | |
| download | rust-af0c8b7f25977a7ad2bfefafd005a5ff4bd50827.tar.gz rust-af0c8b7f25977a7ad2bfefafd005a5ff4bd50827.zip | |
Add anchor for intra-doc links to associated items
Diffstat (limited to 'src')
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide/src/doc_links.rs | 14 | ||||
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide/src/doc_links/tests.rs | 92 |
2 files changed, 99 insertions, 7 deletions
diff --git a/src/tools/rust-analyzer/crates/ide/src/doc_links.rs b/src/tools/rust-analyzer/crates/ide/src/doc_links.rs index e35e47e7471..d88f7c5033e 100644 --- a/src/tools/rust-analyzer/crates/ide/src/doc_links.rs +++ b/src/tools/rust-analyzer/crates/ide/src/doc_links.rs @@ -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()) diff --git a/src/tools/rust-analyzer/crates/ide/src/doc_links/tests.rs b/src/tools/rust-analyzer/crates/ide/src/doc_links/tests.rs index d7291c4b9f3..b09e3a3c804 100644 --- a/src/tools/rust-analyzer/crates/ide/src/doc_links/tests.rs +++ b/src/tools/rust-analyzer/crates/ide/src/doc_links/tests.rs @@ -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"#]], + ); +} |
