Skip to content

Commit

Permalink
feat(format/grit): add formatting for lists (#4378)
Browse files Browse the repository at this point in the history
  • Loading branch information
branberry authored Nov 5, 2024
1 parent 6ea65f4 commit 50093fb
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 7 deletions.
29 changes: 26 additions & 3 deletions crates/biome_grit_formatter/src/grit/auxiliary/list.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
use crate::prelude::*;
use biome_grit_syntax::GritList;
use biome_rowan::AstNode;
use biome_formatter::{format_args, write};
use biome_grit_syntax::{GritList, GritListFields};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatGritList;
impl FormatNodeRule<GritList> for FormatGritList {
fn fmt_fields(&self, node: &GritList, f: &mut GritFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let GritListFields {
l_brack_token,
name,
patterns,
r_brack_token,
} = node.as_fields();

let should_expand = f.comments().has_dangling_comments(node.syntax());

write!(
f,
[
l_brack_token.format(),
name.format(),
group(&soft_block_indent(&format_args![
patterns.format(),
format_dangling_comments(node.syntax())
]))
.should_expand(should_expand),
line_suffix_boundary(),
r_brack_token.format()
]
)
}
}
25 changes: 22 additions & 3 deletions crates/biome_grit_formatter/src/grit/auxiliary/list_accessor.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
use crate::prelude::*;
use biome_grit_syntax::GritListAccessor;
use biome_rowan::AstNode;
use biome_formatter::write;
use biome_grit_syntax::{GritListAccessor, GritListAccessorFields};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatGritListAccessor;
impl FormatNodeRule<GritListAccessor> for FormatGritListAccessor {
fn fmt_fields(&self, node: &GritListAccessor, f: &mut GritFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let GritListAccessorFields {
l_brack_token,
index,
list,
r_brack_token,
} = node.as_fields();

write!(
f,
[
l_brack_token.format(),
space(),
index.format(),
space(),
list.format(),
space(),
r_brack_token.format()
]
)
}
}
19 changes: 18 additions & 1 deletion crates/biome_grit_formatter/src/grit/lists/list_pattern_list.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
use crate::prelude::*;
use biome_formatter::write;
use biome_grit_syntax::GritListPatternList;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatGritListPatternList;
impl FormatRule<GritListPatternList> for FormatGritListPatternList {
type Context = GritFormatContext;
fn fmt(&self, node: &GritListPatternList, f: &mut GritFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let mut filler = f.fill();

for (element, formatted) in node.iter().zip(node.format_separated(",")) {
filler.entry(
&format_once(|f| {
if get_lines_before(element?.syntax()) > 0 {
write!(f, [empty_line()])
} else {
write!(f, [soft_line_break_or_space()])
}
}),
&formatted,
);
}

filler.finish()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
`var $x = [$numbers]`=>`var firstPrimes = [$numbers]`where{$numbers<:[`2`,`3`,`5`]}


`var $x = [$numbers]`=>`var numbersLong = [$numbers]`where{$numbers<:[`2`,`3`,`5`,`6`,`7`,`8`,`2`,`3`,`5`,`6`,`7`,`8`,`2`,`3`,`5`,`6`,`7`,`8`,`2`,`3`,`5`,`6`,`7`,`8`]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: grit/patterns/list_pattern.grit
---
# Input

```grit
`var $x = [$numbers]`=>`var firstPrimes = [$numbers]`where{$numbers<:[`2`,`3`,`5`]}
`var $x = [$numbers]`=>`var numbersLong = [$numbers]`where{$numbers<:[`2`,`3`,`5`,`6`,`7`,`8`,`2`,`3`,`5`,`6`,`7`,`8`,`2`,`3`,`5`,`6`,`7`,`8`,`2`,`3`,`5`,`6`,`7`,`8`]}
```


=============================

# Outputs

## Output 1

-----
Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Attribute Position: Auto
-----

```grit
`var $x = [$numbers]`=>`var firstPrimes = [$numbers]` where {
$numbers <: [`2`, `3`, `5`]
}
`var $x = [$numbers]`=>`var numbersLong = [$numbers]` where {
$numbers <: [
`2`, `3`, `5`, `6`, `7`, `8`, `2`, `3`, `5`, `6`, `7`, `8`, `2`, `3`, `5`,
`6`, `7`, `8`, `2`, `3`, `5`, `6`, `7`, `8`
],
}
```



## Unimplemented nodes/tokens

"`var $x = [$numbers]`=>`var firstPrimes = [$numbers]`" => 0..53
"\t$number" => 62..70
"`2`" => 76..79
" `3" => 80..83
" `5" => 85..88
"`var $x = [$numbers]`=>`var numbersLong = [$numbers]` wh" => 94..150
"\t$number" => 156..164
"\t\t\t" => 171..174
" `3" => 178..181
" `5" => 183..186
" `6" => 188..191
" `7" => 193..196
" `8" => 198..201
" `2" => 203..206
" `3" => 208..211
" `5" => 213..216
" `6" => 218..221
" `7" => 223..226
" `8" => 228..231
" `2" => 233..236
" `3" => 238..241
" `5" => 243..246
"\t\t\t" => 249..252
" `7" => 256..259
" `8" => 261..264
" `2" => 266..269
" `3" => 271..274
" `5" => 276..279
" `6" => 281..284
" `7" => 286..289
" `8" => 291..294

0 comments on commit 50093fb

Please sign in to comment.