aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-07-19 17:05:34 +0100
committerAleksey Kladov <[email protected]>2019-07-19 17:05:34 +0100
commit191a6ba330bd47fc3b9cc05d59b2d456b471eb89 (patch)
tree9c7996d6ac7323da527fb53171b9275545d1640a /crates/ra_syntax/src
parenta6df224f7d3893f5a742b58818eac6c5a953721d (diff)
convenience api
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r--crates/ra_syntax/src/ast/expr_extensions.rs72
-rw-r--r--crates/ra_syntax/src/ast/extensions.rs8
-rw-r--r--crates/ra_syntax/src/ast/traits.rs2
-rw-r--r--crates/ra_syntax/src/syntax_node.rs14
4 files changed, 55 insertions, 41 deletions
diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs
index ca1773908..139bd3ec0 100644
--- a/crates/ra_syntax/src/ast/expr_extensions.rs
+++ b/crates/ra_syntax/src/ast/expr_extensions.rs
@@ -60,7 +60,7 @@ impl ast::PrefixExpr {
60 } 60 }
61 61
62 pub fn op_token(&self) -> Option<SyntaxToken> { 62 pub fn op_token(&self) -> Option<SyntaxToken> {
63 self.syntax().first_child_or_token()?.as_token().cloned() 63 self.syntax().first_child_or_token()?.into_token()
64 } 64 }
65} 65}
66 66
@@ -132,41 +132,41 @@ pub enum BinOp {
132 132
133impl ast::BinExpr { 133impl ast::BinExpr {
134 fn op_details(&self) -> Option<(SyntaxToken, BinOp)> { 134 fn op_details(&self) -> Option<(SyntaxToken, BinOp)> {
135 self.syntax().children_with_tokens().filter_map(|it| it.as_token().cloned()).find_map(|c| { 135 self.syntax().children_with_tokens().filter_map(|it| it.into_token()).find_map(|c| match c
136 match c.kind() { 136 .kind()
137 T![||] => Some((c, BinOp::BooleanOr)), 137 {
138 T![&&] => Some((c, BinOp::BooleanAnd)), 138 T![||] => Some((c, BinOp::BooleanOr)),
139 T![==] => Some((c, BinOp::EqualityTest)), 139 T![&&] => Some((c, BinOp::BooleanAnd)),
140 T![!=] => Some((c, BinOp::NegatedEqualityTest)), 140 T![==] => Some((c, BinOp::EqualityTest)),
141 T![<=] => Some((c, BinOp::LesserEqualTest)), 141 T![!=] => Some((c, BinOp::NegatedEqualityTest)),
142 T![>=] => Some((c, BinOp::GreaterEqualTest)), 142 T![<=] => Some((c, BinOp::LesserEqualTest)),
143 T![<] => Some((c, BinOp::LesserTest)), 143 T![>=] => Some((c, BinOp::GreaterEqualTest)),
144 T![>] => Some((c, BinOp::GreaterTest)), 144 T![<] => Some((c, BinOp::LesserTest)),
145 T![+] => Some((c, BinOp::Addition)), 145 T![>] => Some((c, BinOp::GreaterTest)),
146 T![*] => Some((c, BinOp::Multiplication)), 146 T![+] => Some((c, BinOp::Addition)),
147 T![-] => Some((c, BinOp::Subtraction)), 147 T![*] => Some((c, BinOp::Multiplication)),
148 T![/] => Some((c, BinOp::Division)), 148 T![-] => Some((c, BinOp::Subtraction)),
149 T![%] => Some((c, BinOp::Remainder)), 149 T![/] => Some((c, BinOp::Division)),
150 T![<<] => Some((c, BinOp::LeftShift)), 150 T![%] => Some((c, BinOp::Remainder)),
151 T![>>] => Some((c, BinOp::RightShift)), 151 T![<<] => Some((c, BinOp::LeftShift)),
152 T![^] => Some((c, BinOp::BitwiseXor)), 152 T![>>] => Some((c, BinOp::RightShift)),
153 T![|] => Some((c, BinOp::BitwiseOr)), 153 T![^] => Some((c, BinOp::BitwiseXor)),
154 T![&] => Some((c, BinOp::BitwiseAnd)), 154 T![|] => Some((c, BinOp::BitwiseOr)),
155 T![..] => Some((c, BinOp::RangeRightOpen)), 155 T![&] => Some((c, BinOp::BitwiseAnd)),
156 T![..=] => Some((c, BinOp::RangeRightClosed)), 156 T![..] => Some((c, BinOp::RangeRightOpen)),
157 T![=] => Some((c, BinOp::Assignment)), 157 T![..=] => Some((c, BinOp::RangeRightClosed)),
158 T![+=] => Some((c, BinOp::AddAssign)), 158 T![=] => Some((c, BinOp::Assignment)),
159 T![/=] => Some((c, BinOp::DivAssign)), 159 T![+=] => Some((c, BinOp::AddAssign)),
160 T![*=] => Some((c, BinOp::MulAssign)), 160 T![/=] => Some((c, BinOp::DivAssign)),
161 T![%=] => Some((c, BinOp::RemAssign)), 161 T![*=] => Some((c, BinOp::MulAssign)),
162 T![>>=] => Some((c, BinOp::ShrAssign)), 162 T![%=] => Some((c, BinOp::RemAssign)),
163 T![<<=] => Some((c, BinOp::ShlAssign)), 163 T![>>=] => Some((c, BinOp::ShrAssign)),
164 T![-=] => Some((c, BinOp::SubAssign)), 164 T![<<=] => Some((c, BinOp::ShlAssign)),
165 T![|=] => Some((c, BinOp::BitOrAssign)), 165 T![-=] => Some((c, BinOp::SubAssign)),
166 T![&=] => Some((c, BinOp::BitAndAssign)), 166 T![|=] => Some((c, BinOp::BitOrAssign)),
167 T![^=] => Some((c, BinOp::BitXorAssign)), 167 T![&=] => Some((c, BinOp::BitAndAssign)),
168 _ => None, 168 T![^=] => Some((c, BinOp::BitXorAssign)),
169 } 169 _ => None,
170 }) 170 })
171 } 171 }
172 172
diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs
index 5420f67ff..753fc42c6 100644
--- a/crates/ra_syntax/src/ast/extensions.rs
+++ b/crates/ra_syntax/src/ast/extensions.rs
@@ -239,7 +239,7 @@ impl ast::FnDef {
239 pub fn semicolon_token(&self) -> Option<SyntaxToken> { 239 pub fn semicolon_token(&self) -> Option<SyntaxToken> {
240 self.syntax() 240 self.syntax()
241 .last_child_or_token() 241 .last_child_or_token()
242 .and_then(|it| it.as_token().cloned()) 242 .and_then(|it| it.into_token())
243 .filter(|it| it.kind() == T![;]) 243 .filter(|it| it.kind() == T![;])
244 } 244 }
245} 245}
@@ -332,7 +332,7 @@ impl ast::SelfParam {
332 pub fn self_kw_token(&self) -> SyntaxToken { 332 pub fn self_kw_token(&self) -> SyntaxToken {
333 self.syntax() 333 self.syntax()
334 .children_with_tokens() 334 .children_with_tokens()
335 .filter_map(|it| it.as_token().cloned()) 335 .filter_map(|it| it.into_token())
336 .find(|it| it.kind() == T![self]) 336 .find(|it| it.kind() == T![self])
337 .expect("invalid tree: self param must have self") 337 .expect("invalid tree: self param must have self")
338 } 338 }
@@ -361,7 +361,7 @@ impl ast::LifetimeParam {
361 pub fn lifetime_token(&self) -> Option<SyntaxToken> { 361 pub fn lifetime_token(&self) -> Option<SyntaxToken> {
362 self.syntax() 362 self.syntax()
363 .children_with_tokens() 363 .children_with_tokens()
364 .filter_map(|it| it.as_token().cloned()) 364 .filter_map(|it| it.into_token())
365 .find(|it| it.kind() == LIFETIME) 365 .find(|it| it.kind() == LIFETIME)
366 } 366 }
367} 367}
@@ -370,7 +370,7 @@ impl ast::WherePred {
370 pub fn lifetime_token(&self) -> Option<SyntaxToken> { 370 pub fn lifetime_token(&self) -> Option<SyntaxToken> {
371 self.syntax() 371 self.syntax()
372 .children_with_tokens() 372 .children_with_tokens()
373 .filter_map(|it| it.as_token().cloned()) 373 .filter_map(|it| it.into_token())
374 .find(|it| it.kind() == LIFETIME) 374 .find(|it| it.kind() == LIFETIME)
375 } 375 }
376} 376}
diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs
index ecbd2d427..6ed1b5213 100644
--- a/crates/ra_syntax/src/ast/traits.rs
+++ b/crates/ra_syntax/src/ast/traits.rs
@@ -155,7 +155,7 @@ pub struct CommentIter {
155impl Iterator for CommentIter { 155impl Iterator for CommentIter {
156 type Item = ast::Comment; 156 type Item = ast::Comment;
157 fn next(&mut self) -> Option<ast::Comment> { 157 fn next(&mut self) -> Option<ast::Comment> {
158 self.iter.by_ref().find_map(|el| el.as_token().cloned().and_then(ast::Comment::cast)) 158 self.iter.by_ref().find_map(|el| el.into_token().and_then(ast::Comment::cast))
159 } 159 }
160} 160}
161 161
diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs
index cf680e66a..98955832b 100644
--- a/crates/ra_syntax/src/syntax_node.rs
+++ b/crates/ra_syntax/src/syntax_node.rs
@@ -423,6 +423,13 @@ impl SyntaxElement {
423 } 423 }
424 } 424 }
425 425
426 pub fn into_node(self) -> Option<SyntaxNode> {
427 match self {
428 SyntaxElement::Node(node) => Some(node),
429 SyntaxElement::Token(_) => None,
430 }
431 }
432
426 pub fn as_token(&self) -> Option<&SyntaxToken> { 433 pub fn as_token(&self) -> Option<&SyntaxToken> {
427 match self { 434 match self {
428 SyntaxElement::Node(_) => None, 435 SyntaxElement::Node(_) => None,
@@ -430,6 +437,13 @@ impl SyntaxElement {
430 } 437 }
431 } 438 }
432 439
440 pub fn into_token(self) -> Option<SyntaxToken> {
441 match self {
442 SyntaxElement::Node(_) => None,
443 SyntaxElement::Token(token) => Some(token),
444 }
445 }
446
433 pub fn next_sibling_or_token(&self) -> Option<SyntaxElement> { 447 pub fn next_sibling_or_token(&self) -> Option<SyntaxElement> {
434 match self { 448 match self {
435 SyntaxElement::Node(it) => it.next_sibling_or_token(), 449 SyntaxElement::Node(it) => it.next_sibling_or_token(),