Add suggestion in case a "ignore" doc block has invalid rust code inside

This commit is contained in:
Guillaume Gomez 2021-01-01 15:35:12 +01:00
parent 75e1acb63a
commit 5bf5b12707
2 changed files with 20 additions and 6 deletions

View File

@ -1213,6 +1213,7 @@ crate struct RustCodeBlock {
crate code: Range<usize>,
crate is_fenced: bool,
crate syntax: Option<String>,
crate is_ignore: bool,
}
/// Returns a range of bytes for each code block in the markdown that is tagged as `rust` or
@ -1228,7 +1229,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
while let Some((event, offset)) = p.next() {
if let Event::Start(Tag::CodeBlock(syntax)) = event {
let (syntax, code_start, code_end, range, is_fenced) = match syntax {
let (syntax, code_start, code_end, range, is_fenced, is_ignore) = match syntax {
CodeBlockKind::Fenced(syntax) => {
let syntax = syntax.as_ref();
let lang_string = if syntax.is_empty() {
@ -1249,6 +1250,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
range: offset,
code,
syntax,
is_ignore: lang_string.ignore != Ignore::None,
});
continue;
}
@ -1259,6 +1261,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
range: offset,
code,
syntax,
is_ignore: lang_string.ignore != Ignore::None,
});
continue;
}
@ -1266,7 +1269,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
while let Some((Event::Text(_), offset)) = p.next() {
code_end = offset.end;
}
(syntax, code_start, code_end, offset, true)
(syntax, code_start, code_end, offset, true, lang_string.ignore != Ignore::None)
}
CodeBlockKind::Indented => {
// The ending of the offset goes too far sometime so we reduce it by one in
@ -1278,9 +1281,10 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
offset.end,
Range { start: offset.start, end: offset.end - 1 },
false,
false,
)
} else {
(None, offset.start, offset.end, offset, false)
(None, offset.start, offset.end, offset, false, false)
}
}
};
@ -1290,6 +1294,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
range,
code: Range { start: code_start, end: code_end },
syntax,
is_ignore,
});
}
}

View File

@ -51,10 +51,10 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
let mut diag = if let Some(sp) =
super::source_span_for_markdown_range(self.cx, &dox, &code_block.range, &item.attrs)
{
let warning_message = if buffer.has_errors {
"could not parse code block as Rust code"
let (warning_message, suggest_using_text) = if buffer.has_errors {
("could not parse code block as Rust code", true)
} else {
"Rust code block is empty"
("Rust code block is empty", false)
};
let mut diag = self.cx.sess().struct_span_warn(sp, warning_message);
@ -67,6 +67,15 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
String::from("```text"),
Applicability::MachineApplicable,
);
} else if suggest_using_text && code_block.is_ignore {
let sp = sp.from_inner(InnerSpan::new(0, 3));
diag.span_suggestion(
sp,
"`ignore` code blocks require valid Rust code for syntax highlighting. \
Mark blocks that do not contain Rust code as text",
String::from("```text,"),
Applicability::MachineApplicable,
);
}
diag