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

@ -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()