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

核能气质少年 提交于 2019-11-26 04:36:18

问题


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?


回答1:


Since version 13 it's possible to wrap the code with

// @formatter:off
...
// @formatter:on 

In versions 2016.x enable it in Preferences > Editor > Code Style. (Thanks Philipe)

In version 2018.x enable it in File > Settings > Editor > Code Style (you can change the formatter control markers, as long as they're in comments).

In version 14, enable it in Preferences > Editor > Code Style > Formatter Control (Thanks Emerson Farrugia)




回答2:


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




回答3:


In xml files use:

<!-- @formatter:off -->
<!-- @formatter:on -->



回答4:


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).




回答5:


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



来源:https://stackoverflow.com/questions/3375307/how-to-disable-code-formatting-for-some-part-of-the-code-using-comments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!