From 026a97ecf5df9f430c55e3c722b350fce01e0678 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Mon, 21 Oct 2024 09:51:15 +0900 Subject: [PATCH 1/4] add new snapshot tests --- .../src/grit/lists/list_pattern_list.rs | 2 + .../specs/grit/patterns/list_pattern.grit | 3 ++ .../grit/patterns/list_pattern.grit.snap.new | 42 +++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit create mode 100644 crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap.new diff --git a/crates/biome_grit_formatter/src/grit/lists/list_pattern_list.rs b/crates/biome_grit_formatter/src/grit/lists/list_pattern_list.rs index 1ba7b0693111..8a2b3c7800de 100644 --- a/crates/biome_grit_formatter/src/grit/lists/list_pattern_list.rs +++ b/crates/biome_grit_formatter/src/grit/lists/list_pattern_list.rs @@ -5,6 +5,8 @@ pub(crate) struct FormatGritListPatternList; impl FormatRule for FormatGritListPatternList { type Context = GritFormatContext; fn fmt(&self, node: &GritListPatternList, f: &mut GritFormatter) -> FormatResult<()> { + let separator = soft_line_break_or_space(); + let mut joiner = f.join_with(&separator); format_verbatim_node(node.syntax()).fmt(f) } } diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit b/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit new file mode 100644 index 000000000000..66fc874747ec --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit @@ -0,0 +1,3 @@ +`var $x = [$numbers]` => `var firstPrimes = [$numbers]` where { + $numbers <: [ `2`, `3`, `5` ] +} diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap.new b/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap.new new file mode 100644 index 000000000000..9b5bfcc4681d --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap.new @@ -0,0 +1,42 @@ +--- +source: crates/biome_formatter_test/src/snapshot_builder.rs +assertion_line: 212 +info: grit/patterns/list_pattern.grit +--- +# Input + +```grit +`var $x = [$numbers]` => `var firstPrimes = [$numbers]` where { + $numbers <: [ `2`, `3`, `5` ] +} + +``` + + +============================= + +# 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` ] +} +``` + + + +## Unimplemented nodes/tokens + +"`var $x = [$numbers]` => `var firstPrimes = [$numbers]` " => 0..56 +"\t$numbers <:" => 64..76 +" [ `2`, `3`, `5` " => 76..93 From 4f1a10fafe5fc00da5c134a049cae0d6c460ae89 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Thu, 24 Oct 2024 10:22:12 +0900 Subject: [PATCH 2/4] format lists --- .../src/grit/auxiliary/list.rs | 21 ++++++++-- .../src/grit/auxiliary/list_accessor.rs | 25 +++++++++-- .../src/grit/lists/list_pattern_list.rs | 11 +++-- .../specs/grit/patterns/list_pattern.grit | 7 ++-- .../grit/patterns/list_pattern.grit.snap.new | 42 ------------------- 5 files changed, 52 insertions(+), 54 deletions(-) delete mode 100644 crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap.new diff --git a/crates/biome_grit_formatter/src/grit/auxiliary/list.rs b/crates/biome_grit_formatter/src/grit/auxiliary/list.rs index 368924c142a4..a23e498560d3 100644 --- a/crates/biome_grit_formatter/src/grit/auxiliary/list.rs +++ b/crates/biome_grit_formatter/src/grit/auxiliary/list.rs @@ -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 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(), + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/auxiliary/list_accessor.rs b/crates/biome_grit_formatter/src/grit/auxiliary/list_accessor.rs index 5a0df449aa0b..7e1844e7f7dc 100644 --- a/crates/biome_grit_formatter/src/grit/auxiliary/list_accessor.rs +++ b/crates/biome_grit_formatter/src/grit/auxiliary/list_accessor.rs @@ -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 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() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/lists/list_pattern_list.rs b/crates/biome_grit_formatter/src/grit/lists/list_pattern_list.rs index 8a2b3c7800de..4a8a5ae39069 100644 --- a/crates/biome_grit_formatter/src/grit/lists/list_pattern_list.rs +++ b/crates/biome_grit_formatter/src/grit/lists/list_pattern_list.rs @@ -1,12 +1,17 @@ use crate::prelude::*; use biome_grit_syntax::GritListPatternList; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritListPatternList; impl FormatRule for FormatGritListPatternList { type Context = GritFormatContext; fn fmt(&self, node: &GritListPatternList, f: &mut GritFormatter) -> FormatResult<()> { - let separator = soft_line_break_or_space(); - let mut joiner = f.join_with(&separator); - 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() } } diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit b/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit index 66fc874747ec..55b456d3713d 100644 --- a/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit @@ -1,3 +1,4 @@ -`var $x = [$numbers]` => `var firstPrimes = [$numbers]` where { - $numbers <: [ `2`, `3`, `5` ] -} +`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`]} diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap.new b/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap.new deleted file mode 100644 index 9b5bfcc4681d..000000000000 --- a/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap.new +++ /dev/null @@ -1,42 +0,0 @@ ---- -source: crates/biome_formatter_test/src/snapshot_builder.rs -assertion_line: 212 -info: grit/patterns/list_pattern.grit ---- -# Input - -```grit -`var $x = [$numbers]` => `var firstPrimes = [$numbers]` where { - $numbers <: [ `2`, `3`, `5` ] -} - -``` - - -============================= - -# 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` ] -} -``` - - - -## Unimplemented nodes/tokens - -"`var $x = [$numbers]` => `var firstPrimes = [$numbers]` " => 0..56 -"\t$numbers <:" => 64..76 -" [ `2`, `3`, `5` " => 76..93 From 5e293d3a4efb1007f93556d8f48562b670b9d1bf Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Fri, 25 Oct 2024 11:38:27 +0900 Subject: [PATCH 3/4] Add snapshot --- .../grit/patterns/list_pattern.grit.snap | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap new file mode 100644 index 000000000000..403471d72fe4 --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap @@ -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 From 42da1ebb5a63e8fca83dcc2acbfb059565d16ea7 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Mon, 4 Nov 2024 19:31:02 -0600 Subject: [PATCH 4/4] Fill lines and add new line if needed --- .../src/grit/auxiliary/list.rs | 14 +++++-- .../src/grit/lists/list_pattern_list.rs | 18 ++++++-- .../specs/grit/patterns/list_pattern.grit | 2 +- .../grit/patterns/list_pattern.grit.snap | 41 ++++++++++++++----- 4 files changed, 57 insertions(+), 18 deletions(-) diff --git a/crates/biome_grit_formatter/src/grit/auxiliary/list.rs b/crates/biome_grit_formatter/src/grit/auxiliary/list.rs index a23e498560d3..bc85d7c15926 100644 --- a/crates/biome_grit_formatter/src/grit/auxiliary/list.rs +++ b/crates/biome_grit_formatter/src/grit/auxiliary/list.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use biome_formatter::write; +use biome_formatter::{format_args, write}; use biome_grit_syntax::{GritList, GritListFields}; #[derive(Debug, Clone, Default)] @@ -12,13 +12,21 @@ impl FormatNodeRule for FormatGritList { 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(), - patterns.format(), - r_brack_token.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() ] ) } diff --git a/crates/biome_grit_formatter/src/grit/lists/list_pattern_list.rs b/crates/biome_grit_formatter/src/grit/lists/list_pattern_list.rs index 4a8a5ae39069..337c7242082a 100644 --- a/crates/biome_grit_formatter/src/grit/lists/list_pattern_list.rs +++ b/crates/biome_grit_formatter/src/grit/lists/list_pattern_list.rs @@ -1,4 +1,5 @@ use crate::prelude::*; +use biome_formatter::write; use biome_grit_syntax::GritListPatternList; #[derive(Debug, Clone, Default)] @@ -6,12 +7,21 @@ pub(crate) struct FormatGritListPatternList; impl FormatRule for FormatGritListPatternList { type Context = GritFormatContext; fn fmt(&self, node: &GritListPatternList, f: &mut GritFormatter) -> FormatResult<()> { - let mut joiner = f.fill(); + let mut filler = f.fill(); - for (_, formatted) in node.elements().zip(node.format_separated(",")) { - joiner.entry(&soft_line_break_or_space(), &formatted); + 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, + ); } - joiner.finish() + filler.finish() } } diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit b/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit index 55b456d3713d..3fae7f91f4cf 100644 --- a/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit @@ -1,4 +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`]} +`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`]} diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap index 403471d72fe4..4acafd780f6e 100644 --- a/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap @@ -8,7 +8,7 @@ info: grit/patterns/list_pattern.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`]} +`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`]} ``` @@ -32,8 +32,11 @@ Attribute Position: Auto $numbers <: [`2`, `3`, `5`] } -`var $x = [$numbers]`=>`var firstPrimes = [$numbers]` where { - $numbers <: [`2`, `3`, `5`, `6`, `7`, `8`] +`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` + ], } ``` @@ -46,11 +49,29 @@ Attribute Position: Auto "`2`" => 76..79 " `3" => 80..83 " `5" => 85..88 -"`var $x = [$numbers]`=>`var firstPrimes = [$numbers]` wh" => 94..150 +"`var $x = [$numbers]`=>`var numbersLong = [$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 +"\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