From f604ff5b2f72a593e23953ed8be7e9cbeba5d287 Mon Sep 17 00:00:00 2001 From: csmoe Date: Fri, 4 Jan 2019 21:29:00 +0800 Subject: parse doc comment for items --- crates/ra_syntax/src/ast.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index e968c9728..c5273a7a2 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -115,6 +115,7 @@ pub trait DocCommentsOwner<'a>: AstNode<'a> { /// That is, strips leading `///` and joins lines fn doc_comment_text(self) -> RustString { self.doc_comments() + .filter(|comment| comment.is_doc_comment()) .map(|comment| { let prefix = comment.prefix(); let trimmed = comment @@ -206,6 +207,10 @@ impl<'a> Comment<'a> { } } + pub fn is_doc_comment(&self) -> bool { + self.flavor().is_doc_comment() + } + pub fn prefix(&self) -> &'static str { self.flavor().prefix() } @@ -237,6 +242,13 @@ impl CommentFlavor { Multiline => "/*", } } + + pub fn is_doc_comment(&self) -> bool { + match self { + CommentFlavor::Doc | CommentFlavor::ModuleDoc => true, + _ => false, + } + } } impl<'a> Whitespace<'a> { -- cgit v1.2.3 From a0ef196d0404cf89367928f347dd515ba31d081b Mon Sep 17 00:00:00 2001 From: csmoe Date: Fri, 4 Jan 2019 21:51:45 +0800 Subject: add mod doc comments test in ast --- crates/ra_syntax/src/ast.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index c5273a7a2..3aaa5edda 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -481,3 +481,16 @@ impl<'a> PrefixExpr<'a> { } } } + +#[test] +fn test_doc_comment_of_items() { + let file = SourceFileNode::parse( + r#" + //! doc + // non-doc + mod foo {} + "#, + ); + let module = file.syntax().descendants().find_map(Module::cast).unwrap(); + assert_eq!("doc", module.doc_comment_text()); +} -- cgit v1.2.3