aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src/ast/token_ext.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/syntax/src/ast/token_ext.rs')
-rw-r--r--crates/syntax/src/ast/token_ext.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs
index fa40e64e8..a10b14778 100644
--- a/crates/syntax/src/ast/token_ext.rs
+++ b/crates/syntax/src/ast/token_ext.rs
@@ -24,6 +24,28 @@ impl ast::Comment {
24 .unwrap(); 24 .unwrap();
25 prefix 25 prefix
26 } 26 }
27
28 /// Returns the textual content of a doc comment block as a single string.
29 /// That is, strips leading `///` (+ optional 1 character of whitespace),
30 /// trailing `*/`, trailing whitespace and then joins the lines.
31 pub fn doc_comment(&self) -> Option<&str> {
32 let kind = self.kind();
33 match kind {
34 CommentKind { shape, doc: Some(_) } => {
35 let prefix = kind.prefix();
36 let text = &self.text().as_str()[prefix.len()..];
37 let ws = text.chars().next().filter(|c| c.is_whitespace());
38 let text = ws.map_or(text, |ws| &text[ws.len_utf8()..]);
39 match shape {
40 CommentShape::Block if text.ends_with("*/") => {
41 Some(&text[..text.len() - "*/".len()])
42 }
43 _ => Some(text),
44 }
45 }
46 _ => None,
47 }
48 }
27} 49}
28 50
29#[derive(Debug, PartialEq, Eq, Clone, Copy)] 51#[derive(Debug, PartialEq, Eq, Clone, Copy)]
@@ -73,6 +95,11 @@ impl CommentKind {
73 .unwrap(); 95 .unwrap();
74 kind 96 kind
75 } 97 }
98
99 fn prefix(&self) -> &'static str {
100 let &(prefix, _) = CommentKind::BY_PREFIX.iter().find(|(_, kind)| kind == self).unwrap();
101 prefix
102 }
76} 103}
77 104
78impl ast::Whitespace { 105impl ast::Whitespace {