Skip to content

Commit

Permalink
add singleLineBlockThreshold option
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Mar 23, 2024
1 parent c0a7ffe commit 21c1a3e
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 7 deletions.
49 changes: 49 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,52 @@ div {
height: 0;
}
```

## `singleLineBlockThreshold`

Control the threshold value for putting block on a single line.
If the number of statements in a block is less than or equal to this value, the block will be put on a single line as possible,
but when the code can't fit on single line, it will still break into multiple lines.

Default option value is `null` which means always break into multiple lines. The option value can be an integer which is greater than or equal to 0.

### Example for `null`

[Playground](https://malva-play.vercel.app/?code=H4sIAAAAAAAAA0tUqOZSUNDXUkjOz81NzStR0NLnquXiSgQLl2emlGRYKRhYc9UCAMWBkeUoAAAA&config=H4sIAAAAAAAAA6vmUlBQKs7MS89J9cnMS3XKyU%2FODskoSi3OyM9JUbJSyCvNyeGqBQBUWHQkJgAAAA%3D%3D&syntax=css)

```css
a {
/* comment */
}

a {
width: 0;
}
```

### Example for `1`

[Playground](https://malva-play.vercel.app/?code=H4sIAAAAAAAAA0tUqOZSUNDXUkjOz81NzStR0NLnquXiSgQLl2emlGRYKRhYYxNSUMhIzUzPKIHIAwD%2FjJ20SAAAAA%3D%3D&config=H4sIAAAAAAAAA6vmUlBQKs7MS89J9cnMS3XKyU%2FODskoSi3OyM9JUbJSMOSqBQD%2F4H9jIwAAAA%3D%3D&syntax=css)

```css
a { /* comment */ }

a { width: 0; }

a {
width: 0;
height: 0;
}
```

### Example for `2`

[Playground](https://malva-play.vercel.app/?code=H4sIAAAAAAAAA0tUqOZSUNDXUkjOz81NzStR0NLnquXiSgQLl2emlGRYKRhYYxNSUMhIzUzPKIHIAwD%2FjJ20SAAAAA%3D%3D&config=H4sIAAAAAAAAA6vmUlBQKs7MS89J9cnMS3XKyU%2FODskoSi3OyM9JUbJSMOKqBQCmXjlhIwAAAA%3D%3D&syntax=css)

```css
a { /* comment */ }

a { width: 0; }

