How to disable auto-indenting in NetBeans IDE 14? - netbeans

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.

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 can I get Eclipse to auto-indent code blocks within if and for statements?

I tried to get Eclipse to convert all of the tabs in my project to spaces like this:
Java Editor:
Click Window » Preferences
Expand Java » Code Style
Click Formatter
Click the Edit button
Click the Indentation tab
Under General Settings, set Tab policy to: Spaces only
Click OK ad nauseum to apply the changes.
And now my code is formatted without any indentations within if and for blocks, like this:
private void addAppointment(Resource resource) {
if (resource != null) {
Appt appt1 = new Appt();
appt1.setTime(new Date());
resource.setAppointment(appt1);
}
}
I really don't want to have to manually fix this in the hundreds of files in the project, how can I format to indent within if and for blocks in the whole project?
I should also say that the "Statements within blocks" checkbox in the active Formatter profile is checked. The preview it shows has a for block with an indented body, so I have no idea why that isn't being applied to my project.
#gnac provides some good options, in addition to:
Similarly you can use ctrl+shift+f (Source->Format) on each class to format it on the fly
You can select the project(s) and do Source menu -> Format to format everything in that project in one go. (No keyboard shortcut for it AFAIK.)
So once you set your formatting options you have a couple of options. You can set the preferences to format your files when saving.
Preferences->Java->Editor->Save Actions
However, if you have a lot of files this will be a pain as well. Similarly you can use ctrl+shift+f (Source->Format) on each class to format it on the fly, again having to do it on each file individually.
Inside Eclipse you can use Search->Find, enter "\t" in the text box and select the "Regular Expression" check box and then click the "Replace..." button. When the search is done, it will ask you what to replace it with. Enter 4 spaces into the "With" text field. Click Preview to see what it will do, or OK to make the changes.
I would use a find and sed to find all of the java files in a directory and replace the tabs, although this is outside of eclipse
find -iname ".java" -exec sed -i.orig 's/\t/ /g' {} +
If you're not on Linux you could use cygwin to do the same on Windows.

Disable line breaks after loops without body

Consider a Java while-loop without a body, for example this:
while (map.values().remove(value));
When formatting it (Shift + Alt + F), Eclipse breaks the line before the semicolon and indents it:
while (map.values().remove(value))
;
How can I configure the formatter to not add these line breaks?
Be aware that by setting this semicolon to a new line, you may avoid unintended endless loops and a long search to find them.
However, in Eclipse all code formatting properties are 1) in the project properties and 2) in the Eclipse properties. The project properties inherit from the Eclipse properties.
So if you open the project properties, type "format" in the search field. It will show you a "Formatter" menu item, that may depend on the current language (at least in Java it is called "Formatter"). Then, in this example, allow the project-specific settings, and then click edit. You will see a flood of possible details you can decide.
See here:
Then, in the tab "New Lines", uncheck the checkbox "Put empty statement on new line", in the section "Empty statements".
Often you have to try and search a bit because there are really many options. The part at right shows you a preview of the code formatting.

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)

Is there a way to fold eclipse sub-blocks like an "if" statement?

Currently Eclipse only fold the java doc and at function level, but when reading long methods, there could be quite a lot of if/else etc, is there a way to fold them?
I found the Coffee-Bytes plugin. I downloaded it from this link and found this guide by the author, for using it.
You can find more details in these references:
What code folding plugins work on Eclipse 3.6?
How to use Coffee-Bytes code folding
in updated versions of Eclipse
Change folding preferences at:
Window -> Preferences -> C/C++ -> Editor -> Folding -> Enable folding of preprocessor branches (#if/#else)
Enable folding using ctrl + shift + /
No, in the Preferences Dialog (Menu Window/Prefernces): Java/Editor/Folding you may choose,
Comments
Head Comments
Inner Types
Members and Imports
if Enable Folding is checked.
If you wan't to do this because the blocks are so long that can't reconize the structure
you should consider to split if/else blocks into methods using Alt-Shift-M (Extract Method)
It appears Eclipse does not have built-in support for folding if/else statements but allows folding for other more complex cases like Anonymous inner classes. Try looking for plugins like this one (last modified 2007, try it if it supports your Eclipse version).
Ok, this is a little bit older, but maybe someone could find this useful:
In most cases you can surround the piece of code by an additional pair of scope brackets, and to remember what you folded you can add a line comment.
For example, if you want to collapse the following:
int SectionA_var1;
int SectionA_var2;
int SectionA_var3;
int SectionA_var4;
int SectionA_var5;
int SectionB_var1;
just add the brackets an the comment:
{ // SectionA
int SectionA_var1;
int SectionA_var2;
int SectionA_var3;
int SectionA_var4;
int SectionA_var5;
}
int SectionB_var1;
Then you get the (-) sign and you can collapse the whole section to this:
{ // SectionA[...]
int SectionB_var1;
No plugin necessary, and until now I had no situation where this gave me any downsides, except that you cannot use it on a top level declaration to collapse methods.
For now, there is built-in function.
Click "Window->Preferences->C/C++->Editor->Folding" and then enable appropriate option you want.
Apply, close and either refresh project or reopen eclipse.
As weird as it looks like, sounds like developers never thought about that. if you have a big if statement or any switch/loop ... just use notepad++ to be able to fold/unfold
For Python, i.e. Eclipse/PyDev, go to Windows > Preferences > PyDev > Editor > Code Folding and check all the boxes.
Fold java source code like "if else for" statement
install pluins com.cb.eclipse.folding
restart your Eclipse make sure the pluins enabled
Click "Window->Preferences->Java->Editor->Folding"
Select folding: select "Coffee Bytes Java Folding"
Switch to "User Defined Regions"
"Start Identifier" = { ; End Identifier = }
click "Apply and Close"
Reopen java source editor you will see "if" or "for" block is collapsable