Prettier - allow line breaks between html elements - visual-studio-code

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>

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

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:
Github issue
Is there a config in prettier to keep line breaks?

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.

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.

Related

VS Code: Prettier splits paragraph text to multiple lines

I have a long text in p tag and Prettier splits the text in this tag to multiple lines. I have 'word wrap' enabled.
How can I stop this from happening?
Before Save:
After Save:
Prettier is opinionated, it has only limited number of options.
If you couldn’t find what you want in https://prettier.io/docs/en/options.html, so it’s not supported.
In general, better to accept Prettier behavior instead of fight against it.
The reformatting, that prettier does, doesn’t harm.
You can try to increase width.

VSCode Prettier attribute formatting

I'm using prettier to format my Vue files, but can't find to seem a setting that puts props/attributes of html elements to new lines.
I'd like to make sure that my code is always formatted as it is for v-text-fieldin a yellow frame, rather than below. I set Prose Wrap to "never", but I guess that was not it. Any help appreciated!
You can set the setting of Print Width to higher value.
That's what Prettier does: breaks lines if they get too long. It doesn't make sense to use Prettier if you don't need its line-breaking behavior as this behavior and Prettier are one and the same.

Comparing files in eclipse show same whitespaces as different

Why does the compare files in Eclipse show difference between an identical line that starts and/or stops with white spaces?
Why would anyone ever want this "feature" anyway, all lines become marked as different. Must be a bug.
I know I can use the ignore white spaces setting, but then it ignores the differens in block indentation as well and I don't want that.
Forgot to answer this one.
It was an extra carriage return on one side (PC newline, and the other had Mac/*nix), that would make it ignore the "ignore whitespace" setting for those lines.

Eclipse: Automatic line wrapping to specified width

I'd like for my lines, especially within comments, to be automatically managed so they don't get too long.
I remember once I had a configuration for vim which automatically moved the word I was typing to the next line once I reached 72 characters. It wasn't smart enough to rearrange the paragraph if I edit it, but it was a start.
Is there something that can manage these for me? I have a tendency to write really long comments in my code, and it helps to make them look neat by having consistent width, but it's always a pain to do this because oftentimes editing a sentence requires editing the entire rest of the paragraph.
I have just recently discovered the Ctrl+Shift+F feature. It is amazing and superior to Ctrl+I which is what I was using up till now, but I noticed that it does not do anything to clean up my comments.
Update: The answers are correct when working with Java in Eclipse. It seems like I have to wait for the CDT to incorporate this feature.
In "Windows -> Preferences", go to "Java -> Code style -> Formatter" to customize the formatter (called when you click Ctrl+Shift+F). In the tab "comment", you can set the maximum line width for comments (it can be different then the line width for code).
Tip: in the preferences, "Java -> Editor -> Save actions", you can make Eclipse to automatically format your file when you save it, so your code is always correctly indented !
The automatic formatting of Eclipse great no question.
If your comments are reformatted depends on what comment type and how you already have inserted line breaks.
Writing for example one very long line comment starting with // will be broken down by the formatter into multiple lines.
However you later edit the formatted lines - e.g. delete parts of it the formatter will leave them as they are. Only over-long lines will be changed.
Just in difference to block comments like this: /* comment */
Those comments will always be re-formatted in case the line is too short or too long.
If you want to format your header comment, you have to check Enable header comment formatting - that was the trick for me.
Obviously, to use this, you must create new formatter profile.

Eclipse automated code formation and Hyperlinks

I'm a big fan of the automated code formation (STRG + SHIFT + F) from eclipse. It makes your code so much more readable. However, now that I'm comenting my code, I'm getting the problem with Hyperlinks. Code formation adds line breaks anywhere in your code, so if you have e.g. a very long hyperlink it breaks into multiple lines and makes it unresolvable :(
Is there a way that eclipse doesn't format specific comment parts like Hyperlinks?
Regards,
Stefan
Code formating doesn't put line breaks in links inside a tags:
<a href=
"http://www.example.com/very-long-url">Example</a>
There is a line break just after the href=, so it may look ugly in a paragraph. Enclosing urls in <tt> tags prevents line breaks, but the url won't be a clickable link. Combine both and you get hyperlinks without line breaks. If they exceed the maximum line length, they will start on a new line though.
<tt>Example</tt>
There is also //#formatter:off to disable formatting for the following lines and //#formatter:on to enable it again.
I don't think there is any way from setting that up in the menu. The workaround is to disable block or line comment formatting in the formatter profile:
Window -> Preferences -> Java -> Code Style -> Formatter -> Edit -> tab Comments
As pointed out by Kheldar, you can always customise/extend the Java Codeformatter using the extension point, but that's probably not a one hour job for someone unfamiliar with the JDT.
Please indicate, if you need any assistance coding an extension for the formatter.