From a89206795140209b816eb8e91fa3f1e1d6509269 Mon Sep 17 00:00:00 2001 From: Jeremy Kolb Date: Sat, 26 Jan 2019 10:35:23 -0500 Subject: Make doc comments optional --- crates/ra_syntax/src/ast.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index cdfb9c523..d59890d95 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -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 { + 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() ); } -- cgit v1.2.3