Removes */ in block doc comments

This commit is contained in:
kjeremy 2019-07-31 11:43:00 -04:00
parent 7c0a9718aa
commit d65dc40348
2 changed files with 62 additions and 2 deletions

View File

@ -138,6 +138,55 @@ mod foo {}
assert_eq!("this\nis\nmod\nfoo", module.doc_comment_text().unwrap());
}
#[test]
fn test_doc_comment_single_line_block_strips_suffix() {
let file = SourceFile::parse(
r#"
/** this is mod foo*/
mod foo {}
"#,
)
.ok()
.unwrap();
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
assert_eq!("this is mod foo", module.doc_comment_text().unwrap());
}
#[test]
fn test_doc_comment_single_line_block_strips_suffix_whitespace() {
let file = SourceFile::parse(
r#"
/** this is mod foo */
mod foo {}
"#,
)
.ok()
.unwrap();
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
assert_eq!("this is mod foo", module.doc_comment_text().unwrap());
}
#[test]
fn test_doc_comment_multi_line_block_strips_suffix() {
let file = SourceFile::parse(
r#"
/**
this
is
mod foo
*/
mod foo {}
"#,
)
.ok()
.unwrap();
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
assert_eq!(
" this\n is\n mod foo\n ",
module.doc_comment_text().unwrap()
);
}
#[test]
fn test_where_predicates() {
fn assert_bound(text: &str, bound: Option<TypeBound>) {

View File

@ -115,7 +115,7 @@ fn doc_comments(&self) -> CommentIter {
}
/// Returns the textual content of a doc comment block as a single string.
/// That is, strips leading `///` (+ optional 1 character of whitespace)
/// That is, strips leading `///` or trailing `*/` (+ optional 1 character of whitespace in either direction)
/// and joins lines.
fn doc_comment_text(&self) -> Option<String> {
let mut has_comments = false;
@ -136,7 +136,18 @@ fn doc_comment_text(&self) -> Option<String> {
prefix_len
};
line[pos..].to_owned()
let end = if comment.kind().shape.is_block() && line.ends_with("*/") {
// FIXME: Use `nth_back` here once stable
if line.chars().rev().nth(2).map(|c| c.is_whitespace()).unwrap_or(false) {
line.len() - 3
} else {
line.len() - 2
}
} else {
line.len()
};
line[pos..end].to_owned()
})
.join("\n");