-
-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(css_formatter): support for pseudo selectors (#1291)
- Loading branch information
1 parent
934fa66
commit bf1c3eb
Showing
51 changed files
with
1,243 additions
and
81 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,13 @@ | ||
use crate::prelude::*; | ||
use biome_css_syntax::CssNthOffset; | ||
use biome_rowan::AstNode; | ||
use biome_css_syntax::{CssNthOffset, CssNthOffsetFields}; | ||
use biome_formatter::write; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatCssNthOffset; | ||
impl FormatNodeRule<CssNthOffset> for FormatCssNthOffset { | ||
fn fmt_fields(&self, node: &CssNthOffset, f: &mut CssFormatter) -> FormatResult<()> { | ||
format_verbatim_node(node.syntax()).fmt(f) | ||
let CssNthOffsetFields { sign, value } = node.as_fields(); | ||
|
||
write!(f, [sign.format(), space(), value.format()]) | ||
} | ||
} |
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
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
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_css_syntax::CssPseudoValueList; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatCssPseudoValueList; | ||
impl FormatRule<CssPseudoValueList> for FormatCssPseudoValueList { | ||
type Context = CssFormatContext; | ||
fn fmt(&self, node: &CssPseudoValueList, f: &mut CssFormatter) -> FormatResult<()> { | ||
format_verbatim_node(node.syntax()).fmt(f) | ||
let mut joiner = f.join_nodes_with_soft_line(); | ||
|
||
for (rule, formatted) in node.elements().zip(node.format_separated(",")) { | ||
joiner.entry(rule.node()?.syntax(), &formatted); | ||
} | ||
|
||
joiner.finish() | ||
} | ||
} |
19 changes: 18 additions & 1 deletion
19
crates/biome_css_formatter/src/css/lists/relative_selector_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_css_syntax::CssRelativeSelectorList; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatCssRelativeSelectorList; | ||
impl FormatRule<CssRelativeSelectorList> for FormatCssRelativeSelectorList { | ||
type Context = CssFormatContext; | ||
fn fmt(&self, node: &CssRelativeSelectorList, f: &mut CssFormatter) -> FormatResult<()> { | ||
format_verbatim_node(node.syntax()).fmt(f) | ||
let mut joiner = f.join_nodes_with_soft_line(); | ||
|
||
for (rule, formatted) in node.elements().zip(node.format_separated(",")) { | ||
// Each selector gets `indent` added in case it breaks over multiple | ||
// lines. The break is added here rather than in each selector both | ||
// for simplicity and to avoid recursively adding indents when | ||
// selectors are nested within other rules. The group is then added | ||
// around the indent to ensure that it tries using a flat layout | ||
// first and only expands when the single selector can't fit the line. | ||
// | ||
// For example, a selector like `div span a` is structured like | ||
// `[div, [span, [a]]]`, so `a` would end up double-indented if it | ||
// was handled by the selector rather than here. | ||
joiner.entry(rule.node()?.syntax(), &group(&indent(&formatted))); | ||
} | ||
|
||
joiner.finish() | ||
} | ||
} |
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
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
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
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
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
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
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
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
20 changes: 17 additions & 3 deletions
20
crates/biome_css_formatter/src/css/pseudo/pseudo_class_nth.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,24 @@ | ||
use crate::prelude::*; | ||
use biome_css_syntax::CssPseudoClassNth; | ||
use biome_rowan::AstNode; | ||
use biome_css_syntax::{CssPseudoClassNth, CssPseudoClassNthFields}; | ||
use biome_formatter::write; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatCssPseudoClassNth; | ||
impl FormatNodeRule<CssPseudoClassNth> for FormatCssPseudoClassNth { | ||
fn fmt_fields(&self, node: &CssPseudoClassNth, f: &mut CssFormatter) -> FormatResult<()> { | ||
format_verbatim_node(node.syntax()).fmt(f) | ||
let CssPseudoClassNthFields { | ||
sign, | ||
value, | ||
symbol_token, | ||
offset, | ||
} = node.as_fields(); | ||
|
||
write!(f, [sign.format(), value.format(), symbol_token.format(),])?; | ||
|
||
if offset.is_some() { | ||
write!(f, [soft_line_break_or_space(), offset.format()])?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.