Skip to content

Commit

Permalink
Auto merge of rust-lang#12605 - erhuve:fix/determine-doc-link-type-at…
Browse files Browse the repository at this point in the history
…-start, r=erhuve

fix: doc_links link type - Determine link type at start (fixes rust-lang#12601)

fixes rust-lang#12601
Looked like autolink/inline mismatch happened because end_link_type was parsed from the Text/Code events.
I changed it to be determined from the Start event, which should hopefully remain accurate while staying true to the cases where link type may need to be changed, according to the comment
```
// normally link's type is determined by the type of link tag in the end event,
// however in some cases we want to change the link type, for example,
// `Shortcut` type doesn't make sense for url links
```
Hopefully this is the desired behavior?
![Untitled](https://user-images.githubusercontent.com/59463268/174696581-3b1140a5-cdf0-4eda-9a11-ec648e4e7d21.gif)
  • Loading branch information
bors committed Jun 23, 2022
2 parents 32b40de + 5107123 commit 434e718
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 10 additions & 5 deletions crates/ide/src/doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,15 @@ fn map_links<'e>(
// holds the origin link target on start event and the rewritten one on end event
let mut end_link_target: Option<CowStr> = None;
// normally link's type is determined by the type of link tag in the end event,
// however in same cases we want to change the link type, for example,
// `Shortcut` type doesn't make sense for url links
// however in some cases we want to change the link type, for example,
// `Shortcut` type parsed from Start/End tags doesn't make sense for url links
let mut end_link_type: Option<LinkType> = None;

events.map(move |evt| match evt {
Event::Start(Tag::Link(_, ref target, _)) => {
Event::Start(Tag::Link(link_type, ref target, _)) => {
in_link = true;
end_link_target = Some(target.clone());
end_link_type = Some(link_type);
evt
}
Event::End(Tag::Link(link_type, target, _)) => {
Expand All @@ -384,14 +385,18 @@ fn map_links<'e>(
let (link_type, link_target_s, link_name) =
callback(&end_link_target.take().unwrap(), &s);
end_link_target = Some(CowStr::Boxed(link_target_s.into()));
end_link_type = link_type;
if !matches!(end_link_type, Some(LinkType::Autolink)) {
end_link_type = link_type;
}
Event::Text(CowStr::Boxed(link_name.into()))
}
Event::Code(s) if in_link => {
let (link_type, link_target_s, link_name) =
callback(&end_link_target.take().unwrap(), &s);
end_link_target = Some(CowStr::Boxed(link_target_s.into()));
end_link_type = link_type;
if !matches!(end_link_type, Some(LinkType::Autolink)) {
end_link_type = link_type;
}
Event::Code(CowStr::Boxed(link_name.into()))
}
_ => evt,
Expand Down
2 changes: 2 additions & 0 deletions crates/ide/src/hover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3808,6 +3808,7 @@ fn foo() {
/// [closure]
/// [closures][closure]
/// [threads]
/// <https://doc.rust-lang.org/nightly/book/ch13-01-closures.html>
///
/// [closure]: ../book/ch13-01-closures.html
/// [threads]: ../book/ch16-01-threads.html#using-move-closures-with-threads
Expand All @@ -3825,6 +3826,7 @@ mod move_keyword {}
[closure](https://doc.rust-lang.org/nightly/book/ch13-01-closures.html)
[closures](https://doc.rust-lang.org/nightly/book/ch13-01-closures.html)
[threads](https://doc.rust-lang.org/nightly/book/ch16-01-threads.html#using-move-closures-with-threads)
<https://doc.rust-lang.org/nightly/book/ch13-01-closures.html>
"##]],
);
}
Expand Down

0 comments on commit 434e718

Please sign in to comment.