Skip to content

Commit e66dafc

Browse files
committed
Pass correct line number for indented code blocks.
1 parent cfc856a commit e66dafc

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Diff for: src/librustdoc/html/markdown.rs

+6
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,12 @@ crate fn find_testable_code<T: doctest::Tester>(
689689
.join("\n");
690690

691691
nb_lines += doc[prev_offset..offset.start].lines().count();
692+
// If there are characters between the preceding line ending and
693+
// this code block, `str::lines` will return an additional line,
694+
// which we subtract here.
695+
if nb_lines != 0 && !&doc[prev_offset..offset.start].ends_with("\n") {
696+
nb_lines -= 1;
697+
}
692698
let line = tests.get_line() + nb_lines + 1;
693699
tests.add_test(text, block_info, line);
694700
prev_offset = offset.start;

Diff for: src/librustdoc/html/markdown/tests.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{plain_text_summary, short_markdown_summary};
1+
use super::{find_testable_code, plain_text_summary, short_markdown_summary};
22
use super::{ErrorCodes, IdMap, Ignore, LangString, Markdown, MarkdownHtml};
33
use rustc_span::edition::{Edition, DEFAULT_EDITION};
44

@@ -298,3 +298,25 @@ fn test_markdown_html_escape() {
298298
t("Struct<'a, T>", "<p>Struct&lt;’a, T&gt;</p>\n");
299299
t("Struct<br>", "<p>Struct&lt;br&gt;</p>\n");
300300
}
301+
302+
#[test]
303+
fn test_find_testable_code_line() {
304+
fn t(input: &str, expect: &[usize]) {
305+
impl crate::doctest::Tester for Vec<usize> {
306+
fn add_test(&mut self, _test: String, _config: LangString, line: usize) {
307+
self.push(line);
308+
}
309+
}
310+
let mut lines = Vec::<usize>::new();
311+
find_testable_code(input, &mut lines, ErrorCodes::No, false, None);
312+
assert_eq!(lines, expect);
313+
}
314+
315+
t("", &[]);
316+
t("```rust\n```", &[1]);
317+
t(" ```rust\n```", &[1]);
318+
t("\n```rust\n```", &[2]);
319+
t("\n ```rust\n```", &[2]);
320+
t("```rust\n```\n```rust\n```", &[1, 3]);
321+
t("```rust\n```\n ```rust\n```", &[1, 3]);
322+
}

0 commit comments

Comments
 (0)