3

There is a feature in eclipse that you can enable to automatically insert curly brackets and semicolons into the right location. (More info in the top reply here)

Recently I started picking up the habit of adding the curly brace on the next line so that the braces end up lining up. I find that it makes the code nicer to look at. Is it possible to still use this auto positioning feature with curly braces, but instead of inserting the curly brace at the end, insert it on the next line like this:

for (int i = 0; i < 10; i++)
{

I have my eclipse formatter set to use this format, but unfortunately the auto position curly bracket doesn't appear to follow these rules. Anyone know how to fix this or a workaround without having to highlight all the code and format constantly?

Edit: I guess I wasn't really clear in my original post. I do have my formatter set to having braces on the next line, but eclipse's awesome insert at correct location feature doesn't seem to follow the formatters rules. I was trying to figure out if there was a way to solve this issue so that (like the link I posted above), typing a brace on the line will bring me to the next line instead of placing the brace at the end of the current line. Maybe eclipse has a macro plugin I can use for this?

2 Answers 2

4

Window > Preferences > Java > Code Style > Formatter > Select a profile > [Edit...] > Braces

There you can configure it.

Edit

Try this:

Window > Preferences > Java > Editor > Templates

Select the appropriate for > [Edit...]

Configure it as you want

3
  • That is the issue. I have it all set to new line except arrays, but using the auto complete, it adds it at the end of the line instead of following the formatters instructions. It may sound lazy, but having to go back and fix the brackets from having it insert in the right spot really affects my efficiency. Commented Dec 26, 2013 at 4:03
  • I don't know if you have already tried this. Look at the edit. Commented Dec 26, 2013 at 4:07
  • 1
    This is above and beyond what I was looking for. I had no idea this feature existed. Thanks Commented Dec 26, 2013 at 4:14
2

From the Menu Bar, open Windows -> Preferences
Then in the options table select Java -> Code Style -> Formatter, then configure the braces in the sample code as you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.