Skip to content

Commit 3174e94

Browse files
committed
Simplify ast::Comment api surface
1 parent 2facd95 commit 3174e94

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

crates/syntax/src/ast/token_ext.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,21 @@ impl ast::Comment {
1818
}
1919

2020
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();
2225
prefix
2326
}
2427

25-
pub fn kind_and_prefix(&self) -> &(&'static str, CommentKind) {
26-
CommentKind::with_prefix_from_text(self.text())
27-
}
28-
2928
/// Returns the textual content of a doc comment block as a single string.
3029
/// That is, strips leading `///` (+ optional 1 character of whitespace),
3130
/// trailing `*/`, trailing whitespace and then joins the lines.
3231
pub fn doc_comment(&self) -> Option<&str> {
33-
match self.kind_and_prefix() {
34-
(prefix, CommentKind { shape, doc: Some(_) }) => {
32+
let kind = self.kind();
33+
match kind {
34+
CommentKind { shape, doc: Some(_) } => {
35+
let prefix = kind.prefix();
3536
let text = &self.text().as_str()[prefix.len()..];
3637
let ws = text.chars().next().filter(|c| c.is_whitespace());
3738
let text = ws.map_or(text, |ws| &text[ws.len_utf8()..]);
@@ -88,12 +89,16 @@ impl CommentKind {
8889
];
8990

9091
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();
9296
kind
9397
}
9498

95-
fn with_prefix_from_text(text: &str) -> &(&'static str, CommentKind) {
96-
CommentKind::BY_PREFIX.iter().find(|&(prefix, _kind)| text.starts_with(prefix)).unwrap()
99+
fn prefix(&self) -> &'static str {
100+
let &(prefix, _) = CommentKind::BY_PREFIX.iter().find(|(_, kind)| kind == self).unwrap();
101+
prefix
97102
}
98103
}
99104

0 commit comments

Comments
 (0)