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

Align XML attributes when formatting #272

Closed
xorye opened this issue Jun 9, 2020 · 12 comments · Fixed by eclipse-lemminx/lemminx#1560 or #925
Closed

Align XML attributes when formatting #272

xorye opened this issue Jun 9, 2020 · 12 comments · Fixed by eclipse-lemminx/lemminx#1560 or #925
Assignees
Labels
enhancement New feature or request formatting
Milestone

Comments

@xorye
Copy link

xorye commented Jun 9, 2020

It would be nice if there was an option to align the beginning of attribute names if the attributes exist in different lines when formatting.

Currently by default, when formatting this XML:

<a attr="value"
   attr="value">
    <b></b>
</a>

The resulting XML is:

<a attr="value" attr="value">
    <b></b>
</a>

Ideally, after formatting, the initial XML should stay as is.

@xorye
Copy link
Author

xorye commented Jun 9, 2020

In this case:

<a attr="value"
           attr="value">
    <b></b>
</a>

Since the attributes are in separate lines, it should be aligned after formatting:

<a attr="value"
   attr="value">
    <b></b>
</a>

@BinToss
Copy link

BinToss commented Jul 6, 2023

This is EditorConfig Rule xml_pi_attributes_indent as specified by ReSharper.
Possible values are single_indent, double_indent, and align_by_first_attribute.

See https://www.jetbrains.com/help/resharper/EditorConfig_XML_XmlCodeStylePageSchema.html#resharper_xml_pi_attributes_indent

@angelozerr
Copy link
Contributor

angelozerr commented Jul 6, 2023

@JessicaJHee I think we can close this issue since the provided new formatter, right?

@BinToss
Copy link

BinToss commented Jul 6, 2023

@angelozerr The documentation's example indicates the spaces/indentation of the inner element(s) are formatted, but does not state it also affects tags' attributes.

If this format rule does affect attributes, then the documentation could be clarified.

Edit: also, there's no way to specify that the rule should only apply to attributes i.e. inner elements' spaces should be formatted, but not the attributes.

@JessicaJHee
Copy link
Contributor

The xml.format.splitAttributes setting applies to attributes and not inner elements.
Currently, when this setting is set to true. the following is formatted from

<project a="1" b="2" c="3"></project>

to:

<project
    a="1"
    b="2"
    c="3"></project>

To address the use case in this comment, we can update this settings with a new option: preserve (currently xml.format.splitAttributes = true), split-new-line (currently xml.format.splitAttributes = false), and align-with-first-attr.

The new setting would format to:

<project a="1"
         b="2"
         c="3"></project>

What do you think about this solution @BinToss?

@BinToss
Copy link

BinToss commented Jul 18, 2023

That looks perfect!
That solution may also resolve #631...of which this issue is probably a duplicate.

On the subject of editorconfig files and rules, it seems there's been community interest in this extension supporting them, but that's a different issue with a different solution. A quick search in Lemminx's Issues returned no results.

@angelozerr
Copy link
Contributor

angelozerr commented Jul 26, 2023

@BinToss for your information, @JessicaJHee has implemented the format settings of this issue (thanks so much @JessicaJHee !).

See doc at https://github.com/redhat-developer/vscode-xml/blob/main/docs/Formatting.md#xmlformatsplitattributes

It would be nice if tomorrow you could install prrelease https://github.com/redhat-developer/vscode-xml#contributing and give us feedback. Thanks!

@angelozerr
Copy link
Contributor

angelozerr commented Jul 27, 2023

@BinToss the prerelease is avaiable, please play with it and give us feedback. Thanks!

@BinToss
Copy link

BinToss commented Jul 27, 2023

Roger that. I should have time to try it out this weekend.

@BinToss
Copy link

BinToss commented Jul 31, 2023

Excellent work!

The only thing people may complain about is when the formatter can't handle a file with "mixed" formatting—that is, they desire both preserve and alignWithFirstAttr in the same file. Or they may want preserve without affecting alignment.
However, I'd say this request is resolved.

@angelozerr
Copy link
Contributor

Thanks @BinToss for your feedback.

Thanks so much again @JessicaJHee for your great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment