-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement formatting for GritQL root node
- Loading branch information
Showing
6 changed files
with
144 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<GritRoot> 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?), | ||
] | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
crates/biome_grit_formatter/tests/specs/grit/patterns/if_pattern.grit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
`$method('$message')` where { | ||
if ($message <: r"Hello, .*!") { | ||
$method => `console.info` | ||
} else { | ||
$method => `console.warn` | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
crates/biome_grit_formatter/tests/specs/grit/patterns/if_pattern.grit.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |