Skip to content

Commit 5f0e494

Browse files
committed
auto merge of #7174 : SiegeLord/rust/remove_trim, r=brson
This change prevents the indentation in code blocks inside the /// doc comments from being eaten. The indentation that is the same across the consecutive doc comments is removed by the uindent_pass in librustdoc. The bug can be seen, e.g., here: http://static.rust-lang.org/doc/std/iterator.html#example-12 I also altered how the block comments are treated, for consistency. There isn't much testing done on the documentation output (I added a few tests of my own for the modified function), so I don't know if anything relied on the previous behavior. I checked a number of documentation files and observed either no change in output or changes that consistent of the above bug being fixed.
2 parents 6fbd538 + 6a6ffb4 commit 5f0e494

File tree

2 files changed

+67
-25
lines changed

2 files changed

+67
-25
lines changed

src/librustdoc/attr_parser.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -152,6 +152,6 @@ mod test {
152152
fn should_concatenate_multiple_doc_comments() {
153153
let source = @"/// foo\n/// bar";
154154
let desc = parse_desc(parse_attributes(source));
155-
assert!(desc == Some(~"foo\nbar"));
155+
assert!(desc == Some(~" foo\n bar"));
156156
}
157157
}

src/libsyntax/parse/comments.rs

+65-23
Original file line numberDiff line numberDiff line change
@@ -69,50 +69,59 @@ pub fn strip_doc_comment_decoration(comment: &str) -> ~str {
6969
return lines.slice(i, j).to_owned();
7070
}
7171

72-
// drop leftmost columns that contain only values in chars
73-
fn block_trim(lines: ~[~str], chars: ~str, max: Option<uint>) -> ~[~str] {
74-
75-
let mut i = max.get_or_default(uint::max_value);
76-
for lines.each |line| {
77-
if line.trim().is_empty() {
78-
loop;
79-
}
72+
/// remove a "[ \t]*\*" block from each line, if possible
73+
fn horizontal_trim(lines: ~[~str]) -> ~[~str] {
74+
let mut i = uint::max_value;
75+
let mut can_trim = true;
76+
let mut first = true;
77+
for lines.iter().advance |line| {
8078
for line.iter().enumerate().advance |(j, c)| {
81-
if j >= i {
79+
if j > i || !"* \t".contains_char(c) {
80+
can_trim = false;
8281
break;
8382
}
84-
if !chars.contains_char(c) {
85-
i = j;
83+
if c == '*' {
84+
if first {
85+
i = j;
86+
first = false;
87+
} else if i != j {
88+
can_trim = false;
89+
}
8690
break;
8791
}
8892
}
93+
if i > line.len() {
94+
can_trim = false;
95+
}
96+
if !can_trim {
97+
break;
98+
}
8999
}
90100

91-
return do lines.map |line| {
92-
let chars = line.iter().collect::<~[char]>();
93-
if i > chars.len() {
94-
~""
95-
} else {
96-
str::from_chars(chars.slice(i, chars.len()))
101+
if can_trim {
102+
do lines.map |line| {
103+
line.slice(i + 1, line.len()).to_owned()
97104
}
98-
};
105+
} else {
106+
lines
107+
}
99108
}
100109

101110
if comment.starts_with("//") {
102111
// FIXME #5475:
103-
// return comment.slice(3u, comment.len()).trim().to_owned();
104-
let r = comment.slice(3u, comment.len()); return r.trim().to_owned();
112+
// return comment.slice(3u, comment.len()).to_owned();
113+
let r = comment.slice(3u, comment.len()); return r.to_owned();
105114
}
106115

107116
if comment.starts_with("/*") {
108117
let lines = comment.slice(3u, comment.len() - 2u)
109118
.any_line_iter()
110119
.transform(|s| s.to_owned())
111120
.collect::<~[~str]>();
121+
112122
let lines = vertical_trim(lines);
113-
let lines = block_trim(lines, ~"\t ", None);
114-
let lines = block_trim(lines, ~"*", Some(1u));
115-
let lines = block_trim(lines, ~"\t ", None);
123+
let lines = horizontal_trim(lines);
124+
116125
return lines.connect("\n");
117126
}
118127

@@ -370,3 +379,36 @@ pub fn gather_comments_and_literals(span_diagnostic:
370379

371380
(comments, literals)
372381
}
382+
383+
#[cfg(test)]
384+
mod test {
385+
use super::*;
386+
387+
#[test] fn test_block_doc_comment_1() {
388+
let comment = "/**\n * Test \n ** Test\n * Test\n*/";
389+
let correct_stripped = " Test \n* Test\n Test";
390+
let stripped = strip_doc_comment_decoration(comment);
391+
assert_eq!(stripped.slice(0, stripped.len()), correct_stripped);
392+
}
393+
394+
#[test] fn test_block_doc_comment_2() {
395+
let comment = "/**\n * Test\n * Test\n*/";
396+
let correct_stripped = " Test\n Test";
397+
let stripped = strip_doc_comment_decoration(comment);
398+
assert_eq!(stripped.slice(0, stripped.len()), correct_stripped);
399+
}
400+
401+
#[test] fn test_block_doc_comment_3() {
402+
let comment = "/**\n let a: *int;\n *a = 5;\n*/";
403+
let correct_stripped = " let a: *int;\n *a = 5;";
404+
let stripped = strip_doc_comment_decoration(comment);
405+
assert_eq!(stripped.slice(0, stripped.len()), correct_stripped);
406+
}
407+
408+
#[test] fn test_line_doc_comment() {
409+
let comment = "/// Test";
410+
let correct_stripped = " Test";
411+
let stripped = strip_doc_comment_decoration(comment);
412+
assert_eq!(stripped.slice(0, stripped.len()), correct_stripped);
413+
}
414+
}

0 commit comments

Comments
 (0)