Eclipse Continuation Indent - eclipse

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)

Related

How to disable auto-indenting in NetBeans IDE 14?

Short Version
How do i disable automatic indenting when i press Enter in NetBeans IDE?
Long Version
Consider some code, with my insertion caret at the end of the last line:
byte[] data;
try {
data = Base64.getDecoder().decode(s);‸
When i press Enter, it's because i want to insert a carriage return, and move the caret to the start of the next line:
byte[] data;
try {
data = Base64.getDecoder().decode(s);Enter
​‸
Except what happens in NetBeans, is that also automatically indents for me:
byte[] data;
try {
data = Base64.getDecoder().decode(s);Enter
Tab‸
How do i turn this off?
Research Effort
Disable smart indentation in Netbeans 8.2
Netbeans Narkive: Disable all auto indenting
Bugzilla: Bug 231879 - Unable to disable all automatic indentation and random carriage returns across the editor
How to disable the annoying NetBeans auto-suggest while typing
How do I autoindent in Netbeans?
It is straightforward to turn off automatic indentation in NetBeans 14, but the process is not intuitive for Java source. These settings must be applied in sequence after navigating to the Tools > Options > Editor > Formatting screen:
Select Language: All Languages and Category: Tabs and
Indents, and then uncheck the checkbox Enable Indentation and click the Apply button. This will turn off automatic indentation for all languages by default, but that global setting might still be overridden for individual languages.
Select Language: Java and Category: Tabs and Indents, and then uncheck the checkboxes Use all Languages Settings and Enable Indentation, and click the OK button.
After making those changes the modified fields should look like this if you revisit the Tools > Options > Editor > Formatting screen:
Language: All Languages
Language: Java
Then, using your sample code, if the cursor is positioned at the end of the line containing data = Base64.getDecoder().decode(s); and Enter is pressed:
A new line will be inserted.
The cursor will be positioned at the start of that new line.
This is the sample code I used:
package javaantapplication1;
import java.util.Base64;
public class JavaAntApplication1 {
private static byte[] s;
public static void main(String[] args) {
byte[] data;
try {
data = Base64.getDecoder().decode(s);
}
}
Notes:
The steps to re-enable automatic indentation are not the reverse of those described above. Instead, simply revisit the first screen shown above, check Enable Indentation, and click OK. That is sufficient to enable indentation for Java again.
I don't think it is possible to have automatic indentation disabled only for Java source.
While better than nothing, the GUI for configuring indentation in NetBeans is confusing and inflexible.

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

Spaces instead of tabs in Eclipse (Galileo 3.52)?

Is there any way in Eclipse (Galileo 3.52) to have it replace tabs with spaces ("soft tabs")? I've tried two things so far . . .
Windows>Preferences>Editors>Text Editors... check "Insert spaces for tabs"
...and...
Window->Preferences->Java->Code Style->Formatter->Edit... "Use Spaces to indent wrapped lines"
(... this forces you to create a new profile to save these settings)
This produces partial success - the first tab is turned in to spaces but subsequent ones are still tabs. So if I type
tabtab 1234
...I end up with...
spacespacespacespace tab 1234
You can do:
Window -> Preferences -> Java -> Code Style -> Formatter
Click on “Show” button (or "Edit") right beside the name of the profile you are using.
In the indentantion Tab, change the "Tab Policy" field to "spaces only"
Click Ok, and save the profile with the name you want.
Could the missing setting be the Tab policy: Spaces only combo box in Preferences >Java > Code Style Formatter > Edit > Indentation (right above where you changed Use spaces to indent wrapped lines)?

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".