How to disable auto formatting upon save in Eclipse? - 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

Related

Eclipse - How to format selected code from multiple lines to a single line?

In Eclipse, if I select some code which is split on more than one line and I want to format it on one line only, how can I do that?
I want to know if it's possible to configure the js formatter inside eclipse so to be able to use just SHIFT + CMD + F.
Code split on several lines:
$('a#pop1').fancybox({
'padding': 0,
'margin-left': 30,
'width': 728,
'height': 400,
'maxWidth': 728,
'maxHeight': 400
});
and how I would like to achieve, split on a single line.
$('a#pop1').fancybox({'padding': 0,'margin-left': 30,'width': 728,'height': 400,'maxWidth': 728,'maxHeight': 400});
You need to do couple of thing to prevent code formatting.
Follow below step :
Right click on project and open properties.
Go to Preferences > JavaScript > Code Style > Formatter
Check Enable Project Specific settings.
Click on Edit button. Change Profile Name on top of dialog.
Go to New Line tab. Un-Checked option under Object initializers section.
Now go to Line Wrapping tab. increase Maximum line width parameter value.
Click on Ok than Applly than Ok.
This will now prevent your JavaScript code to format like above.
After that select your code and press Ctrl + Shift + f key combination to format your code.
Maybe this link can be helpful for you: Using the code formatter - Eclipse
Or this one, maybe, that has got some instructions: Formatting Your Code Using the Eclipse Code Formatter
try this -
Preferences > JavaScript > Code Style > Formatter
These are the steps:
Create a new profile (since you cannot edit the builted-in one), if you haven't already, and click Edit....
Open the Line Wrapping tab.
in it edit settings for your function
Click Apply, and Ok.

Eclipse Continuation Indent

Is there a way to configure the indentation eclipse uses for manual line breaks? It indents by 8 spaces no matter what I do.
I get this:
private static final org.apache.log4j.Logger LOG =
org.apache.log4j.Logger.getLogger(myClassname.class);
when I'd like it to give me this:
private static final org.apache.log4j.Logger LOG =
org.apache.log4j.Logger.getLogger(myClassname.class);
Eclipse's line wrapping can be configured to give me the correct result when the line is too long, but I'd like to be able to get less indentation even if the line break is manual.
In Eclipse Juno
open the preferences dialog (Window > Preferences).
go to the preference page Java > Code Style > Formatter.
press the Edit... button (for the Active profile).
if it is the default one from Eclipse (Eclipse [built-in]), you need to give it a new name.
click on the Line Wrapping tab
set the value to 1 for "Default indentation for wrapped lines" (by default it is 2)
(check the Identation tab and check what the indentation size is set at, by default it should be 4)

Unable to change eclipse line length or comment line length

I've tried changing all of the line wrapping parameters, both increasing and decreasing the maximum line length for source and comments, as well as disabling comment formatting. However, I'm unable to affect the code formatter's line-splitting behavior. No matter what changes I make to the format, line splitting occurs at 120 characters for both comments and source code. I've also tried upgrading eclipse from 3.6 to 3.7.
Any ideas why this is happening?
Right-click on your project and go to Properties
Check if Java Code Style > Formatter > Enable project specific settings is checked
If so, check the Active profile
My guess is that there was a different Active profile selected than the one you were editing. Deleting the project and recreating it caused the project to return to the default code style profile, where Enable project specific settings is unchecked.
Did you change the lines width from :
Preferences > Java > Code Style > Formatter >> then editing the 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.. )

How to customize Eclipse's text editor code formatting

How can I set my Eclipse's code formatter to allow code rows longer than 80 characters. I know that very long lines is hard to read but in my opinion 80 characters per line is very small value for wide screen monitors.
In Preferences, go to Java > Code Style > Formatter and edit/create the formatter and go to tab Line Wrapping. There in you can set the Maximum line width.
Further on you can in General > Editors > Text Editors also set the Print margin column which should shift up that margin (a 1px wide vertical line in the right side of your code) to the desired position. This however doesn't affect the formatter, only the human eye.
This answer was a bit old, and the path have changed with new versions of Eclipse.
To set the Maximum line width, go to Project > Properties. In the Properties window, go to Java Code Style > Formatter. Select your Active Profile (let the one which is already selected, this is the one you are currently using) and click on Edit. In the tab Line Wrapping, you can set the Maximum line width (80 by default). You can also go to the tab Comments and uncheck Enable Line Comment Formatting if you want your comments not to be formatted on multiple lines with Ctrl+Shift+F.
Don't forget to change the name of the Profile Name; if you don't change it, you won't be able to save your changes (provided the one you are editing is Built-In
Also, you can set the Print margin column in Window > Preferences > General > Editors > Text Editors.
Regarding the "small" value (80), this has just changed (in August 2014).
See "Eclipse is not a terminal anymore – Default line size of Java code formatter is now 120"
(By Lars Vogel)
Project > Properties, Java Code Style > Formatter.
Edit tab Line Wrapping
A three-years old issue is finally addressed:
bug 356851: Default line size of Java code formatter should be increased to 120
It is not mentioned in the Eclipse Project 4.5 (Mars) M1 - New and Noteworthy, but it is still "Verified for 4.5 M1 using I20140804-2000 build".