Skip to content

Commit

Permalink
Preserve the whole LangSyntax when parsing doctests
Browse files Browse the repository at this point in the history
Previously, only the raw string and the `is_ignore` field were
preserved, which made it hard to recover anything else.
  • Loading branch information
jyn514 committed Sep 26, 2021
1 parent dda2a0e commit f4aa3b5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
23 changes: 8 additions & 15 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,8 +1316,7 @@ crate struct RustCodeBlock {
/// The range in the markdown that the code within the code block occupies.
crate code: Range<usize>,
crate is_fenced: bool,
crate syntax: Option<String>,
crate is_ignore: bool,
crate lang_string: LangString,
}

/// Returns a range of bytes for each code block in the markdown that is tagged as `rust` or
Expand All @@ -1333,7 +1332,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB

while let Some((event, offset)) = p.next() {
if let Event::Start(Tag::CodeBlock(syntax)) = event {
let (syntax, code_start, code_end, range, is_fenced, is_ignore) = match syntax {
let (lang_string, code_start, code_end, range, is_fenced) = match syntax {
CodeBlockKind::Fenced(syntax) => {
let syntax = syntax.as_ref();
let lang_string = if syntax.is_empty() {
Expand All @@ -1344,8 +1343,6 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB
if !lang_string.rust {
continue;
}
let is_ignore = lang_string.ignore != Ignore::None;
let syntax = if syntax.is_empty() { None } else { Some(syntax.to_owned()) };
let (code_start, mut code_end) = match p.next() {
Some((Event::Text(_), offset)) => (offset.start, offset.end),
Some((_, sub_offset)) => {
Expand All @@ -1354,8 +1351,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB
is_fenced: true,
range: offset,
code,
syntax,
is_ignore,
lang_string,
});
continue;
}
Expand All @@ -1365,31 +1361,29 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB
is_fenced: true,
range: offset,
code,
syntax,
is_ignore,
lang_string,
});
continue;
}
};
while let Some((Event::Text(_), offset)) = p.next() {
code_end = offset.end;
}
(syntax, code_start, code_end, offset, true, is_ignore)
(lang_string, code_start, code_end, offset, true)
}
CodeBlockKind::Indented => {
// The ending of the offset goes too far sometime so we reduce it by one in
// these cases.
if offset.end > offset.start && md.get(offset.end..=offset.end) == Some(&"\n") {
(
None,
LangString::default(),
offset.start,
offset.end,
Range { start: offset.start, end: offset.end - 1 },
false,
false,
)
} else {
(None, offset.start, offset.end, offset, false, false)
(LangString::default(), offset.start, offset.end, offset, false)
}
}
};
Expand All @@ -1398,8 +1392,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB
is_fenced,
range,
code: Range { start: code_start, end: code_end },
syntax,
is_ignore,
lang_string,
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/passes/check_code_block_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
};

let hir_id = self.cx.tcx.hir().local_def_id_to_hir_id(local_id);
let empty_block = code_block.syntax.is_none() && code_block.is_fenced;
let is_ignore = code_block.is_ignore;
let empty_block = code_block.lang_string == Default::default() && code_block.is_fenced;
let is_ignore = code_block.lang_string.ignore != markdown::Ignore::None;

// The span and whether it is precise or not.
let (sp, precise_span) = match super::source_span_for_markdown_range(
Expand Down

0 comments on commit f4aa3b5

Please sign in to comment.