Skip to content

Commit

Permalink
Fix horizontal trim for block doc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Feb 7, 2022
1 parent a00e130 commit e394287
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
16 changes: 11 additions & 5 deletions compiler/rustc_ast/src/util/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol {
if i != 0 || j != lines.len() { Some((i, j)) } else { None }
}

fn get_horizontal_trim(lines: &[&str], kind: CommentKind) -> Option<usize> {
fn get_horizontal_trim<'a>(lines: &'a [&str], kind: CommentKind) -> Option<String> {
let mut i = usize::MAX;
let mut first = true;

// In case we have doc comments like `/**` or `/*!`, we want to remove stars if they are
// present. However, we first need to strip the empty lines so they don't get in the middle
// when we try to compute the "horizontal trim".
let lines = if kind == CommentKind::Block {
let mut i = 0;
// Whatever happens, we skip the first line.
let mut i = if lines[0].trim_start().starts_with('*') { 0 } else { 1 };
let mut j = lines.len();

while i < j && lines[i].trim().is_empty() {
Expand Down Expand Up @@ -84,7 +85,7 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol {
return None;
}
}
Some(i)
if lines.is_empty() { None } else { Some(lines[0][..i].into()) }
}

let data_s = data.as_str();
Expand All @@ -102,8 +103,13 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol {
changes = true;
// remove a "[ \t]*\*" block from each line, if possible
for line in lines.iter_mut() {
if horizontal + 1 < line.len() {
*line = &line[horizontal + 1..];
if let Some(tmp) = line.strip_prefix(&horizontal) {
*line = tmp;
if kind == CommentKind::Block
&& (*line == "*" || line.starts_with("* ") || line.starts_with("**"))
{
*line = &line[1..];
}
}
}
}
Expand Down
28 changes: 27 additions & 1 deletion compiler/rustc_ast/src/util/comments/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn test_block_doc_comment_3() {
create_default_session_globals_then(|| {
let comment = "\n let a: *i32;\n *a = 5;\n";
let stripped = beautify_doc_string(Symbol::intern(comment), CommentKind::Block);
assert_eq!(stripped.as_str(), " let a: *i32;\n *a = 5;");
assert_eq!(stripped.as_str(), "let a: *i32;\n*a = 5;");
})
}

Expand All @@ -41,3 +41,29 @@ fn test_line_doc_comment() {
assert_eq!(stripped.as_str(), "!test");
})
}

#[test]
fn test_doc_blocks() {
create_default_session_globals_then(|| {
let stripped = beautify_doc_string(
Symbol::intern(
" # Returns
*
",
),
CommentKind::Block,
);
assert_eq!(stripped.as_str(), " # Returns\n\n");

let stripped = beautify_doc_string(
Symbol::intern(
"
* # Returns
*
",
),
CommentKind::Block,
);
assert_eq!(stripped.as_str(), " # Returns\n\n");
})
}

0 comments on commit e394287

Please sign in to comment.