Get rid of locate() in markdown handling

This function was unfortunate for several reasons:

- It used `unsafe` because it wanted to tell whether a string came from
  the same *allocation* as another, not just whether it was a textual
  match.
- It recalculated spans even though they were already available from
  pulldown
- It sometimes *failed* to calculate the span, which meant it was always
  possible for the span to be `None`, even though in practice that
  should never happen.

This commit has several cleanups:

- Make the span required
- Pass through the span from pulldown in the `HeadingLinks` and
  `Footnotes` iterators
- Only add iterator bounds on the `impl Iterator`, not on `new` and the
  struct itself.
This commit is contained in:
Joshua Nelson 2020-12-20 14:28:20 -05:00
parent 50a90975c0
commit 65f4f39dd8
2 changed files with 87 additions and 99 deletions

View File

@ -447,21 +447,23 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, I> {
} }
/// Make headings links with anchor IDs and build up TOC. /// Make headings links with anchor IDs and build up TOC.
struct HeadingLinks<'a, 'b, 'ids, I: Iterator<Item = Event<'a>>> { struct HeadingLinks<'a, 'b, 'ids, I> {
inner: I, inner: I,
toc: Option<&'b mut TocBuilder>, toc: Option<&'b mut TocBuilder>,
buf: VecDeque<Event<'a>>, buf: VecDeque<(Event<'a>, Range<usize>)>,
id_map: &'ids mut IdMap, id_map: &'ids mut IdMap,
} }
impl<'a, 'b, 'ids, I: Iterator<Item = Event<'a>>> HeadingLinks<'a, 'b, 'ids, I> { impl<'a, 'b, 'ids, I> HeadingLinks<'a, 'b, 'ids, I> {
fn new(iter: I, toc: Option<&'b mut TocBuilder>, ids: &'ids mut IdMap) -> Self { fn new(iter: I, toc: Option<&'b mut TocBuilder>, ids: &'ids mut IdMap) -> Self {
HeadingLinks { inner: iter, toc, buf: VecDeque::new(), id_map: ids } HeadingLinks { inner: iter, toc, buf: VecDeque::new(), id_map: ids }
} }
} }
impl<'a, 'b, 'ids, I: Iterator<Item = Event<'a>>> Iterator for HeadingLinks<'a, 'b, 'ids, I> { impl<'a, 'b, 'ids, I: Iterator<Item = (Event<'a>, Range<usize>)>> Iterator
type Item = Event<'a>; for HeadingLinks<'a, 'b, 'ids, I>
{
type Item = (Event<'a>, Range<usize>);
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
if let Some(e) = self.buf.pop_front() { if let Some(e) = self.buf.pop_front() {
@ -469,31 +471,28 @@ impl<'a, 'b, 'ids, I: Iterator<Item = Event<'a>>> Iterator for HeadingLinks<'a,
} }
let event = self.inner.next(); let event = self.inner.next();
if let Some(Event::Start(Tag::Heading(level))) = event { if let Some((Event::Start(Tag::Heading(level)), _)) = event {
let mut id = String::new(); let mut id = String::new();
for event in &mut self.inner { for event in &mut self.inner {
match &event { match event.0 {
Event::End(Tag::Heading(..)) => break, Event::End(Tag::Heading(..)) => break,
Event::Text(text) | Event::Code(text) => { Event::Text(text) | Event::Code(text) => {
id.extend(text.chars().filter_map(slugify)); id.extend(text.chars().filter_map(slugify));
} }
_ => {}
}
match event {
Event::Start(Tag::Link(_, _, _)) | Event::End(Tag::Link(..)) => {} Event::Start(Tag::Link(_, _, _)) | Event::End(Tag::Link(..)) => {}
event => self.buf.push_back(event), _ => self.buf.push_back(event),
} }
} }
let id = self.id_map.derive(id); let id = self.id_map.derive(id);
if let Some(ref mut builder) = self.toc { if let Some(ref mut builder) = self.toc {
let mut html_header = String::new(); let mut html_header = String::new();
html::push_html(&mut html_header, self.buf.iter().cloned()); html::push_html(&mut html_header, self.buf.iter().map(|(ev, _)| ev.clone()));
let sec = builder.push(level as u32, html_header, id.clone()); let sec = builder.push(level as u32, html_header, id.clone());
self.buf.push_front(Event::Html(format!("{} ", sec).into())); self.buf.push_front((Event::Html(format!("{} ", sec).into()), 0..0));
} }
self.buf.push_back(Event::Html(format!("</a></h{}>", level).into())); self.buf.push_back((Event::Html(format!("</a></h{}>", level).into()), 0..0));
let start_tags = format!( let start_tags = format!(
"<h{level} id=\"{id}\" class=\"section-header\">\ "<h{level} id=\"{id}\" class=\"section-header\">\
@ -501,7 +500,7 @@ impl<'a, 'b, 'ids, I: Iterator<Item = Event<'a>>> Iterator for HeadingLinks<'a,
id = id, id = id,
level = level level = level
); );
return Some(Event::Html(start_tags.into())); return Some((Event::Html(start_tags.into()), 0..0));
} }
event event
} }
@ -575,15 +574,16 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for SummaryLine<'a, I> {
/// Moves all footnote definitions to the end and add back links to the /// Moves all footnote definitions to the end and add back links to the
/// references. /// references.
struct Footnotes<'a, I: Iterator<Item = Event<'a>>> { struct Footnotes<'a, I> {
inner: I, inner: I,
footnotes: FxHashMap<String, (Vec<Event<'a>>, u16)>, footnotes: FxHashMap<String, (Vec<Event<'a>>, u16)>,
} }
impl<'a, I: Iterator<Item = Event<'a>>> Footnotes<'a, I> { impl<'a, I> Footnotes<'a, I> {
fn new(iter: I) -> Self { fn new(iter: I) -> Self {
Footnotes { inner: iter, footnotes: FxHashMap::default() } Footnotes { inner: iter, footnotes: FxHashMap::default() }
} }
fn get_entry(&mut self, key: &str) -> &mut (Vec<Event<'a>>, u16) { fn get_entry(&mut self, key: &str) -> &mut (Vec<Event<'a>>, u16) {
let new_id = self.footnotes.keys().count() + 1; let new_id = self.footnotes.keys().count() + 1;
let key = key.to_owned(); let key = key.to_owned();
@ -591,23 +591,23 @@ impl<'a, I: Iterator<Item = Event<'a>>> Footnotes<'a, I> {
} }
} }
impl<'a, I: Iterator<Item = Event<'a>>> Iterator for Footnotes<'a, I> { impl<'a, I: Iterator<Item = (Event<'a>, Range<usize>)>> Iterator for Footnotes<'a, I> {
type Item = Event<'a>; type Item = (Event<'a>, Range<usize>);
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
loop { loop {
match self.inner.next() { match self.inner.next() {
Some(Event::FootnoteReference(ref reference)) => { Some((Event::FootnoteReference(ref reference), range)) => {
let entry = self.get_entry(&reference); let entry = self.get_entry(&reference);
let reference = format!( let reference = format!(
"<sup id=\"fnref{0}\"><a href=\"#fn{0}\">{0}</a></sup>", "<sup id=\"fnref{0}\"><a href=\"#fn{0}\">{0}</a></sup>",
(*entry).1 (*entry).1
); );
return Some(Event::Html(reference.into())); return Some((Event::Html(reference.into()), range));
} }
Some(Event::Start(Tag::FootnoteDefinition(def))) => { Some((Event::Start(Tag::FootnoteDefinition(def)), _)) => {
let mut content = Vec::new(); let mut content = Vec::new();
for event in &mut self.inner { for (event, _) in &mut self.inner {
if let Event::End(Tag::FootnoteDefinition(..)) = event { if let Event::End(Tag::FootnoteDefinition(..)) = event {
break; break;
} }
@ -638,7 +638,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for Footnotes<'a, I> {
ret.push_str("</li>"); ret.push_str("</li>");
} }
ret.push_str("</ol></div>"); ret.push_str("</ol></div>");
return Some(Event::Html(ret.into())); return Some((Event::Html(ret.into()), 0..0));
} else { } else {
return None; return None;
} }
@ -946,13 +946,14 @@ impl Markdown<'_> {
}; };
let p = Parser::new_with_broken_link_callback(md, opts(), Some(&mut replacer)); let p = Parser::new_with_broken_link_callback(md, opts(), Some(&mut replacer));
let p = p.into_offset_iter();
let mut s = String::with_capacity(md.len() * 3 / 2); let mut s = String::with_capacity(md.len() * 3 / 2);
let p = HeadingLinks::new(p, None, &mut ids); let p = HeadingLinks::new(p, None, &mut ids);
let p = LinkReplacer::new(p, links);
let p = CodeBlocks::new(p, codes, edition, playground);
let p = Footnotes::new(p); let p = Footnotes::new(p);
let p = LinkReplacer::new(p.map(|(ev, _)| ev), links);
let p = CodeBlocks::new(p, codes, edition, playground);
html::push_html(&mut s, p); html::push_html(&mut s, p);
s s
@ -963,7 +964,7 @@ impl MarkdownWithToc<'_> {
crate fn into_string(self) -> String { crate fn into_string(self) -> String {
let MarkdownWithToc(md, mut ids, codes, edition, playground) = self; let MarkdownWithToc(md, mut ids, codes, edition, playground) = self;
let p = Parser::new_ext(md, opts()); let p = Parser::new_ext(md, opts()).into_offset_iter();
let mut s = String::with_capacity(md.len() * 3 / 2); let mut s = String::with_capacity(md.len() * 3 / 2);
@ -971,8 +972,8 @@ impl MarkdownWithToc<'_> {
{ {
let p = HeadingLinks::new(p, Some(&mut toc), &mut ids); let p = HeadingLinks::new(p, Some(&mut toc), &mut ids);
let p = CodeBlocks::new(p, codes, edition, playground);
let p = Footnotes::new(p); let p = Footnotes::new(p);
let p = CodeBlocks::new(p.map(|(ev, _)| ev), codes, edition, playground);
html::push_html(&mut s, p); html::push_html(&mut s, p);
} }
@ -988,19 +989,19 @@ impl MarkdownHtml<'_> {
if md.is_empty() { if md.is_empty() {
return String::new(); return String::new();
} }
let p = Parser::new_ext(md, opts()); let p = Parser::new_ext(md, opts()).into_offset_iter();
// Treat inline HTML as plain text. // Treat inline HTML as plain text.
let p = p.map(|event| match event { let p = p.map(|event| match event.0 {
Event::Html(text) => Event::Text(text), Event::Html(text) => (Event::Text(text), event.1),
_ => event, _ => event,
}); });
let mut s = String::with_capacity(md.len() * 3 / 2); let mut s = String::with_capacity(md.len() * 3 / 2);
let p = HeadingLinks::new(p, None, &mut ids); let p = HeadingLinks::new(p, None, &mut ids);
let p = CodeBlocks::new(p, codes, edition, playground);
let p = Footnotes::new(p); let p = Footnotes::new(p);
let p = CodeBlocks::new(p.map(|(ev, _)| ev), codes, edition, playground);
html::push_html(&mut s, p); html::push_html(&mut s, p);
s s
@ -1153,7 +1154,7 @@ crate fn plain_text_summary(md: &str) -> String {
s s
} }
crate fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> { crate fn markdown_links(md: &str) -> Vec<(String, Range<usize>)> {
if md.is_empty() { if md.is_empty() {
return vec![]; return vec![];
} }
@ -1161,25 +1162,21 @@ crate fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
let mut links = vec![]; let mut links = vec![];
let mut shortcut_links = vec![]; let mut shortcut_links = vec![];
{ let span_for_link = |link: &str, span: Range<usize>| {
let locate = |s: &str| unsafe { // Pulldown includes the `[]` as well as the URL. Only highlight the relevant span.
let s_start = s.as_ptr(); // NOTE: uses `rfind` in case the title and url are the same: `[Ok][Ok]`
let s_end = s_start.add(s.len()); match md[span.clone()].rfind(link) {
let md_start = md.as_ptr(); Some(start) => {
let md_end = md_start.add(md.len()); let start = span.start + start;
if md_start <= s_start && s_end <= md_end { start..start + link.len()
let start = s_start.offset_from(md_start) as usize; }
let end = s_end.offset_from(md_start) as usize; // This can happen for things other than intra-doc links, like `#1` expanded to `https://github.com/rust-lang/rust/issues/1`.
Some(start..end) None => span,
} else {
None
} }
}; };
let mut push = |link: BrokenLink<'_>| { let mut push = |link: BrokenLink<'_>| {
// FIXME: use `link.span` instead of `locate` let span = span_for_link(link.reference, link.span);
// (doing it now includes the `[]` as well as the text) shortcut_links.push((link.reference.to_owned(), span));
shortcut_links.push((link.reference.to_owned(), locate(link.reference)));
None None
}; };
let p = Parser::new_with_broken_link_callback(md, opts(), Some(&mut push)); let p = Parser::new_with_broken_link_callback(md, opts(), Some(&mut push));
@ -1187,16 +1184,13 @@ crate fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
// There's no need to thread an IdMap through to here because // There's no need to thread an IdMap through to here because
// the IDs generated aren't going to be emitted anywhere. // the IDs generated aren't going to be emitted anywhere.
let mut ids = IdMap::new(); let mut ids = IdMap::new();
let iter = Footnotes::new(HeadingLinks::new(p, None, &mut ids)); let iter = Footnotes::new(HeadingLinks::new(p.into_offset_iter(), None, &mut ids));
for ev in iter { for ev in iter {
if let Event::Start(Tag::Link(_, dest, _)) = ev { if let Event::Start(Tag::Link(_, dest, _)) = ev.0 {
debug!("found link: {}", dest); debug!("found link: {}", dest);
links.push(match dest { let span = span_for_link(&dest, ev.1);
CowStr::Borrowed(s) => (s.to_owned(), locate(s)), links.push((dest.into_string(), span));
s @ (CowStr::Boxed(..) | CowStr::Inlined(..)) => (s.into_string(), None),
});
}
} }
} }

View File

@ -180,7 +180,7 @@ struct DiagnosticInfo<'a> {
item: &'a Item, item: &'a Item,
dox: &'a str, dox: &'a str,
ori_link: &'a str, ori_link: &'a str,
link_range: Option<Range<usize>>, link_range: Range<usize>,
} }
#[derive(Clone, Debug, Hash)] #[derive(Clone, Debug, Hash)]
@ -920,7 +920,7 @@ impl LinkCollector<'_, '_> {
parent_node: Option<DefId>, parent_node: Option<DefId>,
krate: CrateNum, krate: CrateNum,
ori_link: String, ori_link: String,
link_range: Option<Range<usize>>, link_range: Range<usize>,
) -> Option<ItemLink> { ) -> Option<ItemLink> {
trace!("considering link '{}'", ori_link); trace!("considering link '{}'", ori_link);
@ -1566,7 +1566,7 @@ fn report_diagnostic(
msg: &str, msg: &str,
item: &Item, item: &Item,
dox: &str, dox: &str,
link_range: &Option<Range<usize>>, link_range: &Range<usize>,
decorate: impl FnOnce(&mut DiagnosticBuilder<'_>, Option<rustc_span::Span>), decorate: impl FnOnce(&mut DiagnosticBuilder<'_>, Option<rustc_span::Span>),
) { ) {
let hir_id = match cx.as_local_hir_id(item.def_id) { let hir_id = match cx.as_local_hir_id(item.def_id) {
@ -1584,11 +1584,7 @@ fn report_diagnostic(
cx.tcx.struct_span_lint_hir(lint, hir_id, sp, |lint| { cx.tcx.struct_span_lint_hir(lint, hir_id, sp, |lint| {
let mut diag = lint.build(msg); let mut diag = lint.build(msg);
let span = link_range let span = super::source_span_for_markdown_range(cx, dox, link_range, attrs);
.as_ref()
.and_then(|range| super::source_span_for_markdown_range(cx, dox, range, attrs));
if let Some(link_range) = link_range {
if let Some(sp) = span { if let Some(sp) = span {
diag.set_span(sp); diag.set_span(sp);
} else { } else {
@ -1609,7 +1605,6 @@ fn report_diagnostic(
found = link_range.len(), found = link_range.len(),
)); ));
} }
}
decorate(&mut diag, span); decorate(&mut diag, span);
@ -1628,7 +1623,7 @@ fn resolution_failure(
path_str: &str, path_str: &str,
disambiguator: Option<Disambiguator>, disambiguator: Option<Disambiguator>,
dox: &str, dox: &str,
link_range: Option<Range<usize>>, link_range: Range<usize>,
kinds: SmallVec<[ResolutionFailure<'_>; 3]>, kinds: SmallVec<[ResolutionFailure<'_>; 3]>,
) { ) {
report_diagnostic( report_diagnostic(
@ -1862,7 +1857,7 @@ fn anchor_failure(
item: &Item, item: &Item,
path_str: &str, path_str: &str,
dox: &str, dox: &str,
link_range: Option<Range<usize>>, link_range: Range<usize>,
failure: AnchorFailure, failure: AnchorFailure,
) { ) {
let msg = match failure { let msg = match failure {
@ -1887,7 +1882,7 @@ fn ambiguity_error(
item: &Item, item: &Item,
path_str: &str, path_str: &str,
dox: &str, dox: &str,
link_range: Option<Range<usize>>, link_range: Range<usize>,
candidates: Vec<Res>, candidates: Vec<Res>,
) { ) {
let mut msg = format!("`{}` is ", path_str); let mut msg = format!("`{}` is ", path_str);
@ -1936,13 +1931,12 @@ fn suggest_disambiguator(
path_str: &str, path_str: &str,
dox: &str, dox: &str,
sp: Option<rustc_span::Span>, sp: Option<rustc_span::Span>,
link_range: &Option<Range<usize>>, link_range: &Range<usize>,
) { ) {
let suggestion = disambiguator.suggestion(); let suggestion = disambiguator.suggestion();
let help = format!("to link to the {}, {}", disambiguator.descr(), suggestion.descr()); let help = format!("to link to the {}, {}", disambiguator.descr(), suggestion.descr());
if let Some(sp) = sp { if let Some(sp) = sp {
let link_range = link_range.as_ref().expect("must have a link range if we have a span");
let msg = if dox.bytes().nth(link_range.start) == Some(b'`') { let msg = if dox.bytes().nth(link_range.start) == Some(b'`') {
format!("`{}`", suggestion.as_help(path_str)) format!("`{}`", suggestion.as_help(path_str))
} else { } else {
@ -1961,7 +1955,7 @@ fn privacy_error(
item: &Item, item: &Item,
path_str: &str, path_str: &str,
dox: &str, dox: &str,
link_range: Option<Range<usize>>, link_range: Range<usize>,
) { ) {
let sym; let sym;
let item_name = match item.name { let item_name = match item.name {