如何配置intellij-idea不格式化代码的某些部分?

Fre*_*ind 31 code-formatting intellij-idea

在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>
Run Code Online (Sandbox Code Playgroud)

是否可以配置intellij-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>
Run Code Online (Sandbox Code Playgroud)

小智 44

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

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

在你可以使用这些评论之前,

// @formatter:off

// @formatter:on 
Run Code Online (Sandbox Code Playgroud)

或者这些(取决于语言):

<!--@formatter:off-->

<!--@formatter:on-->
Run Code Online (Sandbox Code Playgroud)

截图:

屏幕截图突出显示


Cra*_*der 31

此功能已实现,您现在可以使用特殊注释禁用代码区域的格式.

标记

您可以在文档中找到更多详细信息和示例:


2012年的原始答案:

目前还不行,请投票支持此功能请求:

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

  • 我不想在我的代码上使用注释只是为了让 intellij 不破坏我的缩进,我已经努力保持一致,例如定义属性。此设置应该在整个项目中可用,无需注释。但当我说格式或缩进时,我希望能够重新格式化。现在,我只能通过遍历 Java 代码样式和复选框来完全关闭该功能。 (2认同)

Mar*_*ler 8

EAP 13开始,您可以使用以下注释触发格式化程序:

// @formatter:off
...
// @formatter:on 
Run Code Online (Sandbox Code Playgroud)

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

// @formatter:off
$SELECTION$
// @formatter:off
Run Code Online (Sandbox Code Playgroud)

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