aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/src/ast.rs29
1 files changed, 24 insertions, 5 deletions
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 {
117 /// Returns the textual content of a doc comment block as a single string. 117 /// Returns the textual content of a doc comment block as a single string.
118 /// That is, strips leading `///` (+ optional 1 character of whitespace) 118 /// That is, strips leading `///` (+ optional 1 character of whitespace)
119 /// and joins lines. 119 /// and joins lines.
120 fn doc_comment_text(&self) -> std::string::String { 120 fn doc_comment_text(&self) -> Option<std::string::String> {
121 self.doc_comments() 121 let docs = self
122 .doc_comments()
122 .filter(|comment| comment.is_doc_comment()) 123 .filter(|comment| comment.is_doc_comment())
123 .map(|comment| { 124 .map(|comment| {
124 let prefix_len = comment.prefix().len(); 125 let prefix_len = comment.prefix().len();
@@ -139,7 +140,13 @@ pub trait DocCommentsOwner: AstNode {
139 140
140 line[pos..].to_owned() 141 line[pos..].to_owned()
141 }) 142 })
142 .join("\n") 143 .join("\n");
144
145 if docs.is_empty() {
146 None
147 } else {
148 Some(docs)
149 }
143 } 150 }
144} 151}
145 152
@@ -700,6 +707,18 @@ impl BindPat {
700} 707}
701 708
702#[test] 709#[test]
710fn test_doc_comment_none() {
711 let file = SourceFile::parse(
712 r#"
713 // non-doc
714 mod foo {}
715 "#,
716 );
717 let module = file.syntax().descendants().find_map(Module::cast).unwrap();
718 assert!(module.doc_comment_text().is_none());
719}
720
721#[test]
703fn test_doc_comment_of_items() { 722fn test_doc_comment_of_items() {
704 let file = SourceFile::parse( 723 let file = SourceFile::parse(
705 r#" 724 r#"
@@ -709,7 +728,7 @@ fn test_doc_comment_of_items() {
709 "#, 728 "#,
710 ); 729 );
711 let module = file.syntax().descendants().find_map(Module::cast).unwrap(); 730 let module = file.syntax().descendants().find_map(Module::cast).unwrap();
712 assert_eq!("doc", module.doc_comment_text()); 731 assert_eq!("doc", module.doc_comment_text().unwrap());
713} 732}
714 733
715#[test] 734#[test]
@@ -728,6 +747,6 @@ fn test_doc_comment_preserves_indents() {
728 let module = file.syntax().descendants().find_map(Module::cast).unwrap(); 747 let module = file.syntax().descendants().find_map(Module::cast).unwrap();
729 assert_eq!( 748 assert_eq!(
730 "doc1\n```\nfn foo() {\n // ...\n}\n```", 749 "doc1\n```\nfn foo() {\n // ...\n}\n```",
731 module.doc_comment_text() 750 module.doc_comment_text().unwrap()
732 ); 751 );
733} 752}