diff options
Diffstat (limited to 'crates/syntax')
-rw-r--r-- | crates/syntax/src/ast/token_ext.rs | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs index 6167d50e2..a10b14778 100644 --- a/crates/syntax/src/ast/token_ext.rs +++ b/crates/syntax/src/ast/token_ext.rs | |||
@@ -18,20 +18,21 @@ impl ast::Comment { | |||
18 | } | 18 | } |
19 | 19 | ||
20 | pub fn prefix(&self) -> &'static str { | 20 | pub fn prefix(&self) -> &'static str { |
21 | let &(prefix, _kind) = CommentKind::with_prefix_from_text(self.text()); | 21 | let &(prefix, _kind) = CommentKind::BY_PREFIX |
22 | .iter() | ||
23 | .find(|&(prefix, kind)| self.kind() == *kind && self.text().starts_with(prefix)) | ||
24 | .unwrap(); | ||
22 | prefix | 25 | prefix |
23 | } | 26 | } |
24 | 27 | ||
25 | pub fn kind_and_prefix(&self) -> &(&'static str, CommentKind) { | ||
26 | CommentKind::with_prefix_from_text(self.text()) | ||
27 | } | ||
28 | |||
29 | /// Returns the textual content of a doc comment block as a single string. | 28 | /// Returns the textual content of a doc comment block as a single string. |
30 | /// That is, strips leading `///` (+ optional 1 character of whitespace), | 29 | /// That is, strips leading `///` (+ optional 1 character of whitespace), |
31 | /// trailing `*/`, trailing whitespace and then joins the lines. | 30 | /// trailing `*/`, trailing whitespace and then joins the lines. |
32 | pub fn doc_comment(&self) -> Option<&str> { | 31 | pub fn doc_comment(&self) -> Option<&str> { |
33 | match self.kind_and_prefix() { | 32 | let kind = self.kind(); |
34 | (prefix, CommentKind { shape, doc: Some(_) }) => { | 33 | match kind { |
34 | CommentKind { shape, doc: Some(_) } => { | ||
35 | let prefix = kind.prefix(); | ||
35 | let text = &self.text().as_str()[prefix.len()..]; | 36 | let text = &self.text().as_str()[prefix.len()..]; |
36 | let ws = text.chars().next().filter(|c| c.is_whitespace()); | 37 | let ws = text.chars().next().filter(|c| c.is_whitespace()); |
37 | let text = ws.map_or(text, |ws| &text[ws.len_utf8()..]); | 38 | let text = ws.map_or(text, |ws| &text[ws.len_utf8()..]); |
@@ -88,12 +89,16 @@ impl CommentKind { | |||
88 | ]; | 89 | ]; |
89 | 90 | ||
90 | pub(crate) fn from_text(text: &str) -> CommentKind { | 91 | pub(crate) fn from_text(text: &str) -> CommentKind { |
91 | let &(_prefix, kind) = Self::with_prefix_from_text(text); | 92 | let &(_prefix, kind) = CommentKind::BY_PREFIX |
93 | .iter() | ||
94 | .find(|&(prefix, _kind)| text.starts_with(prefix)) | ||
95 | .unwrap(); | ||
92 | kind | 96 | kind |
93 | } | 97 | } |
94 | 98 | ||
95 | fn with_prefix_from_text(text: &str) -> &(&'static str, CommentKind) { | 99 | fn prefix(&self) -> &'static str { |
96 | CommentKind::BY_PREFIX.iter().find(|&(prefix, _kind)| text.starts_with(prefix)).unwrap() | 100 | let &(prefix, _) = CommentKind::BY_PREFIX.iter().find(|(_, kind)| kind == self).unwrap(); |
101 | prefix | ||
97 | } | 102 | } |
98 | } | 103 | } |
99 | 104 | ||