diff options
Diffstat (limited to 'crates/syntax/src/ast/node_ext.rs')
-rw-r--r-- | crates/syntax/src/ast/node_ext.rs | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 171099661..492fbc4a0 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | //! Various extension methods to ast Nodes, which are hard to code-generate. | 1 | //! Various extension methods to ast Nodes, which are hard to code-generate. |
2 | //! Extensions for various expressions live in a sibling `expr_extensions` module. | 2 | //! Extensions for various expressions live in a sibling `expr_extensions` module. |
3 | 3 | ||
4 | use std::fmt; | 4 | use std::{fmt, iter::successors}; |
5 | 5 | ||
6 | use itertools::Itertools; | 6 | use itertools::Itertools; |
7 | use parser::SyntaxKind; | 7 | use parser::SyntaxKind; |
@@ -237,6 +237,26 @@ impl ast::Path { | |||
237 | None => self.segment(), | 237 | None => self.segment(), |
238 | } | 238 | } |
239 | } | 239 | } |
240 | |||
241 | pub fn first_qualifier_or_self(&self) -> ast::Path { | ||
242 | successors(Some(self.clone()), ast::Path::qualifier).last().unwrap() | ||
243 | } | ||
244 | |||
245 | pub fn first_segment(&self) -> Option<ast::PathSegment> { | ||
246 | self.first_qualifier_or_self().segment() | ||
247 | } | ||
248 | |||
249 | pub fn segments(&self) -> impl Iterator<Item = ast::PathSegment> + Clone { | ||
250 | // cant make use of SyntaxNode::siblings, because the returned Iterator is not clone | ||
251 | successors(self.first_segment(), |p| { | ||
252 | p.parent_path().parent_path().and_then(|p| p.segment()) | ||
253 | }) | ||
254 | } | ||
255 | } | ||
256 | impl ast::UseTree { | ||
257 | pub fn is_simple_path(&self) -> bool { | ||
258 | self.use_tree_list().is_none() && self.star_token().is_none() | ||
259 | } | ||
240 | } | 260 | } |
241 | 261 | ||
242 | impl ast::UseTreeList { | 262 | impl ast::UseTreeList { |