diff options
| author | Joshua Nelson <jyn514@gmail.com> | 2021-09-26 16:28:36 +0000 |
|---|---|---|
| committer | Joshua Nelson <jyn514@gmail.com> | 2021-09-26 16:28:36 +0000 |
| commit | f4aa3b544f19290a6d27697f78ba29771df22860 (patch) | |
| tree | 5bbee152e52e7f15296b2ec6b02e20c1ea806145 | |
| parent | dda2a0eca4a70acbdd2b65f5fc5e4cd38c39351b (diff) | |
| download | rust-f4aa3b544f19290a6d27697f78ba29771df22860.tar.gz rust-f4aa3b544f19290a6d27697f78ba29771df22860.zip | |
Preserve the whole LangSyntax when parsing doctests
Previously, only the raw string and the `is_ignore` field were preserved, which made it hard to recover anything else.
| -rw-r--r-- | src/librustdoc/html/markdown.rs | 23 | ||||
| -rw-r--r-- | src/librustdoc/passes/check_code_block_syntax.rs | 4 |
2 files changed, 10 insertions, 17 deletions
diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index c971e231463..fda2512a050 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -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 @@ -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() { @@ -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)) => { @@ -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; } @@ -1365,8 +1361,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; } @@ -1374,22 +1369,21 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB 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) } } }; @@ -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, }); } } diff --git a/src/librustdoc/passes/check_code_block_syntax.rs b/src/librustdoc/passes/check_code_block_syntax.rs index c9f66d096f0..3deede8eeac 100644 --- a/src/librustdoc/passes/check_code_block_syntax.rs +++ b/src/librustdoc/passes/check_code_block_syntax.rs @@ -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( |
