Skip to content

Commit c6a805c

Browse files
authored
Merge pull request #20151 from ChayimFriedman2/keyword-hover-link
fix: Remove keyword prefixes (`macro@` or `macro `) from links in the docs only if the link target is inferred
2 parents 42e9a9f + 51ef8fb commit c6a805c

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub(crate) fn rewrite_links(
6060
let doc = Parser::new_with_broken_link_callback(markdown, MARKDOWN_OPTIONS, Some(&mut cb))
6161
.into_offset_iter();
6262

63-
let doc = map_links(doc, |target, title, range| {
63+
let doc = map_links(doc, |target, title, range, link_type| {
6464
// This check is imperfect, there's some overlap between valid intra-doc links
6565
// and valid URLs so we choose to be too eager to try to resolve what might be
6666
// a URL.
@@ -78,7 +78,7 @@ pub(crate) fn rewrite_links(
7878
.map(|(_, attr_id)| attr_id.is_inner_attr())
7979
.unwrap_or(false);
8080
if let Some((target, title)) =
81-
rewrite_intra_doc_link(db, definition, target, title, is_inner_doc)
81+
rewrite_intra_doc_link(db, definition, target, title, is_inner_doc, link_type)
8282
{
8383
(None, target, title)
8484
} else if let Some(target) = rewrite_url_link(db, definition, target) {
@@ -417,6 +417,7 @@ fn rewrite_intra_doc_link(
417417
target: &str,
418418
title: &str,
419419
is_inner_doc: bool,
420+
link_type: LinkType,
420421
) -> Option<(String, String)> {
421422
let (link, ns) = parse_intra_doc_link(target);
422423

@@ -438,7 +439,21 @@ fn rewrite_intra_doc_link(
438439
url = url.join(&file).ok()?;
439440
url.set_fragment(frag);
440441

441-
Some((url.into(), strip_prefixes_suffixes(title).to_owned()))
442+
// We want to strip the keyword prefix from the title, but only if the target is implicitly the same
443+
// as the title.
444+
let title = match link_type {
445+
LinkType::Email
446+
| LinkType::Autolink
447+
| LinkType::Shortcut
448+
| LinkType::Collapsed
449+
| LinkType::Reference
450+
| LinkType::Inline => title.to_owned(),
451+
LinkType::ShortcutUnknown | LinkType::CollapsedUnknown | LinkType::ReferenceUnknown => {
452+
strip_prefixes_suffixes(title).to_owned()
453+
}
454+
};
455+
456+
Some((url.into(), title))
442457
}
443458

444459
/// Try to resolve path to local documentation via path-based links (i.e. `../gateway/struct.Shard.html`).
@@ -470,7 +485,7 @@ fn mod_path_of_def(db: &RootDatabase, def: Definition) -> Option<String> {
470485
/// Rewrites a markdown document, applying 'callback' to each link.
471486
fn map_links<'e>(
472487
events: impl Iterator<Item = (Event<'e>, Range<usize>)>,
473-
callback: impl Fn(&str, &str, Range<usize>) -> (Option<LinkType>, String, String),
488+
callback: impl Fn(&str, &str, Range<usize>, LinkType) -> (Option<LinkType>, String, String),
474489
) -> impl Iterator<Item = Event<'e>> {
475490
let mut in_link = false;
476491
// holds the origin link target on start event and the rewritten one on end event
@@ -497,7 +512,7 @@ fn map_links<'e>(
497512
}
498513
Event::Text(s) if in_link => {
499514
let (link_type, link_target_s, link_name) =
500-
callback(&end_link_target.take().unwrap(), &s, range);
515+
callback(&end_link_target.take().unwrap(), &s, range, end_link_type.unwrap());
501516
end_link_target = Some(CowStr::Boxed(link_target_s.into()));
502517
if !matches!(end_link_type, Some(LinkType::Autolink)) {
503518
end_link_type = link_type;
@@ -506,7 +521,7 @@ fn map_links<'e>(
506521
}
507522
Event::Code(s) if in_link => {
508523
let (link_type, link_target_s, link_name) =
509-
callback(&end_link_target.take().unwrap(), &s, range);
524+
callback(&end_link_target.take().unwrap(), &s, range, end_link_type.unwrap());
510525
end_link_target = Some(CowStr::Boxed(link_target_s.into()));
511526
if !matches!(end_link_type, Some(LinkType::Autolink)) {
512527
end_link_type = link_type;

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10927,3 +10927,34 @@ fn main() {
1092710927
"#]],
1092810928
);
1092910929
}
10930+
10931+
#[test]
10932+
fn keyword_inside_link() {
10933+
check(
10934+
r#"
10935+
enum Foo {
10936+
MacroExpansion,
10937+
}
10938+
10939+
/// I return a [macro expansion](Foo::MacroExpansion).
10940+
fn bar$0() -> Foo {
10941+
Foo::MacroExpansion
10942+
}
10943+
"#,
10944+
expect![[r#"
10945+
*bar*
10946+
10947+
```rust
10948+
ra_test_fixture
10949+
```
10950+
10951+
```rust
10952+
fn bar() -> Foo
10953+
```
10954+
10955+
---
10956+
10957+
I return a [macro expansion](https://docs.rs/ra_test_fixture/*/ra_test_fixture/enum.Foo.html#variant.MacroExpansion).
10958+
"#]],
10959+
);
10960+
}

0 commit comments

Comments
 (0)