Skip to content

Commit

Permalink
feat: detect special cases of whitespaces and numbers
Browse files Browse the repository at this point in the history
in functions (close #15)
  • Loading branch information
g-plane committed Oct 14, 2024
1 parent 33c3d26 commit 4aa773b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
42 changes: 25 additions & 17 deletions malva/src/doc_gen/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,23 +299,31 @@ impl<'s> DocGen<'s> for Function<'s> {
ctx: &Ctx<'_, 's>,
state: &State,
) -> Doc<'s> {
Doc::list(
itertools::intersperse(
group.iter().map(|arg| {
let arg_span = arg.span();
Doc::list(
ctx.end_spaced_comments(ctx.get_comments_between(
mem::replace(pos, arg_span.end),
arg_span.start,
))
.collect(),
)
.append(arg.doc(ctx, state))
}),
separator,
)
.collect(),
)
Doc::list(group.iter().enumerate().fold(
Vec::with_capacity(group.len()),
|mut docs, (i, arg)| {
let arg_span = arg.span();
if i > 0 && *pos < arg_span.start {
docs.push(separator.clone());
}
docs.extend(ctx.end_spaced_comments(
ctx.get_comments_between(mem::replace(pos, arg_span.end), arg_span.start),
));
match arg {
ComponentValue::Number(number)
if i > 0
&& matches!(
group.get(i - 1),
Some(ComponentValue::InterpolableIdent(..))
) =>
{
docs.push(Doc::text(number.raw));
}
_ => docs.push(arg.doc(ctx, state)),
}
docs
},
))
}

let separator = if args_groups.len() == 1 {
Expand Down
4 changes: 2 additions & 2 deletions malva/tests/fmt/css/color/color-adjuster.snap
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ source: malva/tests/fmt.rs
color: color(red hue(+20deg));
color: color(red hue(-20));
color: color(red hue(-20deg));
color: color(red hue(* 20));
color: color(red hue(* 20deg));
color: color(red hue(*20));
color: color(red hue(*20deg));
color: color(var(--highlightColor) blackness(+ 20%));
}
3 changes: 3 additions & 0 deletions malva/tests/fmt/css/functions/theme.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:root {
--color-accent-fg: theme(colors.slate.100);
}
6 changes: 6 additions & 0 deletions malva/tests/fmt/css/functions/theme.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: malva/tests/fmt.rs
---
:root {
--color-accent-fg: theme(colors.slate.100);
}
2 changes: 1 addition & 1 deletion malva/tests/fmt/css/postcss-8-improment/test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ https://github.com/postcss/postcss/releases/tag/8.0.0
--javascript: function(rule) { console.log(rule) };
}

@supports (--element(".minwidth", { "minWidth" : 300 })) {
@supports (--element(".minwidth", { "minWidth": 300 })) {
[--self] {
background: greenyellow;
}
Expand Down
6 changes: 3 additions & 3 deletions malva/tests/fmt/scss/parens/parens.snap
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ a {
prop31: (#{$width} / 2px) (8px / #{$width}) (#{$width} / 2px) (
8px / #{$width}
);
prop32: func(8px / 2);
prop32: func(8px/2);
prop33: 5px + 8px/2px;
prop34: func(+20px, +20px);
prop35: 1 + 1 + 1 + 1;
Expand Down Expand Up @@ -96,8 +96,8 @@ a {
prop72: 1 - "test" 1 - "test" 1 - "test" 1 - "test";
prop73: calc(100% * 2px) calc(100% * 2px) calc(100% * 2px) calc(100% * 2px);
prop74: calc(100% / 2px) calc(100% / 2px) calc(100% / 2px) calc(100% / 2px);
prop75: calc(100% +2px) calc(100% +2px) calc(100% + 2px) calc(100% + 2px);
prop76: calc(100% -2px) calc(100% -2px) calc(100% - 2px) calc(100% - 2px);
prop75: calc(100%+2px) calc(100% +2px) calc(100% + 2px) calc(100% + 2px);
prop76: calc(100%-2px) calc(100% -2px) calc(100% - 2px) calc(100% - 2px);
prop77: calc(-5px);
prop78: calc(+5px);
prop79: calc(-100px + 100px);
Expand Down

0 comments on commit 4aa773b

Please sign in to comment.