Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to maintain single/multi lines for html attribute #101

Open
rod24574575 opened this issue Feb 3, 2025 · 3 comments
Open

Allow to maintain single/multi lines for html attribute #101

rod24574575 opened this issue Feb 3, 2025 · 3 comments

Comments

@rod24574575
Copy link

rod24574575 commented Feb 3, 2025

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,
};
@g-plane
Copy link
Owner

g-plane commented Feb 3, 2025

Does #42 help you? If so, add 👍 there.

@rod24574575
Copy link
Author

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"
/>

@UnknownPlatypus
Copy link
Contributor

@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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants