From ff73013a86eff6df0a319fa9b436f647f370bded Mon Sep 17 00:00:00 2001 From: Denis Bezrukov <6227442+denbezrukov@users.noreply.github.com> Date: Fri, 19 Apr 2024 22:34:44 +0200 Subject: [PATCH] refactor(css_formatter): implement FormatCssBlockLike (#2531) --- .../src/css/auxiliary/declaration_block.rs | 8 ++- .../auxiliary/declaration_or_at_rule_block.rs | 8 ++- .../auxiliary/declaration_or_rule_block.rs | 8 ++- .../auxiliary/font_feature_values_block.rs | 8 ++- .../src/css/auxiliary/keyframes_block.rs | 8 ++- .../src/css/auxiliary/page_at_rule_block.rs | 8 ++- .../src/css/auxiliary/rule_block.rs | 8 ++- .../src/utils/block_like.rs | 60 +++++-------------- 8 files changed, 56 insertions(+), 60 deletions(-) diff --git a/crates/biome_css_formatter/src/css/auxiliary/declaration_block.rs b/crates/biome_css_formatter/src/css/auxiliary/declaration_block.rs index 6932c0fe7ecd..bbcfbb9936c1 100644 --- a/crates/biome_css_formatter/src/css/auxiliary/declaration_block.rs +++ b/crates/biome_css_formatter/src/css/auxiliary/declaration_block.rs @@ -1,5 +1,6 @@ use crate::prelude::*; -use crate::utils::block_like::CssBlockLike; +use crate::utils::block_like::FormatCssBlockLike; +use biome_css_syntax::stmt_ext::CssBlockLike; use biome_css_syntax::CssDeclarationBlock; use biome_formatter::write; @@ -7,7 +8,10 @@ use biome_formatter::write; pub(crate) struct FormatCssDeclarationBlock; impl FormatNodeRule for FormatCssDeclarationBlock { fn fmt_fields(&self, node: &CssDeclarationBlock, f: &mut CssFormatter) -> FormatResult<()> { - write!(f, [CssBlockLike::from(node.clone())]) + write!( + f, + [FormatCssBlockLike::new(&CssBlockLike::from(node.clone()))] + ) } fn fmt_dangling_comments( diff --git a/crates/biome_css_formatter/src/css/auxiliary/declaration_or_at_rule_block.rs b/crates/biome_css_formatter/src/css/auxiliary/declaration_or_at_rule_block.rs index 4f40f57a9d6a..88aaf6d2b04f 100644 --- a/crates/biome_css_formatter/src/css/auxiliary/declaration_or_at_rule_block.rs +++ b/crates/biome_css_formatter/src/css/auxiliary/declaration_or_at_rule_block.rs @@ -1,5 +1,6 @@ use crate::prelude::*; -use crate::utils::block_like::CssBlockLike; +use crate::utils::block_like::FormatCssBlockLike; +use biome_css_syntax::stmt_ext::CssBlockLike; use biome_css_syntax::CssDeclarationOrAtRuleBlock; use biome_formatter::write; @@ -11,7 +12,10 @@ impl FormatNodeRule for FormatCssDeclarationOrAtRul node: &CssDeclarationOrAtRuleBlock, f: &mut CssFormatter, ) -> FormatResult<()> { - write!(f, [CssBlockLike::from(node.clone())]) + write!( + f, + [FormatCssBlockLike::new(&CssBlockLike::from(node.clone()))] + ) } fn fmt_dangling_comments( diff --git a/crates/biome_css_formatter/src/css/auxiliary/declaration_or_rule_block.rs b/crates/biome_css_formatter/src/css/auxiliary/declaration_or_rule_block.rs index e5422a000a1c..1326171342ce 100644 --- a/crates/biome_css_formatter/src/css/auxiliary/declaration_or_rule_block.rs +++ b/crates/biome_css_formatter/src/css/auxiliary/declaration_or_rule_block.rs @@ -1,5 +1,6 @@ use crate::prelude::*; -use crate::utils::block_like::CssBlockLike; +use crate::utils::block_like::FormatCssBlockLike; +use biome_css_syntax::stmt_ext::CssBlockLike; use biome_css_syntax::CssDeclarationOrRuleBlock; use biome_formatter::write; @@ -11,7 +12,10 @@ impl FormatNodeRule for FormatCssDeclarationOrRuleBlo node: &CssDeclarationOrRuleBlock, f: &mut CssFormatter, ) -> FormatResult<()> { - write!(f, [CssBlockLike::from(node.clone())]) + write!( + f, + [FormatCssBlockLike::new(&CssBlockLike::from(node.clone()))] + ) } fn fmt_dangling_comments( diff --git a/crates/biome_css_formatter/src/css/auxiliary/font_feature_values_block.rs b/crates/biome_css_formatter/src/css/auxiliary/font_feature_values_block.rs index c52b0a280a8f..050224e35ff3 100644 --- a/crates/biome_css_formatter/src/css/auxiliary/font_feature_values_block.rs +++ b/crates/biome_css_formatter/src/css/auxiliary/font_feature_values_block.rs @@ -1,5 +1,6 @@ use crate::prelude::*; -use crate::utils::block_like::CssBlockLike; +use crate::utils::block_like::FormatCssBlockLike; +use biome_css_syntax::stmt_ext::CssBlockLike; use biome_css_syntax::CssFontFeatureValuesBlock; use biome_formatter::write; @@ -11,7 +12,10 @@ impl FormatNodeRule for FormatCssFontFeatureValuesBlo node: &CssFontFeatureValuesBlock, f: &mut CssFormatter, ) -> FormatResult<()> { - write!(f, [CssBlockLike::from(node.clone())]) + write!( + f, + [FormatCssBlockLike::new(&CssBlockLike::from(node.clone()))] + ) } fn fmt_dangling_comments( diff --git a/crates/biome_css_formatter/src/css/auxiliary/keyframes_block.rs b/crates/biome_css_formatter/src/css/auxiliary/keyframes_block.rs index 34076b416fc5..b9bd45b19ee0 100644 --- a/crates/biome_css_formatter/src/css/auxiliary/keyframes_block.rs +++ b/crates/biome_css_formatter/src/css/auxiliary/keyframes_block.rs @@ -1,5 +1,6 @@ use crate::prelude::*; -use crate::utils::block_like::CssBlockLike; +use crate::utils::block_like::FormatCssBlockLike; +use biome_css_syntax::stmt_ext::CssBlockLike; use biome_css_syntax::CssKeyframesBlock; use biome_formatter::write; @@ -7,7 +8,10 @@ use biome_formatter::write; pub(crate) struct FormatCssKeyframesBlock; impl FormatNodeRule for FormatCssKeyframesBlock { fn fmt_fields(&self, node: &CssKeyframesBlock, f: &mut CssFormatter) -> FormatResult<()> { - write!(f, [CssBlockLike::from(node.clone())]) + write!( + f, + [FormatCssBlockLike::new(&CssBlockLike::from(node.clone()))] + ) } fn fmt_dangling_comments( diff --git a/crates/biome_css_formatter/src/css/auxiliary/page_at_rule_block.rs b/crates/biome_css_formatter/src/css/auxiliary/page_at_rule_block.rs index 0675bfc45dcf..964852390ae6 100644 --- a/crates/biome_css_formatter/src/css/auxiliary/page_at_rule_block.rs +++ b/crates/biome_css_formatter/src/css/auxiliary/page_at_rule_block.rs @@ -1,5 +1,6 @@ use crate::prelude::*; -use crate::utils::block_like::CssBlockLike; +use crate::utils::block_like::FormatCssBlockLike; +use biome_css_syntax::stmt_ext::CssBlockLike; use biome_css_syntax::CssPageAtRuleBlock; use biome_formatter::write; @@ -7,7 +8,10 @@ use biome_formatter::write; pub(crate) struct FormatCssPageAtRuleBlock; impl FormatNodeRule for FormatCssPageAtRuleBlock { fn fmt_fields(&self, node: &CssPageAtRuleBlock, f: &mut CssFormatter) -> FormatResult<()> { - write!(f, [CssBlockLike::from(node.clone())]) + write!( + f, + [FormatCssBlockLike::new(&CssBlockLike::from(node.clone()))] + ) } fn fmt_dangling_comments( diff --git a/crates/biome_css_formatter/src/css/auxiliary/rule_block.rs b/crates/biome_css_formatter/src/css/auxiliary/rule_block.rs index 31249fd08218..3038cf02903a 100644 --- a/crates/biome_css_formatter/src/css/auxiliary/rule_block.rs +++ b/crates/biome_css_formatter/src/css/auxiliary/rule_block.rs @@ -1,5 +1,6 @@ use crate::prelude::*; -use crate::utils::block_like::CssBlockLike; +use crate::utils::block_like::FormatCssBlockLike; +use biome_css_syntax::stmt_ext::CssBlockLike; use biome_css_syntax::CssRuleBlock; use biome_formatter::write; @@ -7,7 +8,10 @@ use biome_formatter::write; pub(crate) struct FormatCssRuleBlock; impl FormatNodeRule for FormatCssRuleBlock { fn fmt_fields(&self, node: &CssRuleBlock, f: &mut CssFormatter) -> FormatResult<()> { - write!(f, [CssBlockLike::from(node.clone())]) + write!( + f, + [FormatCssBlockLike::new(&CssBlockLike::from(node.clone()))] + ) } fn fmt_dangling_comments(&self, _: &CssRuleBlock, _: &mut CssFormatter) -> FormatResult<()> { diff --git a/crates/biome_css_formatter/src/utils/block_like.rs b/crates/biome_css_formatter/src/utils/block_like.rs index d378d2916eb9..ee02c4455c76 100644 --- a/crates/biome_css_formatter/src/utils/block_like.rs +++ b/crates/biome_css_formatter/src/utils/block_like.rs @@ -1,54 +1,22 @@ use crate::prelude::*; use biome_formatter::{CstFormatContext, Format, FormatResult}; -use biome_rowan::{declare_node_union, SyntaxResult}; use crate::CssFormatter; -use biome_css_syntax::*; +use biome_css_syntax::stmt_ext::CssBlockLike; use biome_formatter::write; -declare_node_union! { - pub CssBlockLike = CssKeyframesBlock | CssDeclarationOrAtRuleBlock | CssDeclarationBlock | CssRuleBlock | CssFontFeatureValuesBlock | CssPageAtRuleBlock | CssDeclarationOrRuleBlock +#[derive(Debug, Copy, Clone)] +pub(crate) struct FormatCssBlockLike<'a> { + block: &'a CssBlockLike, } -impl CssBlockLike { - fn l_curly_token(&self) -> SyntaxResult { - match self { - CssBlockLike::CssKeyframesBlock(block) => block.l_curly_token(), - CssBlockLike::CssDeclarationOrAtRuleBlock(block) => block.l_curly_token(), - CssBlockLike::CssDeclarationBlock(block) => block.l_curly_token(), - CssBlockLike::CssRuleBlock(block) => block.l_curly_token(), - CssBlockLike::CssFontFeatureValuesBlock(block) => block.l_curly_token(), - CssBlockLike::CssPageAtRuleBlock(block) => block.l_curly_token(), - CssBlockLike::CssDeclarationOrRuleBlock(block) => block.l_curly_token(), - } - } - - fn r_curly_token(&self) -> SyntaxResult { - match self { - CssBlockLike::CssKeyframesBlock(block) => block.r_curly_token(), - CssBlockLike::CssDeclarationOrAtRuleBlock(block) => block.r_curly_token(), - CssBlockLike::CssDeclarationBlock(block) => block.r_curly_token(), - CssBlockLike::CssRuleBlock(block) => block.r_curly_token(), - CssBlockLike::CssFontFeatureValuesBlock(block) => block.r_curly_token(), - CssBlockLike::CssPageAtRuleBlock(block) => block.r_curly_token(), - CssBlockLike::CssDeclarationOrRuleBlock(block) => block.r_curly_token(), - } - } - - fn is_empty(&self) -> bool { - match self { - CssBlockLike::CssKeyframesBlock(block) => block.items().is_empty(), - CssBlockLike::CssDeclarationOrAtRuleBlock(block) => block.items().is_empty(), - CssBlockLike::CssDeclarationBlock(block) => block.declarations().is_empty(), - CssBlockLike::CssRuleBlock(block) => block.rules().is_empty(), - CssBlockLike::CssFontFeatureValuesBlock(block) => block.items().is_empty(), - CssBlockLike::CssPageAtRuleBlock(block) => block.items().is_empty(), - CssBlockLike::CssDeclarationOrRuleBlock(block) => block.items().is_empty(), - } +impl<'a> FormatCssBlockLike<'a> { + pub(crate) fn new(block: &'a CssBlockLike) -> Self { + Self { block } } fn write_items(&self, f: &mut CssFormatter) -> FormatResult<()> { - match self { + match self.block { CssBlockLike::CssKeyframesBlock(block) => { write!(f, [block.items().format()]) } @@ -74,23 +42,23 @@ impl CssBlockLike { } } -impl Format for CssBlockLike { +impl<'a> Format for FormatCssBlockLike<'a> { fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { - write!(f, [self.l_curly_token().format()])?; + write!(f, [self.block.l_curly_token().format()])?; - let r_curly_token = self.r_curly_token()?; + let r_curly_token = self.block.r_curly_token()?; // When the list is empty, we still print a hard line to put the // closing curly on the next line. - if self.is_empty() { + if self.block.is_empty() { let comments = f.context().comments(); - let has_dangling_comments = comments.has_dangling_comments(self.syntax()); + let has_dangling_comments = comments.has_dangling_comments(self.block.syntax()); if has_dangling_comments { write!( f, - [format_dangling_comments(self.syntax()).with_block_indent()] + [format_dangling_comments(self.block.syntax()).with_block_indent()] )?; } else { write!(f, [soft_line_break()])?;