From 4b3737510b97faa7d2fad3c98aa16eed46334703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20Ochagav=C3=ADa?= Date: Fri, 12 Oct 2018 19:20:58 +0200 Subject: Cleanup fold code and split logic to fold single elements --- crates/ra_syntax/src/ast/generated.rs | 18 ++++++++++++++++++ crates/ra_syntax/src/ast/mod.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) (limited to 'crates/ra_syntax/src/ast') diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index ef7b5b1a1..85aa5e0dd 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -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 { + match syntax.kind() { + WHITESPACE => Some(Whitespace { syntax }), + _ => None, + } + } + fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } +} + +impl<'a> Whitespace<'a> {} + diff --git a/crates/ra_syntax/src/ast/mod.rs b/crates/ra_syntax/src/ast/mod.rs index 10dac72e5..00c852274 100644 --- a/crates/ra_syntax/src/ast/mod.rs +++ b/crates/ra_syntax/src/ast/mod.rs @@ -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 { + 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 { + 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() -- cgit v1.2.3