-
-
Notifications
You must be signed in to change notification settings - Fork 535
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): format functions and binary expressions (#1347)
- Loading branch information
1 parent
5ae49cd
commit 3d05420
Showing
24 changed files
with
406 additions
and
142 deletions.
There are no files selected for viewing
22 changes: 19 additions & 3 deletions
22
crates/biome_css_formatter/src/css/auxiliary/binary_expression.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,26 @@ | ||
use crate::prelude::*; | ||
use biome_css_syntax::CssBinaryExpression; | ||
use biome_rowan::AstNode; | ||
use biome_css_syntax::{CssBinaryExpression, CssBinaryExpressionFields}; | ||
use biome_formatter::write; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatCssBinaryExpression; | ||
impl FormatNodeRule<CssBinaryExpression> for FormatCssBinaryExpression { | ||
fn fmt_fields(&self, node: &CssBinaryExpression, f: &mut CssFormatter) -> FormatResult<()> { | ||
format_verbatim_node(node.syntax()).fmt(f) | ||
let CssBinaryExpressionFields { | ||
left, | ||
operator_token, | ||
right, | ||
} = node.as_fields(); | ||
|
||
write!( | ||
f, | ||
[ | ||
left.format(), | ||
space(), | ||
operator_token.format(), | ||
soft_line_break_or_space(), | ||
right.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,23 @@ | ||
use crate::prelude::*; | ||
use biome_css_syntax::CssColor; | ||
use biome_rowan::AstNode; | ||
use crate::utils::string_utils::FormatTokenAsLowercase; | ||
use biome_css_syntax::{CssColor, CssColorFields}; | ||
use biome_formatter::write; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatCssColor; | ||
impl FormatNodeRule<CssColor> for FormatCssColor { | ||
fn fmt_fields(&self, node: &CssColor, f: &mut CssFormatter) -> FormatResult<()> { | ||
format_verbatim_node(node.syntax()).fmt(f) | ||
let CssColorFields { | ||
hash_token, | ||
value_token, | ||
} = node.as_fields(); | ||
|
||
write!( | ||
f, | ||
[ | ||
hash_token.format(), | ||
FormatTokenAsLowercase::from(value_token?) | ||
] | ||
) | ||
} | ||
} |
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,11 +1,13 @@ | ||
use crate::prelude::*; | ||
use biome_css_syntax::CssParameter; | ||
use biome_rowan::AstNode; | ||
use biome_css_syntax::{CssParameter, CssParameterFields}; | ||
use biome_formatter::write; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatCssParameter; | ||
impl FormatNodeRule<CssParameter> for FormatCssParameter { | ||
fn fmt_fields(&self, node: &CssParameter, f: &mut CssFormatter) -> FormatResult<()> { | ||
format_verbatim_node(node.syntax()).fmt(f) | ||
let CssParameterFields { any_css_expression } = node.as_fields(); | ||
|
||
write!(f, [any_css_expression.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
22 changes: 19 additions & 3 deletions
22
crates/biome_css_formatter/src/css/auxiliary/url_function.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,26 @@ | ||
use crate::prelude::*; | ||
use biome_css_syntax::CssUrlFunction; | ||
use biome_rowan::AstNode; | ||
use biome_css_syntax::{CssUrlFunction, CssUrlFunctionFields}; | ||
use biome_formatter::write; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatCssUrlFunction; | ||
impl FormatNodeRule<CssUrlFunction> for FormatCssUrlFunction { | ||
fn fmt_fields(&self, node: &CssUrlFunction, f: &mut CssFormatter) -> FormatResult<()> { | ||
format_verbatim_node(node.syntax()).fmt(f) | ||
let CssUrlFunctionFields { | ||
url_token, | ||
l_paren_token, | ||
any_css_url_value, | ||
r_paren_token, | ||
} = node.as_fields(); | ||
|
||
write!( | ||
f, | ||
[ | ||
url_token.format(), | ||
l_paren_token.format(), | ||
any_css_url_value.format(), | ||
r_paren_token.format() | ||
] | ||
) | ||
} | ||
} |
9 changes: 6 additions & 3 deletions
9
crates/biome_css_formatter/src/css/auxiliary/url_value_raw.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::CssUrlValueRaw; | ||
use biome_rowan::AstNode; | ||
use biome_css_syntax::{CssUrlValueRaw, CssUrlValueRawFields}; | ||
use biome_formatter::write; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatCssUrlValueRaw; | ||
impl FormatNodeRule<CssUrlValueRaw> for FormatCssUrlValueRaw { | ||
fn fmt_fields(&self, node: &CssUrlValueRaw, f: &mut CssFormatter) -> FormatResult<()> { | ||
format_verbatim_node(node.syntax()).fmt(f) | ||
let CssUrlValueRawFields { 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) mod string_utils; |
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,39 @@ | ||
use std::borrow::Cow; | ||
|
||
use crate::prelude::*; | ||
use biome_css_syntax::CssLanguage; | ||
use biome_formatter::{ | ||
prelude::{dynamic_text, write}, | ||
token::string::ToAsciiLowercaseCow, | ||
trivia::format_replaced, | ||
Format, FormatResult, | ||
}; | ||
use biome_rowan::SyntaxToken; | ||
|
||
use crate::{prelude::CssFormatContext, AsFormat, CssFormatter}; | ||
|
||
pub(crate) struct FormatTokenAsLowercase { | ||
token: SyntaxToken<CssLanguage>, | ||
} | ||
|
||
impl From<SyntaxToken<CssLanguage>> for FormatTokenAsLowercase { | ||
fn from(value: SyntaxToken<CssLanguage>) -> Self { | ||
Self { token: value } | ||
} | ||
} | ||
|
||
impl Format<CssFormatContext> for FormatTokenAsLowercase { | ||
fn fmt(&self, f: &mut CssFormatter) -> FormatResult<()> { | ||
let original = self.token.text_trimmed(); | ||
match original.to_ascii_lowercase_cow() { | ||
Cow::Borrowed(_) => write!(f, [self.token.format()]), | ||
Cow::Owned(lowercase) => write!( | ||
f, | ||
[format_replaced( | ||
&self.token, | ||
&dynamic_text(&lowercase, self.token.text_trimmed_range().start()), | ||
)] | ||
), | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -148,10 +148,3 @@ Line width: 80 | |
``` | ||
|
||
|
||
|
||
## Unimplemented nodes/tokens | ||
|
||
"0" => 564..565 | ||
" " => 566..567 | ||
" 25" => 569..572 | ||
|
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
31 changes: 31 additions & 0 deletions
31
crates/biome_css_formatter/tests/specs/css/color/functional_colors.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,31 @@ | ||
.foo { | ||
color: rgb(255 , 0 , 153); | ||
color: rgb(100%, | ||
0%, | ||
60% | ||
); | ||
color: rgba( | ||
51, 170 , 51, 0.1 ) | ||
; | ||
color: | ||
hsl( | ||
.75turn, | ||
60% , 70%); | ||
color: hsla( 240 , 100% , 50%, .05) | ||
} | ||
|
||
.whitespace { | ||
color: rgba( 51 | ||
170 | ||
51 / 0.4); | ||
color: rgba( | ||
51 170 51 / 40%); | ||
color: hsl(270 60% 50% / .15); | ||
color: hsla(240 100% 50% | ||
/ .05); | ||
color: hsla( | ||
240 | ||
100% | ||
50% | ||
/ 5%); | ||
} |
Oops, something went wrong.