cairo_lang_syntax/node/
ids.rsuse std::sync::Arc;
use cairo_lang_filesystem::ids::FileId;
use cairo_lang_filesystem::span::TextWidth;
use cairo_lang_utils::{LookupIntern, define_short_id};
use super::SyntaxNode;
use super::db::SyntaxGroup;
use super::green::GreenNode;
use super::kind::SyntaxKind;
use crate::node::stable_ptr::SyntaxStablePtr;
define_short_id!(GreenId, Arc::<GreenNode>, SyntaxGroup, lookup_intern_green, intern_green);
impl GreenId {
pub fn width(&self, db: &dyn SyntaxGroup) -> TextWidth {
match &self.lookup_intern(db).details {
super::green::GreenNodeDetails::Token(text) => TextWidth::from_str(text),
super::green::GreenNodeDetails::Node { width, .. } => *width,
}
}
}
define_short_id!(
SyntaxStablePtrId,
SyntaxStablePtr,
SyntaxGroup,
lookup_intern_stable_ptr,
intern_stable_ptr
);
impl SyntaxStablePtrId {
pub fn lookup(&self, db: &dyn SyntaxGroup) -> SyntaxNode {
let ptr = self.lookup_intern(db);
match ptr {
SyntaxStablePtr::Root(file_id, green) => SyntaxNode::new_root(db, file_id, green),
SyntaxStablePtr::Child { parent, .. } => {
let parent = parent.lookup(db);
for child in db.get_children(parent).iter() {
if child.stable_ptr() == *self {
return child.clone();
}
}
unreachable!();
}
}
}
pub fn file_id(&self, db: &dyn SyntaxGroup) -> FileId {
let ptr = self.lookup_intern(db);
match ptr {
SyntaxStablePtr::Root(file_id, _) => file_id,
SyntaxStablePtr::Child { parent, .. } => parent.file_id(db),
}
}
pub fn parent(&self, db: &dyn SyntaxGroup) -> SyntaxStablePtrId {
let SyntaxStablePtr::Child { parent, .. } = self.lookup_intern(db) else { panic!() };
parent
}
pub fn nth_parent(&self, db: &dyn SyntaxGroup, n: usize) -> SyntaxStablePtrId {
let mut ptr = *self;
for _ in 0..n {
ptr = ptr.parent(db);
}
ptr
}
pub fn kind(&self, db: &dyn SyntaxGroup) -> SyntaxKind {
let SyntaxStablePtr::Child { kind, .. } = self.lookup_intern(db) else { panic!() };
kind
}
}