Skip to content

Commit

Permalink
refactor formatting Sass list
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Jun 19, 2024
1 parent d9e06f4 commit fab18e5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
63 changes: 32 additions & 31 deletions malva/src/doc_gen/sass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,7 @@ impl<'s> DocGen<'s> for SassEach<'s> {
.append(Doc::text("in"))
.append(helpers::format_operator_suffix_space(ctx))
.concat(ctx.end_spaced_comments(self.in_span.end, self.expr.span().start))
.append(if let ComponentValue::SassList(sass_list) = &self.expr {
sass_list.doc(ctx)
} else {
self.expr.doc(ctx).nest(ctx.indent_width)
})
.append(self.expr.doc(ctx).nest(ctx.indent_width))
.group()
}
}
Expand Down Expand Up @@ -770,10 +766,7 @@ impl<'s> DocGen<'s> for SassParenthesizedExpression<'s> {
ctx.end_spaced_comments(self.span.start, expr_span.start)
.collect(),
)
.append(match &*self.expr {
ComponentValue::SassList(sass_list) => sass_list.doc(ctx),
expr => expr.doc(ctx),
}),
.append(self.expr.doc(ctx)),
expr_span.end,
self.span.end,
ctx,
Expand Down Expand Up @@ -874,29 +867,37 @@ impl<'s> DocGen<'s> for SassVariableDeclaration<'s> {
docs.extend(ctx.start_spaced_comments(self.name.span.end, self.colon_span.start));
docs.push(Doc::text(":"));

let should_group = if let ComponentValue::SassList(SassList {
elements,
comma_spans: Some(comma_spans),
span,
..
}) = &self.value
{
docs.push(Doc::line_or_space());
docs.extend(ctx.end_spaced_comments(self.colon_span.end, value_span.start));
docs.push(
helpers::SeparatedListFormatter::new(",", Doc::line_or_space())
.with_trailing()
.format(elements, comma_spans, span.start, ctx),
);
if elements.len() == 1 {
docs.push(Doc::text(","));
let should_group = match &self.value {
ComponentValue::SassList(SassList {
elements,
comma_spans: Some(comma_spans),
span,
..
}) => {
docs.push(Doc::line_or_space());
docs.extend(ctx.end_spaced_comments(self.colon_span.end, value_span.start));
docs.push(
helpers::SeparatedListFormatter::new(",", Doc::line_or_space())
.with_trailing()
.format(elements, comma_spans, span.start, ctx),
);
if elements.len() == 1 {
docs.push(Doc::text(","));
}
true
}
ComponentValue::SassList(sass_list) => {
docs.push(Doc::space());
docs.extend(ctx.end_spaced_comments(self.colon_span.end, value_span.start));
docs.push(sass_list.doc(ctx).nest(ctx.indent_width));
false
}
_ => {
docs.push(Doc::space());
docs.extend(ctx.end_spaced_comments(self.colon_span.end, value_span.start));
docs.push(self.value.doc(ctx));
false
}
true
} else {
docs.push(Doc::space());
docs.extend(ctx.end_spaced_comments(self.colon_span.end, value_span.start));
docs.push(self.value.doc(ctx));
false
};

docs.extend(
Expand Down
2 changes: 1 addition & 1 deletion malva/src/doc_gen/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl<'s> DocGen<'s> for ComponentValue<'s> {
}
ComponentValue::SassBinaryExpression(sass_binary_expr) => sass_binary_expr.doc(ctx),
ComponentValue::SassKeywordArgument(sass_keyword_arg) => sass_keyword_arg.doc(ctx),
ComponentValue::SassList(sass_list) => sass_list.doc(ctx).nest(ctx.indent_width),
ComponentValue::SassList(sass_list) => sass_list.doc(ctx),
ComponentValue::SassMap(sass_map) => sass_map.doc(ctx),
ComponentValue::SassQualifiedName(sass_qualified_name) => sass_qualified_name.doc(ctx),
ComponentValue::SassNestingDeclaration(sass_nesting_decl) => sass_nesting_decl.doc(ctx),
Expand Down

0 comments on commit fab18e5

Please sign in to comment.