a { width: 0; height: 0; }
```
5 changes: 5 additions & 0 deletions dprint_plugin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ pub(crate) fn resolve_config(
None
}
}),
single_line_block_threshold: get_nullable_value(
&mut config,
"singleLineBlockThreshold",
&mut diagnostics,
),
},
};

Expand Down
4 changes: 4 additions & 0 deletions malva/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ pub struct LanguageOptions {
#[cfg_attr(feature = "config_serde", serde(alias = "declarationOrder"))]
/// See [`declarationOrder`](https://github.com/g-plane/malva/blob/main/docs/config.md#declarationorder) on GitHub
pub declaration_order: Option<DeclarationOrder>,

#[cfg_attr(feature = "config_serde", serde(alias = "singleLineBlockThreshold"))]
/// See [`singleLineBlockThreshold`](https://github.com/g-plane/malva/blob/main/docs/config.md#singlelineblockthreshold) on GitHub
pub single_line_block_threshold: Option<usize>,
}

#[derive(Clone, Debug, Default)]
Expand Down
40 changes: 33 additions & 7 deletions malva/src/doc_gen/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ impl<'s> DocGen<'s> for SimpleBlock<'s> {
docs.push(Doc::text("{"));
}

let line_break_doc = match ctx.options.single_line_block_threshold {
Some(threshold) if self.statements.len() <= threshold => Doc::line_or_space(),
_ => Doc::hard_line(),
};

let mut stmt_docs = vec![];
if !self.statements.is_empty()
|| ctx
Expand All @@ -175,22 +180,32 @@ impl<'s> DocGen<'s> for SimpleBlock<'s> {
.count()
> 0
{
stmt_docs.push(Doc::hard_line());
stmt_docs.push(line_break_doc.clone());
}

format_statements(&mut stmt_docs, &self.statements, &self.span, ctx);
format_statements(
&mut stmt_docs,
&self.statements,
&self.span,
line_break_doc.clone(),
ctx,
);

let has_stmts = !stmt_docs.is_empty();
docs.push(Doc::list(stmt_docs).nest(ctx.indent_width));
if has_stmts {
docs.push(Doc::hard_line());
docs.push(line_break_doc);
}

if !is_sass {
docs.push(Doc::text("}"));
}

Doc::list(docs)
if ctx.options.single_line_block_threshold.is_some() {
Doc::list(docs).group()
} else {
Doc::list(docs)
}
}
}

Expand Down Expand Up @@ -253,7 +268,13 @@ impl<'s> DocGen<'s> for Statement<'s> {
impl<'s> DocGen<'s> for Stylesheet<'s> {
fn doc(&self, ctx: &Ctx<'_, 's>) -> Doc<'s> {
let mut stmt_docs = vec![];
format_statements(&mut stmt_docs, &self.statements, &self.span, ctx);
format_statements(
&mut stmt_docs,
&self.statements,
&self.span,
Doc::hard_line(),
ctx,
);
if ctx.syntax != Syntax::Sass {
stmt_docs.push(Doc::empty_line());
}
Expand All @@ -265,6 +286,7 @@ fn format_statements<'s>(
docs: &mut Vec<Doc<'s>>,
statements: &[Statement<'s>],
outer_span: &Span,
line_break_doc: Doc<'s>,
ctx: &Ctx<'_, 's>,
) {
docs.reserve(statements.len() * 2);
Expand All @@ -291,6 +313,7 @@ fn format_statements<'s>(
&mut pos,
outer_span,
true, /* ignore_leading_whitespace */
line_break_doc.clone(),
ctx,
),
));
Expand Down Expand Up @@ -336,6 +359,7 @@ fn format_statements<'s>(
&mut pos,
outer_span,
false, /* ignore_leading_whitespace */
line_break_doc.clone(),
ctx,
));
is_first_stmt_or_decls_group = false;
Expand All @@ -349,6 +373,7 @@ fn format_statements<'s>(
&mut pos,
outer_span,
false, /* ignore_leading_whitespace */
line_break_doc.clone(),
ctx,
));
}
Expand Down Expand Up @@ -377,6 +402,7 @@ fn format_single_stmt<'s>(
pos: &mut usize,
outer_span: &Span,
ignore_leading_whitespace: bool,
line_break_doc: Doc<'s>,
ctx: &Ctx<'_, 's>,
) -> Vec<Doc<'s>> {
let mut docs = Vec::with_capacity(3);
Expand All @@ -389,7 +415,7 @@ fn format_single_stmt<'s>(
if has_comments && *pos > outer_span.start {
match ctx.line_bounds.line_distance(*pos, comment.span.start) {
0 => docs.push(Doc::space()),
1 => docs.push(Doc::hard_line()),
1 => docs.push(line_break_doc.clone()),
_ => {
docs.push(Doc::empty_line());
docs.push(Doc::hard_line());
Expand All @@ -404,7 +430,7 @@ fn format_single_stmt<'s>(

if has_comments && *pos > outer_span.start {
if ctx.line_bounds.line_distance(*pos, span.start) <= 1 {
docs.push(Doc::hard_line());
docs.push(line_break_doc);
} else {
docs.push(Doc::empty_line());
docs.push(Doc::hard_line());
Expand Down
11 changes: 11 additions & 0 deletions malva/tests/fmt/css/single-line-block-threshold/0.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*cfg singleLineBlockThreshold = 0*/
a {
}

a { /* comment */ }

a {
/* comment */
}

a { width: 0 }
14 changes: 14 additions & 0 deletions malva/tests/fmt/css/single-line-block-threshold/0.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: malva/tests/fmt.rs
---
/*cfg singleLineBlockThreshold = 0*/
a {}

a { /* comment */ }

a { /* comment */ }

a {
width: 0;
}

17 changes: 17 additions & 0 deletions malva/tests/fmt/css/single-line-block-threshold/1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*cfg singleLineBlockThreshold = 1*/
a {
width: 0;
}

a { /* leading */
width: 0; /* trailing */
}

a {
content: "very very very very very very very very very very very very very very long";
}

a {
width: 0;
height: 0;
}
17 changes: 17 additions & 0 deletions malva/tests/fmt/css/single-line-block-threshold/1.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: malva/tests/fmt.rs
---
/*cfg singleLineBlockThreshold = 1*/
a { width: 0; }

a { /* leading */ width: 0; /* trailing */ }

a {
content: "very very very very very very very very very very very very very very long";
}

a {
width: 0;
height: 0;
}

28 changes: 28 additions & 0 deletions malva/tests/fmt/css/single-line-block-threshold/2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*cfg singleLineBlockThreshold = 2*/
a {
}

a {
width: 0
}

a {
width: 0;
height: 0
}

a {
width: 0; /* width */
height: 0; /* height */
}

a {
width: 0;
content: "very very very very very very very very very very very very very very long"
}

a {
width: 0;
height: 0;
display: block;
}
23 changes: 23 additions & 0 deletions malva/tests/fmt/css/single-line-block-threshold/2.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: malva/tests/fmt.rs
---
/*cfg singleLineBlockThreshold = 2*/
a {}

a { width: 0; }

a { width: 0; height: 0; }

a { width: 0; /* width */ height: 0; /* height */ }

a {
width: 0;
content: "very very very very very very very very very very very very very very long";
}

a {
width: 0;
height: 0;
display: block;
}

0 comments on commit 21c1a3e

Please sign in to comment.