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

refactor(css_formatter): implement FormatCssBlockLike #2531

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
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;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatCssDeclarationBlock;
impl FormatNodeRule<CssDeclarationBlock> 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -11,7 +12,10 @@ impl FormatNodeRule<CssDeclarationOrAtRuleBlock> 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -11,7 +12,10 @@ impl FormatNodeRule<CssDeclarationOrRuleBlock> 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -11,7 +12,10 @@ impl FormatNodeRule<CssFontFeatureValuesBlock> 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
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;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatCssKeyframesBlock;
impl FormatNodeRule<CssKeyframesBlock> 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
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;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatCssPageAtRuleBlock;
impl FormatNodeRule<CssPageAtRuleBlock> 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(
Expand Down
8 changes: 6 additions & 2 deletions crates/biome_css_formatter/src/css/auxiliary/rule_block.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
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;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatCssRuleBlock;
impl FormatNodeRule<CssRuleBlock> 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<()> {
Expand Down
60 changes: 14 additions & 46 deletions crates/biome_css_formatter/src/utils/block_like.rs
Original file line number Diff line number Diff line change
@@ -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<CssSyntaxToken> {
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<CssSyntaxToken> {
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()])
}
Expand All @@ -74,23 +42,23 @@ impl CssBlockLike {
}
}

impl Format<CssFormatContext> for CssBlockLike {
impl<'a> Format<CssFormatContext> for FormatCssBlockLike<'a> {
fn fmt(&self, f: &mut Formatter<CssFormatContext>) -> 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()])?;
Expand Down