Eclipse code formatter: do not wrap lines on method call - eclipse

I'm trying the Eclipse Auto Formatter, but I cannot find a proper options to format according to my liking.
When I have a line like this:
MyLongNameClass myLongNameObject = otherObject.callAVeryLongMethodWithParameters(param1, param2, param3)
Eclipse auto formatter wraps it this way:
MyLongNameClass myLongNameObject = otherObject
.callAVeryLongMethodWithParameters(param1, param2, param3)
I'd rather like to have this result:
MyLongNameClass myLongNameObject =
otherObject.callAVeryLongMethodWithParameters(
param1, param2, param3)
Keeping the object and the method accessor on the same line, and avoiding starting a new line with '.'. Is this possible? Is there an option for this?

I'm not sure if you can really do what you wanted, but this might get you close:
In eclipse Mars:
Go to Window -> Preferences -> Java -> Code Style -> Formatter
Create your own profile based on one of the built-ins using the New... button. By default this will take you into the editing window. If you already have a formatter set up that you want to change, select it and use the Edit... button.
In the profile editing window do the following:
Select the Line Wrapping tab
In the left scroll-box, go to Function Calls -> Qualified invocations.
In the Settings for qualified invocations area select Do not wrap.
In the left scroll-box, go to Function Calls -> Arguments
Select Wrap first element, others where necessary in the argument settings then select Indent by one.
You can test these settings by adjusting the line width in the preview window.
I know this is a late submission, but I hope it helps someone else who searches for this same feature.

Go to Windows -> Preferences -> Java -> Code style -> Formatter
You can create your own profile with customised formatting, you can also modify existing formatter.
Edit -> Line Wrapping
Explore line wrapping and change it accordingly.

Related

How to make Eclipse format according to template when typing

I'm using eclipse Version: 2019-09 R (4.13.0) on mac High Sierra.
I didn't like the default formatting. For example when I write a method, it puts a space before the brace like this myMethod() { instead of myMethod(){ which I prefer.
So I changed the formatting template in Preferences -> Java -> Code Style -> Formatter
Now when I do CMD+shift+F it formats it how I want without the whitespace.
However, it STILL adds the white space as I type in the method! I can't figure out how to stop it doing that. When I type CMD+shift+F it corrects it, so it knows what I want, but retains the old behaviour when I type. Please save my sanity!
(Yes I've tried restarting eclipse!)
Thanks!
In Preferences: Java > Editor > Typing in section Automatically insert at correct position uncheck the checkbox Braces.

How to add tab space infront of mutiple lines of code

I am using eclipse IDE for development I have written some code in if-else block and now I want to tab indent every block, Is there any way or command or key sequence so that I can select the lines of code and simply add tab space in front of every line selected.
Use the formatter for this. Right click your file and select Source -> Format. You can also execute this for a single code block. Just select the block and right click -> Source -> Format. You can also format Javadoc this way.
If you don't like the way your code gets formatted you can change the formatter template in Windows -> Preferences -> Java -> Code -> Formatter.
Select the block(or set of statements) and just press TAB to indent. The whole selected block(or set of statements) will be moved.

Netbeans generated code settings

I was wondering where can I change the way Netbeans generates code. For example, I want the opening bracket to be on a new line.
I've been looking in the [Tools] -> [Options] menu but I didn't find anything.
Solution 1 should solve your problem. For any auto-generated code formatting that Solution 1 won't solve, use Solution 2. Beware the "Code Templates" tab though, it has been relatively buggy for me in the past.
Important Note: Attempting to modify the "Code Templates" for the placement of braces won't work correctly because it's overridden by the preferences in Solution 1. Any changes to formatting wanted should first be attempted with Solution 1, as the "Formatting" tab in Netbeans takes precedence over the "Code Templates" tab.
Solution 1:
Go to [Tools] -> [Options] -> [Editor] -> [Formatting]. Choose the language you are using, then, for the Category drop down box, select Braces. Directly under that you'll see Braces Placement. You can modify the placement of the braces for a class declaration, method declaration, or "other". Options are:
Same Line - places bracket on same line as class, method, or "other".
New Line - places bracket on a new line under the class, method, or "other".
New Line Half Indented - same as "New Line" with a half indention added.
New Line Indented - same as "New Line" with a full indention added.
Solution 2:
Go to [Tools] -> [Options] -> [Editor] -> [Code Templates]. Choose the language you are using, then modify the templates according to what formatting you'd prefer.
For example, if Java is selected, I can scroll to the "pm" abbreviation and see how Netbeans generates private methods. Here is a incomplete but helpful abbreviation-to-meaning list for a few common Java abbreviations you might want to change.
m - Method
p - Private
f - Final
s - Static
Therefore, if you want to change the formatting for an auto-generated method that is listed as "private", you can change the formatting for the abbreviation "pm" in the "Expanded Text" area, which shows the auto-generation rules/formatting for a private method.
Note: Combinations of these letters can sometimes mean something different, for example "pr" is expanded to "private", but "prm" is listed as a protected method, not a private method.

How to disable auto formatting upon save in Eclipse?

Eclipse keeps auto formatting upon save, for example: it is insisting the following code should be in a single line once I save, despite breaking it up the way I want to.
#Transactional(rollbackFor = DataAccessException.class, readOnly = false, timeout = 30, propagation = Propagation.SUPPORTS, isolation = Isolation.DEFAULT)
Window > Preferences > Java > Editor > Save Actions.
Can also be configured at the project level. Project > Properties > Java Editor > Save Actions.
Absolutely maddening improvement. For if statements do the following:
Window | Preferences | Java | Code Style | Formatter | Control statements tab
Select Keep simple if on one line.
You might also try the Line wrapping tab
This is a Late answer, But just for novice to understand it clearly I have a snapshot of the setting.
Please follow the path on left.
And change highlighted setting.
If you wish to keep the auto-formatter, but don't want this behavior, you can also configure it in the fomatter :
right click on the project -> properties -> java code style -> formatter
(If you don't have a project specific formatter the global configuration can be accessed this way : Window -> Preference -> Java -> Code Style -> Formatter).
Edit... the active profile, open the Line Wrapping tab, and there check the Never join already wrapped lines option.
This way you can wrap lines wherever you want and eclipse won't remove your line breaks (which is a must if you wish to have readable lambda expressions), but keep the formatter active.
This option is there at least since eclipse Luna
Here is a screenshot where this option is :
Just wanted to add a tip. If you want to avoid the line wrapping you can simply increase the "Maximum line width" value to something enormous. As long as that is sufficiently high eclipse won't do any wrapping.
Preferences > Java > Code Style > Formatter > Edit > Line Wrapping > Maximum line width

Eclipse format question

When I format in eclipse long lines become from this :
String str = instance.someMethod("jhdajhajsha").someOtherMethod("sakjsaksja");
to this :
String str = instance.someMethod("jhdajhajsha")
.someOtherMethod("sakjsaksja");
How do I prevent this?
Good practice is to place line-break like that.
Any way you can edit you format setting
Window > Preferences
It will show the Preferences Window
In that Expand the Java then Code Style and select Formatter
Here you can edit Active profile
In the edit, Line Wrapping tab, The General Settings there is a option to set the
Maximum line width give the maximum limit here, When you format, it will break the line more than this limit
windows > preferences > java > code style > formatter
Create a new profile. in the profile setting, select Line Wrapping [tab]
disable line wrapping for all . (class declaration, constructor declaration etc etc etc.. )