markup_fmt
markup_fmt copied to clipboard
Allow to maintain single/multi lines for html attribute
I found that this plugin would always format html with single attribute to single line when the line length does not exceed the print-width.
before:
<div
class="foo"
/>
after:
<div class="foo" />
Is there any config to change the behavior to maintain the original single/multi lines status, just like the default behavior of dprint's typescript plugin?
For example, dprint's typescript plugin would not change the format of the following code.
const a = { foo: 0 };
const b = {
foo: 0,
};
Does #42 help you? If so, add 👍 there.
No. That issue adds "preferSingleLineOpeningTag" to force single line for single attribute. However, I would like a formatter to maintain the single/multi lines as the original code.
That is, I would expect the following results.
before:
<div class="foo" />
<div
class="foo"
/>
<div class="foo"
id="foo"
/>
<div
class="foo" id="foo"
/>
after:
<div class="foo" />
<div
class="foo"
/>
<div class="foo" id="foo" />
<div
class="foo"
id="foo"
/>
@rod24574575 The behavior you are looking for can already be achieved if you have more than one attribute by inserting a newline before the first attr (if you have the setting preferAttrsSingleLine set to false, but it's the default).
For ex this will be formatted on a single line:
-<div class="foo"
- id="foo"
-/>
+<div class="foo" id="foo" />
But this will be on multiple lines
-<div
- class="foo" id="foo"
-/>
+<div
+ class="foo"
+ id="foo"
+/>
See playground
As for the single attribute use case, see #42 for discussions on the matter. We ended up not making it configurable because we don't see compelling reasons to have a node with a single attr splat on multiple lines. We did make an exception if the attribute is initially on multiple lines tho (see this commit).
@rod24574575 Would you like to do as @UnknownPlatypus said on latest version and see if it solves your problem?
Sorry for the late reply. I think the new feature added by @UnknownPlatypus is great! However, without configuration options, it might not be the best fit for me.
I definitely agree that having a formatter to help format HTML or Vue would be awesome, but I'm still struggling between choosing convenience or formatting comfort.
After all, the biggest reason I prefer dprint over prettier is that it allows me to configure it into a format that feels really comfortable (although performance is also one of the factors I consider).
However, after those changes, single attribute will not always be on single line. This meets your requirements, right?
yes
So I think we can close this issue.
Thanks for looking into this! Just to clarify, was the issue closed because this feature (preserving the formatting of single attributes) is not currently planned, rather than because it has been completed?
It looks not too hard to achieve this, and I will evaluate this in recent. Once we can, I will re-open this issue.