aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
authorAdolfo OchagavĂ­a <[email protected]>2018-10-12 18:20:58 +0100
committerAdolfo OchagavĂ­a <[email protected]>2018-10-12 18:20:58 +0100
commit4b3737510b97faa7d2fad3c98aa16eed46334703 (patch)
tree4d052d23ac53bf7444bb2f6235dc7ad4358152b9 /crates/ra_syntax
parentee0a6bf0535a5a6c7e536d2cffa11959c3ee2ae3 (diff)
Cleanup fold code and split logic to fold single elements
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/src/ast/generated.rs18
-rw-r--r--crates/ra_syntax/src/ast/mod.rs26
-rw-r--r--crates/ra_syntax/src/grammar.ron1
3 files changed, 43 insertions, 2 deletions
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> {
2193 } 2193 }
2194} 2194}
2195 2195
2196// Whitespace
2197#[derive(Debug, Clone, Copy)]
2198pub struct Whitespace<'a> {
2199 syntax: SyntaxNodeRef<'a>,
2200}
2201
2202impl<'a> AstNode<'a> for Whitespace<'a> {
2203 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
2204 match syntax.kind() {
2205 WHITESPACE => Some(Whitespace { syntax }),
2206 _ => None,
2207 }
2208 }
2209 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
2210}
2211
2212impl<'a> Whitespace<'a> {}
2213
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> {
100} 100}
101 101
102impl<'a> Comment<'a> { 102impl<'a> Comment<'a> {
103 pub fn text(&self) -> SmolStr { 103 pub fn text(&self) -> &SmolStr {
104 self.syntax().leaf_text().unwrap().clone() 104 self.syntax().leaf_text().unwrap()
105 } 105 }
106 106
107 pub fn flavor(&self) -> CommentFlavor { 107 pub fn flavor(&self) -> CommentFlavor {
@@ -120,6 +120,14 @@ impl<'a> Comment<'a> {
120 pub fn prefix(&self) -> &'static str { 120 pub fn prefix(&self) -> &'static str {
121 self.flavor().prefix() 121 self.flavor().prefix()
122 } 122 }
123
124 pub fn count_newlines_lazy(&self) -> impl Iterator<Item = &()> {
125 self.text().chars().filter(|&c| c == '\n').map(|_| &())
126 }
127
128 pub fn has_newlines(&self) -> bool {
129 self.count_newlines_lazy().count() > 0
130 }
123} 131}
124 132
125#[derive(Debug)] 133#[derive(Debug)]
@@ -142,6 +150,20 @@ impl CommentFlavor {
142 } 150 }
143} 151}
144 152
153impl<'a> Whitespace<'a> {
154 pub fn text(&self) -> &SmolStr {
155 &self.syntax().leaf_text().unwrap()
156 }
157
158 pub fn count_newlines_lazy(&self) -> impl Iterator<Item = &()> {
159 self.text().chars().filter(|&c| c == '\n').map(|_| &())
160 }
161
162 pub fn has_newlines(&self) -> bool {
163 self.count_newlines_lazy().count() > 0
164 }
165}
166
145impl<'a> Name<'a> { 167impl<'a> Name<'a> {
146 pub fn text(&self) -> SmolStr { 168 pub fn text(&self) -> SmolStr {
147 let ident = self.syntax().first_child() 169 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(
538 options: [ "NameRef" ] 538 options: [ "NameRef" ]
539 ), 539 ),
540 "Comment": (), 540 "Comment": (),
541 "Whitespace": (),
541 }, 542 },
542) 543)