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 line breaks when formatting #247

Closed
pierrecorsini opened this issue Apr 24, 2020 · 14 comments
Closed

Preserve line breaks when formatting #247

pierrecorsini opened this issue Apr 24, 2020 · 14 comments
Assignees
Labels
enhancement New feature or request formatting
Milestone

Comments

@pierrecorsini
Copy link

Hi

Context
A big XML file with long lines, line breaks are used to keep lines visually undestandable (and max 120 chars). Using the formatter, line breaks are removed resulting in extremely long lines (300 chars+). In the other hand, a line break per attribute is not a solution.

Question
How to prevent line breaks refactoring during formatting ? -> "Keep my line breaks"

Thanks

@angelozerr
Copy link
Contributor

@pc-robelbois could you attach a simple XML file which causes your problem please.

@pierrecorsini
Copy link
Author

@angelozerr Here is a minimal sample.

Before

<a>
    <l1 attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" 
        attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" 
        attribute="123456789" attribute="123456789" attribute="123456789" />

    <l1 attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" 
        attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" 
        attribute="123456789" attribute="123456789" attribute="123456789" />
    
    <l1 attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" 
        attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" 
        attribute="123456789" attribute="123456789" attribute="123456789" />
    <l1 attribute="123456789" attribute="123456789" attribute="123456789" 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>

After formatting

<a>
    <l1 attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" />

    <l1 attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" />

    <l1 attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" attribute="123456789" />
    <l1 attribute="123456789" attribute="123456789" attribute="123456789" 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>

@fbricon fbricon added enhancement New feature or request formatting labels Apr 24, 2020
@fbricon
Copy link
Collaborator

fbricon commented Apr 24, 2020

@pc-robelbois what editor/IDE do you usually use to format your XML documents while preserving line breaks between attributes?

@pierrecorsini
Copy link
Author

@pc-robelbois what editor/IDE do you usually use to format your XML documents while preserving line breaks between attributes?

Previously, theses files were edited as raw text files (not formatter).

@fbricon
Copy link
Collaborator

fbricon commented Apr 24, 2020

@angelozerr FYI, here are the Intellij IDEA settings related to word wrapping in XML docs:
Adding a hard wrap limit might also help getting consistent formatting, in @pc-robelbois's case:
Screen Shot 2020-04-24 at 1 06 16 PM

@angelozerr
Copy link
Contributor

@fbricon HTML Language Server have

"html.format.wrapLineLength": 120,

I think it's a setting which could fix this issue?

@fbricon @pc-robelbois what do you think ?

@pierrecorsini
Copy link
Author

@angelozerr I do identify two different features.

  • I want to preserve the line breaks of existing lines.
  • I want to split the line so that max line length < X.

While both are imo required, my request is about the first one. Your proposal is about the second one.

@fbricon
Copy link
Collaborator

fbricon commented Apr 24, 2020

indeed, that why I said it might also help.

@fbricon fbricon added this to the 0.12.0 milestone May 13, 2020
@fbricon
Copy link
Collaborator

fbricon commented Jun 3, 2020

Let's add "xml.format.preserveLineBreaks":true, that would apply for non empty lines.
For empty lines, there's already "xml.format.preservedNewlines": 0-4,

@xorye
Copy link

xorye commented Jun 9, 2020

Hi @pc-robelbois, I made a PR for this issue: eclipse-lemminx/lemminx#772 that preserves the line breaks that exist beside attributes, instead of preserving all line breaks of all non-empty lines.

Here is a demo:
demo

The idea was behind it is that in some cases, it may be beneficial to remove newlines while keeping some.

For example, in this case:

<li attr="value" attr="value"
    attr="value" attr="value"></li


>

it would be nice to have it formatted to:

<li attr="value" attr="value"
    attr="value" attr="value"></li>

and

<li attr="value" attr="value"
    attr="value" attr
=

"value"></li>

formatted to

<li attr="value" attr="value"
    attr="value" attr="value"></li>

How does this sound?

@pierrecorsini
Copy link
Author

Hi @pc-robelbois, I made a PR for this issue: eclipse/lemminx#772 that preserves the line breaks that exist beside attributes, instead of preserving all line breaks of all non-empty lines.

Here is a demo:
demo

The idea was behind it is that in some cases, it may be beneficial to remove newlines while keeping some.

For example, in this case:

<li attr="value" attr="value"
    attr="value" attr="value"></li


>

it would be nice to have it formatted to:

<li attr="value" attr="value"
    attr="value" attr="value"></li>

and

<li attr="value" attr="value"
    attr="value" attr
=

"value"></li>

formatted to

<li attr="value" attr="value"
    attr="value" attr="value"></li>

How does this sound?

This sounds amazing. Yes formatting line breaks in some cases such as described seems acceptable.

@xorye
Copy link

xorye commented Jun 9, 2020

Great, thank you for the feedback.

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

PR eclipse-lemminx/lemminx#772 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

@angelozerr
Copy link
Contributor

Can we close this issue?

@fbricon
Copy link
Collaborator

fbricon commented Jun 17, 2020

Closing as we provided xml.format.preserveAttributeLineBreaks

@fbricon fbricon closed this as completed Jun 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request formatting
Projects
None yet
Development

No branches or pull requests

4 participants