Cleanup fold code and split logic to fold single elements
This commit is contained in:
@@ -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> {}
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -538,5 +538,6 @@ Grammar(
|
||||
options: [ "NameRef" ]
|
||||
),
|
||||
"Comment": (),
|
||||
"Whitespace": (),
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user