【问题标题】:How to config intellij-idea not format some part of the code?如何配置 intellij-idea 不格式化代码的某些部分?
【发布时间】:2012-12-10 19:23:48
【问题描述】:

在一个html文件中,有一些代码使用了自定义模板语言:

<script type="text/javascript">
    function ($scope, JsRoutes) {
        $scope.rows = [
            ${"#{list rows, as: 'row', separator: ','}"}
            {
            #{list fieldConfigs, as: 'f', separator: ','}
            ${f.name} : ${'$'}{row.${f.name}.toJson()}#{/list}
            }
            ${'#{/list}'}
        ]
    }
</script>

是否可以将 intellij-idea 配置为不格式化(但格式化文档的其他部分)?由于idea在格式化后会让人难以阅读:

<script type="text/javascript">
    function ($scope, JsRoutes) {
        $scope.rows = [
            ${"#{list rows, as: 'row', separator: ','}"}
            {
    #{list fieldConfigs, as: 'f', separator: ','}
    ${f.name} :
        ${'$'}{
            row.${f.name}.toJson()
        }#{/list}
    }
    ${'#{/list}'}
    ]
    }
</script>

【问题讨论】:

标签: intellij-idea code-formatting


【解决方案1】:

此功能已实现,您现在可以使用特殊 cmets 禁用代码区域的格式化。

在设置中启用此功能后,在您所在区域的开头添加//@formatter:off,并在其末尾添加//@formatter:on

这些是默认标记,它们是可配置的。

你可以找到more details and examples in the documentation:


2012 年的原始答案:

目前还不可能,请为这个功能请求投票:

  • IDEA-56995 使用 cmets 禁用每个区域的代码格式化

【讨论】:

  • 我不想在我的代码中使用 cmets 只是为了让 intellij 不破坏我的缩进,我一直在努力对齐,例如定义属性。此设置应在整个项目中可用,无需评论。但是当我说格式或缩进时,我希望能够重新格式化。现在我可能会通过遍历 Java 代码样式和复选框来完全关闭该功能。
  • 此问题已修复 - 请参阅下文了解如何启用它。
【解决方案2】:

值得注意的是,格式化程序控件似乎在 javadoc cmets 中不起作用。它必须是常规的 C 或 C++ 风格的注释。所以// @formatter:off 有效,/* @formatter:off */ 也有效,但 /** @formatter:off */ 无效。

【讨论】:

    【解决方案3】:

    在我的 EAP 13.1 版本中,您必须在设置中启用以下选项,

    Preferences -> Code Style -> General -> Formatter Control -> Enable formatter markers in comments

    在您可以使用这些 cmets 之前,

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

    或这些(取决于语言):

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

    截图:

    【讨论】:

    • 这对我有用,谢谢!我认为这应该是公认的答案,因为它同时作为 IntelliJ IDE 的一项功能添加。
    【解决方案4】:

    由于EAP 13,您可以使用以下 cmets 触发格式化程序:

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

    要在“环绕”中启用它,您可以在“环绕”部分中定义一个实时模板:

    // @formatter:off
    $SELECTION$
    // @formatter:off
    

    您可以在Settings -> Code Style -> General 中找到这些设置并设置您自己的触发词。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2014-04-20
      • 2018-06-22
      • 2013-07-26
      • 1970-01-01
      • 2017-02-16
      相关资源
      最近更新 更多