Skip to content

Commit c787251

Browse files
authored
Rollup merge of rust-lang#48335 - Manishearth:shortcut-links, r=QuietMisdreavus
Implement implied shortcut links for intra-rustdoc-links cc rust-lang#43466 Needs pulldown-cmark/pulldown-cmark#126 r? @QuietMisdreavus
2 parents b7b3e3a + 5fdc10c commit c787251

File tree

5 files changed

+61
-16
lines changed

5 files changed

+61
-16
lines changed

src/Cargo.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustdoc/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ path = "lib.rs"
1010
doctest = false
1111

1212
[dependencies]
13-
pulldown-cmark = { version = "0.1.0", default-features = false }
13+
pulldown-cmark = { version = "0.1.2", default-features = false }
1414
tempdir = "0.3"

src/librustdoc/clean/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option
10081008

10091009
/// Resolve a string as a macro
10101010
fn macro_resolve(cx: &DocContext, path_str: &str) -> Option<Def> {
1011-
use syntax::ext::base::MacroKind;
1011+
use syntax::ext::base::{MacroKind, SyntaxExtension};
10121012
use syntax::ext::hygiene::Mark;
10131013
let segment = ast::PathSegment {
10141014
identifier: ast::Ident::from_str(path_str),
@@ -1025,7 +1025,11 @@ fn macro_resolve(cx: &DocContext, path_str: &str) -> Option<Def> {
10251025
let res = resolver
10261026
.resolve_macro_to_def_inner(mark, &path, MacroKind::Bang, false);
10271027
if let Ok(def) = res {
1028-
Some(def)
1028+
if let SyntaxExtension::DeclMacro(..) = *resolver.get_macro(def) {
1029+
Some(def)
1030+
} else {
1031+
None
1032+
}
10291033
} else if let Some(def) = resolver.all_macros.get(&path_str.into()) {
10301034
Some(*def)
10311035
} else {

src/librustdoc/html/markdown.rs

+38-9
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,15 @@ impl<'a> fmt::Display for Markdown<'a> {
591591
opts.insert(OPTION_ENABLE_TABLES);
592592
opts.insert(OPTION_ENABLE_FOOTNOTES);
593593

594-
let p = Parser::new_ext(md, opts);
594+
let replacer = |_: &str, s: &str| {
595+
if let Some(&(_, ref replace)) = links.into_iter().find(|link| &*link.0 == s) {
596+
Some((replace.clone(), s.to_owned()))
597+
} else {
598+
None
599+
}
600+
};
601+
602+
let p = Parser::new_with_broken_link_callback(md, opts, Some(&replacer));
595603

596604
let mut s = String::with_capacity(md.len() * 3 / 2);
597605

@@ -662,7 +670,16 @@ impl<'a> fmt::Display for MarkdownSummaryLine<'a> {
662670
// This is actually common enough to special-case
663671
if md.is_empty() { return Ok(()) }
664672

665-
let p = Parser::new(md);
673+
let replacer = |_: &str, s: &str| {
674+
if let Some(&(_, ref replace)) = links.into_iter().find(|link| &*link.0 == s) {
675+
Some((replace.clone(), s.to_owned()))
676+
} else {
677+
None
678+
}
679+
};
680+
681+
let p = Parser::new_with_broken_link_callback(md, Options::empty(),
682+
Some(&replacer));
666683

667684
let mut s = String::new();
668685

@@ -731,18 +748,30 @@ pub fn markdown_links(md: &str) -> Vec<String> {
731748
opts.insert(OPTION_ENABLE_TABLES);
732749
opts.insert(OPTION_ENABLE_FOOTNOTES);
733750

734-
let p = Parser::new_ext(md, opts);
735-
736-
let iter = Footnotes::new(HeadingLinks::new(p, None));
737751
let mut links = vec![];
752+
let shortcut_links = RefCell::new(vec![]);
753+
754+
{
755+
let push = |_: &str, s: &str| {
756+
shortcut_links.borrow_mut().push(s.to_owned());
757+
None
758+
};
759+
let p = Parser::new_with_broken_link_callback(md, opts,
760+
Some(&push));
761+
762+
let iter = Footnotes::new(HeadingLinks::new(p, None));
738763

739-
for ev in iter {
740-
if let Event::Start(Tag::Link(dest, _)) = ev {
741-
debug!("found link: {}", dest);
742-
links.push(dest.into_owned());
764+
for ev in iter {
765+
if let Event::Start(Tag::Link(dest, _)) = ev {
766+
debug!("found link: {}", dest);
767+
links.push(dest.into_owned());
768+
}
743769
}
744770
}
745771

772+
let mut shortcut_links = shortcut_links.into_inner();
773+
links.extend(shortcut_links.drain(..));
774+
746775
links
747776
}
748777

src/test/rustdoc/intra-links.rs

+12
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,15 @@ pub trait SoAmbiguous {}
7777

7878
#[allow(bad_style)]
7979
pub fn SoAmbiguous() {}
80+
81+
82+
// @has - '//a/@href' '../intra_links/struct.ThisType.html'
83+
// @has - '//a/@href' '../intra_links/struct.ThisType.html#method.this_method'
84+
// @has - '//a/@href' '../intra_links/enum.ThisEnum.html'
85+
// @has - '//a/@href' '../intra_links/enum.ThisEnum.html#ThisVariant.v'
86+
/// Shortcut links for:
87+
/// * [`ThisType`]
88+
/// * [`ThisType::this_method`]
89+
/// * [ThisEnum]
90+
/// * [ThisEnum::ThisVariant]
91+
pub struct SomeOtherType;

0 commit comments

Comments
 (0)