From d2bcd1a38636a669d2232156fa29010a82e37a3e Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Tue, 30 Oct 2018 16:57:33 -0400 Subject: `ast::DocCommentsOwner` which represents a documentation comment owner --- crates/ra_syntax/src/ast/generated.rs | 1 + crates/ra_syntax/src/ast/mod.rs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) (limited to 'crates/ra_syntax/src/ast') diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index d0cd060d3..b6a15dbdc 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -864,6 +864,7 @@ impl<'a> AstNode<'a> for FnDef<'a> { impl<'a> ast::NameOwner<'a> for FnDef<'a> {} impl<'a> ast::TypeParamsOwner<'a> for FnDef<'a> {} impl<'a> ast::AttrsOwner<'a> for FnDef<'a> {} +impl<'a> ast::DocCommentsOwner<'a> for FnDef<'a> {} impl<'a> FnDef<'a> { pub fn param_list(self) -> Option> { super::child_opt(self) diff --git a/crates/ra_syntax/src/ast/mod.rs b/crates/ra_syntax/src/ast/mod.rs index c033263a1..3aa11b9dd 100644 --- a/crates/ra_syntax/src/ast/mod.rs +++ b/crates/ra_syntax/src/ast/mod.rs @@ -65,6 +65,24 @@ pub trait AttrsOwner<'a>: AstNode<'a> { } } +pub trait DocCommentsOwner<'a>: AstNode<'a> { + fn doc_comments(self) -> AstChildren<'a, Comment<'a>> { children(self) } + + /// Returns the textual content of a doc comment block as a single string. + /// That is, strips leading `///` and joins lines + fn doc_comment_text(self) -> String { + self.doc_comments() + .map(|comment| { + let prefix = comment.prefix(); + let trimmed = comment.text().as_str() + .trim() + .trim_start_matches(prefix) + .trim_start(); + trimmed.to_owned() + }).join("\n") + } +} + impl<'a> FnDef<'a> { pub fn has_atom_attr(&self, atom: &str) -> bool { self.attrs().filter_map(|x| x.as_atom()).any(|x| x == atom) -- cgit v1.2.3