Cleanup fold code and split logic to fold single elements

This commit is contained in:
Adolfo Ochagavía
2018-10-12 19:20:58 +02:00
parent ee0a6bf053
commit 4b3737510b
4 changed files with 115 additions and 43 deletions

View File

@@ -2193,3 +2193,21 @@ impl<'a> WhileExpr<'a> {
}
}
// Whitespace
#[derive(Debug, Clone, Copy)]
pub struct Whitespace<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for Whitespace<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
WHITESPACE => Some(Whitespace { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> Whitespace<'a> {}

View File

@@ -100,8 +100,8 @@ impl<'a> Lifetime<'a> {
}
impl<'a> Comment<'a> {
pub fn text(&self) -> SmolStr {
self.syntax().leaf_text().unwrap().clone()
pub fn text(&self) -> &SmolStr {
self.syntax().leaf_text().unwrap()
}
pub fn flavor(&self) -> CommentFlavor {
@@ -120,6 +120,14 @@ impl<'a> Comment<'a> {
pub fn prefix(&self) -> &'static str {
self.flavor().prefix()
}
pub fn count_newlines_lazy(&self) -> impl Iterator<Item = &()> {
self.text().chars().filter(|&c| c == '\n').map(|_| &())
}
pub fn has_newlines(&self) -> bool {
self.count_newlines_lazy().count() > 0
}
}
#[derive(Debug)]
@@ -142,6 +150,20 @@ impl CommentFlavor {
}
}
impl<'a> Whitespace<'a> {
pub fn text(&self) -> &SmolStr {
&self.syntax().leaf_text().unwrap()
}
pub fn count_newlines_lazy(&self) -> impl Iterator<Item = &()> {
self.text().chars().filter(|&c| c == '\n').map(|_| &())
}
pub fn has_newlines(&self) -> bool {
self.count_newlines_lazy().count() > 0
}
}
impl<'a> Name<'a> {
pub fn text(&self) -> SmolStr {
let ident = self.syntax().first_child()

View File

@@ -538,5 +538,6 @@ Grammar(
options: [ "NameRef" ]
),
"Comment": (),
"Whitespace": (),
},
)