prettier icon indicating copy to clipboard operation
prettier copied to clipboard

Prettier prints 1 attribute per line

Open themafia98 opened this issue 7 years ago • 30 comments

Input:

<div class="main__image-list main-image-list--margin main__image-list-one"></div>

Output:

<div
      class="main__image-list main-image-list--margin main__image-list-one"
    ></div>

Another example Input:

   <link
      rel="stylesheet"href="https://use.fontawesome.com/releases/v5.6.3/css/all.css"  integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous" />

Output:

   <link
      rel="stylesheet"
      href="https://use.fontawesome.com/releases/v5.6.3/css/all.css"
      integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/"
      crossorigin="anonymous"
    />

And more others tags (img.....)

this is a bug or a trick? What do you think about ?

themafia98 avatar Jan 13 '19 14:01 themafia98

The <link> case is intended. There are several long attributes and they don’t fit on a single line, so Prettier put them on separate lines.

The <div> case looks odd though. If there’s just one attribute we don’t gain much from moving it to a new line.

Prettier 1.15.3 Playground link

--parser html

Input:

<div class="main__image-list main-image-list--margin main__image-list-one final-class"></div>

Output:

<div
  class="main__image-list main-image-list--margin main__image-list-one final-class"
></div>

Compare with JSX:

Prettier 1.15.3 Playground link

--parser babylon

Input:

<div class="main__image-list main-image-list--margin main__image-list-one final-class"></div>

Output:

<div class="main__image-list main-image-list--margin main__image-list-one final-class" />;

Maybe we should bring that special case to HTML as well.

lydell avatar Jan 13 '19 14:01 lydell

The issue here is that we don't know if the content of the attribute can be broken into multiple line so we did not force one attribute to be single line, for example we may add support for class (#5482), which should break it into multiple line if it does not fit the print width.

ikatyang avatar Jan 13 '19 14:01 ikatyang

@ikatyang None of the examples have multiline attributes though so it's not the case of forcing them to be single line.

I think what @lydell is suggesting is that we keep tags with a single attribute and no children in a single line even if it exceeds print width:

<div class="main__image-list main-image-list--margin main__image-list-one final-class"></div>

duailibe avatar Jan 14 '19 11:01 duailibe

I meant if we force 1 attr to be single line, the breakParent magic would no longer work:

Prettier 1.15.3 Playground link

--parser html

Input:

<img srcset="/assets/[email protected]  400w, /assets/visual.png      805w,	/assets/[email protected]   1610w,	/assets/[email protected]   2415w" />

Output:

<img
  srcset="
    /assets/[email protected]  400w,
    /assets/visual.png      805w,
    /assets/[email protected]  1610w,
    /assets/[email protected]  2415w
  "
/>

Output if we force 1 attr to be single line:

<img srcset="
    /assets/[email protected]  400w,
    /assets/visual.png      805w,
    /assets/[email protected]  1610w,
    /assets/[email protected]  2415w
  "/>

ikatyang avatar Jan 14 '19 13:01 ikatyang

Yeah obviously in that case we wouldn't force it to be single line, just like we don't force it with JSX:

Prettier 1.15.3 Playground link

--parser babylon

Input:

<Foo bar="foo
	bar" />;

Output:

<Foo
  bar="foo
	bar"
/>;

duailibe avatar Jan 14 '19 14:01 duailibe

Not sure if my case refers to this issue, but it seems kinda similar. I've set Prettier VSC extension html.format.wrapLineLength and prettier.printWidth properties to the same value. Every time the line length is larger that defined, it causes not needed formatting - each property goes into the new line:

<grid-column
	field="formatCode"
	title="Format"
	width="65"
	class="status-column cell-with-button"
	*ngIf="width > 1"
	[class]="{ codeColumn: true }"
	[hidden]="columns[7].hidden"
></grid-column>

I would like to have the line wrapped only to necessary number of lines, which means the next property which exceed the line length goes to next line etc.:

<grid-column field="incoTermCode" title="Incoterm" width="65"
	class="status-column cell-with-button" *ngIf="width > 1"
	[class]="{ codeColumn: true }" [hidden]="columns[7].hidden"
></grid-column>

I'm not sure if this feature already exists, but it would be nice to have it.

mpro7 avatar Jan 16 '19 13:01 mpro7

That’s not the way Prettier works. We decided it’s usually more readable to split each thing onto its own line.

j-f1 avatar Jan 16 '19 14:01 j-f1

Could be this feature possible as optional @j-f1 ? If not, could somebody suggest please another tool/solution to get expected result?

mpro7 avatar Jan 16 '19 20:01 mpro7

@mpro7 No, it's been suggested in the past and we've already ruled out. An alternative is to fork Prettier and change this behavior.

duailibe avatar Jan 17 '19 14:01 duailibe

@duailibe thanks for your suggestion. I may have a try, will be this possible to release with prettier or as forked side-project only?

mpro7 avatar Jan 17 '19 14:01 mpro7

As forked side-project only.

duailibe avatar Jan 17 '19 15:01 duailibe

