Skip to content

Commit

Permalink
Rollup merge of rust-lang#48335 - Manishearth:shortcut-links, r=Quiet…
Browse files Browse the repository at this point in the history
…Misdreavus

Implement implied shortcut links for intra-rustdoc-links

cc rust-lang#43466

Needs pulldown-cmark/pulldown-cmark#126

r? @QuietMisdreavus
  • Loading branch information
Manishearth authored Feb 19, 2018
2 parents b7b3e3a + 5fdc10c commit c787251
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/librustdoc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ path = "lib.rs"
doctest = false

[dependencies]
pulldown-cmark = { version = "0.1.0", default-features = false }
pulldown-cmark = { version = "0.1.2", default-features = false }
tempdir = "0.3"
8 changes: 6 additions & 2 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option

/// Resolve a string as a macro
fn macro_resolve(cx: &DocContext, path_str: &str) -> Option<Def> {
use syntax::ext::base::MacroKind;
use syntax::ext::base::{MacroKind, SyntaxExtension};
use syntax::ext::hygiene::Mark;
let segment = ast::PathSegment {
identifier: ast::Ident::from_str(path_str),
Expand All @@ -1025,7 +1025,11 @@ fn macro_resolve(cx: &DocContext, path_str: &str) -> Option<Def> {
let res = resolver
.resolve_macro_to_def_inner(mark, &path, MacroKind::Bang, false);
if let Ok(def) = res {
Some(def)
if let SyntaxExtension::DeclMacro(..) = *resolver.get_macro(def) {
Some(def)
} else {
None
}
} else if let Some(def) = resolver.all_macros.get(&path_str.into()) {
Some(*def)
} else {
Expand Down
47 changes: 38 additions & 9 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,15 @@ impl<'a> fmt::Display for Markdown<'a> {
opts.insert(OPTION_ENABLE_TABLES);
opts.insert(OPTION_ENABLE_FOOTNOTES);

let p = Parser::new_ext(md, opts);
let replacer = |_: &str, s: &str| {
if let Some(&(_, ref replace)) = links.into_iter().find(|link| &*link.0 == s) {
Some((replace.clone(), s.to_owned()))
} else {
None
}
};

let p = Parser::new_with_broken_link_callback(md, opts, Some(&replacer));

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

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

let p = Parser::new(md);
let replacer = |_: &str, s: &str| {
if let Some(&(_, ref replace)) = links.into_iter().find(|link| &*link.0 == s) {
Some((replace.clone(), s.to_owned()))
} else {
None
}
};

let p = Parser::new_with_broken_link_callback(md, Options::empty(),
Some(&replacer));

let mut s = String::new();

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

let p = Parser::new_ext(md, opts);

let iter = Footnotes::new(HeadingLinks::new(p, None));
let mut links = vec![];
let shortcut_links = RefCell::new(vec![]);

{
let push = |_: &str, s: &str| {
shortcut_links.borrow_mut().push(s.to_owned());
None
};
let p = Parser::new_with_broken_link_callback(md, opts,
Some(&push));

let iter = Footnotes::new(HeadingLinks::new(p, None));

for ev in iter {
if let Event::Start(Tag::Link(dest, _)) = ev {
debug!("found link: {}", dest);
links.push(dest.into_owned());
for ev in iter {
if let Event::Start(Tag::Link(dest, _)) = ev {
debug!("found link: {}", dest);
links.push(dest.into_owned());
}
}
}

let mut shortcut_links = shortcut_links.into_inner();
links.extend(shortcut_links.drain(..));

links
}

Expand Down
12 changes: 12 additions & 0 deletions src/test/rustdoc/intra-links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,15 @@ pub trait SoAmbiguous {}

#[allow(bad_style)]
pub fn SoAmbiguous() {}


// @has - '//a/@href' '../intra_links/struct.ThisType.html'
// @has - '//a/@href' '../intra_links/struct.ThisType.html#method.this_method'
// @has - '//a/@href' '../intra_links/enum.ThisEnum.html'
// @has - '//a/@href' '../intra_links/enum.ThisEnum.html#ThisVariant.v'
/// Shortcut links for:
/// * [`ThisType`]
/// * [`ThisType::this_method`]
/// * [ThisEnum]
/// * [ThisEnum::ThisVariant]
pub struct SomeOtherType;

0 comments on commit c787251

Please sign in to comment.