How to Turn Off the Eclipse Code Formatter for Certain Sections of Java Code

How to turn off the Eclipse code formatter for certain sections of Java code?

Eclipse 3.6 allows you to turn off formatting by placing a special comment, like

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

The on/off features have to be turned "on" in Eclipse preferences: Java > Code Style > Formatter. Click on Edit, Off/On Tags, enable Enable Off/On tags.

It's also possible to change the magic strings in the preferences — check out the Eclipse 3.6 docs here.

More Information

Java
>
Code Style
>
Formatter
>
Edit
>
Off/On Tags

This preference allows you to define one tag to disable and one tag to enable the formatter (see the Off/On Tags tab in your formatter profile):

Sample Image

You also need to enable the flags from Java Formatting

Turn off Eclipse formatter for selected code area?

To prevent specific portions of Java code from being formatted, go to "Window > Preferences > Java > Code Style > Formatter". Click the "Edit..." button, go to the "Off/On Tags" tab and enable the tags. Afterwards, you can simply embed those tags in Java code to disable the formatting in-between them. If you don't change the default tags, something like this will do :

//@formatter:off
this.
portion.of(code
); // will not be touched by the formatter
//@formatter:on
but this will be
reformatted.

IIRC, this option only exists since Eclipse 3.6.

As for css code, if you have installed Eclipse WTP, go to "Window > Preferences > Web > CSS Files > Editor" and you will find some basic formatting options there.

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

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?

Turn off Eclipse formatter everywhere

I am not sure you can actually disable that one command ("Format", part of the "Java" command groups).

But you can at least:

  • remove the key binding (ctrl+shift+f)to that command, through the "Keys" preferences.
  • make sure the properties of the project don't define a "format on save" action.

Prevent linebreaks with eclipse code formatter

Steps to format cascading function calls:

1. Window => Preferences => Java => Code Style => Formatter

2. Click Edit.

3. Expand Line Wrapping => Wrapping Settings => Function Calls

4. Change Qualified invocations to Wrap all elements, except first
element if not necessary
.

  • Additionally, ensure Force split, even if line shorter than maximum line width is toggled on.

See image below for what it looks like when set correctly:

Qualified Invocation Location

5. Change Profile Name if necessary.

6. Apply and close.

Is it possible to mark a part of java code in eclipse to be not auto formatted?

I think you can use @formatter:off and @formatter:on

// @formatter:off
public void fluentIterfaceThingy() {
...
}
// @formatter:on

You might have to turn this option on in the code style section: Window->Preferences->Java->Formatter->Edit->On/Of Tags



Related Topics



Leave a reply



Submit