-
-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add primitive values support and more tests
- Loading branch information
1 parent
b50b85a
commit 23db478
Showing
28 changed files
with
717 additions
and
34 deletions.
There are no files selected for viewing
49 changes: 46 additions & 3 deletions
49
crates/biome_css_formatter/src/css/auxiliary/identifier.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,13 +1,56 @@ | ||
use std::borrow::Cow; | ||
|
||
use crate::prelude::*; | ||
use biome_css_syntax::{CssIdentifier, CssIdentifierFields}; | ||
use biome_formatter::write; | ||
use biome_formatter::{token::string::ToAsciiLowercaseCow, write, FormatRuleWithOptions}; | ||
|
||
#[derive(Default, Debug)] | ||
pub(crate) struct FormatCssIdentifierOptions { | ||
/// Whether the formatter should rewrite the identifier using lowercase | ||
/// letters. | ||
pub(crate) forced_lowercase: bool, | ||
} | ||
|
||
impl FormatRuleWithOptions<CssIdentifier> for FormatCssIdentifier { | ||
type Options = FormatCssIdentifierOptions; | ||
|
||
fn with_options(mut self, options: Self::Options) -> Self { | ||
self.forced_lowercase = options.forced_lowercase; | ||
self | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatCssIdentifier; | ||
pub(crate) struct FormatCssIdentifier { | ||
forced_lowercase: bool, | ||
} | ||
|
||
impl FormatNodeRule<CssIdentifier> for FormatCssIdentifier { | ||
fn fmt_fields(&self, node: &CssIdentifier, f: &mut CssFormatter) -> FormatResult<()> { | ||
let CssIdentifierFields { value_token } = node.as_fields(); | ||
|
||
write!(f, [value_token.format()]) | ||
// The parser uses identifiers to represent a few different things: | ||
// selector names, rule names, values, and also units. For formatting, | ||
// we always want to write units in lowercase, but all of the others | ||
// we want to preserve their casing. | ||
if self.forced_lowercase { | ||
let value_token = value_token?; | ||
|
||
let original = value_token.text_trimmed(); | ||
match original.to_ascii_lowercase_cow() { | ||
Cow::Borrowed(_) => write![f, [value_token.format()]], | ||
Cow::Owned(lowercase) => { | ||
write!( | ||
f, | ||
[format_replaced( | ||
&value_token, | ||
&dynamic_text(&lowercase, value_token.text_trimmed_range().start()) | ||
)] | ||
) | ||
} | ||
} | ||
} else { | ||
write!(f, [value_token.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,13 @@ | ||
use crate::prelude::*; | ||
use biome_css_syntax::CssNumber; | ||
use biome_rowan::AstNode; | ||
use biome_css_syntax::{CssNumber, CssNumberFields}; | ||
use biome_formatter::write; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatCssNumber; | ||
impl FormatNodeRule<CssNumber> for FormatCssNumber { | ||
fn fmt_fields(&self, node: &CssNumber, f: &mut CssFormatter) -> FormatResult<()> { | ||
format_verbatim_node(node.syntax()).fmt(f) | ||
let CssNumberFields { value_token } = node.as_fields(); | ||
|
||
write!(f, [value_token.format()]) | ||
} | ||
} |
9 changes: 6 additions & 3 deletions
9
crates/biome_css_formatter/src/css/value/percent_dimension.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,13 @@ | ||
use crate::prelude::*; | ||
use biome_css_syntax::CssPercentDimension; | ||
use biome_rowan::AstNode; | ||
use biome_css_syntax::{CssPercentDimension, CssPercentDimensionFields}; | ||
use biome_formatter::write; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatCssPercentDimension; | ||
impl FormatNodeRule<CssPercentDimension> for FormatCssPercentDimension { | ||
fn fmt_fields(&self, node: &CssPercentDimension, f: &mut CssFormatter) -> FormatResult<()> { | ||
format_verbatim_node(node.syntax()).fmt(f) | ||
let CssPercentDimensionFields { value, unit_token } = node.as_fields(); | ||
|
||
write!(f, [value.format(), unit_token.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
use crate::prelude::*; | ||
use biome_css_syntax::CssPercentage; | ||
use biome_rowan::AstNode; | ||
use biome_css_syntax::{CssPercentage, CssPercentageFields}; | ||
use biome_formatter::write; | ||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatCssPercentage; | ||
impl FormatNodeRule<CssPercentage> for FormatCssPercentage { | ||
fn fmt_fields(&self, node: &CssPercentage, f: &mut CssFormatter) -> FormatResult<()> { | ||
format_verbatim_node(node.syntax()).fmt(f) | ||
let CssPercentageFields { | ||
value, | ||
reminder_token, | ||
} = node.as_fields(); | ||
|
||
write!(f, [value.format(), reminder_token.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,26 @@ | ||
use crate::prelude::*; | ||
use biome_css_syntax::CssRatio; | ||
use biome_rowan::AstNode; | ||
use biome_css_syntax::{CssRatio, CssRatioFields}; | ||
use biome_formatter::{format_args, write}; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatCssRatio; | ||
impl FormatNodeRule<CssRatio> for FormatCssRatio { | ||
fn fmt_fields(&self, node: &CssRatio, f: &mut CssFormatter) -> FormatResult<()> { | ||
format_verbatim_node(node.syntax()).fmt(f) | ||
let CssRatioFields { | ||
numerator, | ||
slash_token, | ||
denominator, | ||
} = node.as_fields(); | ||
|
||
write!( | ||
f, | ||
[group(&format_args![ | ||
numerator.format(), | ||
space(), | ||
slash_token.format(), | ||
soft_line_break_or_space(), | ||
denominator.format() | ||
])] | ||
) | ||
} | ||
} |
22 changes: 18 additions & 4 deletions
22
crates/biome_css_formatter/src/css/value/regular_dimension.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::CssRegularDimension; | ||
use biome_rowan::AstNode; | ||
use crate::{css::auxiliary::identifier::FormatCssIdentifierOptions, prelude::*}; | ||
use biome_css_syntax::{CssRegularDimension, CssRegularDimensionFields}; | ||
use biome_formatter::write; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatCssRegularDimension; | ||
impl FormatNodeRule<CssRegularDimension> for FormatCssRegularDimension { | ||
fn fmt_fields(&self, node: &CssRegularDimension, f: &mut CssFormatter) -> FormatResult<()> { | ||
format_verbatim_node(node.syntax()).fmt(f) | ||
let CssRegularDimensionFields { value, unit } = node.as_fields(); | ||
|
||
write!(f, [value.format()])?; | ||
|
||
if let Ok(unit) = unit { | ||
write!( | ||
f, | ||
[unit.format().with_options(FormatCssIdentifierOptions { | ||
forced_lowercase: true | ||
})] | ||
)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
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::CssString; | ||
use biome_rowan::AstNode; | ||
use biome_css_syntax::{CssString, CssStringFields}; | ||
use biome_formatter::write; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatCssString; | ||
impl FormatNodeRule<CssString> for FormatCssString { | ||
fn fmt_fields(&self, node: &CssString, f: &mut CssFormatter) -> FormatResult<()> { | ||
format_verbatim_node(node.syntax()).fmt(f) | ||
let CssStringFields { value_token } = node.as_fields(); | ||
|
||
write!(f, [value_token.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
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,6 @@ | ||
p { | ||
font-size: 100 ; | ||
font-size: 100% ; | ||
font-size: 10 / 5 ; | ||
font-size: "foo" ; | ||
} |
40 changes: 40 additions & 0 deletions
40
crates/biome_css_formatter/tests/specs/css/dimensions.css.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,40 @@ | ||
--- | ||
source: crates/biome_formatter_test/src/snapshot_builder.rs | ||
info: css/dimensions.css | ||
--- | ||
|
||
# Input | ||
|
||
```css | ||
p { | ||
font-size: 100 ; | ||
font-size: 100% ; | ||
font-size: 10 / 5 ; | ||
font-size: "foo" ; | ||
} | ||
``` | ||
|
||
|
||
============================= | ||
|
||
# Outputs | ||
|
||
## Output 1 | ||
|
||
----- | ||
Indent style: Tab | ||
Indent width: 2 | ||
Line ending: LF | ||
Line width: 80 | ||
----- | ||
|
||
```css | ||
p { | ||
font-size: 100; | ||
font-size: 100%; | ||
font-size: 10 / 5; | ||
font-size: "foo"; | ||
} | ||
``` | ||
|
||
|
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,7 @@ | ||
html { | ||
font-size: 12px!important; | ||
font-size: 12px !important; | ||
color: red ! important; | ||
background-color: white ! | ||
important; | ||
} |
41 changes: 41 additions & 0 deletions
41
crates/biome_css_formatter/tests/specs/css/important.css.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,41 @@ | ||
--- | ||
source: crates/biome_formatter_test/src/snapshot_builder.rs | ||
info: css/important.css | ||
--- | ||
|
||
# Input | ||
|
||
```css | ||
html { | ||
font-size: 12px!important; | ||
font-size: 12px !important; | ||
color: red ! important; | ||
background-color: white ! | ||
important; | ||
} | ||
``` | ||
|
||
|
||
============================= | ||
|
||
# Outputs | ||
|
||
## Output 1 | ||
|
||
----- | ||
Indent style: Tab | ||
Indent width: 2 | ||
Line ending: LF | ||
Line width: 80 | ||
----- | ||
|
||
```css | ||
html { | ||
font-size: 12px !important; | ||
font-size: 12px !important; | ||
color: red !important; | ||
background-color: white !important; | ||
} | ||
``` | ||
|
||
|
18 changes: 18 additions & 0 deletions
18
crates/biome_css_formatter/tests/specs/css/selectors/class_selector.css
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,18 @@ | ||
.one {} | ||
.one.two {} | ||
|
||
.one .two {} | ||
.one.two .three.four {} | ||
|
||
|
||
div.one.two {} | ||
div#one.two {} | ||
div.one#two {} | ||
div .one {} | ||
div .one.two {} | ||
|
||
.one.two.three.four.five.six.seven.eight.nine.ten.eleven.twelve.thirteen.fourteen.fifteen.sixteen.seventeen.eighteen.nineteen.twenty {} | ||
|
||
.one, .two {} | ||
.one.two, .three.four {} | ||
.one .two.three, .four {} |
Oops, something went wrong.