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

Preserve attribute line breaks #772

Merged
merged 1 commit into from
Jun 10, 2020
Merged

Preserve attribute line breaks #772

merged 1 commit into from
Jun 10, 2020

Conversation

xorye
Copy link

@xorye xorye commented Jun 8, 2020

Signed-off-by: David Kwon [email protected]

@xorye
Copy link
Author

xorye commented Jun 9, 2020

This PR is ready for review.

Instead of introducing a formatting setting that keeps all newlines from non-empty lines, this PR introduces a new formatting setting, (ie, in VS Code: xml.format.preserveAttributeLineBreaks) that selectively keeps newlines from non-empty lines that come before and after element attributes.

For example:

<a \nattribute=\n"value"\n/>
    ^           ^        ^
    1           2        3

The formatter will keep 1 because it comes before an attribute.
The formatter will lose 2 because it exists inside an attribute.
The formatter will keep 3 because it comes after an attribute. (Note: formatting will only keep 3 if the element a has a self-closing tag. In this case, a it is a self-closing tag.)

These characteristics are useful in these cases:

End tag closing bracket is in a new line

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

formats to

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

Attribute name/delimiter/value on separate lines

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

formats to

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

As a plus, this setting will not will not keep newlines inside of the XML declaration (<?xml version="1.0" encoding="UTF-8"?>).

To review this PR with VS Code, please also check out redhat-developer/vscode-xml#271, which adds the xml.format.preserveAttributeLineBreaks setting:
image

Demo:

<?xml version="1.0" encoding="UTF-8"?>
<a>
    <li attribute="123456789" attribute="123456789" attribute="123456789"
        attribute="123456789" attribute="123456789" attribute="123456789"
        attribute="123456789" attribute="123456789" attribute="123456789"
        attribute="123456789" attribute="123456789" attribute="123456789"
    />
</a>

demo

@xorye xorye marked this pull request as ready for review June 9, 2020 21:05
@xorye
Copy link
Author

xorye commented Jun 9, 2020

The gif is a bit misleading since the attribute names are all aligned since the tab size is 4 spaces.

This PR will indent the attribute names depending on the element's indentation and will not align attribute names for the moment. I created a new issue for attribute aligning here: #272

@fbricon
Copy link
Contributor

fbricon commented Jun 10, 2020

The closing bracket is moved to the previous line, it wasn't supposed to happen
Jun-10-2020 13-00-49


private void formatAttributes(DOMElement element) throws BadLocationException {
List<DOMAttr> attributes = element.getAttributeNodes();
if (hasSingleAttributeInFullDoc(element)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The process looks pretty similar for one attribute and many attributes. Is it possible to avoid checking if there is one attribute, and use a general process that works for any number?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@fbricon fbricon merged commit 48444df into eclipse-lemminx:master Jun 10, 2020
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

Successfully merging this pull request may close these issues.

5 participants