Question

Auto-Format by eclipse for java code is brilliant! You can write terrible code and then simple type CTRL+SHIFT+f - and the code is amazing.

But, sometime I want to mark part of code to be not automatically formatted. For example by fluent interface:

public void fluentInterfaceJooqDemo() {
    create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count())
          .from(AUTHOR)
          .join(BOOK).on(AUTHOR.ID.equal(BOOK.AUTHOR_ID))
          .where(BOOK.LANGUAGE.eq("DE"))
          .and(BOOK.PUBLISHED.gt(date("2008-01-01")))
          .groupBy(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
          .having(count().gt(5))
          .orderBy(AUTHOR.LAST_NAME.asc().nullsFirst())
          .limit(2)
          .offset(1)
          .forUpdate()
          .of(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME);
}

and after type CTRL+SHIFT+f

public void fluentInterfaceJooqDemo() {
    create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count()).from(AUTHOR).join(BOOK).on(AUTHOR.ID.equal(BOOK.AUTHOR_ID))
            .where(BOOK.LANGUAGE.eq("DE")).and(BOOK.PUBLISHED.gt(date("2008-01-01"))).groupBy(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
            .having(count().gt(5)).orderBy(AUTHOR.LAST_NAME.asc().nullsFirst()).limit(2).offset(1).forUpdate()
            .of(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME);
}

However, I'm looking for some method to mark such code non-autoformat, e.g.

//non-format
public void fluentInterfaceJooqDemo() {
    ...
}
Was it helpful?

Solution

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

OTHER TIPS

Yes, that is configurable in eclipse. And you can redefine the on/off keywords.

enter image description here

I am not sure if there is additional configuration needed (we have a customized eclipse in our company) but this works for me:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top