Skip to content

Commit

Permalink
feat: optimize formatting pseudo class selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Jul 27, 2024
1 parent c67e811 commit d43394d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
17 changes: 15 additions & 2 deletions malva/src/doc_gen/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,18 @@ impl<'s> DocGen<'s> for PseudoClassSelector<'s> {
docs.push(Doc::text("("));

let arg_span = arg.kind.span();
let force_break = ctx
.line_bounds
.line_distance(arg.l_paren.end, arg_span.start)
> 0;
let mut arg_doc = vec![];

if ctx.options.linebreak_in_pseudo_parens {
arg_doc.push(Doc::line_or_nil());
if force_break {
arg_doc.push(Doc::hard_line());
} else {
arg_doc.push(Doc::line_or_nil());
}
}

arg_doc.extend(ctx.end_spaced_comments(arg.l_paren.end, arg_span.start));
Expand All @@ -321,7 +329,12 @@ impl<'s> DocGen<'s> for PseudoClassSelector<'s> {
}
PseudoClassSelectorArgKind::SelectorList(selector_list) => {
if ctx.options.linebreak_in_pseudo_parens {
selector_list.doc(ctx).group().nest(ctx.indent_width)
let doc = selector_list.doc(ctx);
if force_break {
doc
} else {
doc.group()
}
} else {
helpers::SeparatedListFormatter::new(",", Doc::space()).format(
&selector_list.selectors,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@
.js-resolvable-timeline-thread-container[data-resolved='true'],
.js-resolvable-timeline-thread-container[data-resolved='false'] .minimized-comment
) {}

/* force break */
:where(
.a,
.b, .c
) {}
17 changes: 12 additions & 5 deletions malva/tests/fmt/css/pseudo-call/linebreak-in-pseudo-parens.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ source: malva/tests/fmt.rs
/*cfg linebreakInPseudoParens = true*/
:where(
#p0:checked ~ #play:checked ~ #c1:checked,
#p1:checked ~ #play:checked ~ #c2:checked,
#p2:checked ~ #play:checked ~ #cO:checked
#p1:checked ~ #play:checked ~ #c2:checked,
#p2:checked ~ #play:checked ~ #cO:checked
) {}
::unknown(
#p0:checked ~ #play:checked ~ #c1:checked, #p1:checked ~ #play:checked ~ #c2:checked, #p2:checked ~ #play:checked ~ #co:checked
Expand All @@ -14,7 +14,14 @@ source: malva/tests/fmt.rs
[data-rgh-conversation-activity-filter="hideEventsAndCollapsedComments"]
:is(
.rgh-conversation-activity-collapsed,
.js-resolvable-timeline-thread-container[data-resolved="true"],
.js-resolvable-timeline-thread-container[data-resolved="false"]
.minimized-comment
.js-resolvable-timeline-thread-container[data-resolved="true"],
.js-resolvable-timeline-thread-container[data-resolved="false"]
.minimized-comment
) {}

/* force break */
:where(
.a,
.b,
.c
) {}

0 comments on commit d43394d

Please sign in to comment.