Skip to content
Advertisement

How to stop Eclipse from indenting extra spaces in this specific scenario?

When I am writing anonymous classes, I want my anonymous class to look like:

SaleTodayOnly sale = new SaleTodayOnly() // line 1
{                                        // line 2
    some implementation
}

But when I hit enter after line 1, Eclipse will automatically position my cursor at | on line 2:

SaleTodayOnly sale = new SaleTodayOnly() // line 1
        |                                // line 2
    some implementation

And when I backspace my way to the front and write {, Eclipse will reposition this { to:

SaleTodayOnly sale = new SaleTodayOnly() // line 1
        {                                // line 2
    some implementation

How can I set my own indentation preferences (for this specific scenario only)?

edit: I have my anonymous class set to next line. It’s probably a wrapping issue.

edit2: I give up. I’ll just use java conventions of { on the same line as the anonymous class declaration…

edit3: after hunting around the Preference window, toggling without much effect + seeing how Format produces the right output whereas the problem described still persists — I’d agree that this is probably a bug and I will file a report when I have time.

Advertisement

Answer

Go into your preferences. (Window -> Preferences, probably; on mac it’ll be under the leftmost menu option (‘Eclipse’)) – in the filter type ‘formatter’ to find the entry Java > Code Style > Formatter.

The behaviour you are witnessing is non-standard so you must already have a format defined; you picked this indent behavior, or somebody did who set this as default formatter.

edit this format. Alternatively, check if your project has a custom formatting rule in which case, this same answer applies, but instead go via your project’s properties and update the formatting rules there.

The specific rule you are looking for is Brace positions, Anonymous class declaration. You have this set to Next line indented. Set it to something else. It sounds like you want Next line (not indented).

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement