Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap HTML attributes on new lines set by user when formatting in Visual Studio Code

In our team some people use Webstorm and others use Visual Studio Code. Webstorm aligns attributes vertically when they're put on a new line by the user. It will align with the last attribute in the line. Visual Studio Code has a setting where you can wrap the attributes on format, but it wraps all attributes.

It would be nice if both editors would handle formatting the same.

I've tried several extensions (Prettier, Beautify, Alignment, Code alignment, Better Align) in Visual Studio Code, but none does what I want it to do. Does anybody know of any setting or extension I might have missed that does what I want? Or is it simply not possible in Visual Studio Code?

User setting

"html.format.wrapAttributes": "force-aligned",

Result (how VS Code aligns attributes)

<input type="text"
       name="input_name"
       placeholder="What's your name">

Desired result (how Webstorm aligns attributes)

<input type="text" name="input_name"
                   placeholder="What's your name">
like image 403
Paul van den Dool Avatar asked Apr 25 '18 10:04

Paul van den Dool


People also ask

How do you stop prettier in VS code splitting attributes onto multiple lines?

A quick fix is to go to Prettier Extension Settings (ctrl + shift + X) and in Prettier Extension Settings search for "Print Width" set it to 250 or anything that works for you. 2: Change the value of Print Width to your liking. To disable format code on save.

How do I beautify a HTML file in Visual Studio code?

To improve the formatting of your HTML source code, you can use the Format Document command Ctrl+Shift+I to format the entire file or Format Selection Ctrl+K Ctrl+F to just format the selected text. The HTML formatter is based on js-beautify.


1 Answers

Try these options for the html.format.wrapAttributes setting.

  // Wrap attributes.
  //  - auto: Wrap attributes only when line length is exceeded.
  //  - force: Wrap each attribute except first.
  //  - force-aligned: Wrap each attribute except first and keep aligned.
  //  - force-expand-multiline: Wrap each attribute.
  //  - aligned-multiple: Wrap when line length is exceeded, align attributes vertically.
like image 89
Nox Avatar answered Sep 23 '22 16:09

Nox