4

I'd like to keep some space between some html blocks in my code. However, Prettier wants to remove these lines. Is there a Prettier setting to keep this spacing?

Before prettier:

<template>
  <form>

    <div>First block</div>

    <div>Second block</div>
    
  </form>
</template>

After pretter:

<template>
  <form>
    <div>First block</div>

    <div>Second block</div>
  </form>
</template>
Mads Hjorth
  • 439
  • 1
  • 9
  • 23
  • I never used Prettier, but there seems to be a configuration section in their documentation: https://prettier.io/docs/en/configuration.html I hope this helps – Nicholas Obert Jun 28 '22 at 08:41

4 Answers4

0

There is no way to actually do this, you might only use tricks.

First trick

Ignore completely the file (or file extensions) but you will not benefit from Prettier

Second trick

Deny Prettier to trim whitespaces

trim_trailing_whitespace = true

And add whitespaces in line breaks. Once again this is not an effective solution, it's only a trick.

An issue has been closed by devs

adding and removing line breaks is core to prettier's formatting algorithm

IQbrod
  • 2,060
  • 1
  • 6
  • 28
0

According to @suchipi , Github member:

adding and removing line breaks is core to prettier's formatting algorithm, so preserving them isn't consistently possible.

As a work around you can try:

  • setting very short printWidth
  • put // prettier-ignore comment above your code.

For Refernce:

krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
0

Don't know if this will work as intended however, in the document for Prettier, there is a section for "Empty Line" that says;

"Prettier collapses multiple blank lines into a single blank line."

By this, you may want to add a double blank line instead of a single blank, where you are wanting a space to be, as it should compress it into a single line rather than removing it all together.

Aidan H M
  • 11
  • 3
0

Take a look on this thread https://github.com/prettier/prettier/issues/4870, it's pretty good explaining why this is still non-existing feature.

There is no simple way for formatter to determine what to keep and what to remove.

So the only practical things you can do

  • Add comments to disable formatting in certain places (which is bad idea and never work well, dev gents become annoyed with this super quickly).

  • Accept that empty lines would be collapsed (still bad, because of poor readability)

  • Disable this feature and maintain empty lines yourself (alternative editor). This one "arguably" seems to me the simplest (best) option in a long-term.

Maksym
  • 820
  • 1
  • 8