diff options
author | Lukas Wirth <[email protected]> | 2021-03-17 13:38:11 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-03-17 13:48:57 +0000 |
commit | ec824a92d05caa1908cb25cbd5b969c8e995aaa7 (patch) | |
tree | 4d64f1a745735200fbdbc3df564f1471d1b44be1 /crates/syntax/src/ast | |
parent | 0fbfab3b45af7a02b1224bca4a53e9b8f6ec049e (diff) |
Better handling of block doc comments
Diffstat (limited to 'crates/syntax/src/ast')
-rw-r--r-- | crates/syntax/src/ast/token_ext.rs | 19 | ||||
-rw-r--r-- | crates/syntax/src/ast/traits.rs | 16 |
2 files changed, 13 insertions, 22 deletions
diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs index 977eb8181..6c242d126 100644 --- a/crates/syntax/src/ast/token_ext.rs +++ b/crates/syntax/src/ast/token_ext.rs | |||
@@ -33,23 +33,20 @@ impl ast::Comment { | |||
33 | prefix | 33 | prefix |
34 | } | 34 | } |
35 | 35 | ||
36 | /// Returns the textual content of a doc comment block as a single string. | 36 | /// Returns the textual content of a doc comment node as a single string with prefix and suffix |
37 | /// That is, strips leading `///` (+ optional 1 character of whitespace), | 37 | /// removed. |
38 | /// trailing `*/`, trailing whitespace and then joins the lines. | ||
39 | pub fn doc_comment(&self) -> Option<&str> { | 38 | pub fn doc_comment(&self) -> Option<&str> { |
40 | let kind = self.kind(); | 39 | let kind = self.kind(); |
41 | match kind { | 40 | match kind { |
42 | CommentKind { shape, doc: Some(_) } => { | 41 | CommentKind { shape, doc: Some(_) } => { |
43 | let prefix = kind.prefix(); | 42 | let prefix = kind.prefix(); |
44 | let text = &self.text()[prefix.len()..]; | 43 | let text = &self.text()[prefix.len()..]; |
45 | let ws = text.chars().next().filter(|c| c.is_whitespace()); | 44 | let text = if shape == CommentShape::Block { |
46 | let text = ws.map_or(text, |ws| &text[ws.len_utf8()..]); | 45 | text.strip_suffix("*/").unwrap_or(text) |
47 | match shape { | 46 | } else { |
48 | CommentShape::Block if text.ends_with("*/") => { | 47 | text |
49 | Some(&text[..text.len() - "*/".len()]) | 48 | }; |
50 | } | 49 | Some(text) |
51 | _ => Some(text), | ||
52 | } | ||
53 | } | 50 | } |
54 | _ => None, | 51 | _ => None, |
55 | } | 52 | } |
diff --git a/crates/syntax/src/ast/traits.rs b/crates/syntax/src/ast/traits.rs index 96d4cc997..ddd213637 100644 --- a/crates/syntax/src/ast/traits.rs +++ b/crates/syntax/src/ast/traits.rs | |||
@@ -1,8 +1,6 @@ | |||
1 | //! Various traits that are implemented by ast nodes. | 1 | //! Various traits that are implemented by ast nodes. |
2 | //! | 2 | //! |
3 | //! The implementations are usually trivial, and live in generated.rs | 3 | //! The implementations are usually trivial, and live in generated.rs |
4 | use itertools::Itertools; | ||
5 | |||
6 | use crate::{ | 4 | use crate::{ |
7 | ast::{self, support, AstChildren, AstNode, AstToken}, | 5 | ast::{self, support, AstChildren, AstNode, AstToken}, |
8 | syntax_node::SyntaxElementChildren, | 6 | syntax_node::SyntaxElementChildren, |
@@ -76,10 +74,6 @@ pub trait DocCommentsOwner: AttrsOwner { | |||
76 | fn doc_comments(&self) -> CommentIter { | 74 | fn doc_comments(&self) -> CommentIter { |
77 | CommentIter { iter: self.syntax().children_with_tokens() } | 75 | CommentIter { iter: self.syntax().children_with_tokens() } |
78 | } | 76 | } |
79 | |||
80 | fn doc_comment_text(&self) -> Option<String> { | ||
81 | self.doc_comments().doc_comment_text() | ||
82 | } | ||
83 | } | 77 | } |
84 | 78 | ||
85 | impl CommentIter { | 79 | impl CommentIter { |
@@ -87,12 +81,12 @@ impl CommentIter { | |||
87 | CommentIter { iter: syntax_node.children_with_tokens() } | 81 | CommentIter { iter: syntax_node.children_with_tokens() } |
88 | } | 82 | } |
89 | 83 | ||
90 | /// Returns the textual content of a doc comment block as a single string. | 84 | #[cfg(test)] |
91 | /// That is, strips leading `///` (+ optional 1 character of whitespace), | ||
92 | /// trailing `*/`, trailing whitespace and then joins the lines. | ||
93 | pub fn doc_comment_text(self) -> Option<String> { | 85 | pub fn doc_comment_text(self) -> Option<String> { |
94 | let docs = | 86 | let docs = itertools::Itertools::join( |
95 | self.filter_map(|comment| comment.doc_comment().map(ToOwned::to_owned)).join("\n"); | 87 | &mut self.filter_map(|comment| comment.doc_comment().map(ToOwned::to_owned)), |
88 | "\n", | ||
89 | ); | ||
96 | if docs.is_empty() { | 90 | if docs.is_empty() { |
97 | None | 91 | None |
98 | } else { | 92 | } else { |