Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 434e718

Browse files
committed
Auto merge of rust-lang#12605 - erhuve:fix/determine-doc-link-type-at-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)
2 parents 32b40de + 5107123 commit 434e718

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

crates/ide/src/doc_links.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,15 @@ fn map_links<'e>(
362362
// holds the origin link target on start event and the rewritten one on end event
363363
let mut end_link_target: Option<CowStr> = None;
364364
// normally link's type is determined by the type of link tag in the end event,
365-
// however in same cases we want to change the link type, for example,
366-
// `Shortcut` type doesn't make sense for url links
365+
// however in some cases we want to change the link type, for example,
366+
// `Shortcut` type parsed from Start/End tags doesn't make sense for url links
367367
let mut end_link_type: Option<LinkType> = None;
368368

369369
events.map(move |evt| match evt {
370-
Event::Start(Tag::Link(_, ref target, _)) => {
370+
Event::Start(Tag::Link(link_type, ref target, _)) => {
371371
in_link = true;
372372
end_link_target = Some(target.clone());
373+
end_link_type = Some(link_type);
373374
evt
374375
}
375376
Event::End(Tag::Link(link_type, target, _)) => {
@@ -384,14 +385,18 @@ fn map_links<'e>(
384385
let (link_type, link_target_s, link_name) =
385386
callback(&end_link_target.take().unwrap(), &s);
386387
end_link_target = Some(CowStr::Boxed(link_target_s.into()));
387-
end_link_type = link_type;
388+
if !matches!(end_link_type, Some(LinkType::Autolink)) {
389+
end_link_type = link_type;
390+
}
388391
Event::Text(CowStr::Boxed(link_name.into()))
389392
}
390393
Event::Code(s) if in_link => {
391394
let (link_type, link_target_s, link_name) =
392395
callback(&end_link_target.take().unwrap(), &s);
393396
end_link_target = Some(CowStr::Boxed(link_target_s.into()));
394-
end_link_type = link_type;
397+
if !matches!(end_link_type, Some(LinkType::Autolink)) {
398+
end_link_type = link_type;
399+
}
395400
Event::Code(CowStr::Boxed(link_name.into()))
396401
}
397402
_ => evt,

crates/ide/src/hover/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3808,6 +3808,7 @@ fn foo() {
38083808
/// [closure]
38093809
/// [closures][closure]
38103810
/// [threads]
3811+
/// <https://doc.rust-lang.org/nightly/book/ch13-01-closures.html>
38113812
///
38123813
/// [closure]: ../book/ch13-01-closures.html
38133814
/// [threads]: ../book/ch16-01-threads.html#using-move-closures-with-threads
@@ -3825,6 +3826,7 @@ mod move_keyword {}
38253826
[closure](https://doc.rust-lang.org/nightly/book/ch13-01-closures.html)
38263827
[closures](https://doc.rust-lang.org/nightly/book/ch13-01-closures.html)
38273828
[threads](https://doc.rust-lang.org/nightly/book/ch16-01-threads.html#using-move-closures-with-threads)
3829+
<https://doc.rust-lang.org/nightly/book/ch13-01-closures.html>
38283830
"##]],
38293831
);
38303832
}

0 commit comments

Comments
 (0)