Skip to content

Remove horizontal trimming from libsyntax::parse::comments::strip_doc_comment_decoration #7135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/librustdoc/attr_parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand Down Expand Up @@ -152,6 +152,6 @@ mod test {
fn should_concatenate_multiple_doc_comments() {
let source = @"/// foo\n/// bar";
let desc = parse_desc(parse_attributes(source));
assert!(desc == Some(~"foo\nbar"));
assert!(desc == Some(~" foo\n bar"));
}
}
74 changes: 35 additions & 39 deletions src/libsyntax/parse/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,53 +70,30 @@ pub fn strip_doc_comment_decoration(comment: &str) -> ~str {
return lines.slice(i, j).to_owned();
}

// drop leftmost columns that contain only values in chars
fn block_trim(lines: ~[~str], chars: ~str, max: Option<uint>) -> ~[~str] {

let mut i = max.get_or_default(uint::max_value);
for lines.each |line| {
if line.trim().is_empty() {
loop;
}
for line.iter().enumerate().advance |(j, c)| {
if j >= i {
break;
}
if !chars.contains_char(c) {
i = j;
break;
}
}
}

return do lines.map |line| {
let mut chars = ~[];
for line.iter().advance |c| { chars.push(c) }
if i > chars.len() {
~""
} else {
str::from_chars(chars.slice(i, chars.len()).to_owned())
}
};
}

if comment.starts_with("//") {
// FIXME #5475:
// return comment.slice(3u, comment.len()).trim().to_owned();
let r = comment.slice(3u, comment.len()); return r.trim().to_owned();

// return comment.slice(3u, comment.len()).to_owned();
let r = comment.slice(3u, comment.len()); return r.to_owned();
}

if comment.starts_with("/*") {
let mut lines = ~[];
for str::each_line_any(comment.slice(3u, comment.len() - 2u)) |line| {
lines.push(line.to_owned())
let mut i = 0u;
for line.iter().enumerate().advance |(j, c)| {
if !"* \t".contains_char(c) {
i = j;
break;
}
if c == '*' {
i = j + 1;
break;
}
}
let r = line.slice(i, line.len());
lines.push(r.to_owned())
}
let lines = vertical_trim(lines);
let lines = block_trim(lines, ~"\t ", None);
let lines = block_trim(lines, ~"*", Some(1u));
let lines = block_trim(lines, ~"\t ", None);
return lines.connect("\n");
return vertical_trim(lines).connect("\n");
}

fail!("not a doc-comment: %s", comment);
Expand Down Expand Up @@ -364,3 +341,22 @@ pub fn gather_comments_and_literals(span_diagnostic:

(comments, literals)
}

#[cfg(test)]
mod test {
use super::*;

#[test] fn test_block_doc_comment() {
let comment = "/**\n * Test \n** Test\n * Test\n*/";
let correct_stripped = " Test \n* Test\n Test";
let stripped = strip_doc_comment_decoration(comment);
assert_eq!(stripped.slice(0, stripped.len()), correct_stripped);
}

#[test] fn test_line_doc_comment() {
let comment = "/// Test";
let correct_stripped = " Test";
let stripped = strip_doc_comment_decoration(comment);
assert_eq!(stripped.slice(0, stripped.len()), correct_stripped);
}
}