@duailibe, so maybe there is an option to disable just Prettier line wrapping - I cannot see it right now? I do not want to remove Prettier at all, because I like it's other features, but I think that with this option I would get what I want with inbuilt Visual Studio Code formatting rules.

mpro7 avatar Jan 22 '19 15:01 mpro7

Correct. Line wrapping is at the heart of Prettier and cannot be disabled.

lydell avatar Jan 22 '19 15:01 lydell

I had the same issue and realized @lydell is right. So I went op my settings, searched prettier and found 'Prettier: Print Width' option which was set to 80. I increased it to 150 and it worked for me. This answer is late but maybe someone else will find this.

clumsyChef avatar Nov 27 '19 11:11 clumsyChef

Thanks sjMonsta, it works. "prettier.printWidth": 150

andrew-ma avatar Dec 10 '19 07:12 andrew-ma

+1, breaking 100 attribute to 100 line doesn't make sense. the printWidth rule should be respected and when there is still room to fill in one line, why break things in a new line?

image

here for a small text like icon=something & size="m" spared one line each?

mhamri avatar Jan 09 '20 04:01 mhamri

why break things in a new line?

I would say fewer merge conflicts and cleaner change history. Though i would love if there was some threshold and until reaching it prettier wouldn't break attributes into multiline format.

silvermario avatar Jan 17 '20 13:01 silvermario

This is exactly what printWidth (default: 80) does @silvermario, per the docs:

Print Width
Specify the line length that the printer will wrap on.

amcolash avatar Jan 20 '20 03:01 amcolash

@silvermario it should be a separate option, html.respectLineBreak:false if not the line break is a rule, it shouldn't behave two different way for two different file type

mhamri avatar Jan 20 '20 09:01 mhamri

I cannot understand the reason for putting every attribute on separate line. With the new version I see changes which will be valid for this case as well

// Prettier 1.19
if (
  foo
    .one()
    .two()
    .three() ===
  bar
    .four()
    .five()
    .six()
) {
  // ...
}

// Prettier 2.0
if (foo.one().two().three() === bar.four().five().six()) {
  // ...
}

Lonli-Lokli avatar Apr 21 '20 12:04 Lonli-Lokli

@Lonli-Lokli Hi! Have you read through the thread? As mentioned, if the attributes don’t fit in a single line they’re split into one per line. That’s how Prettier works. If you don’t like that, Prettier is probably not the tool you are looking for.

lydell avatar Apr 21 '20 13:04 lydell

@lydell my concern is not about splitting single attribute into several lines but splitting each attribute on separate line, like mentioned in https://github.com/prettier/prettier/issues/5751#issuecomment-454776010.

That's the way how Js works before 2.0

Lonli-Lokli avatar Apr 21 '20 14:04 Lonli-Lokli

@Lonli-Lokli I’m sorry, I don’t understand what you mean.

lydell avatar Apr 21 '20 15:04 lydell

https://github.com/prettier/prettier/issues/5751#issuecomment-454776010

This will work. Go to setting and find html.format.wrapLineLength and prettier.printWidth change to 300 :) Work perfect. Ex:

<input 
className="input disabled" 
type="number" 
placeholder="Origin price " 
value="59" />

will change to <input className="input disabled" type="number" placeholder="Origin price " value="59" /> No hurt to your eyes

IRediTOTO avatar May 05 '20 14:05 IRediTOTO

problem still exist does someone has solution for this ?

tahonaPL avatar Jul 16 '20 19:07 tahonaPL

up = )

EvelRus avatar Jul 01 '21 07:07 EvelRus

It's almost 2.5 years. 😢

liudonghua123 avatar Jul 01 '21 08:07 liudonghua123

problem still exist does someone has solution for this ?

Its not clear, but printWidth fixes this https://github.com/prettier/prettier/issues/5751#issuecomment-576096259

iantearle avatar Jun 01 '22 12:06 iantearle

不确定我的案例是否涉及这个问题,但看起来有点相似。我已将Prettier VSC 扩展 html.format.wrapLineLengthprettier.printWidth属性设置为相同的值。每次行长大于定义的长度时,都会导致不需要的格式 - 每个属性都进入新行:

<grid-column
	field="formatCode"
	title="Format"
	width="65"
	class="status-column cell-with-button"
	*ngIf="width > 1"
	[class]="{ codeColumn: true }"
	[hidden]="columns[7].hidden"
></grid-column>

我希望只将行换行到必要的行数,这意味着超过行长度的下一个属性转到下一行等:

<grid-column field="incoTermCode" title="Incoterm" width="65"
	class="status-column cell-with-button" *ngIf="width > 1"
	[class]="{ codeColumn: true }" [hidden]="columns[7].hidden"
></grid-column>

我不确定这个功能是否已经存在,但拥有它会很好。

I think you're right, when there are many properties, it will take up a lot of rows. Even each item of the array wraps. I think it makes sense to wrap when the property doesn't fit in one line

chen-ccy avatar Jun 17 '23 16:06 chen-ccy