Make doc comments optional

This commit is contained in:
Jeremy Kolb 2019-01-26 10:35:23 -05:00
parent e9e0ea0398
commit a892067951
4 changed files with 27 additions and 19 deletions

View File

@ -27,10 +27,5 @@ pub trait Docs {
}
pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option<Documentation> {
let comments = node.doc_comment_text();
if comments.is_empty() {
None
} else {
Some(Documentation::new(&comments))
}
node.doc_comment_text().map(|it| Documentation::new(&it))
}

View File

@ -120,8 +120,7 @@ impl CallInfo {
};
let mut doc = None;
let docs = node.doc_comment_text();
if !docs.is_empty() {
if let Some(docs) = node.doc_comment_text() {
// Massage markdown
let mut processed_lines = Vec::new();
let mut in_code_block = false;

View File

@ -100,12 +100,7 @@ impl NavigationTarget {
fn docs(&self, db: &RootDatabase) -> Option<String> {
let node = self.node(db)?;
fn doc_comments<N: ast::DocCommentsOwner>(node: &N) -> Option<String> {
let comments = node.doc_comment_text();
if comments.is_empty() {
None
} else {
Some(comments)
}
node.doc_comment_text()
}
visitor()

View File

@ -117,8 +117,9 @@ pub trait DocCommentsOwner: AstNode {
/// Returns the textual content of a doc comment block as a single string.
/// That is, strips leading `///` (+ optional 1 character of whitespace)
/// and joins lines.
fn doc_comment_text(&self) -> std::string::String {
self.doc_comments()
fn doc_comment_text(&self) -> Option<std::string::String> {
let docs = self
.doc_comments()
.filter(|comment| comment.is_doc_comment())
.map(|comment| {
let prefix_len = comment.prefix().len();
@ -139,7 +140,13 @@ pub trait DocCommentsOwner: AstNode {
line[pos..].to_owned()
})
.join("\n")
.join("\n");
if docs.is_empty() {
None
} else {
Some(docs)
}
}
}
@ -699,6 +706,18 @@ impl BindPat {
}
}
#[test]
fn test_doc_comment_none() {
let file = SourceFile::parse(
r#"
// non-doc
mod foo {}
"#,
);
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
assert!(module.doc_comment_text().is_none());
}
#[test]
fn test_doc_comment_of_items() {
let file = SourceFile::parse(
@ -709,7 +728,7 @@ fn test_doc_comment_of_items() {
"#,
);
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
assert_eq!("doc", module.doc_comment_text());
assert_eq!("doc", module.doc_comment_text().unwrap());
}
#[test]
@ -728,6 +747,6 @@ fn test_doc_comment_preserves_indents() {
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
assert_eq!(
"doc1\n```\nfn foo() {\n // ...\n}\n```",
module.doc_comment_text()
module.doc_comment_text().unwrap()
);
}