Skip to content

Update pulldown_cmark to 0.5 #4029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clippy_lints/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ serde = "1.0"
serde_derive = "1.0"
toml = "0.4"
unicode-normalization = "0.1"
pulldown-cmark = "0.2"
pulldown-cmark = "0.5.0"
url = "1.7.0"
if_chain = "0.1.3"
smallvec = { version = "0.6.5", features = ["union"] }
Expand Down
63 changes: 23 additions & 40 deletions clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use pulldown_cmark;
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
use rustc::{declare_tool_lint, impl_lint_pass};
use rustc_data_structures::fx::FxHashSet;
use std::ops::Range;
use syntax::ast;
use syntax::source_map::{BytePos, Span};
use syntax_pos::Pos;
Expand Down Expand Up @@ -57,25 +58,6 @@ impl EarlyLintPass for DocMarkdown {
}
}

struct Parser<'a> {
parser: pulldown_cmark::Parser<'a>,
}

impl<'a> Parser<'a> {
fn new(parser: pulldown_cmark::Parser<'a>) -> Self {
Self { parser }
}
}

impl<'a> Iterator for Parser<'a> {
type Item = (usize, pulldown_cmark::Event<'a>);

fn next(&mut self) -> Option<Self::Item> {
let offset = self.parser.get_offset();
self.parser.next().map(|event| (offset, event))
}
}

/// Cleanup documentation decoration (`///` and such).
///
/// We can't use `syntax::attr::AttributeMethods::with_desugared_doc` or
Expand Down Expand Up @@ -159,30 +141,31 @@ pub fn check_attrs<'a>(cx: &EarlyContext<'_>, valid_idents: &FxHashSet<String>,
}

if !doc.is_empty() {
let parser = Parser::new(pulldown_cmark::Parser::new(&doc));
let parser = parser.coalesce(|x, y| {
let parser = pulldown_cmark::Parser::new(&doc).into_offset_iter();
// Iterate over all `Events` and combine consecutive events into one
let events = parser.coalesce(|previous, current| {
use pulldown_cmark::Event::*;

let x_offset = x.0;
let y_offset = y.0;
let previous_range = previous.1;
let current_range = current.1;

match (x.1, y.1) {
(Text(x), Text(y)) => {
let mut x = x.into_owned();
x.push_str(&y);
Ok((x_offset, Text(x.into())))
match (previous.0, current.0) {
(Text(previous), Text(current)) => {
let mut previous = previous.to_string();
previous.push_str(&current);
Ok((Text(previous.into()), previous_range))
},
(x, y) => Err(((x_offset, x), (y_offset, y))),
(previous, current) => Err(((previous, previous_range), (current, current_range))),
}
});
check_doc(cx, valid_idents, parser, &spans);
check_doc(cx, valid_idents, events, &spans);
}
}

fn check_doc<'a, Events: Iterator<Item = (usize, pulldown_cmark::Event<'a>)>>(
fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize>)>>(
cx: &EarlyContext<'_>,
valid_idents: &FxHashSet<String>,
docs: Events,
events: Events,
spans: &[(usize, Span)],
) {
use pulldown_cmark::Event::*;
Expand All @@ -191,15 +174,15 @@ fn check_doc<'a, Events: Iterator<Item = (usize, pulldown_cmark::Event<'a>)>>(
let mut in_code = false;
let mut in_link = None;

for (offset, event) in docs {
for (event, range) in events {
match event {
Start(CodeBlock(_)) | Start(Code) => in_code = true,
End(CodeBlock(_)) | End(Code) => in_code = false,
Start(Link(link, _)) => in_link = Some(link),
End(Link(_, _)) => in_link = None,
Start(CodeBlock(_)) => in_code = true,
End(CodeBlock(_)) => in_code = false,
Start(Link(_, url, _)) => in_link = Some(url),
End(Link(..)) => in_link = None,
Start(_tag) | End(_tag) => (), // We don't care about other tags
Html(_html) | InlineHtml(_html) => (), // HTML is weird, just ignore it
SoftBreak | HardBreak => (),
SoftBreak | HardBreak | TaskListMarker(_) | Code(_) => (),
FootnoteReference(text) | Text(text) => {
if Some(&text) == in_link.as_ref() {
// Probably a link of the form `<http://example.com>`
Expand All @@ -209,15 +192,15 @@ fn check_doc<'a, Events: Iterator<Item = (usize, pulldown_cmark::Event<'a>)>>(
}

if !in_code {
let index = match spans.binary_search_by(|c| c.0.cmp(&offset)) {
let index = match spans.binary_search_by(|c| c.0.cmp(&range.start)) {
Ok(o) => o,
Err(e) => e - 1,
};

let (begin, span) = spans[index];

// Adjust for the beginning of the current `Event`
let span = span.with_lo(span.lo() + BytePos::from_usize(offset - begin));
let span = span.with_lo(span.lo() + BytePos::from_usize(range.start - begin));

check_text(cx, valid_idents, &text, span);
}
Expand Down