Skip to content

Commit

Permalink
insert heuristic linebreak when formatting lists for some AST nodes (c…
Browse files Browse the repository at this point in the history
…lose #11)
  • Loading branch information
g-plane committed Jun 16, 2024
1 parent 9c1065b commit 494fb90
Show file tree
Hide file tree
Showing 17 changed files with 248 additions and 63 deletions.
11 changes: 5 additions & 6 deletions malva/src/doc_gen/at_rule/custom_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ impl<'s> DocGen<'s> for CustomSelectorArg<'s> {
impl<'s> DocGen<'s> for CustomSelectorArgs<'s> {
fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> {
helpers::format_parenthesized(
helpers::SeparatedListFormatter::new(",", Doc::line_or_space()).format(
&self.args,
&self.comma_spans,
self.span.start,
ctx,
),
helpers::SeparatedListFormatter::new(
",",
helpers::get_smart_linebreak(self.span.start, &self.args, ctx),
)
.format(&self.args, &self.comma_spans, self.span.start, ctx),
self.args
.last()
.map(|arg| arg.span.end)
Expand Down
16 changes: 16 additions & 0 deletions malva/src/doc_gen/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,19 @@ pub(super) fn ident_to_lowercase<'s>(
name => name.doc(ctx),
}
}

pub(super) fn get_smart_linebreak<'s, N>(
start: usize,
elements: &[N],
ctx: &Ctx<'_, 's>,
) -> Doc<'static>
where
N: Spanned,
{
match elements.first() {
Some(element) if ctx.line_bounds.line_distance(start, element.span().start) > 0 => {
Doc::hard_line()
}
_ => Doc::line_or_space(),
}
}
13 changes: 8 additions & 5 deletions malva/src/doc_gen/less.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,12 @@ impl<'s> DocGen<'s> for LessFormatFunction {
impl<'s> DocGen<'s> for LessImportOptions<'s> {
fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> {
helpers::format_parenthesized(
helpers::SeparatedListFormatter::new(",", Doc::line_or_space())
.with_trailing()
.format(&self.names, &self.comma_spans, self.span.start, ctx),
helpers::SeparatedListFormatter::new(
",",
helpers::get_smart_linebreak(self.span.start, &self.names, ctx),
)
.with_trailing()
.format(&self.names, &self.comma_spans, self.span.start, ctx),
self.names
.last()
.map(|name| name.span.end)
Expand Down Expand Up @@ -382,7 +385,7 @@ impl<'s> DocGen<'s> for LessMixinArguments<'s> {
.append(
helpers::SeparatedListFormatter::new(
if self.is_comma_separated { "," } else { ";" },
Doc::line_or_space(),
helpers::get_smart_linebreak(self.span.start, &self.args, ctx),
)
.with_trailing()
.format(
Expand Down Expand Up @@ -556,7 +559,7 @@ impl<'s> DocGen<'s> for LessMixinParameters<'s> {
helpers::format_parenthesized(
helpers::SeparatedListFormatter::new(
if self.is_comma_separated { "," } else { ";" },
Doc::line_or_space(),
helpers::get_smart_linebreak(self.span.start, &self.params, ctx),
)
.with_trailing()
.format(&self.params, &self.separator_spans, self.span.start, ctx),
Expand Down
45 changes: 30 additions & 15 deletions malva/src/doc_gen/sass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,12 @@ impl<'s> DocGen<'s> for SassConditionalClause<'s> {
impl<'s> DocGen<'s> for SassContent<'s> {
fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> {
helpers::format_parenthesized(
helpers::SeparatedListFormatter::new(",", Doc::line_or_space())
.with_trailing()
.format(&self.args, &self.comma_spans, self.span.start, ctx),
helpers::SeparatedListFormatter::new(
",",
helpers::get_smart_linebreak(self.span.start, &self.args, ctx),
)
.with_trailing()
.format(&self.args, &self.comma_spans, self.span.start, ctx),
self.args
.len()
.checked_sub(1)
Expand Down Expand Up @@ -409,9 +412,12 @@ impl<'s> DocGen<'s> for SassInclude<'s> {
impl<'s> DocGen<'s> for SassIncludeArgs<'s> {
fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> {
helpers::format_parenthesized(
helpers::SeparatedListFormatter::new(",", Doc::line_or_space())
.with_trailing()
.format(&self.args, &self.comma_spans, self.span.start, ctx),
helpers::SeparatedListFormatter::new(
",",
helpers::get_smart_linebreak(self.span.start, &self.args, ctx),
)
.with_trailing()
.format(&self.args, &self.comma_spans, self.span.start, ctx),
self.args
.len()
.checked_sub(1)
Expand Down Expand Up @@ -576,9 +582,12 @@ impl<'s> DocGen<'s> for SassList<'s> {
impl<'s> DocGen<'s> for SassMap<'s> {
fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> {
helpers::format_parenthesized(
helpers::SeparatedListFormatter::new(",", Doc::line_or_space())
.with_trailing()
.format(&self.items, &self.comma_spans, self.span.start, ctx),
helpers::SeparatedListFormatter::new(
",",
helpers::get_smart_linebreak(self.span.start, &self.items, ctx),
)
.with_trailing()
.format(&self.items, &self.comma_spans, self.span.start, ctx),
self.items
.last()
.map(|item| item.span.end)
Expand Down Expand Up @@ -616,9 +625,12 @@ impl<'s> DocGen<'s> for SassModuleConfig<'s> {
Doc::text("with ")
.concat(ctx.end_spaced_comments(self.with_span.end, self.lparen_span.start))
.append(helpers::format_parenthesized(
helpers::SeparatedListFormatter::new(",", Doc::line_or_space())
.with_trailing()
.format(&self.items, &self.comma_spans, self.lparen_span.end, ctx),
helpers::SeparatedListFormatter::new(
",",
helpers::get_smart_linebreak(self.span.start, &self.items, ctx),
)
.with_trailing()
.format(&self.items, &self.comma_spans, self.lparen_span.end, ctx),
self.items
.last()
.map(|item| item.span.end)
Expand Down Expand Up @@ -727,9 +739,12 @@ impl<'s> DocGen<'s> for SassParameters<'s> {
)
.collect::<Vec<_>>();
helpers::format_parenthesized(
helpers::SeparatedListFormatter::new(",", Doc::line_or_space())
.with_trailing()
.format(&params, &self.comma_spans, self.span.start, ctx),
helpers::SeparatedListFormatter::new(
",",
helpers::get_smart_linebreak(self.span.start, &params, ctx),
)
.with_trailing()
.format(&params, &self.comma_spans, self.span.start, ctx),
self.params
.len()
.checked_sub(1)
Expand Down
21 changes: 18 additions & 3 deletions malva/tests/fmt/scss/at-rule/each.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ source: malva/tests/fmt.rs
@each $element, $size in (h1: 20px, h2: 16px, h3: 14px) {}
@each $element, $size in (h1: 20px, h2: 16px, h3: 14px) {}
@each $element, $size in (h1: 20px, h2: 16px, h3: 14px) {}
@each $element, $size in (h1: 20px, h2: 16px, h3: 14px) {}
@each $element, $size in (h1: 20px, h2: 16px, h3: 14px) {}
@each $element, $size in (h1: 20px, h2: 16px, h3: 14px) {}
@each $element, $size in
(
h1: 20px,
h2: 16px,
h3: 14px
) {}
@each $element, $size in
(
h1: 20px,
h2: 16px,
h3: 14px
) {}
@each $element, $size in
(
h1: 20px,
h2: 16px,
h3: 14px
) {}
28 changes: 24 additions & 4 deletions malva/tests/fmt/scss/at-rule/function.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,36 @@ source: malva/tests/fmt.rs
@function func($arg, $arg1, $arg2: 10, $args...) {
@return "Func";
}
@function func($arg, $arg1, $arg2: 10, $args...) {
@function func(
$arg,
$arg1,
$arg2: 10,
$args...
) {
@return "Func";
}
@function func($arg, $arg1, $arg2: 10, $args...) {
@function func(
$arg,
$arg1,
$arg2: 10,
$args...
) {
@return "Func";
}
@function func($arg, $arg1, $arg2: 10, $args...) {
@function func(
$arg,
$arg1,
$arg2: 10,
$args...
) {
@return "Func";
}
@function func($arg, $arg1, $arg2: 10, $args...) {
@function func(
$arg,
$arg1,
$arg2: 10,
$args...
) {
@return "Func";
}
@function func(
Expand Down
33 changes: 28 additions & 5 deletions malva/tests/fmt/scss/at-rule/include.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,31 @@ source: malva/tests/fmt.rs
@include mix(1px, 2px, $arg2: 10, 2px 4px 6px);
@include mix(1px, 2px, $arg2: 10, 2px 4px 6px);
@include mix(1px, 2px, $arg2: 10, 2px 4px 6px);
@include mix(1px, 2px, $arg2: 10, 2px 4px 6px);
@include mix(1px, 2px, $arg2: 10, 2px 4px 6px);
@include mix(1px, 2px, $arg2: 10, 2px 4px 6px);
@include mix(
1px,
2px,
$arg2: 10,
2px 4px 6px
);
@include mix(
1px,
2px,
$arg2: 10,
2px 4px 6px
);
@include mix(
1px,
2px,
$arg2: 10,
2px 4px 6px
);

@include mix(1px, 2px, $arg2: 10, 2px 4px 6px);
@include mix(
1px,
2px,
$arg2: 10,
2px 4px 6px
);

@include mix(
$very-very-very-very-very-very-very-very-very-very-very-very-very-very-long-arg: 1px,
Expand Down Expand Up @@ -61,7 +81,10 @@ a {
a {
@include item-spotlight-properties-transition(
"-title",
(box-shadow: 0 3px 10px rgba(0, 0, 0, 0.15), border-color: $brand-primary)
(
box-shadow: 0 3px 10px rgba(0, 0, 0, 0.15),
border-color: $brand-primary
)
);
}

Expand Down
28 changes: 24 additions & 4 deletions malva/tests/fmt/scss/at-rule/mixin.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,30 @@ source: malva/tests/fmt.rs
@mixin mix($arg, $arg1, $arg2: 10, $args...) {}
@mixin mix($arg, $arg1, $arg2: 10, $args...) {}
@mixin mix($arg, $arg1, $arg2: 10, $args...) {}
@mixin mix($arg, $arg1, $arg2: 10, $args...) {}
@mixin mix($arg, $arg1, $arg2: 10, $args...) {}
@mixin mix($arg, $arg1, $arg2: 10, $args...) {}
@mixin mix($arg, $arg1, $arg2: 10, $args...) {}
@mixin mix(
$arg,
$arg1,
$arg2: 10,
$args...
) {}
@mixin mix(
$arg,
$arg1,
$arg2: 10,
$args...
) {}
@mixin mix(
$arg,
$arg1,
$arg2: 10,
$args...
) {}
@mixin mix(
$arg,
$arg1,
$arg2: 10,
$args...
) {}
@mixin mix(
$very-very-very-very-very-very-very-very-very-very-very-very-very-very-long-arg,
$very-very-very-very-very-very-very-very-very-very-very-very-very-very-long-arg1,
Expand Down
5 changes: 4 additions & 1 deletion malva/tests/fmt/scss/configuration/use.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ source: malva/tests/fmt.rs
$h3-font-weight: 500
);

@use "library" with ($black: #222, $border-radius: 0.1rem);
@use "library" with (
$black: #222,
$border-radius: 0.1rem
);
5 changes: 4 additions & 1 deletion malva/tests/fmt/scss/ignore/default.snap
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ a, b {}
// malva-ignore-will-not-apply
a, b {}

$size: (sm: 16px, md: 24px);
$size: (
sm: 16px,
md: 24px
);
10 changes: 8 additions & 2 deletions malva/tests/fmt/scss/parens/2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
source: malva/tests/fmt.rs
---
$icons: (
cal-day-group: (left: 253, top: 73),
/* Should preserve empty lines */ cal-week-group: (left: 1, top: 169)
cal-day-group: (
left: 253,
top: 73
),
/* Should preserve empty lines */ cal-week-group: (
left: 1,
top: 169
)
);
6 changes: 5 additions & 1 deletion malva/tests/fmt/scss/scss/arbitrary-arguments.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ $form-selectors: "input.name", "input.address", "input.zip" !default;
background-color: nth($gradients, 1);
background-image: linear-gradient($direction, $gradients...);
}
$parameters: ("c": "kittens", "a": true, "b": 42);
$parameters: (
"c": "kittens",
"a": true,
"b": 42
);
$value: dummy($parameters...);

body {
Expand Down
Loading

0 comments on commit 494fb90

Please sign in to comment.