diff --git a/crates/biome_grit_formatter/src/cst.rs b/crates/biome_grit_formatter/src/cst.rs index 58595b25d5a5..bc077d4ce78e 100644 --- a/crates/biome_grit_formatter/src/cst.rs +++ b/crates/biome_grit_formatter/src/cst.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -use biome_formatter::{FormatOwnedWithRule, FormatRefWithRule, FormatResult}; -use biome_grit_syntax::{map_syntax_node, GritSyntaxNode}; +use biome_formatter::{FormatOwnedWithRule, FormatRefWithRule, FormatResult, FormatToken}; +use biome_grit_syntax::{map_syntax_node, GritSyntaxNode, GritSyntaxToken}; #[derive(Debug, Copy, Clone, Default)] pub struct FormatGritSyntaxNode; @@ -28,3 +28,22 @@ impl IntoFormat for GritSyntaxNode { FormatOwnedWithRule::new(self, FormatGritSyntaxNode) } } + +/// Format implementation specific to GritQL tokens. +pub(crate) type FormatGritSyntaxToken = FormatToken; + +impl AsFormat for GritSyntaxToken { + type Format<'a> = FormatRefWithRule<'a, GritSyntaxToken, FormatGritSyntaxToken>; + + fn format(&self) -> Self::Format<'_> { + FormatRefWithRule::new(self, FormatGritSyntaxToken::default()) + } +} + +impl IntoFormat for GritSyntaxToken { + type Format = FormatOwnedWithRule; + + fn into_format(self) -> Self::Format { + FormatOwnedWithRule::new(self, FormatGritSyntaxToken::default()) + } +} diff --git a/crates/biome_grit_formatter/src/grit/auxiliary/root.rs b/crates/biome_grit_formatter/src/grit/auxiliary/root.rs index cbcded68cb44..9b29028b703b 100644 --- a/crates/biome_grit_formatter/src/grit/auxiliary/root.rs +++ b/crates/biome_grit_formatter/src/grit/auxiliary/root.rs @@ -1,10 +1,30 @@ use crate::prelude::*; -use biome_grit_syntax::GritRoot; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritRoot, GritRootFields}; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritRoot; + impl FormatNodeRule for FormatGritRoot { fn fmt_fields(&self, node: &GritRoot, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritRootFields { + bom_token, + version, + language, + definitions, + eof_token, + } = node.as_fields(); + + write!( + f, + [ + bom_token.format(), + version.format(), + language.format(), + definitions.format(), + hard_line_break(), + format_removed(&eof_token?), + ] + ) } } diff --git a/crates/biome_grit_formatter/tests/quick_test.rs b/crates/biome_grit_formatter/tests/quick_test.rs new file mode 100644 index 000000000000..d8605439ac83 --- /dev/null +++ b/crates/biome_grit_formatter/tests/quick_test.rs @@ -0,0 +1,44 @@ +use biome_formatter::{IndentStyle, LineWidth, QuoteStyle}; +use biome_formatter_test::check_reformat::CheckReformat; +use biome_grit_formatter::context::GritFormatOptions; +use biome_grit_formatter::{format_node, GritFormatLanguage}; +use biome_grit_parser::parse_grit; + +mod language { + include!("language.rs"); +} + +#[ignore] +#[test] +// use this test check if your snippet prints as you wish, without using a snapshot +fn quick_test() { + let src = r#" +`$method('$message')` where { + if ($message <: r"Hello, .*!") { + $method => `console.info` + } else { + $method => `console.warn` + } +} +"#; + let tree = parse_grit(src); + let options = GritFormatOptions::new() + .with_indent_style(IndentStyle::Space) + .with_line_width(LineWidth::try_from(80).unwrap()) + .with_quote_style(QuoteStyle::Double); + + let doc = format_node(options.clone(), &tree.syntax()).unwrap(); + let result = doc.print().unwrap(); + + println!("{}", doc.into_document()); + eprintln!("{}", result.as_code()); + + CheckReformat::new( + &tree.syntax(), + result.as_code(), + "testing", + &language::GritTestFormatLanguage, + GritFormatLanguage::new(options), + ) + .check_reformat(); +} diff --git a/crates/biome_grit_formatter/tests/specs/grit/file_node.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/file_node.grit.snap index 6af8139fa4cf..8637af7ec1b5 100644 --- a/crates/biome_grit_formatter/tests/specs/grit/file_node.grit.snap +++ b/crates/biome_grit_formatter/tests/specs/grit/file_node.grit.snap @@ -24,7 +24,8 @@ Attribute Position: Auto ----- ```grit -file(body = contains `console.$method` => `println`)``` +file(body = contains `console.$method` => `println`) +``` diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/if_pattern.grit b/crates/biome_grit_formatter/tests/specs/grit/patterns/if_pattern.grit new file mode 100644 index 000000000000..97bc4bb4f96f --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/if_pattern.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_pattern.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/patterns/if_pattern.grit.snap new file mode 100644 index 000000000000..354668dcdfa0 --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/if_pattern.grit.snap @@ -0,0 +1,47 @@ +--- +source: crates/biome_formatter_test/src/snapshot_builder.rs +info: grit/patterns/if_pattern.grit +--- +# 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` + } +} +``` + + + +## Unimplemented nodes/tokens + +"`$method('$message')` where {\n if ($message <: r\"Hello, .*!\") {\n $method => `console.info`\n } else {\n $method => `console.warn`\n }\n}" => 0..141