-
-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(format/grit): add formatting for lists (#4378)
- Loading branch information
Showing
5 changed files
with
147 additions
and
7 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
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
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() | ||
] | ||
) | ||
} | ||
} |
19 changes: 18 additions & 1 deletion
19
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,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() | ||
} | ||
} |
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 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`]} |
77 changes: 77 additions & 0 deletions
77
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,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 |