Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ is not formatting HTML

Have been using IntelliJ for years, and been happy and hope this is a fluke. IntelliJ is not formatting HTML code when I do Ctrl-Alt-L. The dialog pops, and it pretends to format, but no code is actually moved. I switched extension of the file from HTML to XML, and formatting worked as expected. Actually I noticed this in Freemarker templates, but then experimented with pure HTML with the same results. My version is 13.0.2

Any help is appreciated.

like image 394
ipolevoy Avatar asked Apr 22 '14 00:04

ipolevoy


People also ask

How do I beautify HTML in IntelliJ?

In the Project tool window, right-click a module or a directory and select Reformat Code or press Ctrl+Alt+L . In the dialog that opens, specify the reformatting options.

How do I fix format in IntelliJ?

Sometimes code formatting can get out of sync, but there's an easy fix in IntelliJ IDEA. You can use ⌘⌥L (macOS), or Ctrl+Alt+L (Windows/Linux) to reformat a selection of code according to your reformat settings.

Does IntelliJ work with HTML?

HTML IntelliJ IDEA brings powerful support for HTML that includes syntax and error highlighting, formatting according to the code style, structure validation, code completion, on-the-fly preview during a debugging session (Live Edit) or in the dedicated preview tab in the code editor, and much more.


4 Answers

Had the same problem, the statements under the tag were not indented by default. Reformatting and auto-indentation worked after I changed the following:

Settings -> Editor -> Code Style -> HTML -> Other (tab) -> Do not indent children of: -> remove "body" from the list.

Maybe it is the same for you or something related at this section. Hope it works for you aswell.

like image 157
w32-mydoom Avatar answered Oct 07 '22 22:10

w32-mydoom


Settings > Editor > Code Style > HTML.
Check your "tabs and indents", but most important, "other".

For example, the default "insert new lines before" value is "body,div,p,form,h1,h2,h3"

When I export my code style as .xml, (Settings > Editor > Code Style > Html > Manage button), I get a file default.xml with the following contents:

<code_scheme name="Default" />

You could try creating a "default.xml" file with this content, and using "import scheme". The fact that it's empty points out that all settings are IntelliJ default (as far as I know).

Also, make sure your extension is .html, as this is what intelliJ uses to detect languages. Make sure you format the whole file (in case it'd be formatting an empty selection, or it could be trying to format VCS changed text while there is none.

You could also try checking Settings > Editor > File types > Html:
This list should contain all extensions for which HTML syntax check and formatting is used. (see also How can I open a file and see the HTML highlights in Intellij IDEA? )

I use IntelliJ 2016.1, but this part shouldn't be any different from IntelliJ 13

like image 33
Bertware Avatar answered Oct 07 '22 23:10

Bertware


Note that invalid HTML will not be reformatted. So if your badly outlined HTML is actually invalid as well, you can reformat all you like, but the lines that are not valid will not change.

This can be confusing at times, but once you know it it makes for good eye-catchers hinting at invalid code.

Common example, misuse of block elements:

<span>
    <span>1</span>
    <span>1</span>
</span>
like image 2
Alex Avatar answered Oct 07 '22 23:10

Alex


You can also check if you are using any Code Style plugins and try turning them off - looks like they take precedence over and may give unexpected results. At least for me, it was the solution.

In the Settings/Preferences dialog (Ctrl+Alt+S), click Plugins.

like image 1
Piotr Niewinski Avatar answered Oct 07 '22 23:10

Piotr Niewinski