Skip to content

Commit e8e145f

Browse files
Merge #8561
8561: Accept `E<error_number>` notation in doctests r=Veykril a=ChayimFriedman2 ```` ```compile_fail,E0000 ``` ```` The code was stolen from rustdoc at https://github.com/rust-lang/rust/blob/392ba2ba1a7d6c542d2459fb8133bebf62a4a423/src/librustdoc/html/markdown.rs#L866-L867 Co-authored-by: Chayim Refael Friedman <chayimfr@gmail.com>
2 parents 2ace128 + 6c287e1 commit e8e145f

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

crates/ide/src/syntax_highlighting/inject.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[
9090
"edition2021",
9191
];
9292

93+
fn is_rustdoc_fence_token(token: &str) -> bool {
94+
if RUSTDOC_FENCE_TOKENS.contains(&token) {
95+
return true;
96+
}
97+
token.starts_with('E') && token.len() == 5 && token[1..].parse::<u32>().is_ok()
98+
}
99+
93100
/// Injection of syntax highlighting of doctests.
94101
pub(super) fn doc_comment(
95102
hl: &mut Highlights,
@@ -174,8 +181,7 @@ pub(super) fn doc_comment(
174181
is_codeblock = !is_codeblock;
175182
// Check whether code is rust by inspecting fence guards
176183
let guards = &line[idx + RUSTDOC_FENCE.len()..];
177-
let is_rust =
178-
guards.split(',').all(|sub| RUSTDOC_FENCE_TOKENS.contains(&sub.trim()));
184+
let is_rust = guards.split(',').all(|sub| is_rustdoc_fence_token(sub.trim()));
179185
is_doctest = is_codeblock && is_rust;
180186
continue;
181187
}

crates/rust-analyzer/src/markdown.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ pub(crate) fn format_docs(src: &str) -> String {
2727
in_code_block ^= true;
2828

2929
if in_code_block {
30-
is_rust = header
31-
.split(',')
32-
.all(|sub| RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUST_SPECIFIC.contains(&sub.trim()));
30+
is_rust =
31+
header.split(',').all(|sub| is_rust_specific_code_block_attribute(sub.trim()));
3332

3433
if is_rust {
3534
line = "```rust";
@@ -42,6 +41,13 @@ pub(crate) fn format_docs(src: &str) -> String {
4241
processed_lines.join("\n")
4342
}
4443

44+
fn is_rust_specific_code_block_attribute(attr: &str) -> bool {
45+
if RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUST_SPECIFIC.contains(&attr) {
46+
return true;
47+
}
48+
attr.starts_with('E') && attr.len() == 5 && attr[1..].parse::<u32>().is_ok()
49+
}
50+
4551
fn code_line_ignored_by_rustdoc(line: &str) -> bool {
4652
let trimmed = line.trim();
4753
trimmed == "#" || trimmed.starts_with("# ") || trimmed.starts_with("#\t")
@@ -81,6 +87,12 @@ mod tests {
8187
assert_eq!(format_docs(comment), "```rust\nlet z = 55;\n```");
8288
}
8389

90+
#[test]
91+
fn test_format_docs_handles_error_codes() {
92+
let comment = "```compile_fail,E0641\nlet b = 0 as *const _;\n```";
93+
assert_eq!(format_docs(comment), "```rust\nlet b = 0 as *const _;\n```");
94+
}
95+
8496
#[test]
8597
fn test_format_docs_skips_comments_in_rust_block() {
8698
let comment =

0 commit comments

Comments
 (0)