From 143520dbf98ef735ae957febf7508a12d246b040 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Tue, 12 Nov 2024 17:32:43 +0000 Subject: [PATCH] feat(grit): complete formatting of all nodes --- .../src/generated/node_factory.rs | 32 +-- .../src/generated/syntax_factory.rs | 70 +++--- crates/biome_grit_formatter/src/generated.rs | 80 +++---- .../src/grit/auxiliary/annotation.rs | 4 +- .../src/grit/auxiliary/sequential.rs | 43 ++-- .../src/grit/auxiliary/within.rs | 12 +- .../grit/declarations/function_definition.rs | 27 ++- .../src/grit/patterns/add_operation.rs | 21 +- .../grit/patterns/assignment_as_pattern.rs | 21 +- .../src/grit/patterns/bracketed_pattern.rs | 19 +- .../src/grit/patterns/curly_pattern.rs | 22 +- .../src/grit/patterns/div_operation.rs | 21 +- .../src/grit/patterns/mod_operation.rs | 21 +- .../src/grit/patterns/mul_operation.rs | 21 +- .../src/grit/patterns/pattern_accumulate.rs | 20 +- .../src/grit/patterns/pattern_after.rs | 11 +- .../src/grit/patterns/pattern_before.rs | 11 +- .../patterns/pattern_contains_until_clause.rs | 8 +- .../src/grit/patterns/pattern_else_clause.rs | 11 +- .../src/grit/patterns/pattern_if_else.rs | 31 ++- .../src/grit/patterns/pattern_limit.rs | 21 +- .../src/grit/patterns/pattern_maybe.rs | 11 +- .../src/grit/patterns/pattern_not.rs | 8 +- .../src/grit/patterns/regex_pattern.rs | 8 +- .../grit/patterns/regex_pattern_variables.rs | 19 +- .../src/grit/patterns/sub_operation.rs | 21 +- .../grit/predicates/bracketed_predicate.rs | 18 +- .../grit/predicates/curly_predicate_list.rs | 24 --- .../src/grit/predicates/mod.rs | 2 +- .../src/grit/predicates/predicate_curly.rs | 27 +++ .../grit/predicates/predicate_definition.rs | 26 ++- .../grit/predicates/predicate_else_clause.rs | 11 +- .../src/grit/predicates/predicate_equal.rs | 22 +- .../src/grit/predicates/predicate_greater.rs | 21 +- .../predicates/predicate_greater_equal.rs | 21 +- .../src/grit/predicates/predicate_if_else.rs | 31 ++- .../src/grit/predicates/predicate_less.rs | 21 +- .../grit/predicates/predicate_less_equal.rs | 21 +- .../src/grit/predicates/predicate_maybe.rs | 11 +- .../src/grit/predicates/predicate_not.rs | 8 +- .../grit/predicates/predicate_not_equal.rs | 21 +- .../biome_grit_formatter/tests/quick_test.rs | 7 +- .../tests/specs/grit/function_definition.grit | 25 +++ .../specs/grit/function_definition.grit.snap | 79 +++++++ .../specs/grit/patterns/contains.grit.snap | 6 - .../grit/patterns/create_new_files.grit.snap | 6 - .../tests/specs/grit/patterns/if_else.grit | 7 + .../specs/grit/patterns/if_else.grit.snap | 40 ++++ .../tests/specs/grit/patterns/regex.grit | 1 + .../tests/specs/grit/patterns/regex.grit.snap | 30 +++ .../tests/specs/grit/predicates/and.grit.snap | 6 - .../tests/specs/grit/predicates/any.grit.snap | 6 - .../tests/specs/grit/sequential.grit.snap | 14 +- .../src/parser/definitions.rs | 2 +- .../err/invalid_predicate.grit.snap | 4 +- .../err/invalid_predicate_subject.grit.snap | 4 +- .../ok/function_definition.grit.snap | 8 +- .../ok/predicate_definition.grit.snap | 4 +- .../biome_grit_syntax/src/generated/kind.rs | 3 +- .../biome_grit_syntax/src/generated/macros.rs | 8 +- .../biome_grit_syntax/src/generated/nodes.rs | 200 +++++++++--------- .../src/generated/nodes_mut.rs | 44 ++-- xtask/codegen/gritql.ungram | 18 +- xtask/codegen/src/grit_kinds_src.rs | 2 +- 64 files changed, 979 insertions(+), 424 deletions(-) delete mode 100644 crates/biome_grit_formatter/src/grit/predicates/curly_predicate_list.rs create mode 100644 crates/biome_grit_formatter/src/grit/predicates/predicate_curly.rs create mode 100644 crates/biome_grit_formatter/tests/specs/grit/function_definition.grit create mode 100644 crates/biome_grit_formatter/tests/specs/grit/function_definition.grit.snap create mode 100644 crates/biome_grit_formatter/tests/specs/grit/patterns/if_else.grit create mode 100644 crates/biome_grit_formatter/tests/specs/grit/patterns/if_else.grit.snap create mode 100644 crates/biome_grit_formatter/tests/specs/grit/patterns/regex.grit create mode 100644 crates/biome_grit_formatter/tests/specs/grit/patterns/regex.grit.snap diff --git a/crates/biome_grit_factory/src/generated/node_factory.rs b/crates/biome_grit_factory/src/generated/node_factory.rs index 272dd6a4a1f4..0c3512e3e6d1 100644 --- a/crates/biome_grit_factory/src/generated/node_factory.rs +++ b/crates/biome_grit_factory/src/generated/node_factory.rs @@ -147,20 +147,6 @@ pub fn grit_curly_pattern( ], )) } -pub fn grit_curly_predicate_list( - l_curly_token: SyntaxToken, - predicates: GritPredicateList, - r_curly_token: SyntaxToken, -) -> GritCurlyPredicateList { - GritCurlyPredicateList::unwrap_cast(SyntaxNode::new_detached( - GritSyntaxKind::GRIT_CURLY_PREDICATE_LIST, - [ - Some(SyntaxElement::Token(l_curly_token)), - Some(SyntaxElement::Node(predicates.into_syntax())), - Some(SyntaxElement::Token(r_curly_token)), - ], - )) -} pub fn grit_div_operation( left: AnyGritPattern, slash_token: SyntaxToken, @@ -244,7 +230,7 @@ pub fn grit_function_definition( l_paren_token: SyntaxToken, args: GritVariableList, r_paren_token: SyntaxToken, - body: GritCurlyPredicateList, + body: GritPredicateCurly, ) -> GritFunctionDefinition { GritFunctionDefinition::unwrap_cast(SyntaxNode::new_detached( GritSyntaxKind::GRIT_FUNCTION_DEFINITION, @@ -1005,13 +991,27 @@ pub fn grit_predicate_call( ], )) } +pub fn grit_predicate_curly( + l_curly_token: SyntaxToken, + predicates: GritPredicateList, + r_curly_token: SyntaxToken, +) -> GritPredicateCurly { + GritPredicateCurly::unwrap_cast(SyntaxNode::new_detached( + GritSyntaxKind::GRIT_PREDICATE_CURLY, + [ + Some(SyntaxElement::Token(l_curly_token)), + Some(SyntaxElement::Node(predicates.into_syntax())), + Some(SyntaxElement::Token(r_curly_token)), + ], + )) +} pub fn grit_predicate_definition( predicate_token: SyntaxToken, name: GritName, l_paren_token: SyntaxToken, args: GritVariableList, r_paren_token: SyntaxToken, - body: GritCurlyPredicateList, + body: GritPredicateCurly, ) -> GritPredicateDefinition { GritPredicateDefinition::unwrap_cast(SyntaxNode::new_detached( GritSyntaxKind::GRIT_PREDICATE_DEFINITION, diff --git a/crates/biome_grit_factory/src/generated/syntax_factory.rs b/crates/biome_grit_factory/src/generated/syntax_factory.rs index 7dd53505d98c..98481d7b13af 100644 --- a/crates/biome_grit_factory/src/generated/syntax_factory.rs +++ b/crates/biome_grit_factory/src/generated/syntax_factory.rs @@ -332,39 +332,6 @@ impl SyntaxFactory for GritSyntaxFactory { } slots.into_node(GRIT_CURLY_PATTERN, children) } - GRIT_CURLY_PREDICATE_LIST => { - let mut elements = (&children).into_iter(); - let mut slots: RawNodeSlots<3usize> = RawNodeSlots::default(); - let mut current_element = elements.next(); - if let Some(element) = ¤t_element { - if element.kind() == T!['{'] { - slots.mark_present(); - current_element = elements.next(); - } - } - slots.next_slot(); - if let Some(element) = ¤t_element { - if GritPredicateList::can_cast(element.kind()) { - slots.mark_present(); - current_element = elements.next(); - } - } - slots.next_slot(); - if let Some(element) = ¤t_element { - if element.kind() == T!['}'] { - slots.mark_present(); - current_element = elements.next(); - } - } - slots.next_slot(); - if current_element.is_some() { - return RawSyntaxNode::new( - GRIT_CURLY_PREDICATE_LIST.to_bogus(), - children.into_iter().map(Some), - ); - } - slots.into_node(GRIT_CURLY_PREDICATE_LIST, children) - } GRIT_DIV_OPERATION => { let mut elements = (&children).into_iter(); let mut slots: RawNodeSlots<3usize> = RawNodeSlots::default(); @@ -565,7 +532,7 @@ impl SyntaxFactory for GritSyntaxFactory { } slots.next_slot(); if let Some(element) = ¤t_element { - if GritCurlyPredicateList::can_cast(element.kind()) { + if GritPredicateCurly::can_cast(element.kind()) { slots.mark_present(); current_element = elements.next(); } @@ -2035,6 +2002,39 @@ impl SyntaxFactory for GritSyntaxFactory { } slots.into_node(GRIT_PREDICATE_CALL, children) } + GRIT_PREDICATE_CURLY => { + let mut elements = (&children).into_iter(); + let mut slots: RawNodeSlots<3usize> = RawNodeSlots::default(); + let mut current_element = elements.next(); + if let Some(element) = ¤t_element { + if element.kind() == T!['{'] { + slots.mark_present(); + current_element = elements.next(); + } + } + slots.next_slot(); + if let Some(element) = ¤t_element { + if GritPredicateList::can_cast(element.kind()) { + slots.mark_present(); + current_element = elements.next(); + } + } + slots.next_slot(); + if let Some(element) = ¤t_element { + if element.kind() == T!['}'] { + slots.mark_present(); + current_element = elements.next(); + } + } + slots.next_slot(); + if current_element.is_some() { + return RawSyntaxNode::new( + GRIT_PREDICATE_CURLY.to_bogus(), + children.into_iter().map(Some), + ); + } + slots.into_node(GRIT_PREDICATE_CURLY, children) + } GRIT_PREDICATE_DEFINITION => { let mut elements = (&children).into_iter(); let mut slots: RawNodeSlots<6usize> = RawNodeSlots::default(); @@ -2075,7 +2075,7 @@ impl SyntaxFactory for GritSyntaxFactory { } slots.next_slot(); if let Some(element) = ¤t_element { - if GritCurlyPredicateList::can_cast(element.kind()) { + if GritPredicateCurly::can_cast(element.kind()) { slots.mark_present(); current_element = elements.next(); } diff --git a/crates/biome_grit_formatter/src/generated.rs b/crates/biome_grit_formatter/src/generated.rs index ab9a7b9f7016..57772ca51aeb 100644 --- a/crates/biome_grit_formatter/src/generated.rs +++ b/crates/biome_grit_formatter/src/generated.rs @@ -442,46 +442,6 @@ impl IntoFormat for biome_grit_syntax::GritCurlyPattern { ) } } -impl FormatRule - for crate::grit::predicates::curly_predicate_list::FormatGritCurlyPredicateList -{ - type Context = GritFormatContext; - #[inline(always)] - fn fmt( - &self, - node: &biome_grit_syntax::GritCurlyPredicateList, - f: &mut GritFormatter, - ) -> FormatResult<()> { - FormatNodeRule::::fmt(self, node, f) - } -} -impl AsFormat for biome_grit_syntax::GritCurlyPredicateList { - type Format<'a> = FormatRefWithRule< - 'a, - biome_grit_syntax::GritCurlyPredicateList, - crate::grit::predicates::curly_predicate_list::FormatGritCurlyPredicateList, - >; - fn format(&self) -> Self::Format<'_> { - #![allow(clippy::default_constructed_unit_structs)] - FormatRefWithRule::new( - self, - crate::grit::predicates::curly_predicate_list::FormatGritCurlyPredicateList::default(), - ) - } -} -impl IntoFormat for biome_grit_syntax::GritCurlyPredicateList { - type Format = FormatOwnedWithRule< - biome_grit_syntax::GritCurlyPredicateList, - crate::grit::predicates::curly_predicate_list::FormatGritCurlyPredicateList, - >; - fn into_format(self) -> Self::Format { - #![allow(clippy::default_constructed_unit_structs)] - FormatOwnedWithRule::new( - self, - crate::grit::predicates::curly_predicate_list::FormatGritCurlyPredicateList::default(), - ) - } -} impl FormatRule for crate::grit::patterns::div_operation::FormatGritDivOperation { @@ -2432,6 +2392,46 @@ impl IntoFormat for biome_grit_syntax::GritPredicateCall { ) } } +impl FormatRule + for crate::grit::predicates::predicate_curly::FormatGritPredicateCurly +{ + type Context = GritFormatContext; + #[inline(always)] + fn fmt( + &self, + node: &biome_grit_syntax::GritPredicateCurly, + f: &mut GritFormatter, + ) -> FormatResult<()> { + FormatNodeRule::::fmt(self, node, f) + } +} +impl AsFormat for biome_grit_syntax::GritPredicateCurly { + type Format<'a> = FormatRefWithRule< + 'a, + biome_grit_syntax::GritPredicateCurly, + crate::grit::predicates::predicate_curly::FormatGritPredicateCurly, + >; + fn format(&self) -> Self::Format<'_> { + #![allow(clippy::default_constructed_unit_structs)] + FormatRefWithRule::new( + self, + crate::grit::predicates::predicate_curly::FormatGritPredicateCurly::default(), + ) + } +} +impl IntoFormat for biome_grit_syntax::GritPredicateCurly { + type Format = FormatOwnedWithRule< + biome_grit_syntax::GritPredicateCurly, + crate::grit::predicates::predicate_curly::FormatGritPredicateCurly, + >; + fn into_format(self) -> Self::Format { + #![allow(clippy::default_constructed_unit_structs)] + FormatOwnedWithRule::new( + self, + crate::grit::predicates::predicate_curly::FormatGritPredicateCurly::default(), + ) + } +} impl FormatRule for crate::grit::predicates::predicate_definition::FormatGritPredicateDefinition { diff --git a/crates/biome_grit_formatter/src/grit/auxiliary/annotation.rs b/crates/biome_grit_formatter/src/grit/auxiliary/annotation.rs index 9a1d3514788e..c635b08d5434 100644 --- a/crates/biome_grit_formatter/src/grit/auxiliary/annotation.rs +++ b/crates/biome_grit_formatter/src/grit/auxiliary/annotation.rs @@ -1,10 +1,10 @@ use crate::prelude::*; +use biome_formatter::write; use biome_grit_syntax::GritAnnotation; -use biome_rowan::AstNode; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritAnnotation; impl FormatNodeRule for FormatGritAnnotation { fn fmt_fields(&self, node: &GritAnnotation, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + write!(f, [node.value_token().format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/auxiliary/sequential.rs b/crates/biome_grit_formatter/src/grit/auxiliary/sequential.rs index fc945446b178..467aaba2cb9b 100644 --- a/crates/biome_grit_formatter/src/grit/auxiliary/sequential.rs +++ b/crates/biome_grit_formatter/src/grit/auxiliary/sequential.rs @@ -1,29 +1,32 @@ use crate::prelude::*; -use biome_grit_syntax::GritSequential; +use biome_formatter::{write, FormatOptions}; +use biome_grit_syntax::{GritSequential, GritSequentialFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritSequential; impl FormatNodeRule for FormatGritSequential { fn fmt_fields(&self, node: &GritSequential, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritSequentialFields { + l_curly_token, + sequential_token, + sequential, + r_curly_token, + } = node.as_fields(); - // TODO: investigate the verbatim panic when this code runs - // let GritSequentialFields { - // l_curly_token, - // sequential_token, - // sequential, - // r_curly_token, - // } = node.as_fields(); - // - // write!( - // f, - // [ - // sequential_token.format(), - // space(), - // l_curly_token.format(), - // sequential.format(), - // r_curly_token.format() - // ] - // ) + write!( + f, + [sequential_token.format(), space(), l_curly_token.format(),] + )?; + + let should_insert_space_around_brackets = f.options().bracket_spacing().value(); + write!( + f, + [group(&soft_block_indent_with_maybe_space( + &sequential.format(), + should_insert_space_around_brackets + ),)] + )?; + + write!(f, [r_curly_token.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/auxiliary/within.rs b/crates/biome_grit_formatter/src/grit/auxiliary/within.rs index edc63b832e3b..8dbe35c091d2 100644 --- a/crates/biome_grit_formatter/src/grit/auxiliary/within.rs +++ b/crates/biome_grit_formatter/src/grit/auxiliary/within.rs @@ -1,10 +1,16 @@ use crate::prelude::*; -use biome_grit_syntax::GritWithin; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritWithin, GritWithinFields}; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritWithin; impl FormatNodeRule for FormatGritWithin { fn fmt_fields(&self, node: &GritWithin, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritWithinFields { + pattern, + within_token, + } = node.as_fields(); + + write!(f, [within_token.format(), space(), pattern.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/declarations/function_definition.rs b/crates/biome_grit_formatter/src/grit/declarations/function_definition.rs index bee82b18d280..5da7d46fc55b 100644 --- a/crates/biome_grit_formatter/src/grit/declarations/function_definition.rs +++ b/crates/biome_grit_formatter/src/grit/declarations/function_definition.rs @@ -1,10 +1,31 @@ use crate::prelude::*; -use biome_grit_syntax::GritFunctionDefinition; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritFunctionDefinition, GritFunctionDefinitionFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritFunctionDefinition; impl FormatNodeRule for FormatGritFunctionDefinition { fn fmt_fields(&self, node: &GritFunctionDefinition, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritFunctionDefinitionFields { + function_token, + name, + l_paren_token, + args, + r_paren_token, + body, + } = node.as_fields(); + + write!( + f, + [ + function_token.format(), + space(), + name.format(), + l_paren_token.format(), + group(&args.format()), + r_paren_token.format(), + space(), + body.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/add_operation.rs b/crates/biome_grit_formatter/src/grit/patterns/add_operation.rs index e063453ce77b..a2f4e2485fe5 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/add_operation.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/add_operation.rs @@ -1,10 +1,25 @@ use crate::prelude::*; -use biome_grit_syntax::GritAddOperation; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritAddOperation, GritAddOperationFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritAddOperation; impl FormatNodeRule for FormatGritAddOperation { fn fmt_fields(&self, node: &GritAddOperation, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritAddOperationFields { + left, + right, + plus_token, + } = node.as_fields(); + + write!( + f, + [ + left.format(), + space(), + plus_token.format(), + space(), + right.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/assignment_as_pattern.rs b/crates/biome_grit_formatter/src/grit/patterns/assignment_as_pattern.rs index a3879bbaaaba..cf9692a13108 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/assignment_as_pattern.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/assignment_as_pattern.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -use biome_grit_syntax::GritAssignmentAsPattern; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritAssignmentAsPattern, GritAssignmentAsPatternFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritAssignmentAsPattern; impl FormatNodeRule for FormatGritAssignmentAsPattern { @@ -9,6 +9,21 @@ impl FormatNodeRule for FormatGritAssignmentAsPattern { node: &GritAssignmentAsPattern, f: &mut GritFormatter, ) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritAssignmentAsPatternFields { + pattern, + eq_token, + container, + } = node.as_fields(); + + write!( + f, + [ + container.format(), + space(), + eq_token.format(), + space(), + pattern.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/bracketed_pattern.rs b/crates/biome_grit_formatter/src/grit/patterns/bracketed_pattern.rs index c21f9c9bc1ec..1d216cb520ff 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/bracketed_pattern.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/bracketed_pattern.rs @@ -1,10 +1,23 @@ use crate::prelude::*; -use biome_grit_syntax::GritBracketedPattern; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritBracketedPattern, GritBracketedPatternFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritBracketedPattern; impl FormatNodeRule for FormatGritBracketedPattern { fn fmt_fields(&self, node: &GritBracketedPattern, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritBracketedPatternFields { + l_paren_token, + r_paren_token, + pattern, + } = node.as_fields(); + + write!( + f, + [ + l_paren_token.format(), + pattern.format(), + r_paren_token.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/curly_pattern.rs b/crates/biome_grit_formatter/src/grit/patterns/curly_pattern.rs index 264d9f7b3f38..006bb231681f 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/curly_pattern.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/curly_pattern.rs @@ -1,10 +1,26 @@ use crate::prelude::*; -use biome_grit_syntax::GritCurlyPattern; -use biome_rowan::AstNode; +use biome_formatter::{write, FormatOptions}; +use biome_grit_syntax::{GritCurlyPattern, GritCurlyPatternFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritCurlyPattern; impl FormatNodeRule for FormatGritCurlyPattern { fn fmt_fields(&self, node: &GritCurlyPattern, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritCurlyPatternFields { + pattern, + l_curly_token, + r_curly_token, + } = node.as_fields(); + + write!(f, [space(), l_curly_token.format()])?; + let should_insert_space_around_brackets = f.options().bracket_spacing().value(); + write!( + f, + [group(&soft_block_indent_with_maybe_space( + &pattern.format(), + should_insert_space_around_brackets + ),)] + )?; + + write!(f, [r_curly_token.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/div_operation.rs b/crates/biome_grit_formatter/src/grit/patterns/div_operation.rs index 2088be6d0e22..e8e06415f334 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/div_operation.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/div_operation.rs @@ -1,10 +1,25 @@ use crate::prelude::*; -use biome_grit_syntax::GritDivOperation; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritDivOperation, GritDivOperationFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritDivOperation; impl FormatNodeRule for FormatGritDivOperation { fn fmt_fields(&self, node: &GritDivOperation, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritDivOperationFields { + left, + right, + slash_token, + } = node.as_fields(); + + write!( + f, + [ + left.format(), + space(), + slash_token.format(), + space(), + right.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/mod_operation.rs b/crates/biome_grit_formatter/src/grit/patterns/mod_operation.rs index 6d0d55c053e5..72603c7e9a20 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/mod_operation.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/mod_operation.rs @@ -1,10 +1,25 @@ use crate::prelude::*; -use biome_grit_syntax::GritModOperation; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritModOperation, GritModOperationFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritModOperation; impl FormatNodeRule for FormatGritModOperation { fn fmt_fields(&self, node: &GritModOperation, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritModOperationFields { + left, + right, + remainder_token, + } = node.as_fields(); + + write!( + f, + [ + left.format(), + space(), + remainder_token.format(), + space(), + right.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/mul_operation.rs b/crates/biome_grit_formatter/src/grit/patterns/mul_operation.rs index e88079f187d9..18434b19e387 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/mul_operation.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/mul_operation.rs @@ -1,10 +1,25 @@ use crate::prelude::*; -use biome_grit_syntax::GritMulOperation; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritMulOperation, GritMulOperationFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritMulOperation; impl FormatNodeRule for FormatGritMulOperation { fn fmt_fields(&self, node: &GritMulOperation, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritMulOperationFields { + left, + right, + star_token, + } = node.as_fields(); + + write!( + f, + [ + left.format(), + space(), + star_token.format(), + space(), + right.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/pattern_accumulate.rs b/crates/biome_grit_formatter/src/grit/patterns/pattern_accumulate.rs index bc08abc66204..b7b31b82810c 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/pattern_accumulate.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/pattern_accumulate.rs @@ -1,10 +1,24 @@ use crate::prelude::*; -use biome_grit_syntax::GritPatternAccumulate; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPatternAccumulate, GritPatternAccumulateFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPatternAccumulate; impl FormatNodeRule for FormatGritPatternAccumulate { fn fmt_fields(&self, node: &GritPatternAccumulate, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPatternAccumulateFields { + right, + left, + add_assign_token, + } = node.as_fields(); + write!( + f, + [ + left.format(), + space(), + add_assign_token.format(), + space(), + right.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/pattern_after.rs b/crates/biome_grit_formatter/src/grit/patterns/pattern_after.rs index 271f565d3763..6679b82a36fa 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/pattern_after.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/pattern_after.rs @@ -1,10 +1,15 @@ use crate::prelude::*; -use biome_grit_syntax::GritPatternAfter; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPatternAfter, GritPatternAfterFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPatternAfter; impl FormatNodeRule for FormatGritPatternAfter { fn fmt_fields(&self, node: &GritPatternAfter, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPatternAfterFields { + pattern, + after_token, + } = node.as_fields(); + + write!(f, [after_token.format(), space(), pattern.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/pattern_before.rs b/crates/biome_grit_formatter/src/grit/patterns/pattern_before.rs index f52af58a7df0..c8503c0b2e17 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/pattern_before.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/pattern_before.rs @@ -1,10 +1,15 @@ use crate::prelude::*; -use biome_grit_syntax::GritPatternBefore; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPatternBefore, GritPatternBeforeFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPatternBefore; impl FormatNodeRule for FormatGritPatternBefore { fn fmt_fields(&self, node: &GritPatternBefore, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPatternBeforeFields { + pattern, + before_token, + } = node.as_fields(); + + write!(f, [before_token.format(), space(), pattern.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/pattern_contains_until_clause.rs b/crates/biome_grit_formatter/src/grit/patterns/pattern_contains_until_clause.rs index 53f1f98cac29..55ec9f217742 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/pattern_contains_until_clause.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/pattern_contains_until_clause.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -use biome_grit_syntax::GritPatternContainsUntilClause; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPatternContainsUntilClause, GritPatternContainsUntilClauseFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPatternContainsUntilClause; impl FormatNodeRule for FormatGritPatternContainsUntilClause { @@ -9,6 +9,8 @@ impl FormatNodeRule for FormatGritPatternContain node: &GritPatternContainsUntilClause, f: &mut GritFormatter, ) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPatternContainsUntilClauseFields { until, until_token } = node.as_fields(); + + write!(f, [until_token.format(), space(), until.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/pattern_else_clause.rs b/crates/biome_grit_formatter/src/grit/patterns/pattern_else_clause.rs index 89b46cb100a6..2fe845897f10 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/pattern_else_clause.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/pattern_else_clause.rs @@ -1,10 +1,15 @@ use crate::prelude::*; -use biome_grit_syntax::GritPatternElseClause; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPatternElseClause, GritPatternElseClauseFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPatternElseClause; impl FormatNodeRule for FormatGritPatternElseClause { fn fmt_fields(&self, node: &GritPatternElseClause, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPatternElseClauseFields { + else_token, + else_pattern, + } = node.as_fields(); + + write!(f, [else_token.format(), space(), else_pattern.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/pattern_if_else.rs b/crates/biome_grit_formatter/src/grit/patterns/pattern_if_else.rs index abaabd20812c..c8e6506970b9 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/pattern_if_else.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/pattern_if_else.rs @@ -1,10 +1,35 @@ use crate::prelude::*; -use biome_grit_syntax::GritPatternIfElse; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPatternIfElse, GritPatternIfElseFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPatternIfElse; impl FormatNodeRule for FormatGritPatternIfElse { fn fmt_fields(&self, node: &GritPatternIfElse, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPatternIfElseFields { + r_paren_token, + l_paren_token, + else_clause, + if_predicate, + if_token, + then_pattern, + } = node.as_fields(); + + write!( + f, + [ + if_token.format(), + space(), + l_paren_token.format(), + if_predicate.format(), + r_paren_token.format(), + then_pattern.format() + ] + )?; + + if let Some(else_clause) = else_clause { + write!(f, [space(), else_clause.format()])?; + } + + Ok(()) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/pattern_limit.rs b/crates/biome_grit_formatter/src/grit/patterns/pattern_limit.rs index ed1a4d8de56e..891fc43eea17 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/pattern_limit.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/pattern_limit.rs @@ -1,10 +1,25 @@ use crate::prelude::*; -use biome_grit_syntax::GritPatternLimit; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPatternLimit, GritPatternLimitFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPatternLimit; impl FormatNodeRule for FormatGritPatternLimit { fn fmt_fields(&self, node: &GritPatternLimit, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPatternLimitFields { + limit_token, + limit, + pattern, + } = node.as_fields(); + + write!( + f, + [ + pattern.format(), + space(), + limit_token.format(), + space(), + limit.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/pattern_maybe.rs b/crates/biome_grit_formatter/src/grit/patterns/pattern_maybe.rs index 39cd74f5b900..1029a0c9dae8 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/pattern_maybe.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/pattern_maybe.rs @@ -1,10 +1,15 @@ use crate::prelude::*; -use biome_grit_syntax::GritPatternMaybe; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPatternMaybe, GritPatternMaybeFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPatternMaybe; impl FormatNodeRule for FormatGritPatternMaybe { fn fmt_fields(&self, node: &GritPatternMaybe, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPatternMaybeFields { + pattern, + maybe_token, + } = node.as_fields(); + + write!(f, [maybe_token.format(), space(), pattern.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/pattern_not.rs b/crates/biome_grit_formatter/src/grit/patterns/pattern_not.rs index 1c2a05713966..7726acd73cfc 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/pattern_not.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/pattern_not.rs @@ -1,10 +1,12 @@ use crate::prelude::*; -use biome_grit_syntax::GritPatternNot; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPatternNot, GritPatternNotFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPatternNot; impl FormatNodeRule for FormatGritPatternNot { fn fmt_fields(&self, node: &GritPatternNot, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPatternNotFields { pattern, not } = node.as_fields(); + + write!(f, [not.format(), pattern.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/regex_pattern.rs b/crates/biome_grit_formatter/src/grit/patterns/regex_pattern.rs index c5845bb12eac..3c4ad8d662c5 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/regex_pattern.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/regex_pattern.rs @@ -1,10 +1,12 @@ use crate::prelude::*; -use biome_grit_syntax::GritRegexPattern; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritRegexPattern, GritRegexPatternFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritRegexPattern; impl FormatNodeRule for FormatGritRegexPattern { fn fmt_fields(&self, node: &GritRegexPattern, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritRegexPatternFields { variables, regex } = node.as_fields(); + + write!(f, [regex.format(), variables.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/regex_pattern_variables.rs b/crates/biome_grit_formatter/src/grit/patterns/regex_pattern_variables.rs index a850bb0470f3..bf53e523cd03 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/regex_pattern_variables.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/regex_pattern_variables.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -use biome_grit_syntax::GritRegexPatternVariables; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritRegexPatternVariables, GritRegexPatternVariablesFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritRegexPatternVariables; impl FormatNodeRule for FormatGritRegexPatternVariables { @@ -9,6 +9,19 @@ impl FormatNodeRule for FormatGritRegexPatternVariabl node: &GritRegexPatternVariables, f: &mut GritFormatter, ) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritRegexPatternVariablesFields { + r_paren_token, + l_paren_token, + args, + } = node.as_fields(); + + write!( + f, + [ + l_paren_token.format(), + args.format(), + r_paren_token.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/sub_operation.rs b/crates/biome_grit_formatter/src/grit/patterns/sub_operation.rs index 92c3206cdc2a..e23edfcbe9a0 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/sub_operation.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/sub_operation.rs @@ -1,10 +1,25 @@ use crate::prelude::*; -use biome_grit_syntax::GritSubOperation; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritSubOperation, GritSubOperationFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritSubOperation; impl FormatNodeRule for FormatGritSubOperation { fn fmt_fields(&self, node: &GritSubOperation, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritSubOperationFields { + left, + right, + minus_token, + } = node.as_fields(); + + write!( + f, + [ + left.format(), + space(), + minus_token.format(), + space(), + right.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/predicates/bracketed_predicate.rs b/crates/biome_grit_formatter/src/grit/predicates/bracketed_predicate.rs index f695719aa418..6939a9a75fef 100644 --- a/crates/biome_grit_formatter/src/grit/predicates/bracketed_predicate.rs +++ b/crates/biome_grit_formatter/src/grit/predicates/bracketed_predicate.rs @@ -1,10 +1,22 @@ use crate::prelude::*; -use biome_grit_syntax::GritBracketedPredicate; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritBracketedPredicate, GritBracketedPredicateFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritBracketedPredicate; impl FormatNodeRule for FormatGritBracketedPredicate { fn fmt_fields(&self, node: &GritBracketedPredicate, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritBracketedPredicateFields { + l_paren_token, + predicate, + r_paren_token, + } = node.as_fields(); + write!( + f, + [ + l_paren_token.format(), + predicate.format(), + r_paren_token.format(), + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/predicates/curly_predicate_list.rs b/crates/biome_grit_formatter/src/grit/predicates/curly_predicate_list.rs deleted file mode 100644 index 0770972a6e12..000000000000 --- a/crates/biome_grit_formatter/src/grit/predicates/curly_predicate_list.rs +++ /dev/null @@ -1,24 +0,0 @@ -use crate::prelude::*; -use biome_formatter::write; -use biome_grit_syntax::{GritCurlyPredicateList, GritCurlyPredicateListFields}; - -#[derive(Debug, Clone, Default)] -pub(crate) struct FormatGritCurlyPredicateList; -impl FormatNodeRule for FormatGritCurlyPredicateList { - fn fmt_fields(&self, node: &GritCurlyPredicateList, f: &mut GritFormatter) -> FormatResult<()> { - let GritCurlyPredicateListFields { - predicates, - r_curly_token, - l_curly_token, - } = node.as_fields(); - - write!( - f, - [ - l_curly_token.format(), - predicates.format(), - r_curly_token.format() - ] - ) - } -} diff --git a/crates/biome_grit_formatter/src/grit/predicates/mod.rs b/crates/biome_grit_formatter/src/grit/predicates/mod.rs index 054a1bd5c6f5..cb98085f4ec9 100644 --- a/crates/biome_grit_formatter/src/grit/predicates/mod.rs +++ b/crates/biome_grit_formatter/src/grit/predicates/mod.rs @@ -1,12 +1,12 @@ //! This is a generated file. Don't modify it by hand! Run 'cargo codegen formatter' to re-generate the file. pub(crate) mod bracketed_predicate; -pub(crate) mod curly_predicate_list; pub(crate) mod predicate_accumulate; pub(crate) mod predicate_and; pub(crate) mod predicate_any; pub(crate) mod predicate_assignment; pub(crate) mod predicate_call; +pub(crate) mod predicate_curly; pub(crate) mod predicate_definition; pub(crate) mod predicate_else_clause; pub(crate) mod predicate_equal; diff --git a/crates/biome_grit_formatter/src/grit/predicates/predicate_curly.rs b/crates/biome_grit_formatter/src/grit/predicates/predicate_curly.rs new file mode 100644 index 000000000000..2f180322b388 --- /dev/null +++ b/crates/biome_grit_formatter/src/grit/predicates/predicate_curly.rs @@ -0,0 +1,27 @@ +use crate::prelude::*; +use biome_formatter::write; +use biome_formatter::FormatOptions; +use biome_grit_syntax::{GritPredicateCurly, GritPredicateCurlyFields}; +#[derive(Debug, Clone, Default)] +pub(crate) struct FormatGritPredicateCurly; +impl FormatNodeRule for FormatGritPredicateCurly { + fn fmt_fields(&self, node: &GritPredicateCurly, f: &mut GritFormatter) -> FormatResult<()> { + let GritPredicateCurlyFields { + l_curly_token, + predicates, + r_curly_token, + } = node.as_fields(); + + write!(f, [l_curly_token.format()])?; + let should_insert_space_around_brackets = f.options().bracket_spacing().value(); + write!( + f, + [group(&soft_block_indent_with_maybe_space( + &predicates.format(), + should_insert_space_around_brackets + ),)] + )?; + + write!(f, [r_curly_token.format()]) + } +} diff --git a/crates/biome_grit_formatter/src/grit/predicates/predicate_definition.rs b/crates/biome_grit_formatter/src/grit/predicates/predicate_definition.rs index 3f4aa712a887..d8e8557377ab 100644 --- a/crates/biome_grit_formatter/src/grit/predicates/predicate_definition.rs +++ b/crates/biome_grit_formatter/src/grit/predicates/predicate_definition.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -use biome_grit_syntax::GritPredicateDefinition; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPredicateDefinition, GritPredicateDefinitionFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPredicateDefinition; impl FormatNodeRule for FormatGritPredicateDefinition { @@ -9,6 +9,26 @@ impl FormatNodeRule for FormatGritPredicateDefinition { node: &GritPredicateDefinition, f: &mut GritFormatter, ) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPredicateDefinitionFields { + l_paren_token, + r_paren_token, + args, + body, + name, + predicate_token, + } = node.as_fields(); + write!( + f, + [ + predicate_token.format(), + space(), + name.format(), + l_paren_token.format(), + group(&args.format()), + r_paren_token.format(), + space(), + body.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/predicates/predicate_else_clause.rs b/crates/biome_grit_formatter/src/grit/predicates/predicate_else_clause.rs index 8fa86cb3d49d..fe20eb5b4a62 100644 --- a/crates/biome_grit_formatter/src/grit/predicates/predicate_else_clause.rs +++ b/crates/biome_grit_formatter/src/grit/predicates/predicate_else_clause.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -use biome_grit_syntax::GritPredicateElseClause; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPredicateElseClause, GritPredicateElseClauseFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPredicateElseClause; impl FormatNodeRule for FormatGritPredicateElseClause { @@ -9,6 +9,11 @@ impl FormatNodeRule for FormatGritPredicateElseClause { node: &GritPredicateElseClause, f: &mut GritFormatter, ) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPredicateElseClauseFields { + else_predicate, + else_token, + } = node.as_fields(); + + write!(f, [else_token.format(), space(), else_predicate.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/predicates/predicate_equal.rs b/crates/biome_grit_formatter/src/grit/predicates/predicate_equal.rs index e890306ecf87..677e6bff3e75 100644 --- a/crates/biome_grit_formatter/src/grit/predicates/predicate_equal.rs +++ b/crates/biome_grit_formatter/src/grit/predicates/predicate_equal.rs @@ -1,10 +1,26 @@ use crate::prelude::*; -use biome_grit_syntax::GritPredicateEqual; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPredicateEqual, GritPredicateEqualFields}; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPredicateEqual; impl FormatNodeRule for FormatGritPredicateEqual { fn fmt_fields(&self, node: &GritPredicateEqual, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPredicateEqualFields { + right, + left, + equality_token, + } = node.as_fields(); + + write!( + f, + [ + left.format(), + space(), + equality_token.format(), + space(), + right.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/predicates/predicate_greater.rs b/crates/biome_grit_formatter/src/grit/predicates/predicate_greater.rs index 42ea64490fbb..f2f0420851c2 100644 --- a/crates/biome_grit_formatter/src/grit/predicates/predicate_greater.rs +++ b/crates/biome_grit_formatter/src/grit/predicates/predicate_greater.rs @@ -1,10 +1,25 @@ use crate::prelude::*; -use biome_grit_syntax::GritPredicateGreater; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPredicateGreater, GritPredicateGreaterFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPredicateGreater; impl FormatNodeRule for FormatGritPredicateGreater { fn fmt_fields(&self, node: &GritPredicateGreater, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPredicateGreaterFields { + right, + left, + r_angle_token, + } = node.as_fields(); + + write!( + f, + [ + left.format(), + space(), + r_angle_token.format(), + space(), + right.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/predicates/predicate_greater_equal.rs b/crates/biome_grit_formatter/src/grit/predicates/predicate_greater_equal.rs index 9e522b07daa9..ff155ea9273e 100644 --- a/crates/biome_grit_formatter/src/grit/predicates/predicate_greater_equal.rs +++ b/crates/biome_grit_formatter/src/grit/predicates/predicate_greater_equal.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -use biome_grit_syntax::GritPredicateGreaterEqual; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPredicateGreaterEqual, GritPredicateGreaterEqualFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPredicateGreaterEqual; impl FormatNodeRule for FormatGritPredicateGreaterEqual { @@ -9,6 +9,21 @@ impl FormatNodeRule for FormatGritPredicateGreaterEqu node: &GritPredicateGreaterEqual, f: &mut GritFormatter, ) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPredicateGreaterEqualFields { + right, + left, + greater_than_equal_token, + } = node.as_fields(); + + write!( + f, + [ + left.format(), + space(), + greater_than_equal_token.format(), + space(), + right.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/predicates/predicate_if_else.rs b/crates/biome_grit_formatter/src/grit/predicates/predicate_if_else.rs index 6488b650584d..dd87a03f3239 100644 --- a/crates/biome_grit_formatter/src/grit/predicates/predicate_if_else.rs +++ b/crates/biome_grit_formatter/src/grit/predicates/predicate_if_else.rs @@ -1,10 +1,35 @@ use crate::prelude::*; -use biome_grit_syntax::GritPredicateIfElse; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPredicateIfElse, GritPredicateIfElseFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPredicateIfElse; impl FormatNodeRule for FormatGritPredicateIfElse { fn fmt_fields(&self, node: &GritPredicateIfElse, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPredicateIfElseFields { + r_paren_token, + l_paren_token, + else_clause, + if_predicate, + if_token, + then_predicate, + } = node.as_fields(); + + write!( + f, + [ + if_token.format(), + space(), + l_paren_token.format(), + if_predicate.format(), + r_paren_token.format(), + then_predicate.format() + ] + )?; + + if let Some(else_clause) = else_clause { + write!(f, [space(), else_clause.format()])?; + } + + Ok(()) } } diff --git a/crates/biome_grit_formatter/src/grit/predicates/predicate_less.rs b/crates/biome_grit_formatter/src/grit/predicates/predicate_less.rs index 480d8b99403f..b1c8e744cf97 100644 --- a/crates/biome_grit_formatter/src/grit/predicates/predicate_less.rs +++ b/crates/biome_grit_formatter/src/grit/predicates/predicate_less.rs @@ -1,10 +1,25 @@ use crate::prelude::*; -use biome_grit_syntax::GritPredicateLess; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPredicateLess, GritPredicateLessFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPredicateLess; impl FormatNodeRule for FormatGritPredicateLess { fn fmt_fields(&self, node: &GritPredicateLess, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPredicateLessFields { + right, + left, + l_angle_token, + } = node.as_fields(); + + write!( + f, + [ + left.format(), + space(), + l_angle_token.format(), + space(), + right.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/predicates/predicate_less_equal.rs b/crates/biome_grit_formatter/src/grit/predicates/predicate_less_equal.rs index 334d06775d65..91b1c0ec3ac0 100644 --- a/crates/biome_grit_formatter/src/grit/predicates/predicate_less_equal.rs +++ b/crates/biome_grit_formatter/src/grit/predicates/predicate_less_equal.rs @@ -1,10 +1,25 @@ use crate::prelude::*; -use biome_grit_syntax::GritPredicateLessEqual; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPredicateLessEqual, GritPredicateLessEqualFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPredicateLessEqual; impl FormatNodeRule for FormatGritPredicateLessEqual { fn fmt_fields(&self, node: &GritPredicateLessEqual, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPredicateLessEqualFields { + right, + left, + less_than_equal_token, + } = node.as_fields(); + + write!( + f, + [ + left.format(), + space(), + less_than_equal_token.format(), + space(), + right.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/predicates/predicate_maybe.rs b/crates/biome_grit_formatter/src/grit/predicates/predicate_maybe.rs index bb8beb4b4ef1..d92633097c99 100644 --- a/crates/biome_grit_formatter/src/grit/predicates/predicate_maybe.rs +++ b/crates/biome_grit_formatter/src/grit/predicates/predicate_maybe.rs @@ -1,10 +1,15 @@ use crate::prelude::*; -use biome_grit_syntax::GritPredicateMaybe; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPredicateMaybe, GritPredicateMaybeFields}; +use std::fmt::Debug; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPredicateMaybe; impl FormatNodeRule for FormatGritPredicateMaybe { fn fmt_fields(&self, node: &GritPredicateMaybe, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPredicateMaybeFields { + predicate, + maybe_token, + } = node.as_fields(); + write!(f, [maybe_token.format(), space(), predicate.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/predicates/predicate_not.rs b/crates/biome_grit_formatter/src/grit/predicates/predicate_not.rs index 2f8be0c994d5..3cb95f850357 100644 --- a/crates/biome_grit_formatter/src/grit/predicates/predicate_not.rs +++ b/crates/biome_grit_formatter/src/grit/predicates/predicate_not.rs @@ -1,10 +1,12 @@ use crate::prelude::*; -use biome_grit_syntax::GritPredicateNot; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPredicateNot, GritPredicateNotFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPredicateNot; impl FormatNodeRule for FormatGritPredicateNot { fn fmt_fields(&self, node: &GritPredicateNot, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPredicateNotFields { not, predicate } = node.as_fields(); + + write!(f, [not.format(), predicate.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/predicates/predicate_not_equal.rs b/crates/biome_grit_formatter/src/grit/predicates/predicate_not_equal.rs index e5c1fec59721..de78bd594e38 100644 --- a/crates/biome_grit_formatter/src/grit/predicates/predicate_not_equal.rs +++ b/crates/biome_grit_formatter/src/grit/predicates/predicate_not_equal.rs @@ -1,10 +1,25 @@ use crate::prelude::*; -use biome_grit_syntax::GritPredicateNotEqual; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPredicateNotEqual, GritPredicateNotEqualFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPredicateNotEqual; impl FormatNodeRule for FormatGritPredicateNotEqual { fn fmt_fields(&self, node: &GritPredicateNotEqual, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPredicateNotEqualFields { + right, + left, + inequality_token, + } = node.as_fields(); + + write!( + f, + [ + left.format(), + space(), + inequality_token.format(), + space(), + right.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/tests/quick_test.rs b/crates/biome_grit_formatter/tests/quick_test.rs index 9098cf27522a..4372e070aadd 100644 --- a/crates/biome_grit_formatter/tests/quick_test.rs +++ b/crates/biome_grit_formatter/tests/quick_test.rs @@ -12,11 +12,8 @@ mod language { #[test] // use this test check if your snippet prints as you wish, without using a snapshot fn quick_test() { - let src = r#"`function $functionName($_) {$_}` as $f where { - $functionName <: r"test.*", - $f => ., - $new_file_name = `$functionName.test.js`, - $new_files += file(name = $new_file_name, body = $f) + let src = r#"function lines($string) { + return split($string, separator=`\n`) } "#; diff --git a/crates/biome_grit_formatter/tests/specs/grit/function_definition.grit b/crates/biome_grit_formatter/tests/specs/grit/function_definition.grit new file mode 100644 index 000000000000..b2adf02f3ad9 --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/function_definition.grit @@ -0,0 +1,25 @@ +/ define a lines function +function lines($string) { + return split($string, separator=`\n`) +} + +// Define a my_todo function +function my_todo($target, $message) { + if($message <: undefined) { + $message = "This requires manual intervention." + }, + $lines = lines(string = $message), + $lines <: some bubble($result) $x where { + if ($result <: undefined) { + $result = `// TODO: $x` + } else { + $result += `\n// $x` + } + }, + $target_lines = lines(string = $target), + $target_lines <: some bubble($result) $x where { $result += `\n// $x` }, + return $result, +} + +// Use the my_todo function +`module.exports = $_` as $x => my_todo(target=$x, message=`Fix this\nAnd that`) diff --git a/crates/biome_grit_formatter/tests/specs/grit/function_definition.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/function_definition.grit.snap new file mode 100644 index 000000000000..15fc7ad76e19 --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/function_definition.grit.snap @@ -0,0 +1,79 @@ +--- +source: crates/biome_formatter_test/src/snapshot_builder.rs +info: grit/function_definition.grit +snapshot_kind: text +--- +# Input + +```grit +/ define a lines function +function lines($string) { + return split($string, separator=`\n`) +} + +// Define a my_todo function +function my_todo($target, $message) { + if($message <: undefined) { + $message = "This requires manual intervention." + }, + $lines = lines(string = $message), + $lines <: some bubble($result) $x where { + if ($result <: undefined) { + $result = `// TODO: $x` + } else { + $result += `\n// $x` + } + }, + $target_lines = lines(string = $target), + $target_lines <: some bubble($result) $x where { $result += `\n// $x` }, + return $result, +} + +// Use the my_todo function +`module.exports = $_` as $x => my_todo(target=$x, message=`Fix this\nAnd that`) + +``` + + +============================= + +# Outputs + +## Output 1 + +----- +Indent style: Tab +Indent width: 2 +Line ending: LF +Line width: 80 +Attribute Position: Auto +----- + +```grit +/ define a lines function +function lines($string) { return split($string, separator = `\n`) } + +// Define a my_todo function +function my_todo($target, $message) { + if ($message <: undefined) { + $message = "This requires manual intervention." + }, + $lines = lines(string = $message), + $lines <: some bubble($result) $x where { + if ($result <: undefined) { $result = `// TODO: $x` } else { + $result += `\n// $x` + } + }, + $target_lines = lines(string = $target), + $target_lines <: some bubble($result) $x where { $result += `\n// $x` }, + return $result +} + +// Use the my_todo function +`module.exports = $_` as $x => my_todo(target = $x, message = `Fix this\nAnd that`) +``` + +# Lines exceeding max width of 80 characters +``` + 21: `module.exports = $_` as $x => my_todo(target = $x, message = `Fix this\nAnd that`) +``` diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/contains.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/patterns/contains.grit.snap index 3051011f4e39..334914d55204 100644 --- a/crates/biome_grit_formatter/tests/specs/grit/patterns/contains.grit.snap +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/contains.grit.snap @@ -34,9 +34,3 @@ Attribute Position: Auto $content <: contains `secret` until `sanitized($_)` } ``` - - - -## Unimplemented nodes/tokens - -" until `sanitized($_)`" => 123..145 diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/create_new_files.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/patterns/create_new_files.grit.snap index 1c9324644451..79721ef99317 100644 --- a/crates/biome_grit_formatter/tests/specs/grit/patterns/create_new_files.grit.snap +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/create_new_files.grit.snap @@ -32,9 +32,3 @@ Attribute Position: Auto $new_files += file(name = $new_file_name, body = $f) } ``` - - - -## Unimplemented nodes/tokens - -" r\"test.*" => 65..74 diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/if_else.grit b/crates/biome_grit_formatter/tests/specs/grit/patterns/if_else.grit new file mode 100644 index 000000000000..97bc4bb4f96f --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/if_else.grit @@ -0,0 +1,7 @@ +`$method('$message')` where { + if ($message <: r"Hello, .*!") { + $method => `console.info` + } else { + $method => `console.warn` + } +} diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/if_else.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/patterns/if_else.grit.snap new file mode 100644 index 000000000000..af79d7af0990 --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/if_else.grit.snap @@ -0,0 +1,40 @@ +--- +source: crates/biome_formatter_test/src/snapshot_builder.rs +info: grit/patterns/if_else.grit +snapshot_kind: text +--- +# Input + +```grit +`$method('$message')` where { + if ($message <: r"Hello, .*!") { + $method => `console.info` + } else { + $method => `console.warn` + } +} + +``` + + +============================= + +# Outputs + +## Output 1 + +----- +Indent style: Tab +Indent width: 2 +Line ending: LF +Line width: 80 +Attribute Position: Auto +----- + +```grit +`$method('$message')` where { + if ($message <: r"Hello, .*!") { $method => `console.info` } else { + $method => `console.warn` + } +} +``` diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/regex.grit b/crates/biome_grit_formatter/tests/specs/grit/patterns/regex.grit new file mode 100644 index 000000000000..2a143f1b67ca --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/regex.grit @@ -0,0 +1 @@ +r"test\\.*[^'\"]" diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/regex.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/patterns/regex.grit.snap new file mode 100644 index 000000000000..3c50840a9a44 --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/regex.grit.snap @@ -0,0 +1,30 @@ +--- +source: crates/biome_formatter_test/src/snapshot_builder.rs +info: grit/patterns/regex.grit +snapshot_kind: text +--- +# Input + +```grit +r"test\\.*[^'\"]" + +``` + + +============================= + +# Outputs + +## Output 1 + +----- +Indent style: Tab +Indent width: 2 +Line ending: LF +Line width: 80 +Attribute Position: Auto +----- + +```grit +r"test\\.*[^'\"]" +``` diff --git a/crates/biome_grit_formatter/tests/specs/grit/predicates/and.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/predicates/and.grit.snap index f263816a8303..3f06a6204199 100644 --- a/crates/biome_grit_formatter/tests/specs/grit/predicates/and.grit.snap +++ b/crates/biome_grit_formatter/tests/specs/grit/predicates/and.grit.snap @@ -34,9 +34,3 @@ Attribute Position: Auto and { $message <: r"Hello, .*!", $method <: `log` } } ``` - - - -## Unimplemented nodes/tokens - -" r\"Hello, .*!" => 88..101 diff --git a/crates/biome_grit_formatter/tests/specs/grit/predicates/any.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/predicates/any.grit.snap index 6f043729825f..012ad155590e 100644 --- a/crates/biome_grit_formatter/tests/specs/grit/predicates/any.grit.snap +++ b/crates/biome_grit_formatter/tests/specs/grit/predicates/any.grit.snap @@ -34,9 +34,3 @@ Attribute Position: Auto any { $message <: r"Hello, .*!", $method <: `log` } } ``` - - - -## Unimplemented nodes/tokens - -" r\"Hello, .*!" => 88..101 diff --git a/crates/biome_grit_formatter/tests/specs/grit/sequential.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/sequential.grit.snap index 06e860fbe631..58f2c76dd59f 100644 --- a/crates/biome_grit_formatter/tests/specs/grit/sequential.grit.snap +++ b/crates/biome_grit_formatter/tests/specs/grit/sequential.grit.snap @@ -1,6 +1,7 @@ --- source: crates/biome_formatter_test/src/snapshot_builder.rs info: grit/sequential.grit +snapshot_kind: text --- # Input @@ -29,18 +30,13 @@ Attribute Position: Auto ```grit sequential { - bubble file($body) where $body <: contains `console.log($message)` => `console.warn($message)`, - bubble file($body) where $body <: contains `console.warn($message)` => `console.info($message)` + bubble file($body) where $body <: contains `console.log($message)` => `console.warn($message)`, + bubble file($body) where $body <: contains `console.warn($message)` => `console.info($message)` } ``` - - -## Unimplemented nodes/tokens - -"sequential {\n bubble file($body) where $body <: contains `console.log($message)` => `console.warn($message)`,\n bubble file($body) where $body <: contains `console.warn($message)` => `console.info($message)`\n}" => 0..214 # Lines exceeding max width of 80 characters ``` - 2: bubble file($body) where $body <: contains `console.log($message)` => `console.warn($message)`, - 3: bubble file($body) where $body <: contains `console.warn($message)` => `console.info($message)` + 2: bubble file($body) where $body <: contains `console.log($message)` => `console.warn($message)`, + 3: bubble file($body) where $body <: contains `console.warn($message)` => `console.info($message)` ``` diff --git a/crates/biome_grit_parser/src/parser/definitions.rs b/crates/biome_grit_parser/src/parser/definitions.rs index e186ce76e5fb..81d0d034410d 100644 --- a/crates/biome_grit_parser/src/parser/definitions.rs +++ b/crates/biome_grit_parser/src/parser/definitions.rs @@ -103,7 +103,7 @@ fn parse_curly_predicate_list(p: &mut GritParser) -> ParsedSyntax { PredicateList.parse_list(p); p.expect(T!['}']); - Present(m.complete(p, GRIT_CURLY_PREDICATE_LIST)) + Present(m.complete(p, GRIT_PREDICATE_CURLY)) } #[inline] diff --git a/crates/biome_grit_parser/tests/grit_test_suite/err/invalid_predicate.grit.snap b/crates/biome_grit_parser/tests/grit_test_suite/err/invalid_predicate.grit.snap index f6f797dd6576..3b8373af248a 100644 --- a/crates/biome_grit_parser/tests/grit_test_suite/err/invalid_predicate.grit.snap +++ b/crates/biome_grit_parser/tests/grit_test_suite/err/invalid_predicate.grit.snap @@ -29,7 +29,7 @@ GritRoot { l_paren_token: L_PAREN@33..34 "(" [] [], args: GritVariableList [], r_paren_token: R_PAREN@34..36 ")" [] [Whitespace(" ")], - body: GritCurlyPredicateList { + body: GritPredicateCurly { l_curly_token: L_CURLY@36..37 "{" [] [], predicates: GritPredicateList [ GritBogusPredicate { @@ -77,7 +77,7 @@ GritRoot { 2: L_PAREN@33..34 "(" [] [] 3: GRIT_VARIABLE_LIST@34..34 4: R_PAREN@34..36 ")" [] [Whitespace(" ")] - 5: GRIT_CURLY_PREDICATE_LIST@36..74 + 5: GRIT_PREDICATE_CURLY@36..74 0: L_CURLY@36..37 "{" [] [] 1: GRIT_PREDICATE_LIST@37..72 0: GRIT_BOGUS_PREDICATE@37..47 diff --git a/crates/biome_grit_parser/tests/grit_test_suite/err/invalid_predicate_subject.grit.snap b/crates/biome_grit_parser/tests/grit_test_suite/err/invalid_predicate_subject.grit.snap index e751c918ee6e..4ae6b8d8a91f 100644 --- a/crates/biome_grit_parser/tests/grit_test_suite/err/invalid_predicate_subject.grit.snap +++ b/crates/biome_grit_parser/tests/grit_test_suite/err/invalid_predicate_subject.grit.snap @@ -29,7 +29,7 @@ GritRoot { l_paren_token: L_PAREN@33..34 "(" [] [], args: GritVariableList [], r_paren_token: R_PAREN@34..36 ")" [] [Whitespace(" ")], - body: GritCurlyPredicateList { + body: GritPredicateCurly { l_curly_token: L_CURLY@36..37 "{" [] [], predicates: GritPredicateList [ GritBogusPredicate { @@ -99,7 +99,7 @@ GritRoot { 2: L_PAREN@33..34 "(" [] [] 3: GRIT_VARIABLE_LIST@34..34 4: R_PAREN@34..36 ")" [] [Whitespace(" ")] - 5: GRIT_CURLY_PREDICATE_LIST@36..114 + 5: GRIT_PREDICATE_CURLY@36..114 0: L_CURLY@36..37 "{" [] [] 1: GRIT_PREDICATE_LIST@37..112 0: GRIT_BOGUS_PREDICATE@37..68 diff --git a/crates/biome_grit_parser/tests/grit_test_suite/ok/function_definition.grit.snap b/crates/biome_grit_parser/tests/grit_test_suite/ok/function_definition.grit.snap index 7dc74d5af3d2..2d1653ca2b68 100644 --- a/crates/biome_grit_parser/tests/grit_test_suite/ok/function_definition.grit.snap +++ b/crates/biome_grit_parser/tests/grit_test_suite/ok/function_definition.grit.snap @@ -53,7 +53,7 @@ GritRoot { }, ], r_paren_token: R_PAREN@49..51 ")" [] [Whitespace(" ")], - body: GritCurlyPredicateList { + body: GritPredicateCurly { l_curly_token: L_CURLY@51..52 "{" [] [], predicates: GritPredicateList [ GritPredicateReturn { @@ -104,7 +104,7 @@ GritRoot { }, ], r_paren_token: R_PAREN@161..163 ")" [] [Whitespace(" ")], - body: GritCurlyPredicateList { + body: GritPredicateCurly { l_curly_token: L_CURLY@163..164 "{" [] [], predicates: GritPredicateList [ GritPredicateIfElse { @@ -402,7 +402,7 @@ GritRoot { 0: GRIT_VARIABLE@42..49 0: GRIT_VARIABLE@42..49 "$string" [] [] 4: R_PAREN@49..51 ")" [] [Whitespace(" ")] - 5: GRIT_CURLY_PREDICATE_LIST@51..96 + 5: GRIT_PREDICATE_CURLY@51..96 0: L_CURLY@51..52 "{" [] [] 1: GRIT_PREDICATE_LIST@52..94 0: GRIT_PREDICATE_RETURN@52..94 @@ -437,7 +437,7 @@ GritRoot { 2: GRIT_VARIABLE@153..161 0: GRIT_VARIABLE@153..161 "$message" [] [] 4: R_PAREN@161..163 ")" [] [Whitespace(" ")] - 5: GRIT_CURLY_PREDICATE_LIST@163..617 + 5: GRIT_PREDICATE_CURLY@163..617 0: L_CURLY@163..164 "{" [] [] 1: GRIT_PREDICATE_LIST@164..615 0: GRIT_PREDICATE_IF_ELSE@164..255 diff --git a/crates/biome_grit_parser/tests/grit_test_suite/ok/predicate_definition.grit.snap b/crates/biome_grit_parser/tests/grit_test_suite/ok/predicate_definition.grit.snap index ec0401c8c830..886f880ee7b7 100644 --- a/crates/biome_grit_parser/tests/grit_test_suite/ok/predicate_definition.grit.snap +++ b/crates/biome_grit_parser/tests/grit_test_suite/ok/predicate_definition.grit.snap @@ -30,7 +30,7 @@ GritRoot { l_paren_token: L_PAREN@33..34 "(" [] [], args: GritVariableList [], r_paren_token: R_PAREN@34..36 ")" [] [Whitespace(" ")], - body: GritCurlyPredicateList { + body: GritPredicateCurly { l_curly_token: L_CURLY@36..37 "{" [] [], predicates: GritPredicateList [ GritPredicateMatch { @@ -105,7 +105,7 @@ GritRoot { 2: L_PAREN@33..34 "(" [] [] 3: GRIT_VARIABLE_LIST@34..34 4: R_PAREN@34..36 ")" [] [Whitespace(" ")] - 5: GRIT_CURLY_PREDICATE_LIST@36..71 + 5: GRIT_PREDICATE_CURLY@36..71 0: L_CURLY@36..37 "{" [] [] 1: GRIT_PREDICATE_LIST@37..69 0: GRIT_PREDICATE_MATCH@37..69 diff --git a/crates/biome_grit_syntax/src/generated/kind.rs b/crates/biome_grit_syntax/src/generated/kind.rs index 634af0c07d7b..826891d45a08 100644 --- a/crates/biome_grit_syntax/src/generated/kind.rs +++ b/crates/biome_grit_syntax/src/generated/kind.rs @@ -162,7 +162,7 @@ pub enum GritSyntaxKind { GRIT_PATTERN_DEFINITION, GRIT_PATTERN_ARG_LIST, GRIT_PREDICATE_LIST, - GRIT_CURLY_PREDICATE_LIST, + GRIT_PREDICATE_CURLY, GRIT_PREDICATE_DEFINITION, GRIT_FUNCTION_DEFINITION, GRIT_PREDICATE_NOT, @@ -243,7 +243,6 @@ impl GritSyntaxKind { | GRIT_LIST_PATTERN_LIST | GRIT_PATTERN_ARG_LIST | GRIT_PREDICATE_LIST - | GRIT_CURLY_PREDICATE_LIST | GRIT_VARIABLE_LIST => true, _ => false, } diff --git a/crates/biome_grit_syntax/src/generated/macros.rs b/crates/biome_grit_syntax/src/generated/macros.rs index 23b429df19b7..5de9f8c84e0a 100644 --- a/crates/biome_grit_syntax/src/generated/macros.rs +++ b/crates/biome_grit_syntax/src/generated/macros.rs @@ -61,10 +61,6 @@ macro_rules! map_syntax_node { let $pattern = unsafe { $crate::GritCurlyPattern::new_unchecked(node) }; $body } - $crate::GritSyntaxKind::GRIT_CURLY_PREDICATE_LIST => { - let $pattern = unsafe { $crate::GritCurlyPredicateList::new_unchecked(node) }; - $body - } $crate::GritSyntaxKind::GRIT_DIV_OPERATION => { let $pattern = unsafe { $crate::GritDivOperation::new_unchecked(node) }; $body @@ -272,6 +268,10 @@ macro_rules! map_syntax_node { let $pattern = unsafe { $crate::GritPredicateCall::new_unchecked(node) }; $body } + $crate::GritSyntaxKind::GRIT_PREDICATE_CURLY => { + let $pattern = unsafe { $crate::GritPredicateCurly::new_unchecked(node) }; + $body + } $crate::GritSyntaxKind::GRIT_PREDICATE_DEFINITION => { let $pattern = unsafe { $crate::GritPredicateDefinition::new_unchecked(node) }; $body diff --git a/crates/biome_grit_syntax/src/generated/nodes.rs b/crates/biome_grit_syntax/src/generated/nodes.rs index 5ee92e280e88..c3df73cb4057 100644 --- a/crates/biome_grit_syntax/src/generated/nodes.rs +++ b/crates/biome_grit_syntax/src/generated/nodes.rs @@ -478,51 +478,6 @@ pub struct GritCurlyPatternFields { pub r_curly_token: SyntaxResult, } #[derive(Clone, PartialEq, Eq, Hash)] -pub struct GritCurlyPredicateList { - pub(crate) syntax: SyntaxNode, -} -impl GritCurlyPredicateList { - #[doc = r" Create an AstNode from a SyntaxNode without checking its kind"] - #[doc = r""] - #[doc = r" # Safety"] - #[doc = r" This function must be guarded with a call to [AstNode::can_cast]"] - #[doc = r" or a match on [SyntaxNode::kind]"] - #[inline] - pub const unsafe fn new_unchecked(syntax: SyntaxNode) -> Self { - Self { syntax } - } - pub fn as_fields(&self) -> GritCurlyPredicateListFields { - GritCurlyPredicateListFields { - l_curly_token: self.l_curly_token(), - predicates: self.predicates(), - r_curly_token: self.r_curly_token(), - } - } - pub fn l_curly_token(&self) -> SyntaxResult { - support::required_token(&self.syntax, 0usize) - } - pub fn predicates(&self) -> GritPredicateList { - support::list(&self.syntax, 1usize) - } - pub fn r_curly_token(&self) -> SyntaxResult { - support::required_token(&self.syntax, 2usize) - } -} -impl Serialize for GritCurlyPredicateList { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - self.as_fields().serialize(serializer) - } -} -#[derive(Serialize)] -pub struct GritCurlyPredicateListFields { - pub l_curly_token: SyntaxResult, - pub predicates: GritPredicateList, - pub r_curly_token: SyntaxResult, -} -#[derive(Clone, PartialEq, Eq, Hash)] pub struct GritDivOperation { pub(crate) syntax: SyntaxNode, } @@ -806,7 +761,7 @@ impl GritFunctionDefinition { pub fn r_paren_token(&self) -> SyntaxResult { support::required_token(&self.syntax, 4usize) } - pub fn body(&self) -> SyntaxResult { + pub fn body(&self) -> SyntaxResult { support::required_node(&self.syntax, 5usize) } } @@ -825,7 +780,7 @@ pub struct GritFunctionDefinitionFields { pub l_paren_token: SyntaxResult, pub args: GritVariableList, pub r_paren_token: SyntaxResult, - pub body: SyntaxResult, + pub body: SyntaxResult, } #[derive(Clone, PartialEq, Eq, Hash)] pub struct GritIntLiteral { @@ -2813,6 +2768,51 @@ pub struct GritPredicateCallFields { pub r_paren_token: SyntaxResult, } #[derive(Clone, PartialEq, Eq, Hash)] +pub struct GritPredicateCurly { + pub(crate) syntax: SyntaxNode, +} +impl GritPredicateCurly { + #[doc = r" Create an AstNode from a SyntaxNode without checking its kind"] + #[doc = r""] + #[doc = r" # Safety"] + #[doc = r" This function must be guarded with a call to [AstNode::can_cast]"] + #[doc = r" or a match on [SyntaxNode::kind]"] + #[inline] + pub const unsafe fn new_unchecked(syntax: SyntaxNode) -> Self { + Self { syntax } + } + pub fn as_fields(&self) -> GritPredicateCurlyFields { + GritPredicateCurlyFields { + l_curly_token: self.l_curly_token(), + predicates: self.predicates(), + r_curly_token: self.r_curly_token(), + } + } + pub fn l_curly_token(&self) -> SyntaxResult { + support::required_token(&self.syntax, 0usize) + } + pub fn predicates(&self) -> GritPredicateList { + support::list(&self.syntax, 1usize) + } + pub fn r_curly_token(&self) -> SyntaxResult { + support::required_token(&self.syntax, 2usize) + } +} +impl Serialize for GritPredicateCurly { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + self.as_fields().serialize(serializer) + } +} +#[derive(Serialize)] +pub struct GritPredicateCurlyFields { + pub l_curly_token: SyntaxResult, + pub predicates: GritPredicateList, + pub r_curly_token: SyntaxResult, +} +#[derive(Clone, PartialEq, Eq, Hash)] pub struct GritPredicateDefinition { pub(crate) syntax: SyntaxNode, } @@ -2851,7 +2851,7 @@ impl GritPredicateDefinition { pub fn r_paren_token(&self) -> SyntaxResult { support::required_token(&self.syntax, 4usize) } - pub fn body(&self) -> SyntaxResult { + pub fn body(&self) -> SyntaxResult { support::required_node(&self.syntax, 5usize) } } @@ -2870,7 +2870,7 @@ pub struct GritPredicateDefinitionFields { pub l_paren_token: SyntaxResult, pub args: GritVariableList, pub r_paren_token: SyntaxResult, - pub body: SyntaxResult, + pub body: SyntaxResult, } #[derive(Clone, PartialEq, Eq, Hash)] pub struct GritPredicateElseClause { @@ -5495,52 +5495,6 @@ impl From for SyntaxElement { n.syntax.into() } } -impl AstNode for GritCurlyPredicateList { - type Language = Language; - const KIND_SET: SyntaxKindSet = - SyntaxKindSet::from_raw(RawSyntaxKind(GRIT_CURLY_PREDICATE_LIST as u16)); - fn can_cast(kind: SyntaxKind) -> bool { - kind == GRIT_CURLY_PREDICATE_LIST - } - fn cast(syntax: SyntaxNode) -> Option { - if Self::can_cast(syntax.kind()) { - Some(Self { syntax }) - } else { - None - } - } - fn syntax(&self) -> &SyntaxNode { - &self.syntax - } - fn into_syntax(self) -> SyntaxNode { - self.syntax - } -} -impl std::fmt::Debug for GritCurlyPredicateList { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("GritCurlyPredicateList") - .field( - "l_curly_token", - &support::DebugSyntaxResult(self.l_curly_token()), - ) - .field("predicates", &self.predicates()) - .field( - "r_curly_token", - &support::DebugSyntaxResult(self.r_curly_token()), - ) - .finish() - } -} -impl From for SyntaxNode { - fn from(n: GritCurlyPredicateList) -> SyntaxNode { - n.syntax - } -} -impl From for SyntaxElement { - fn from(n: GritCurlyPredicateList) -> SyntaxElement { - n.syntax.into() - } -} impl AstNode for GritDivOperation { type Language = Language; const KIND_SET: SyntaxKindSet = @@ -7805,6 +7759,52 @@ impl From for SyntaxElement { n.syntax.into() } } +impl AstNode for GritPredicateCurly { + type Language = Language; + const KIND_SET: SyntaxKindSet = + SyntaxKindSet::from_raw(RawSyntaxKind(GRIT_PREDICATE_CURLY as u16)); + fn can_cast(kind: SyntaxKind) -> bool { + kind == GRIT_PREDICATE_CURLY + } + fn cast(syntax: SyntaxNode) -> Option { + if Self::can_cast(syntax.kind()) { + Some(Self { syntax }) + } else { + None + } + } + fn syntax(&self) -> &SyntaxNode { + &self.syntax + } + fn into_syntax(self) -> SyntaxNode { + self.syntax + } +} +impl std::fmt::Debug for GritPredicateCurly { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("GritPredicateCurly") + .field( + "l_curly_token", + &support::DebugSyntaxResult(self.l_curly_token()), + ) + .field("predicates", &self.predicates()) + .field( + "r_curly_token", + &support::DebugSyntaxResult(self.r_curly_token()), + ) + .finish() + } +} +impl From for SyntaxNode { + fn from(n: GritPredicateCurly) -> SyntaxNode { + n.syntax + } +} +impl From for SyntaxElement { + fn from(n: GritPredicateCurly) -> SyntaxElement { + n.syntax.into() + } +} impl AstNode for GritPredicateDefinition { type Language = Language; const KIND_SET: SyntaxKindSet = @@ -11458,11 +11458,6 @@ impl std::fmt::Display for GritCurlyPattern { std::fmt::Display::fmt(self.syntax(), f) } } -impl std::fmt::Display for GritCurlyPredicateList { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - std::fmt::Display::fmt(self.syntax(), f) - } -} impl std::fmt::Display for GritDivOperation { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) @@ -11718,6 +11713,11 @@ impl std::fmt::Display for GritPredicateCall { std::fmt::Display::fmt(self.syntax(), f) } } +impl std::fmt::Display for GritPredicateCurly { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(self.syntax(), f) + } +} impl std::fmt::Display for GritPredicateDefinition { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) diff --git a/crates/biome_grit_syntax/src/generated/nodes_mut.rs b/crates/biome_grit_syntax/src/generated/nodes_mut.rs index 1a9821ff5d3d..d5152f505e5a 100644 --- a/crates/biome_grit_syntax/src/generated/nodes_mut.rs +++ b/crates/biome_grit_syntax/src/generated/nodes_mut.rs @@ -175,26 +175,6 @@ impl GritCurlyPattern { ) } } -impl GritCurlyPredicateList { - pub fn with_l_curly_token(self, element: SyntaxToken) -> Self { - Self::unwrap_cast( - self.syntax - .splice_slots(0usize..=0usize, once(Some(element.into()))), - ) - } - pub fn with_predicates(self, element: GritPredicateList) -> Self { - Self::unwrap_cast( - self.syntax - .splice_slots(1usize..=1usize, once(Some(element.into_syntax().into()))), - ) - } - pub fn with_r_curly_token(self, element: SyntaxToken) -> Self { - Self::unwrap_cast( - self.syntax - .splice_slots(2usize..=2usize, once(Some(element.into()))), - ) - } -} impl GritDivOperation { pub fn with_left(self, element: AnyGritPattern) -> Self { Self::unwrap_cast( @@ -316,7 +296,7 @@ impl GritFunctionDefinition { .splice_slots(4usize..=4usize, once(Some(element.into()))), ) } - pub fn with_body(self, element: GritCurlyPredicateList) -> Self { + pub fn with_body(self, element: GritPredicateCurly) -> Self { Self::unwrap_cast( self.syntax .splice_slots(5usize..=5usize, once(Some(element.into_syntax().into()))), @@ -1209,6 +1189,26 @@ impl GritPredicateCall { ) } } +impl GritPredicateCurly { + pub fn with_l_curly_token(self, element: SyntaxToken) -> Self { + Self::unwrap_cast( + self.syntax + .splice_slots(0usize..=0usize, once(Some(element.into()))), + ) + } + pub fn with_predicates(self, element: GritPredicateList) -> Self { + Self::unwrap_cast( + self.syntax + .splice_slots(1usize..=1usize, once(Some(element.into_syntax().into()))), + ) + } + pub fn with_r_curly_token(self, element: SyntaxToken) -> Self { + Self::unwrap_cast( + self.syntax + .splice_slots(2usize..=2usize, once(Some(element.into()))), + ) + } +} impl GritPredicateDefinition { pub fn with_predicate_token(self, element: SyntaxToken) -> Self { Self::unwrap_cast( @@ -1240,7 +1240,7 @@ impl GritPredicateDefinition { .splice_slots(4usize..=4usize, once(Some(element.into()))), ) } - pub fn with_body(self, element: GritCurlyPredicateList) -> Self { + pub fn with_body(self, element: GritPredicateCurly) -> Self { Self::unwrap_cast( self.syntax .splice_slots(5usize..=5usize, once(Some(element.into_syntax().into()))), diff --git a/xtask/codegen/gritql.ungram b/xtask/codegen/gritql.ungram index 335c0ea3e0e4..1ceca6d02985 100644 --- a/xtask/codegen/gritql.ungram +++ b/xtask/codegen/gritql.ungram @@ -158,13 +158,13 @@ GritSubOperation = left: AnyGritPattern '-' right: AnyGritPattern GritPatternAs = - pattern: AnyGritPattern - 'as' + pattern: AnyGritPattern + 'as' variable: GritVariable GritPatternLimit = - pattern: AnyGritPattern - 'limit' + pattern: AnyGritPattern + 'limit' limit: GritIntLiteral // statement, in the engine this is a predicate that always evaluates to true @@ -378,7 +378,7 @@ GritRegexPatternVariables = ////////////// // `pattern` where { predicates } -// ^^^^^^^^^^^^^^ +// ^^^^^^^^^^^^^^ AnyGritPredicate = GritPredicateNot | GritPredicateMaybe @@ -491,7 +491,7 @@ GritPredicateEqual = right: AnyGritPattern // `log($message)` where $message <: `Hello, world!` -// ^^^^^^^^ ^^ ^^^^^^^^^^^^^^^ +// ^^^^^^^^ ^^ ^^^^^^^^^^^^^^^ GritPredicateMatch = left: AnyGritPredicateMatchSubject match: '<:' @@ -532,7 +532,7 @@ GritFunctionDefinition = '(' args: GritVariableList ')' - body: GritCurlyPredicateList + body: GritPredicateCurly // private pattern my_function($arg1, $arg2) { ... } // ^^^^^^^ ^^^^^^^ ^^^^^^^^^^^^^^^^^^ ^^^^^^ ^ ^^^ ^ @@ -559,9 +559,9 @@ GritPredicateDefinition = '(' args: GritVariableList ')' - body: GritCurlyPredicateList + body: GritPredicateCurly -GritCurlyPredicateList = +GritPredicateCurly = '{' predicates: GritPredicateList '}' diff --git a/xtask/codegen/src/grit_kinds_src.rs b/xtask/codegen/src/grit_kinds_src.rs index 5e8767c642f6..ab6a5977c704 100644 --- a/xtask/codegen/src/grit_kinds_src.rs +++ b/xtask/codegen/src/grit_kinds_src.rs @@ -171,7 +171,7 @@ pub const GRIT_KINDS_SRC: KindsSrc = KindsSrc { "GRIT_PATTERN_DEFINITION", "GRIT_PATTERN_ARG_LIST", "GRIT_PREDICATE_LIST", - "GRIT_CURLY_PREDICATE_LIST", + "GRIT_PREDICATE_CURLY", "GRIT_PREDICATE_DEFINITION", "GRIT_FUNCTION_DEFINITION", "GRIT_PREDICATE_NOT",