How to disable code formatting for some part of the code using comments? - intellij-idea

I'd like to selectively disable the IntelliJ IDEA code formatter for a portion of code just like I can in Eclipse. Does IntelliJ support this feature, and if so, how do I use it?

Since version 13 it's possible to wrap the code with
// #formatter:off
...
// #formatter:on
IntelliJ IDEA v.2018+:
File > Settings > Editor > Code Style
IntelliJ IDEA v.2016+:
Preferences > Editor > Code Style
IntelliJ IDEA v.14+:
Preferences > Editor > Code Style > Formatter Control
You can change the formatter control markers, as long as they're in comments.
Ensure formatter markers in comments are enabled, as shown in the following figure:

Note for Carlos' answer of #formatter:off and #formatter:on to work, you may need to enable the preference first:
Discovered via How to config intellij-idea not format some part of the code?
IDEA-56995 Disabled code formatting per region using comments

In xml files, use:
<!-- #formatter:off -->
<!-- #formatter:on -->

Obsolete answer from 2010:
No, it's not possible right now, I've submitted a new issue which you can track.
As a temporary solution you can use external formatter plug-in for IDEA which can utilize Eclipse code formatter (I didn't check whether this particular option is supported).

It's currently not possible to exclude entire files (like regression test data xmls) from formatting. See https://youtrack.jetbrains.com/issue/IDEA-167112

Related

Intellij Code completion in (s)css annoying

I am developing in IntelliJ IDEA mainly scss and js. When I want to type
th, td {
some style
}
Intellij autocompletes td to "text-decoration:;" (and th to text-height) which is highly annoying. I have checked all my settings and plugins and am currently lost. I have the setting "Code Completion > Insert selected suggestion" turned off under "Show suggestions as you type".
My enabled custom plugins are Emmet Everywhere, Save Actions and Vue.js. Disabling those does not solve anything.
I would honestly love to enable autocompletion per language/per extension and not as a global setting.
Any thoughts?
As #lena pointed out, Settings > Live templates > By default expand with was set on space which caused this behaviour. With so many options it can sometimes be hard to find unusual settings.

Is there a plugin to tell IntelliJ not to format a particular block when I use auto format?

My question is a duplicate of: How to turn off the Eclipse code formatter for certain sections of Java code? but for IntelliJ.
Does a similar feature exist for IntelliJ?
Yes, and It works the same way with:
#formatter:off
and
#formatter:on
Navigate to:
Settings > Editor > Code Style > Formatter control

How do I turn on syntax highlighting for StringTemplate templates (*.st) in intellij?

I have a string template file (.st) in IntelliJ that I would like to see syntax highlighting on. How can I turn this on? I can't seem to find it anywhere.
The best solution is to use HTML syntax for *.st files. You can set this in Preference > File types > HTML.
Short answer is - you can't.
Someone must first implement a syntax highlighter plugin for the format.
The StringTemplate author, Terrence Parr, has begun work on such a plugin for String Template 4 but it does not support the older .st file format.
I tried the plugin. Even on .stg files it is not ideal if you use a dark editor theme.

Intellij idea deprecated no strikeout/strikethrough

I'm using IntelliJ IDEA v12. When I import a project, there’s a strikeout/strikethrough line on the method which has the annotation #Deprecated, but I used alt+enter to disable it. So, how can I reactivate this feature? I want to display that strikethrough, thanks!
Open Settings -> Type Inspections to go to inspection settings -> Type Deprecated to find out all deprecated related inspection settings -> Check Deprecated API usage.
IntelliJ had a bug in 2018.3, 2018.2.2 (182.4129.33). It was fixed in builds 191.65, 183.3691.
Setting -Dide.text.effect.new.metrics=false in the .vmoptions fixes the problem.
In addition to Ionghua's answer .....
If you find that IntelliJ is still not striking out deprecated methods, even though you have enabled the inspection as above, check to make sure that your code is not ignoring deprecation warnings with #SuppressWarnings("deprecation").
In addition to longhua's answer... and vegemite4me...
Another possible source for this problem might be custom color themes.
If you are using a custom theme, make sure that it contains strikethrough. I was using monokai-sublime and there is no strikethrough for #Deprecated annotations. When I changed the color theme from monokai to darcula strikethrough worked as expected.
I found that the only thing that worked for me with version 2019.2 192.5728.98, runtime 11.0.3+12-b304.10 was to use Help | Edit VM Options and add -Dide.text.effect.new.metrics=false

IntelliJ wrapping comments in Javascript

Is it possible to wrap long comment lines in IntelliJ in javascript files? I have turned on line wrapping as shown in the settings below, but when I do a code reformat, it doesn't wrap the line.
It's not supported yet on code reformatting, added a new feature request, please vote.
I'm using IntelliJ 14 on a Mac, which has a Fill Paragraph command. Access it via the awesome universal Command-Shift-A action search feature. Works like a charm!