首页 文章

如何使用注释禁用代码的某些部分的代码格式?

提问于
浏览
220

我想有选择地禁用the IntelliJ IDEA code formatter代码的一部分just like I can in Eclipse . IntelliJ是否支持此功能,如果支持,我该如何使用它?

5 回答

  • 9

    从版本13开始,可以用代码包装代码

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

    在版本2016.x中,在“首选项”>“编辑器”>“代码样式”中启(谢谢Philipe

    在2018.x版本中,在文件>设置>编辑器>代码样式中启用它(您可以更改格式化程序控件标记,只要它们在注释中) .

    在版本14中,在首选项>编辑器>代码样式>格式化程序控件(感谢Emerson Farrugia)中启用它

  • 139

    请注意Carlos对 @formatter:off@formatter:on 的回答是否有效,您可能需要先启用首选项:

    In IntelliJ Preferences, under Code Style, General, Formatter Control there is a checkbox "Enable formatter markers in comments"

    通过How to config intellij-idea not format some part of the code?发现

    IDEA-56995使用注释禁用每个区域的代码格式

  • 400

    在xml文件中使用:

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

    2010年的过时答案:

    不,你可以跟踪's not possible right now, I' ve submitted a new issue .

    作为临时解决方案,您可以使用external formatter plug-in for IDEA,它可以使用Eclipse代码格式化程序(我没有检查是否支持此特定选项) .

  • 20

    目前无法从格式中排除整个文件(如回归测试数据xmls) . 见https://youtrack.jetbrains.com/issue/IDEA-167112

相关问题