aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ast/extensions.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/ast/extensions.rs')
-rw-r--r--crates/ra_syntax/src/ast/extensions.rs60
1 files changed, 38 insertions, 22 deletions
diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs
index f3466c585..e4c99784c 100644
--- a/crates/ra_syntax/src/ast/extensions.rs
+++ b/crates/ra_syntax/src/ast/extensions.rs
@@ -3,7 +3,12 @@
3 3
4use itertools::Itertools; 4use itertools::Itertools;
5 5
6use crate::{SmolStr, SyntaxToken, ast::{self, AstNode, children, child_opt}, SyntaxKind::*, SyntaxElement}; 6use crate::{
7 SmolStr, SyntaxToken,
8 ast::{self, AstNode, children, child_opt},
9 SyntaxKind::*,
10 SyntaxElement, T,
11};
7use ra_parser::SyntaxKind; 12use ra_parser::SyntaxKind;
8 13
9impl ast::Name { 14impl ast::Name {
@@ -32,7 +37,7 @@ impl ast::Attr {
32 Some(prev) => prev, 37 Some(prev) => prev,
33 }; 38 };
34 39
35 prev.kind() == EXCL 40 prev.kind() == T![!]
36 } 41 }
37 42
38 pub fn as_atom(&self) -> Option<SmolStr> { 43 pub fn as_atom(&self) -> Option<SmolStr> {
@@ -102,9 +107,9 @@ impl ast::PathSegment {
102 PathSegmentKind::Name(name_ref) 107 PathSegmentKind::Name(name_ref)
103 } else { 108 } else {
104 match self.syntax().first_child_or_token()?.kind() { 109 match self.syntax().first_child_or_token()?.kind() {
105 SELF_KW => PathSegmentKind::SelfKw, 110 T![self] => PathSegmentKind::SelfKw,
106 SUPER_KW => PathSegmentKind::SuperKw, 111 T![super] => PathSegmentKind::SuperKw,
107 CRATE_KW => PathSegmentKind::CrateKw, 112 T![crate] => PathSegmentKind::CrateKw,
108 _ => return None, 113 _ => return None,
109 } 114 }
110 }; 115 };
@@ -113,7 +118,7 @@ impl ast::PathSegment {
113 118
114 pub fn has_colon_colon(&self) -> bool { 119 pub fn has_colon_colon(&self) -> bool {
115 match self.syntax.first_child_or_token().map(|s| s.kind()) { 120 match self.syntax.first_child_or_token().map(|s| s.kind()) {
116 Some(COLONCOLON) => true, 121 Some(T![::]) => true,
117 _ => false, 122 _ => false,
118 } 123 }
119 } 124 }
@@ -129,14 +134,14 @@ impl ast::Module {
129 pub fn has_semi(&self) -> bool { 134 pub fn has_semi(&self) -> bool {
130 match self.syntax().last_child_or_token() { 135 match self.syntax().last_child_or_token() {
131 None => false, 136 None => false,
132 Some(node) => node.kind() == SEMI, 137 Some(node) => node.kind() == T![;],
133 } 138 }
134 } 139 }
135} 140}
136 141
137impl ast::UseTree { 142impl ast::UseTree {
138 pub fn has_star(&self) -> bool { 143 pub fn has_star(&self) -> bool {
139 self.syntax().children_with_tokens().any(|it| it.kind() == STAR) 144 self.syntax().children_with_tokens().any(|it| it.kind() == T![*])
140 } 145 }
141} 146}
142 147
@@ -172,7 +177,7 @@ impl ast::ImplBlock {
172 } 177 }
173 178
174 pub fn is_negative(&self) -> bool { 179 pub fn is_negative(&self) -> bool {
175 self.syntax().children_with_tokens().any(|t| t.kind() == EXCL) 180 self.syntax().children_with_tokens().any(|t| t.kind() == T![!])
176 } 181 }
177} 182}
178 183
@@ -196,6 +201,17 @@ impl StructKind<'_> {
196} 201}
197 202
198impl ast::StructDef { 203impl ast::StructDef {
204 pub fn is_union(&self) -> bool {
205 for child in self.syntax().children_with_tokens() {
206 match child.kind() {
207 T![struct] => return false,
208 T![union] => return true,
209 _ => (),
210 }
211 }
212 false
213 }
214
199 pub fn kind(&self) -> StructKind { 215 pub fn kind(&self) -> StructKind {
200 StructKind::from_node(self) 216 StructKind::from_node(self)
201 } 217 }
@@ -219,7 +235,7 @@ impl ast::FnDef {
219 self.syntax() 235 self.syntax()
220 .last_child_or_token() 236 .last_child_or_token()
221 .and_then(|it| it.as_token()) 237 .and_then(|it| it.as_token())
222 .filter(|it| it.kind() == SEMI) 238 .filter(|it| it.kind() == T![;])
223 } 239 }
224} 240}
225 241
@@ -227,7 +243,7 @@ impl ast::LetStmt {
227 pub fn has_semi(&self) -> bool { 243 pub fn has_semi(&self) -> bool {
228 match self.syntax().last_child_or_token() { 244 match self.syntax().last_child_or_token() {
229 None => false, 245 None => false,
230 Some(node) => node.kind() == SEMI, 246 Some(node) => node.kind() == T![;],
231 } 247 }
232 } 248 }
233} 249}
@@ -236,7 +252,7 @@ impl ast::ExprStmt {
236 pub fn has_semi(&self) -> bool { 252 pub fn has_semi(&self) -> bool {
237 match self.syntax().last_child_or_token() { 253 match self.syntax().last_child_or_token() {
238 None => false, 254 None => false,
239 Some(node) => node.kind() == SEMI, 255 Some(node) => node.kind() == T![;],
240 } 256 }
241 } 257 }
242} 258}
@@ -270,29 +286,29 @@ impl ast::FieldExpr {
270 286
271impl ast::RefPat { 287impl ast::RefPat {
272 pub fn is_mut(&self) -> bool { 288 pub fn is_mut(&self) -> bool {
273 self.syntax().children_with_tokens().any(|n| n.kind() == MUT_KW) 289 self.syntax().children_with_tokens().any(|n| n.kind() == T![mut])
274 } 290 }
275} 291}
276 292
277impl ast::BindPat { 293impl ast::BindPat {
278 pub fn is_mutable(&self) -> bool { 294 pub fn is_mutable(&self) -> bool {
279 self.syntax().children_with_tokens().any(|n| n.kind() == MUT_KW) 295 self.syntax().children_with_tokens().any(|n| n.kind() == T![mut])
280 } 296 }
281 297
282 pub fn is_ref(&self) -> bool { 298 pub fn is_ref(&self) -> bool {
283 self.syntax().children_with_tokens().any(|n| n.kind() == REF_KW) 299 self.syntax().children_with_tokens().any(|n| n.kind() == T![ref])
284 } 300 }
285} 301}
286 302
287impl ast::PointerType { 303impl ast::PointerType {
288 pub fn is_mut(&self) -> bool { 304 pub fn is_mut(&self) -> bool {
289 self.syntax().children_with_tokens().any(|n| n.kind() == MUT_KW) 305 self.syntax().children_with_tokens().any(|n| n.kind() == T![mut])
290 } 306 }
291} 307}
292 308
293impl ast::ReferenceType { 309impl ast::ReferenceType {
294 pub fn is_mut(&self) -> bool { 310 pub fn is_mut(&self) -> bool {
295 self.syntax().children_with_tokens().any(|n| n.kind() == MUT_KW) 311 self.syntax().children_with_tokens().any(|n| n.kind() == T![mut])
296 } 312 }
297} 313}
298 314
@@ -311,19 +327,19 @@ impl ast::SelfParam {
311 self.syntax() 327 self.syntax()
312 .children_with_tokens() 328 .children_with_tokens()
313 .filter_map(|it| it.as_token()) 329 .filter_map(|it| it.as_token())
314 .find(|it| it.kind() == SELF_KW) 330 .find(|it| it.kind() == T![self])
315 .expect("invalid tree: self param must have self") 331 .expect("invalid tree: self param must have self")
316 } 332 }
317 333
318 pub fn kind(&self) -> SelfParamKind { 334 pub fn kind(&self) -> SelfParamKind {
319 let borrowed = self.syntax().children_with_tokens().any(|n| n.kind() == AMP); 335 let borrowed = self.syntax().children_with_tokens().any(|n| n.kind() == T![&]);
320 if borrowed { 336 if borrowed {
321 // check for a `mut` coming after the & -- `mut &self` != `&mut self` 337 // check for a `mut` coming after the & -- `mut &self` != `&mut self`
322 if self 338 if self
323 .syntax() 339 .syntax()
324 .children_with_tokens() 340 .children_with_tokens()
325 .skip_while(|n| n.kind() != AMP) 341 .skip_while(|n| n.kind() != T![&])
326 .any(|n| n.kind() == MUT_KW) 342 .any(|n| n.kind() == T![mut])
327 { 343 {
328 SelfParamKind::MutRef 344 SelfParamKind::MutRef
329 } else { 345 } else {
@@ -355,6 +371,6 @@ impl ast::WherePred {
355 371
356impl ast::TraitDef { 372impl ast::TraitDef {
357 pub fn is_auto(&self) -> bool { 373 pub fn is_auto(&self) -> bool {
358 self.syntax().children_with_tokens().any(|t| t.kind() == AUTO_KW) 374 self.syntax().children_with_tokens().any(|t| t.kind() == T![auto])
359 } 375 }
360} 376}