Skip to content
Open
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
16 changes: 10 additions & 6 deletions crates/cairo-lang-defs/src/patcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ impl<'db> RewriteNode<'db> {
match self {
RewriteNode::Copied(syntax_node) => {
*self = RewriteNode::new_modified(
syntax_node.get_children(db).map(RewriteNode::Copied).collect(),
syntax_node
.get_children(db)
.map(|child| RewriteNode::Copied(child.build(db)))
.collect(),
);
extract_matches!(self, RewriteNode::Modified)
}
Expand All @@ -75,16 +78,17 @@ impl<'db> RewriteNode<'db> {
let mut new_children = Vec::new();

// Get the index of the leftmost nonempty child.
let Some((left_idx, first_syntax)) =
let Some((left_idx, first_syntax_builder)) =
children.find(|(_, child)| child.width(db) != TextWidth::default())
else {
*self = RewriteNode::Modified(ModifiedNode { children: None });
return extract_matches!(self, RewriteNode::Modified);
};
let first_syntax = first_syntax_builder.build(db);
// Get the index of the rightmost nonempty child.
let (right_idx, last_syntax) = children
let (right_idx, last_syntax_builder) = children
.rfind(|(_, child)| child.width(db) != TextWidth::default())
.unwrap_or((left_idx, first_syntax));
.unwrap_or((left_idx, first_syntax_builder));
new_children.extend(itertools::repeat_n(
RewriteNode::Modified(ModifiedNode { children: None }),
left_idx,
Expand All @@ -107,10 +111,10 @@ impl<'db> RewriteNode<'db> {
trim_right: false,
});
for (_, child) in children {
new_children.push(RewriteNode::Copied(child));
new_children.push(RewriteNode::Copied(child.build(db)));
}
new_children.push(RewriteNode::Trimmed {
node: last_syntax,
node: last_syntax_builder.build(db),
trim_left: false,
trim_right: *trim_right,
});
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-defs/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fn test_generic_item_id(
}
_ => {}
}
for child in node.get_children(db) {
for child in node.get_children(db).nodes(db) {
find_generics(db, module_id, &child, output);
}
}
Expand Down
33 changes: 17 additions & 16 deletions crates/cairo-lang-doc/src/documentable_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,8 @@ fn write_function_signature<'db>(
/// Retrieves [`SyntaxKind::TypeClause`] text from [`SyntaxNode`].
fn get_type_clause<'db>(syntax_node: SyntaxNode<'db>, db: &'db dyn Database) -> Option<String> {
for child in syntax_node.get_children(db) {
if child.kind(db) == SyntaxKind::TypeClause {
if child.kind() == SyntaxKind::TypeClause {
let child = child.build(db);
return Some(child.get_text_without_all_comment_trivia(db));
}
}
Expand Down Expand Up @@ -960,7 +961,7 @@ fn write_struct_attributes_syntax<'db>(
for attribute in attributes {
let syntax_node = attribute.stable_ptr.lookup(f.db).as_syntax_node();
for child in syntax_node.get_children(f.db) {
let to_text = child.get_text_without_all_comment_trivia(f.db);
let to_text = child.build(f.db).get_text_without_all_comment_trivia(f.db);
let cleaned_text = to_text.replace("\n", "");
f.write_str(&cleaned_text)?;
}
Expand All @@ -979,20 +980,20 @@ fn write_syntactic_evaluation<'db>(
if matches!(&syntax_node.green_node(f.db).details, green::GreenNodeDetails::Node { .. }) {
let mut is_after_evaluation_value = false;
for child in syntax_node.get_children(f.db) {
let kind = child.kind(f.db);
if !matches!(kind, SyntaxKind::Trivia) {
if matches!(kind, SyntaxKind::TerminalSemicolon) {
f.buf.write_str(";")?;
return Ok(());
}
if is_after_evaluation_value {
f.buf.write_str(&SyntaxNode::get_text_without_all_comment_trivia(
&child, f.db,
))?;
};
if matches!(kind, SyntaxKind::TerminalEq) {
is_after_evaluation_value = true;
}
let kind = child.kind();
if matches!(kind, SyntaxKind::Trivia) {
continue;
}
if matches!(kind, SyntaxKind::TerminalSemicolon) {
f.buf.write_str(";")?;
return Ok(());
}
if is_after_evaluation_value {
f.buf
.write_str(&child.build(f.db).get_text_without_all_comment_trivia(f.db))?;
}
if matches!(kind, SyntaxKind::TerminalEq) {
is_after_evaluation_value = true;
}
}
};
Expand Down
26 changes: 17 additions & 9 deletions crates/cairo-lang-doc/src/location_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,25 @@ fn collect_green_nodes<'db>(
syntax_node: &SyntaxNode<'db>,
green_nodes: &mut Vec<(SyntaxKind, String)>,
) -> Vec<(SyntaxKind, String)> {
let green_node = syntax_node.green_node(db);

match &green_node.details {
GreenNodeDetails::Token(text) => green_nodes.push((green_node.kind, text.to_string(db))),
GreenNodeDetails::Node { .. } => {
let syntax_node_children = syntax_node.get_children(db);
syntax_node_children.for_each(|child| {
collect_green_nodes(db, &child, green_nodes);
});
fn collect_from_green<'db>(
db: &'db dyn Database,
green_node: &cairo_lang_syntax::node::green::GreenNode<'db>,
green_nodes: &mut Vec<(SyntaxKind, String)>,
) {
match &green_node.details {
GreenNodeDetails::Token(text) => {
green_nodes.push((green_node.kind, text.to_string(db)));
}
GreenNodeDetails::Node { children, .. } => {
for child in children {
collect_from_green(db, child.long(db), green_nodes);
}
}
}
}

let green_node = syntax_node.green_node(db);
collect_from_green(db, green_node, green_nodes);
green_nodes.to_owned()
}

Expand Down
10 changes: 5 additions & 5 deletions crates/cairo-lang-formatter/src/formatter_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ impl<'a> FormatterImpl<'a> {
let internal_break_line_points_positions =
syntax_node.get_internal_break_line_point_properties(self.db, &self.config);
// TODO(ilya): consider not copying here.
let mut children: Vec<_> = syntax_node.get_children(self.db).collect();
let mut children: Vec<_> = syntax_node.get_children(self.db).nodes(self.db).collect();
let n_children = children.len();

if self.config.merge_use_items {
Expand Down Expand Up @@ -1119,7 +1119,7 @@ impl<'a> FormatterImpl<'a> {

// Add merged children to the new_children list.
if let Some(child) = merged_node.get_children(self.db).next() {
new_children.extend(child.get_children(self.db));
new_children.extend(child.build(self.db).get_children(self.db).nodes(self.db));
}
}
}
Expand Down Expand Up @@ -1217,11 +1217,11 @@ impl<'a> FormatterImpl<'a> {
};

// The first newlines is the leading trivia correspond exactly to empty lines.
self.format_trivia(ast::Trivia::from_syntax_node(self.db, leading), true);
self.format_trivia(ast::Trivia::from_syntax_node(self.db, leading.build(self.db)), true);
if !syntax_node.should_skip_terminal(self.db) {
self.format_token(&token);
self.format_token(&token.build(self.db));
}
self.format_trivia(ast::Trivia::from_syntax_node(self.db, trailing), false);
self.format_trivia(ast::Trivia::from_syntax_node(self.db, trailing.build(self.db)), false);
}
/// Appends a trivia node (if needed) to the result.
fn format_trivia(&mut self, trivia: syntax::node::ast::Trivia<'a>, is_leading: bool) {
Expand Down
15 changes: 9 additions & 6 deletions crates/cairo-lang-formatter/src/node_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl<'a> SyntaxNodeFormat for SyntaxNode<'a> {
.parent(db)
.unwrap()
.get_children(db)
.any(|c| c.kind(db) == SyntaxKind::PatternEnumInnerPattern) =>
.any(|c| c.kind() == SyntaxKind::PatternEnumInnerPattern) =>
{
true
}
Expand Down Expand Up @@ -952,15 +952,15 @@ impl<'a> SyntaxNodeFormat for SyntaxNode<'a> {
)
{
let parent_node = self.parent(db).unwrap();
let children = parent_node.get_children(db);
let mut children = parent_node.get_children(db);
// Check if it's an ExprList or PatternList with len > 2, or any other list type.
let is_expr_or_pattern_list = matches!(
self.parent_kind(db),
Some(SyntaxKind::ExprList | SyntaxKind::PatternList)
);
if (!is_expr_or_pattern_list || children.len() > 2)
// Ensure that this node is the last element in the list.
&& children.last() == Some(*self)
&& children.next_back().map(|c| c.build(db)) == Some(*self)
{
return true;
}
Expand All @@ -975,8 +975,9 @@ impl<'a> SyntaxNodeFormat for SyntaxNode<'a> {
let statements_node = statement_node.parent(db).unwrap();
// Checking if not the last statement, as `;` may be there to prevent the block from
// returning the value of the current block.
let statements_children = statements_node.get_children(db);
let not_last = statements_children.last() != Some(statement_node);
let mut statements_children = statements_node.get_children(db);
let not_last =
statements_children.next_back().map(|c| c.build(db)) != Some(statement_node);
if not_last
&& matches!(
statement_node.get_child(db, 1).kind(db),
Expand All @@ -996,7 +997,9 @@ impl<'a> SyntaxNodeFormat for SyntaxNode<'a> {
{
let path_segment_node = self.parent(db).unwrap();
let path_node = path_segment_node.parent(db).unwrap();
if path_node.get_children(db).last() != Some(path_segment_node) {
if path_node.get_children(db).next_back().map(|c| c.build(db))
!= Some(path_segment_node)
{
false
} else {
matches!(
Expand Down
1 change: 1 addition & 0 deletions crates/cairo-lang-parser/src/colored_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl ColoredPrinter<'_> {
self.result.push_str(format!("{}", "<e>".red()).as_str());
} else {
for child in syntax_node.get_children(self.db) {
let child = child.build(self.db);
self.print(&child);
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/cairo-lang-parser/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,14 @@ impl<'a> Printer<'a> {
NodeKind::Struct { members: expected_children }
| NodeKind::Terminal { members: expected_children, .. } => {
self.print_internal_struct(
syntax_node.get_children(self.db),
syntax_node.get_children(self.db).nodes(self.db),
&expected_children,
indent.as_str(),
under_top_level,
);
}
NodeKind::List { .. } => {
for (i, child) in syntax_node.get_children(self.db).enumerate() {
for (i, child) in syntax_node.get_children(self.db).nodes(self.db).enumerate() {
self.print_tree(
format!("child #{i}").as_str(),
&child,
Expand All @@ -227,7 +227,7 @@ impl<'a> Printer<'a> {
}
}
NodeKind::SeparatedList { .. } => {
for (i, child) in syntax_node.get_children(self.db).enumerate() {
for (i, child) in syntax_node.get_children(self.db).nodes(self.db).enumerate() {
let description = if i.is_multiple_of(2) { "item" } else { "separator" };
self.print_tree(
format!("{description} #{}", i / 2).as_str(),
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-plugins/src/plugins/generate_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ fn generate_trait_for_impl<'db>(
builder.add_node(decl.name(db).as_syntax_node());
builder.add_node(decl.generic_params(db).as_syntax_node());
builder.add_node(signature.lparen(db).as_syntax_node());
for node in signature.parameters(db).node.get_children(db) {
for node in signature.parameters(db).node.get_children(db).nodes(db) {
if let Some(param) = ast::Param::cast(db, node) {
for modifier in param.modifiers(db).elements(db) {
// `mut` modifiers are only relevant for impls, not traits.
Expand Down
6 changes: 3 additions & 3 deletions crates/cairo-lang-semantic/src/items/macro_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ fn collect_expansion_placeholders<'db>(
return placeholders;
}
if !node.kind(db).is_terminal() {
for child in node.get_children(db) {
for child in node.get_children(db).nodes(db) {
placeholders.extend(collect_expansion_placeholders(db, child));
}
}
Expand Down Expand Up @@ -571,7 +571,7 @@ fn expand_macro_rule_ex(
return Ok(());
}

for child in node.get_children(db) {
for child in node.get_children(db).nodes(db) {
expand_macro_rule_ex(db, child, matcher_ctx, res_buffer, code_mappings)?;
}
return Ok(());
Expand All @@ -581,7 +581,7 @@ fn expand_macro_rule_ex(
res_buffer.push_str(node.get_text(db));
return Ok(());
}
for child in node.get_children(db) {
for child in node.get_children(db).nodes(db) {
expand_macro_rule_ex(db, child, matcher_ctx, res_buffer, code_mappings)?;
}
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-syntax/src/node/element_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl<'db, T: TypedSyntaxNode<'db>> ElementList<'db, T, 1> {
&self,
db: &'db dyn Database,
) -> impl ExactSizeIterator<Item = T> + DoubleEndedIterator + use<'db, T> + 'db {
self.node.get_children(db).map(|x| T::from_syntax_node(db, x))
self.node.get_children(db).map(move |x| T::from_syntax_node(db, x.build(db)))
}
pub fn has_tail(&self, _db: &dyn Database) -> bool {
false
Expand All @@ -39,7 +39,7 @@ impl<'db, T: TypedSyntaxNode<'db>> ElementList<'db, T, 2> {
&self,
db: &'db dyn Database,
) -> impl ExactSizeIterator<Item = T> + DoubleEndedIterator + use<'db, T> + 'db {
self.node.get_children(db).step_by(2).map(|x| T::from_syntax_node(db, x))
self.node.get_children(db).step_by(2).map(move |x| T::from_syntax_node(db, x.build(db)))
}
pub fn has_tail(&self, db: &dyn Database) -> bool {
!self.node.get_children(db).len().is_multiple_of(2)
Expand Down
3 changes: 2 additions & 1 deletion crates/cairo-lang-syntax/src/node/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ impl<'a> Iterator for Preorder<'a> {
// emitted.
Some(WalkEvent::Leave(layer.start))
}
Some(start) => {
Some(start_builder) => {
let start = start_builder.build(self.db);
// #3: Otherwise the iterator is just in the middle of visiting a child, so
// push a new layer to iterate it. To avoid
// recursion, step #1 is duplicated and
Expand Down
Loading