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 ++++++++++++++++++++++++-- crates/ra_syntax/src/grammar.ron | 1 + 3 files changed, 43 insertions(+), 2 deletions(-) (limited to 'crates/ra_syntax') 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() diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 9da0c2c13..d538739de 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -538,5 +538,6 @@ Grammar( options: [ "NameRef" ] ), "Comment": (), + "Whitespace": (), }, ) -- cgit v1.2.3 From c5069eeef5ba0260f2daed513a28b43ae45445bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20Ochagav=C3=ADa?= Date: Fri, 12 Oct 2018 19:49:08 +0200 Subject: Only fold groups of similar comments --- crates/ra_syntax/src/ast/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/mod.rs b/crates/ra_syntax/src/ast/mod.rs index 00c852274..12ddc0210 100644 --- a/crates/ra_syntax/src/ast/mod.rs +++ b/crates/ra_syntax/src/ast/mod.rs @@ -130,7 +130,7 @@ impl<'a> Comment<'a> { } } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub enum CommentFlavor { Line, Doc, -- cgit v1.2.3