diff options
Diffstat (limited to 'crates/syntax/src/ast/node_ext.rs')
-rw-r--r-- | crates/syntax/src/ast/node_ext.rs | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 3d27d2c1a..e33e5bb03 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs | |||
@@ -8,7 +8,7 @@ use parser::SyntaxKind; | |||
8 | use rowan::{GreenNodeData, GreenTokenData}; | 8 | use rowan::{GreenNodeData, GreenTokenData}; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | ast::{self, support, AstNode, AstToken, AttrsOwner, NameOwner, SyntaxNode}, | 11 | ast::{self, support, AstChildren, AstNode, AstToken, AttrsOwner, NameOwner, SyntaxNode}, |
12 | NodeOrToken, SmolStr, SyntaxElement, SyntaxToken, TokenText, T, | 12 | NodeOrToken, SmolStr, SyntaxElement, SyntaxToken, TokenText, T, |
13 | }; | 13 | }; |
14 | 14 | ||
@@ -45,6 +45,12 @@ fn text_of_first_token(node: &SyntaxNode) -> TokenText<'_> { | |||
45 | } | 45 | } |
46 | } | 46 | } |
47 | 47 | ||
48 | impl ast::BlockExpr { | ||
49 | pub fn items(&self) -> AstChildren<ast::Item> { | ||
50 | support::children(self.syntax()) | ||
51 | } | ||
52 | } | ||
53 | |||
48 | #[derive(Debug, PartialEq, Eq, Clone)] | 54 | #[derive(Debug, PartialEq, Eq, Clone)] |
49 | pub enum Macro { | 55 | pub enum Macro { |
50 | MacroRules(ast::MacroRules), | 56 | MacroRules(ast::MacroRules), |
@@ -281,6 +287,15 @@ impl ast::Path { | |||
281 | successors(self.qualifier(), |p| p.qualifier()) | 287 | successors(self.qualifier(), |p| p.qualifier()) |
282 | } | 288 | } |
283 | } | 289 | } |
290 | |||
291 | impl ast::Use { | ||
292 | pub fn is_simple_glob(&self) -> bool { | ||
293 | self.use_tree() | ||
294 | .map(|use_tree| use_tree.use_tree_list().is_none() && use_tree.star_token().is_some()) | ||
295 | .unwrap_or(false) | ||
296 | } | ||
297 | } | ||
298 | |||
284 | impl ast::UseTree { | 299 | impl ast::UseTree { |
285 | pub fn is_simple_path(&self) -> bool { | 300 | pub fn is_simple_path(&self) -> bool { |
286 | self.use_tree_list().is_none() && self.star_token().is_none() | 301 | self.use_tree_list().is_none() && self.star_token().is_none() |
@@ -325,6 +340,15 @@ impl ast::Impl { | |||
325 | let second = types.next(); | 340 | let second = types.next(); |
326 | (first, second) | 341 | (first, second) |
327 | } | 342 | } |
343 | |||
344 | pub fn for_trait_name_ref(name_ref: &ast::NameRef) -> Option<ast::Impl> { | ||
345 | let this = name_ref.syntax().ancestors().find_map(ast::Impl::cast)?; | ||
346 | if this.trait_()?.syntax().text_range().start() == name_ref.syntax().text_range().start() { | ||
347 | Some(this) | ||
348 | } else { | ||
349 | None | ||
350 | } | ||
351 | } | ||
328 | } | 352 | } |
329 | 353 | ||
330 | #[derive(Debug, Clone, PartialEq, Eq)] | 354 | #[derive(Debug, Clone, PartialEq, Eq)] |
@@ -666,6 +690,14 @@ impl ast::LifetimeParam { | |||
666 | } | 690 | } |
667 | } | 691 | } |
668 | 692 | ||
693 | impl ast::Module { | ||
694 | /// Returns the parent ast::Module, this is different than the semantic parent in that this only | ||
695 | /// considers parent declarations in the AST | ||
696 | pub fn parent(&self) -> Option<ast::Module> { | ||
697 | self.syntax().ancestors().nth(2).and_then(ast::Module::cast) | ||
698 | } | ||
699 | } | ||
700 | |||
669 | impl ast::RangePat { | 701 | impl ast::RangePat { |
670 | pub fn start(&self) -> Option<ast::Pat> { | 702 | pub fn start(&self) -> Option<ast::Pat> { |
671 | self.syntax() | 703 | self.syntax() |