5

For each method, I want to add a space between the method name and the right curly bracket. For example,

public void setName(String name) {
}

I want a space between ) and {. Is it possible to automatically set this in Eclipse or somewhere? It's annoying to add this space every time.

4 Answers 4

5

in eclipse menu-->Windows-->preferences, search for the formatter, then edit then set the checkbox: insert space before opening brace

enter image description here


enter image description here

Sign up to request clarification or add additional context in comments.

Comments

2

Default code style will add space where you want it, and it can be autoformatted on every save:

Window->Preferences->Java->Editor->SaveActions: check "Format source code"

In case you want to change some of the default styles, you can do so if you navigate to formatter and edit active profile there:

Save Actions

Comments

1

See the eclipse manual:

http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Ftasks%2Fcdt_t_add_codetemp.htm

They're generally called "coding templates" for future reference in any IDE.

Comments

1

There are various coding templates you can use that will change the formatting of your code to the specified specifications. But even without a custom template, the default template adds the space, you just have to auto-format your code.

On Windows : Ctrl + Shift + F

On Mac : + + F

It will format your code and automatically add the space.

5 Comments

Yes. I am using the default template. The shortcut works for all, but is there a way to immediately format according to the template right after I type the code?
No there is no way of doing that. You can have it happen after save action like @Zack mentioned, but the easiest, in my opinion, is just to format the code when you are done typing.
So the best practice is to write all your code and invoke the shortcut key once every time your code is changed?
@user697911 yea, imagine if your editor was constantly changing in realtime while you are typing. That would be crazy. I just type everything I am going to write in a messy way, and periodically I just press cmd+shift+f and everything gets formatted correctly ¯_(ツ)_/¯
@EduardoDennis Visual Studio does something very similar. Every time you type ";" it fixes that line, and any time you close the "}" it fixes everything in it.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.