How to config intellij-idea not format some part of the code?
Asked Answered
C

4

45

In a html file, there is some code used custom template-language:

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

Is it possible to configure intellij-idea not to format it(but format the other part of the document)? Since idea will make it hard to read after formatting:

<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>
Commensurate answered 24/12, 2012 at 10:38 Comment(1)
possible duplicate of How to disable code formatter for some part of the code?Enforce
S
46

This feature has been implemented and you can now disable formatting for the regions of code using the special comments.

markers

After enabling this feature in the settings, add //@formatter:off at the start of your region, and //@formatter:on at the end of it.

These are the default markers, and they are configurable.

You can find more details and examples in the documentation:


Original answer from 2012:

It's not possible yet, please vote for this feature request:

  • IDEA-56995 Disable code formatting per region using comments
Stolid answered 24/12, 2012 at 10:59 Comment(2)
I don't want to use comments on my code just for intellij not to destroy my indentation I have worked so hard to get aligned, for instance defining properties. This setting should be available on full project, without a comment. But I want to be able to reformat when I say format, or indent. Now I am left with possibly turning off the functionality completely by walking through the Java code style and checking boxes.Guru
This has been fixed - see below for how to enable it.Brandi
R
51

In my version EAP 13.1, you must enable the following option in settings,

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

before you can use these comments,

// @formatter:off

// @formatter:on 

or these (depending on language):

<!--@formatter:off-->

<!--@formatter:on-->

Screenshot:

Screenshot highlighting the checkbox for Enable formatter markers in comments under Preferences

Rubin answered 6/5, 2014 at 4:9 Comment(1)
This did the trick for me, thank you! I think this should be the accepted answer as it was meanwhile added as a feature to IntelliJ IDE.Medici
S
46

This feature has been implemented and you can now disable formatting for the regions of code using the special comments.

markers

After enabling this feature in the settings, add //@formatter:off at the start of your region, and //@formatter:on at the end of it.

These are the default markers, and they are configurable.

You can find more details and examples in the documentation:


Original answer from 2012:

It's not possible yet, please vote for this feature request:

  • IDEA-56995 Disable code formatting per region using comments
Stolid answered 24/12, 2012 at 10:59 Comment(2)
I don't want to use comments on my code just for intellij not to destroy my indentation I have worked so hard to get aligned, for instance defining properties. This setting should be available on full project, without a comment. But I want to be able to reformat when I say format, or indent. Now I am left with possibly turning off the functionality completely by walking through the Java code style and checking boxes.Guru
This has been fixed - see below for how to enable it.Brandi
S
9

Since EAP 13 you can trigger the formatter with the following comments:

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

To enable it in "surround with", you can define a Live Template in the section "surround" with:

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

You can find these settings in Settings -> Code Style -> General and set your own trigger words.

Swop answered 24/1, 2014 at 8:33 Comment(0)
C
2

It's worth noting that the formatter control does not appear to work in javadoc comments. It needs to be either in a regular C or C++-style comment. So // @formatter:off works /* @formatter:off */ also works, but /** @formatter:off */ does not.

Charest answered 9/3, 2017 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.