Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/cairo-lang-defs/src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1680,7 +1680,8 @@ impl SyntaxNodeCached {
let green = inner.green.embed(ctx);
let id = inner.id.embed(ctx);
let offset = inner.offset;
let node = new_syntax_node(ctx.db, green, offset, id);
let kind = green.long(ctx.db).kind;
let node = new_syntax_node(ctx.db, green, offset, id, kind);
ctx.syntax_nodes.insert(*self, node);
node
}
Expand Down
34 changes: 24 additions & 10 deletions crates/cairo-lang-syntax/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ pub struct SyntaxNode<'a> {
data: SyntaxNodeData<'a>,
/// Cached parent data to avoid database lookups. None for root nodes.
parent: Option<SyntaxNodeData<'a>>,
/// Cached kind to avoid database lookups.
kind: SyntaxKind,
/// Cached parent kind to avoid database lookups. None for root nodes.
parent_kind: Option<SyntaxKind>,
}

impl<'db> std::fmt::Debug for SyntaxNode<'db> {
Expand Down Expand Up @@ -206,20 +210,22 @@ pub fn new_syntax_node<'db>(
green: GreenId<'db>,
offset: TextOffset,
id: SyntaxNodeId<'db>,
kind: SyntaxKind,
) -> SyntaxNode<'db> {
let parent = match &id {
SyntaxNodeId::Child { parent, .. } => Some(parent.data),
SyntaxNodeId::Root(_) => None,
let (parent, parent_kind) = match &id {
SyntaxNodeId::Child { parent, .. } => (Some(parent.data), Some(parent.kind)),
SyntaxNodeId::Root(_) => (None, None),
};
let data = SyntaxNodeData::new(db, green, offset, id);
SyntaxNode { data, parent }
SyntaxNode { data, parent, kind, parent_kind }
}

// Construction methods
impl<'a> SyntaxNode<'a> {
/// Create a new root syntax node.
pub fn new_root(db: &'a dyn Database, file_id: FileId<'a>, green: GreenId<'a>) -> Self {
new_syntax_node(db, green, TextOffset::START, SyntaxNodeId::Root(file_id))
let kind = green.long(db).kind;
new_syntax_node(db, green, TextOffset::START, SyntaxNodeId::Root(file_id), kind)
}

/// Create a new root syntax node with a custom initial offset.
Expand All @@ -229,7 +235,14 @@ impl<'a> SyntaxNode<'a> {
green: GreenId<'a>,
initial_offset: Option<TextOffset>,
) -> Self {
new_syntax_node(db, green, initial_offset.unwrap_or_default(), SyntaxNodeId::Root(file_id))
let kind = green.long(db).kind;
new_syntax_node(
db,
green,
initial_offset.unwrap_or_default(),
SyntaxNodeId::Root(file_id),
kind,
)
}

// Basic accessors
Expand All @@ -240,8 +253,8 @@ impl<'a> SyntaxNode<'a> {
}

/// Get the syntax kind of this node.
pub fn kind(&self, db: &dyn Database) -> SyntaxKind {
self.green_node(db).kind
pub fn kind(&self, _db: &dyn Database) -> SyntaxKind {
self.kind
}

/// Get the span of this syntax node.
Expand Down Expand Up @@ -310,6 +323,7 @@ impl<'a> SyntaxNode<'a> {
*green_id,
offset,
SyntaxNodeId::Child { parent: *self, index, key_fields: Box::from(key_fields) },
kind,
));

offset = offset.add_width(width);
Expand Down Expand Up @@ -553,8 +567,8 @@ impl<'a> SyntaxNode<'a> {
}

/// Gets the kind of the given node's parent if it exists.
pub fn parent_kind(&self, db: &dyn Database) -> Option<SyntaxKind> {
Some(self.parent?.green(db).long(db).kind)
pub fn parent_kind(&self, _db: &dyn Database) -> Option<SyntaxKind> {
self.parent_kind
}

/// Gets the kind of the given node's grandparent if it exists.
Expand Down