aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-07-19 17:08:17 +0100
committerbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-07-19 17:08:17 +0100
commitf697d27c31ba4486b9711893a93505a05319a2dc (patch)
tree9c7996d6ac7323da527fb53171b9275545d1640a /crates
parent7bc9dec7cf195bc73a88d7c740f9d0bed1019e98 (diff)
parent191a6ba330bd47fc3b9cc05d59b2d456b471eb89 (diff)
Merge #1554
1554: convenience api r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/ast_editor.rs8
-rw-r--r--crates/ra_ide_api/src/extend_selection.rs4
-rw-r--r--crates/ra_ide_api/src/join_lines.rs2
-rw-r--r--crates/ra_ide_api/src/syntax_highlighting.rs3
-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
8 files changed, 63 insertions, 50 deletions
diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs
index 5fbcadfee..a35334d7e 100644
--- a/crates/ra_assists/src/ast_editor.rs
+++ b/crates/ra_assists/src/ast_editor.rs
@@ -299,7 +299,7 @@ mod tokens {
299 .tree() 299 .tree()
300 .syntax() 300 .syntax()
301 .descendants_with_tokens() 301 .descendants_with_tokens()
302 .filter_map(|it| it.as_token().cloned()) 302 .filter_map(|it| it.into_token())
303 .find(|it| it.kind() == T![,]) 303 .find(|it| it.kind() == T![,])
304 .unwrap() 304 .unwrap()
305 } 305 }
@@ -309,7 +309,7 @@ mod tokens {
309 .tree() 309 .tree()
310 .syntax() 310 .syntax()
311 .descendants_with_tokens() 311 .descendants_with_tokens()
312 .filter_map(|it| it.as_token().cloned()) 312 .filter_map(|it| it.into_token())
313 .find(|it| it.kind() == WHITESPACE && it.text().as_str() == " ") 313 .find(|it| it.kind() == WHITESPACE && it.text().as_str() == " ")
314 .unwrap() 314 .unwrap()
315 } 315 }
@@ -320,7 +320,7 @@ mod tokens {
320 .tree() 320 .tree()
321 .syntax() 321 .syntax()
322 .descendants_with_tokens() 322 .descendants_with_tokens()
323 .filter_map(|it| it.as_token().cloned()) 323 .filter_map(|it| it.into_token())
324 .find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n") 324 .find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n")
325 .unwrap() 325 .unwrap()
326 } 326 }
@@ -332,7 +332,7 @@ mod tokens {
332 WsBuilder(SourceFile::parse(text).ok().unwrap()) 332 WsBuilder(SourceFile::parse(text).ok().unwrap())
333 } 333 }
334 pub(crate) fn ws(&self) -> SyntaxToken { 334 pub(crate) fn ws(&self) -> SyntaxToken {
335 self.0.syntax().first_child_or_token().unwrap().as_token().cloned().unwrap() 335 self.0.syntax().first_child_or_token().unwrap().into_token().unwrap()
336 } 336 }
337 } 337 }
338 338
diff --git a/crates/ra_ide_api/src/extend_selection.rs b/crates/ra_ide_api/src/extend_selection.rs
index 8c49960f5..292f61f4a 100644
--- a/crates/ra_ide_api/src/extend_selection.rs
+++ b/crates/ra_ide_api/src/extend_selection.rs
@@ -156,7 +156,7 @@ fn extend_list_item(node: &SyntaxNode) -> Option<TextRange> {
156 SyntaxElement::Token(it) => is_single_line_ws(it), 156 SyntaxElement::Token(it) => is_single_line_ws(it),
157 }) 157 })
158 .next() 158 .next()
159 .and_then(|it| it.as_token().cloned()) 159 .and_then(|it| it.into_token())
160 .filter(|node| node.kind() == T![,]) 160 .filter(|node| node.kind() == T![,])
161 } 161 }
162 162
@@ -167,7 +167,7 @@ fn extend_list_item(node: &SyntaxNode) -> Option<TextRange> {
167 // Include any following whitespace when comma if after list item. 167 // Include any following whitespace when comma if after list item.
168 let final_node = comma_node 168 let final_node = comma_node
169 .next_sibling_or_token() 169 .next_sibling_or_token()
170 .and_then(|it| it.as_token().cloned()) 170 .and_then(|it| it.into_token())
171 .filter(|node| is_single_line_ws(node)) 171 .filter(|node| is_single_line_ws(node))
172 .unwrap_or(comma_node); 172 .unwrap_or(comma_node);
173 173
diff --git a/crates/ra_ide_api/src/join_lines.rs b/crates/ra_ide_api/src/join_lines.rs
index 50bcfb5b7..9b81ad9e4 100644
--- a/crates/ra_ide_api/src/join_lines.rs
+++ b/crates/ra_ide_api/src/join_lines.rs
@@ -27,7 +27,7 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit {
27 SyntaxElement::Token(token) => token.parent(), 27 SyntaxElement::Token(token) => token.parent(),
28 }; 28 };
29 let mut edit = TextEditBuilder::default(); 29 let mut edit = TextEditBuilder::default();
30 for token in node.descendants_with_tokens().filter_map(|it| it.as_token().cloned()) { 30 for token in node.descendants_with_tokens().filter_map(|it| it.into_token()) {
31 let range = match range.intersection(&token.range()) { 31 let range = match range.intersection(&token.range()) {
32 Some(range) => range, 32 Some(range) => range,
33 None => continue, 33 None => continue,
diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs
index 0e5253025..477827fa7 100644
--- a/crates/ra_ide_api/src/syntax_highlighting.rs
+++ b/crates/ra_ide_api/src/syntax_highlighting.rs
@@ -237,8 +237,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
237 let mut buf = String::new(); 237 let mut buf = String::new();
238 buf.push_str(&STYLE); 238 buf.push_str(&STYLE);
239 buf.push_str("<pre><code>"); 239 buf.push_str("<pre><code>");
240 let tokens = 240 let tokens = parse.tree().syntax().descendants_with_tokens().filter_map(|it| it.into_token());
241 parse.tree().syntax().descendants_with_tokens().filter_map(|it| it.as_token().cloned());
242 for token in tokens { 241 for token in tokens {
243 could_intersect.retain(|it| token.range().start() <= it.range.end()); 242 could_intersect.retain(|it| token.range().start() <= it.range.end());
244 while let Some(r) = ranges.get(frontier) { 243 while let Some(r) = ranges.get(frontier) {
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(),