Formatting of the `//@formatter:off` tag in eclipse
Asked Answered
R

1

9

As you may know, Eclips lets you disable the code formatter for certain sections of source code (see for example this question).

Now, my problem is, that the formatter apparently stops formatting code at the very beginning of the line where the //@formatter:off tag is found. This has the result, that the formatter tag itself (which is just a comment line essentially) is placed in a queer location, namely without indentation at the very beginning of the line.

See this example.

What I enter:

   //@formatter:off
   Some code {
     with indentation
       that I dont    want to be    formatted.
   } 
   //@formatter:on

After hitting [Ctrl + F] it looks like this:

//@formatter:off
   Some code {
     with indentation...
       that I dont    want to be    formatted.
   } 
   //@formatter:on

Ok, I realize that this is purely a cosmetic issue, but my OCD is driving me nuts when I see this everywhere in the code. Especially after specifically using the formatting tag to make the code look nicer.

Rousseau answered 16/10, 2013 at 9:3 Comment(0)
K
1

What version of Eclipse are you using?

I just tried this in Eclipse 4.3.1, and it seems to work exactly as you want. Even better, if your code does end up like in your second code block, the formatter actually indents that first //@formatter:off block to the correct indentation.

Perhaps you need to upgrade your version of Eclipse?

Kaleighkalends answered 20/10, 2013 at 22:7 Comment(2)
Just rechecked the issue - seems I stumbled upon some strange edge case. What I first tried was to introduce the //@formatter:off tag between an object reference and its method calls. In that specific case it does not work, or rather does work as expressed above. You are right that it works fine in (most?) other cases.Rousseau
@Rousseau thanks, your comment solved it. If I place the off/on tags within an expression (I used it on an array initialization new Object[] {//@formatter:off ... //@formatter:on};) it removes indentation, if you place it outside of the expression it keeps the indentation.Cleland

© 2022 - 2024 — McMap. All rights reserved.