Skip to content

Commit ea3078d

Browse files
include comments in doctest partition logic
1 parent 22de23e commit ea3078d

File tree

1 file changed

+55
-16
lines changed

1 file changed

+55
-16
lines changed

src/librustdoc/test.rs

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -496,32 +496,71 @@ pub fn make_test(s: &str,
496496

497497
// FIXME(aburka): use a real parser to deal with multiline attributes
498498
fn partition_source(s: &str) -> (String, String, String) {
499-
let mut after_header = false;
499+
#[derive(Copy, Clone, PartialEq)]
500+
enum PartitionState {
501+
Attrs,
502+
Crates,
503+
Other,
504+
}
505+
let mut state = PartitionState::Attrs;
500506
let mut before = String::new();
501507
let mut crates = String::new();
502508
let mut after = String::new();
503509

504510
for line in s.lines() {
505511
let trimline = line.trim();
506-
let header = trimline.chars().all(|c| c.is_whitespace()) ||
507-
trimline.starts_with("#![") ||
508-
trimline.starts_with("#[macro_use] extern crate") ||
509-
trimline.starts_with("extern crate");
510-
if !header || after_header {
511-
after_header = true;
512-
after.push_str(line);
513-
after.push_str("\n");
514-
} else {
515-
if trimline.starts_with("#[macro_use] extern crate")
516-
|| trimline.starts_with("extern crate") {
512+
513+
// FIXME(misdreavus): if a doc comment is placed on an extern crate statement, it will be
514+
// shunted into "everything else"
515+
match state {
516+
PartitionState::Attrs => {
517+
state = if trimline.starts_with("#![") ||
518+
trimline.chars().all(|c| c.is_whitespace()) ||
519+
(trimline.starts_with("//") && !trimline.starts_with("///"))
520+
{
521+
PartitionState::Attrs
522+
} else if trimline.starts_with("extern crate") ||
523+
trimline.starts_with("#[macro_use] extern crate")
524+
{
525+
PartitionState::Crates
526+
} else {
527+
PartitionState::Other
528+
};
529+
}
530+
PartitionState::Crates => {
531+
state = if trimline.starts_with("extern crate") ||
532+
trimline.starts_with("#[macro_use] extern crate") ||
533+
trimline.chars().all(|c| c.is_whitespace()) ||
534+
(trimline.starts_with("//") && !trimline.starts_with("///"))
535+
{
536+
PartitionState::Crates
537+
} else {
538+
PartitionState::Other
539+
};
540+
}
541+
PartitionState::Other => {}
542+
}
543+
544+
match state {
545+
PartitionState::Attrs => {
546+
before.push_str(line);
547+
before.push_str("\n");
548+
}
549+
PartitionState::Crates => {
517550
crates.push_str(line);
518551
crates.push_str("\n");
519552
}
520-
before.push_str(line);
521-
before.push_str("\n");
553+
PartitionState::Other => {
554+
after.push_str(line);
555+
after.push_str("\n");
556+
}
522557
}
523558
}
524559

560+
debug!("before:\n{}", before);
561+
debug!("crates:\n{}", crates);
562+
debug!("after:\n{}", after);
563+
525564
(before, after, crates)
526565
}
527566

@@ -1038,8 +1077,8 @@ fn main() {
10381077
assert_eq!(2+2, 4);";
10391078
let expected =
10401079
"#![allow(unused)]
1041-
fn main() {
10421080
//Ceci n'est pas une `fn main`
1081+
fn main() {
10431082
assert_eq!(2+2, 4);
10441083
}".to_string();
10451084
let output = make_test(input, None, false, &opts);
@@ -1086,8 +1125,8 @@ assert_eq!(2+2, 4);";
10861125

10871126
let expected =
10881127
"#![allow(unused)]
1089-
fn main() {
10901128
// fn main
1129+
fn main() {
10911130
assert_eq!(2+2, 4);
10921131
}".to_string();
10931132

0 commit comments

Comments
 (0)