Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(format/grit): add formatting for lists #4378

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 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,25 @@
use crate::prelude::*;
use biome_grit_syntax::GritList;
use biome_rowan::AstNode;
use biome_formatter::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();
write!(
f,
[
l_brack_token.format(),
name.format(),
patterns.format(),
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()
]
)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
use crate::prelude::*;
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 joiner = f.fill();

for (_, formatted) in node.elements().zip(node.format_separated(",")) {
joiner.entry(&soft_line_break_or_space(), &formatted);
}

joiner.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 firstPrimes = [$numbers]`where{$numbers<:[`2`,`3`,`5`,`6`,`7`,`8`]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
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 firstPrimes = [$numbers]`where{$numbers<:[`2`,`3`,`5`,`6`,`7`,`8`]}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest adding also long lists that go over the default line width, so you can see how it behaves.

For example, here you used .fill, which doesn't - if memory serves me right - breakdown the lists on multiple lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's correct. I'll update the tests to go over the default line length, and refactor the code so that it breaks down the list on multiple lines once it reaches a certain length.

Thank you!


```


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

# 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 firstPrimes = [$numbers]` where {
$numbers <: [`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 firstPrimes = [$numbers]` wh" => 94..150
"\t$number" => 156..164
"`2`" => 170..173
" `3" => 174..177
" `5" => 179..182
" `6" => 184..187
" `7" => 189..192
" `8" => 194..197