Skip to content

Commit

Permalink
add perfer_attrs_single_line option (fix #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Mar 1, 2024
1 parent 3ad3f58 commit 16ae477
Show file tree
Hide file tree
Showing 23 changed files with 279 additions and 10 deletions.
31 changes: 31 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ Default option is `"fit"`.
Control the maximum number of attributes in one line.
If this option is unset, there won't be any limitations.

This option conflicts with `perferAttrsSingleLine` option.

Default option is `null`. This option can't be `0`.

### Example for `null`
Expand Down Expand Up @@ -262,6 +264,35 @@ Default option is `null`. This option can't be `0`.
></div>
```

## `preferAttrsSingleLine`

Control whether attributes should be put on single line when possible.

This option conflicts with `maxAttrsPerLine` option.

Default option is `false`.

### Example for `false`

This `<div>` is short enough to be put on single line,
but it won't because attributes in source code span multiple lines.

```html
<div
data-a
data-b
></div>
```

### Example for `true`

This `<div>` is short enough so it will be put on single line
though attributes span multiple lines in source code.

```html
<div data-a data-b></div>
```

## `*.selfClosing`

This group of options controls whether an element should be self-closed or not if
Expand Down
4 changes: 4 additions & 0 deletions dprint_plugin/deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@
"default": null,
"minimum": 1
},
"preferAttrsSingleLine": {
"type": "boolean",
"default": false
},
"html.normal.selfClosing": {
"type": ["boolean", "null"],
"default": null
Expand Down
6 changes: 6 additions & 0 deletions dprint_plugin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ pub(crate) fn resolve_config(
"maxAttrsPerLine",
&mut diagnostics,
),
prefer_attrs_single_line: get_value(
&mut config,
"preferAttrsSingleLine",
false,
&mut diagnostics,
),
html_normal_self_closing: get_nullable_value(
&mut config,
"html.normal.selfClosing",
Expand Down
1 change: 1 addition & 0 deletions markup_fmt/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct VueDirective<'s> {
pub struct Element<'s> {
pub tag_name: &'s str,
pub attrs: Vec<Attribute<'s>>,
pub first_attr_same_line: bool,
pub children: Vec<Node<'s>>,
pub self_closing: bool,
pub void_element: bool,
Expand Down
4 changes: 4 additions & 0 deletions markup_fmt/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ pub struct LanguageOptions {
/// See [`maxAttrsPerLine`](https://github.com/g-plane/markup_fmt/blob/main/docs/config.md#maxattrsperline) on GitHub
pub max_attrs_per_line: Option<NonZeroUsize>,

#[cfg_attr(feature = "config_serde", serde(alias = "preferAttrsSingleLine"))]
/// See [`preferAttrsSingleLine`](https://github.com/g-plane/markup_fmt/blob/main/docs/config.md#preferattrssingleline) on GitHub
pub prefer_attrs_single_line: bool,

#[cfg_attr(
feature = "config_serde",
serde(rename = "html.normal.self_closing", alias = "html.normal.selfClosing")
Expand Down
14 changes: 13 additions & 1 deletion markup_fmt/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,16 @@ impl<'s> Parser<'s> {
let void_element = helpers::is_void_element(tag_name, self.language.clone());

let mut attrs = vec![];
let mut first_attr_same_line = true;
loop {
self.skip_ws();
match self.chars.peek() {
Some((_, '/')) => {
self.chars.next();
if self.chars.next_if(|(_, c)| *c == '>').is_some() {
return Ok(Element {
tag_name,
attrs,
first_attr_same_line,
children: vec![],
self_closing: true,
void_element,
Expand All @@ -434,13 +435,23 @@ impl<'s> Parser<'s> {
return Ok(Element {
tag_name,
attrs,
first_attr_same_line,
children: vec![],
self_closing: false,
void_element,
});
}
break;
}
Some((_, '\n')) => {
if attrs.is_empty() {
first_attr_same_line = false;
}
self.chars.next();
}
Some((_, c)) if c.is_ascii_whitespace() => {
self.chars.next();
}
_ => {
attrs.push(self.parse_attr()?);
}
Expand Down Expand Up @@ -501,6 +512,7 @@ impl<'s> Parser<'s> {
Ok(Element {
tag_name,
attrs,
first_attr_same_line,
children,
self_closing: false,
void_element,
Expand Down
17 changes: 15 additions & 2 deletions markup_fmt/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,19 @@ impl<'s> DocGen<'s> for Element<'s> {
Cow::from(self.tag_name)
}));

let attrs_sep = if !self.first_attr_same_line
&& !ctx.options.prefer_attrs_single_line
&& self.attrs.len() > 1
&& !ctx
.options
.max_attrs_per_line
.map(|value| value.get() > 1)
.unwrap_or_default()
{
Doc::hard_line()
} else {
Doc::line_or_space()
};
let attrs = if let Some(max) = ctx.options.max_attrs_per_line {
// fix #2
if self.attrs.is_empty() {
Expand All @@ -218,7 +231,7 @@ impl<'s> DocGen<'s> for Element<'s> {
Doc::list(
itertools::intersperse(
chunk.iter().map(|attr| attr.doc(ctx)),
Doc::line_or_space(),
attrs_sep.clone(),
)
.collect(),
)
Expand All @@ -231,7 +244,7 @@ impl<'s> DocGen<'s> for Element<'s> {
Doc::list(
self.attrs
.iter()
.flat_map(|attr| [Doc::line_or_space(), attr.doc(ctx)].into_iter())
.flat_map(|attr| [attrs_sep.clone(), attr.doc(ctx)].into_iter())
.collect(),
)
.nest_with_ctx(ctx)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: markup_fmt/tests/fmt.rs
---
<div></div>

<div a="a"></div>

<div a="a" b="b" c="c" d="d"></div>

<div
a="a"
b="b"
c="c"
d="d"
>
</div>

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: markup_fmt/tests/fmt.rs
---
<div></div>

<div a="a"></div>

<div
a="a"
b="b"
c="c"
d="d"
>
</div>

<div
a="a"
b="b"
c="c"
d="d"
>
</div>

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: markup_fmt/tests/fmt.rs
---
<div></div>

<div a="a"></div>

<div
a="a" b="b"
c="c" d="d"
>
</div>

<div
a="a" b="b"
c="c" d="d"
>
</div>

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: markup_fmt/tests/fmt.rs
---
<div></div>

<div a="a"></div>

<div a="a" b="b" c="c" d="d"></div>

<div
a="a"
b="b"
c="c"
d="d"
>
</div>

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: markup_fmt/tests/fmt.rs
---
<div></div>

<div a="a"></div>

<div
a="a"
b="b"
c="c"
d="d"
>
</div>

<div
a="a"
b="b"
c="c"
d="d"
>
</div>

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: markup_fmt/tests/fmt.rs
---
<div></div>

<div a="a"></div>

<div
a="a" b="b"
c="c" d="d"
>
</div>

<div
a="a" b="b"
c="c" d="d"
>
</div>

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: markup_fmt/tests/fmt.rs
---
<div></div>

<div a="a"></div>

<div a="a" b="b" c="c" d="d"></div>

<div a="a" b="b" c="c" d="d"></div>

11 changes: 11 additions & 0 deletions markup_fmt/tests/fmt/html/prefer-attrs-single-line/attrs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div>
</div>

<div
a="a"
></div>

<div a="a" b="b" c="c" d="d"></div>

<div
a="a" b="b" c="c" d="d"></div>
23 changes: 23 additions & 0 deletions markup_fmt/tests/fmt/html/prefer-attrs-single-line/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[default]

[disabled]
preferAttrsSingleLine = false

[enabled]
preferAttrsSingleLine = true

[disabled-with-max-one-attr-per-line]
preferAttrsSingleLine = false
maxAttrsPerLine = 1

[enabled-with-max-one-attr-per-line]
preferAttrsSingleLine = true
maxAttrsPerLine = 1

[disabled-with-max-two-attr-per-line]
preferAttrsSingleLine = false
maxAttrsPerLine = 2

[enabled-with-max-two-attr-per-line]
preferAttrsSingleLine = true
maxAttrsPerLine = 2
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ source: markup_fmt/tests/fmt.rs

<marquee direction="up">This text will scroll from bottom to top</marquee>

<marquee direction="down" width="250" height="200" behavior="alternate" style="border:solid">
<marquee
direction="down"
width="250"
height="200"
behavior="alternate"
style="border:solid"
>
<marquee behavior="alternate"> This text will bounce </marquee>
</marquee>

Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@ source: markup_fmt/tests/fmt.rs

<p>Want to write us a letter? Use our<a href="contacts.html#Mailing_address"><b><a>mailing address</a></b></a>.</p>

<p>Want to write us a letter? Use our<a href="contacts.html#Mailing_address" href1="contacts.html#Mailing_address" href2="contacts.html#Mailing_address" href3="contacts.html#Mailing_address" href4="contacts.html#Mailing_address"><b><a>mailing address</a></b></a>.</p>
<p>
Want to write us a letter? Use our<a
href="contacts.html#Mailing_address"
href1="contacts.html#Mailing_address"
href2="contacts.html#Mailing_address"
href3="contacts.html#Mailing_address"
href4="contacts.html#Mailing_address"
><b><a>mailing address</a></b></a>.
</p>

7 changes: 6 additions & 1 deletion markup_fmt/tests/fmt/html/tags/tags.default.snap
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ source: markup_fmt/tests/fmt.rs
<div>string</div>
</div>
<ul>
123<li class="foo" id="bar">First</li>
123<li
class="foo"
id="bar"
>
First
</li>
456<li class="baz">Second</li>
789
</ul>
Expand Down
Loading

0 comments on commit 16ae477

Please sign in to comment.