-
-
Notifications
You must be signed in to change notification settings - Fork 514
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
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
25
crates/biome_grit_formatter/src/grit/auxiliary/list_accessor.rs
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,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() | ||
] | ||
) | ||
} | ||
} |
9 changes: 8 additions & 1 deletion
9
crates/biome_grit_formatter/src/grit/lists/list_pattern_list.rs
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,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() | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
crates/biome_grit_formatter/tests/specs/grit/patterns/list_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,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`]} |
56 changes: 56 additions & 0 deletions
56
crates/biome_grit_formatter/tests/specs/grit/patterns/list_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,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`]} | ||
|
||
``` | ||
|
||
|
||
============================= | ||
|
||
# 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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!