Skip to main content

squawk_syntax/ast/
token_ext.rs

1use squawk_line_index::find_newline;
2
3use crate::ast::{self, AstToken};
4
5impl ast::Whitespace {
6    pub fn spans_multiple_lines(&self) -> bool {
7        let text = self.text();
8        find_newline(text).is_some_and(|(idx, line_ending)| {
9            find_newline(&text[idx + line_ending.as_str().len()..]).is_some()
10        })
11    }
12}
13
14impl ast::Comment {
15    pub fn kind(&self) -> CommentKind {
16        CommentKind::from_text(self.text())
17    }
18}
19
20#[derive(Debug, PartialEq, Eq, Clone, Copy)]
21pub enum CommentKind {
22    Line,
23    Block,
24}
25
26impl CommentKind {
27    const BY_PREFIX: [(&'static str, CommentKind); 2] =
28        [("/*", CommentKind::Block), ("--", CommentKind::Line)];
29    pub(crate) fn from_text(text: &str) -> CommentKind {
30        let &(_prefix, kind) = CommentKind::BY_PREFIX
31            .iter()
32            .find(|&(prefix, _kind)| text.starts_with(prefix))
33            .unwrap();
34        kind
35    }
36
37    pub fn is_line(self) -> bool {
38        self == CommentKind::Line
39    }
40
41    pub fn is_block(self) -> bool {
42        self == CommentKind::Block
43    }
44}