Skip to content

Commit bcc7daa

Browse files
committed
rustdoc: Improve comment stripping
There is less implicit removal of various comment styles, and it also removes extraneous stars occasionally found in docblock comments. It turns out that the bug for getops was just a differently formatted block. Closes #9425 Closes #9417
1 parent 35c0cdf commit bcc7daa

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

src/libextra/getopts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
//! that requires an input file to be specified, accepts an optional output
3030
//! file name following -o, and accepts both -h and --help as optional flags.
3131
//!
32-
//! ```
32+
//! ~~~{.rust}
3333
//! exter mod extra;
3434
//! use extra::getopts::*;
3535
//! use std::os;
@@ -75,7 +75,7 @@
7575
//! };
7676
//! do_work(input, output);
7777
//! }
78-
//! ```
78+
//! ~~~
7979
8080
use std::cmp::Eq;
8181
use std::result::{Err, Ok};

src/libsyntax/parse/comments.rs

+40-15
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,19 @@ pub fn strip_doc_comment_decoration(comment: &str) -> ~str {
5959
fn vertical_trim(lines: ~[~str]) -> ~[~str] {
6060
let mut i = 0u;
6161
let mut j = lines.len();
62+
// first line of all-stars should be omitted
63+
if lines.len() > 0 && lines[0].iter().all(|c| c == '*') {
64+
i += 1;
65+
}
6266
while i < j && lines[i].trim().is_empty() {
63-
i += 1u;
67+
i += 1;
68+
}
69+
// like the first, a last line of all stars should be omitted
70+
if j > i && lines[j - 1].iter().skip(1).all(|c| c == '*') {
71+
j -= 1;
6472
}
65-
while j > i && lines[j - 1u].trim().is_empty() {
66-
j -= 1u;
73+
while j > i && lines[j - 1].trim().is_empty() {
74+
j -= 1;
6775
}
6876
return lines.slice(i, j).to_owned();
6977
}
@@ -106,8 +114,12 @@ pub fn strip_doc_comment_decoration(comment: &str) -> ~str {
106114
}
107115
}
108116

109-
if comment.starts_with("//") {
110-
return comment.slice(3u, comment.len()).to_owned();
117+
// one-line comments lose their prefix
118+
static ONLINERS: &'static [&'static str] = &["///!", "///", "//!", "//"];
119+
for prefix in ONLINERS.iter() {
120+
if comment.starts_with(*prefix) {
121+
return comment.slice_from(prefix.len()).to_owned();
122+
}
111123
}
112124

113125
if comment.starts_with("/*") {
@@ -384,29 +396,42 @@ mod test {
384396

385397
#[test] fn test_block_doc_comment_1() {
386398
let comment = "/**\n * Test \n ** Test\n * Test\n*/";
387-
let correct_stripped = " Test \n* Test\n Test";
388399
let stripped = strip_doc_comment_decoration(comment);
389-
assert_eq!(stripped.slice(0, stripped.len()), correct_stripped);
400+
assert_eq!(stripped, ~" Test \n* Test\n Test");
390401
}
391402
392403
#[test] fn test_block_doc_comment_2() {
393404
let comment = "/**\n * Test\n * Test\n*/";
394-
let correct_stripped = " Test\n Test";
395405
let stripped = strip_doc_comment_decoration(comment);
396-
assert_eq!(stripped.slice(0, stripped.len()), correct_stripped);
406+
assert_eq!(stripped, ~" Test\n Test");
397407
}
398408
399409
#[test] fn test_block_doc_comment_3() {
400410
let comment = "/**\n let a: *int;\n *a = 5;\n*/";
401-
let correct_stripped = " let a: *int;\n *a = 5;";
402411
let stripped = strip_doc_comment_decoration(comment);
403-
assert_eq!(stripped.slice(0, stripped.len()), correct_stripped);
412+
assert_eq!(stripped, ~" let a: *int;\n *a = 5;");
404413
}
405414
406-
#[test] fn test_line_doc_comment() {
407-
let comment = "/// Test";
408-
let correct_stripped = " Test";
415+
#[test] fn test_block_doc_comment_4() {
416+
let comment = "/*******************\n test\n *********************/";
409417
let stripped = strip_doc_comment_decoration(comment);
410-
assert_eq!(stripped.slice(0, stripped.len()), correct_stripped);
418+
assert_eq!(stripped, ~" test");
419+
}
420+
421+
#[test] fn test_line_doc_comment() {
422+
let stripped = strip_doc_comment_decoration("/// test");
423+
assert_eq!(stripped, ~" test");
424+
let stripped = strip_doc_comment_decoration("///! test");
425+
assert_eq!(stripped, ~" test");
426+
let stripped = strip_doc_comment_decoration("// test");
427+
assert_eq!(stripped, ~" test");
428+
let stripped = strip_doc_comment_decoration("// test");
429+
assert_eq!(stripped, ~" test");
430+
let stripped = strip_doc_comment_decoration("///test");
431+
assert_eq!(stripped, ~"test");
432+
let stripped = strip_doc_comment_decoration("///!test");
433+
assert_eq!(stripped, ~"test");
434+
let stripped = strip_doc_comment_decoration("//test");
435+
assert_eq!(stripped, ~"test");
411436
}
412437
}

0 commit comments

Comments
 (0)