cairo_lang_syntax/node/
utils.rs

1use super::SyntaxNode;
2use super::db::SyntaxGroup;
3use super::kind::SyntaxKind;
4
5/// Checks whether the given node has a parent of the given kind.
6pub fn is_parent_of_kind(db: &dyn SyntaxGroup, node: &SyntaxNode, kind: SyntaxKind) -> bool {
7    let Some(parent) = node.parent() else {
8        return false;
9    };
10    parent.kind(db) == kind
11}
12
13/// Checks whether the given node has a grandparent of the given kind.
14pub fn is_grandparent_of_kind(db: &dyn SyntaxGroup, node: &SyntaxNode, kind: SyntaxKind) -> bool {
15    let Some(parent) = node.parent() else {
16        return false;
17    };
18    let Some(grandparent) = parent.parent() else {
19        return false;
20    };
21    grandparent.kind(db) == kind
22}
23
24/// Gets the kind of the parent of the given node, if it exists.
25pub fn parent_kind(db: &dyn SyntaxGroup, syntax_node: &SyntaxNode) -> Option<SyntaxKind> {
26    Some(syntax_node.parent()?.kind(db))
27}
28
29/// Gets the kind of the grandparent of the given node, if it exists.
30pub fn grandparent_kind(db: &dyn SyntaxGroup, syntax_node: &SyntaxNode) -> Option<SyntaxKind> {
31    Some(syntax_node.parent()?.parent()?.kind(db))
32}