Eclipse - Only Auto-Indent On Typing - java

Is it possible to have Eclipse only indent new lines when you start typing code? For example, hitting enter brings the text cursor back to the left-hand margin, but starting to type on that line would tab it to the appropriate indentation?
I currently strip trailing whitespace on save, but that's not really the behavior I want.

Eclipse can automatically add/remove indentation in some cases (one of them is when line is empty). For that you need to enable auto formatting on save action.
Go to Window -> Preferences -> Java -> Editor -> Save Actions check both Perform the selected actions on save and Format source code.
Then make sure that currently used formatter doesn't indent empty lines.
Go to Window -> Preferences -> Java -> Code Style -> Formatter click Edit... button. In the Indentation tab un-check (if checked) Empty lines under Indent section.

Related

Eclipse: Adding Spaces to Parentheses

I am looking for a setting in Eclipse (using Photon) which will alter the auto-completion style of parentheses.
Eclipse's default looks like this:
function_call(arg1, arg2, arg3);
I prefer to write code like so, as I find it more readable:
function_call( arg1, arg2, arg3 );
Is there a setting under Window > Preferences which can edit auto-completion in this way? Editing them manually is a hassle, and I like my code to look consistent, stylistically.
Yes you can do it simply. Follow the following steps:
From menu bar go to Window -> Preference
A dialog will open as follow:
2.1. From the left tree styled menu, go to Java -> Code Style -> Formatter
2.2. Now On the right you see the "Active Profile" combo box, click the Edit button
Another dialog appears as follow:
3.1. In this dialog you may check the following check boxes:
After opening parenthesis
Before closing parenthesis
(as shown in the above)
Now you want to save these changes but the OK button seems disabled. You should change the Profile name on the top of this dialog to some name of your own. Then you can hit the OK button and all set.
Then hit the Apply and Close button on the Preferences dialog.
Now every time you format you codes (using menu or Ctrl+Shift+F) your code would be formatted you desired.
As you might see in that preferences dialog there are so many other formatting options you can change and use as you want.
Hope this would be helpful.
Yes, go to Preferences > Java > Code Style > Formatter and then edit the built in formatter.
On the White Space tab expand 'Expressions' and select Function Invocations. You can then select white space after the opening and before the closing parenthesis.

Eclipse Formatter: Break at Greater-Than Sign Rather Than at Dot

I have this line showing the problem:
Which setting in the Eclipse Java formatter do I have to change so the line will break like
return this.data.getNumberOfGenerationsWithoutSignificantImprovementPassed()
> MAX_NUMBER_OF_GENERATIONS_WITHOUT_SIGNIFICANT_IMPROVEMENT;
or like
return this.data.getNumberOfGenerationsWithoutSignificantImprovementPassed() >
MAX_NUMBER_OF_GENERATIONS_WITHOUT_SIGNIFICANT_IMPROVEMENT;
? If both is possible, I prefer the former.
On Mac, go to Eclipse -> preferences. On Windows, I believe it is Windows -> preferences... but don't quote me on that!
Expand Java -> Code Style -> Formatter
To the right of the "Active profile" bar, choose edit.
A new window will open- choose the "Line Wrapping" tab.
Scroll down to "Expressions" and choose conditionals. Pick your favorite!
Bonus points: This is where you can set all sorts of different styles, if you have not before, like number of spaces per tab character. Once you get everything set up just right, name your profile and export it. I use the same theme on 3 different computers and the consistency is nice!

Is there a way in Eclipse to keep the values in multiple arrays lined up?

I want the values of two (or more) arrays to be lined up like in the bottom row of this example.
Is there a way to do this in Eclipse?
You can switch off Eclipse formatter (see https://stackoverflow.com/a/3353765/5277820) and format the lines by yourself.
If I misunderstood your question I apologize, I assume you are talking about the formatting in Eclipse.
(Windows version)
In Eclipse, go to
Window -> Preferences -> Java -> Code Style -> Formatter.
Open the profile (should be default, but I'd suggest creating your own)
In the Line Wrapping tab, there is a "Maximum Line Width" that you can change (default is 80). That will prevent lots of these automatic line wrappings.
See the image below. For the white spaces you need to navigate to the "White Space" tab.

How to avoid eclipse to create 2 lines when autoindent is ON?

Eclipse code-formatting options are vast (see Java -> CodeStyle -> code formatting)
What I want is to avoid that the autoindent split my lines of code in 2 (or more) lines (see below). When reading-debugging code, is far more clear to read complete lines (IMHO)
Ex
float vsleep = (float) (((nodelenght) * obj
.getFactorperchar()) * 1000);
If I understand correctly, you want to stop eclipse from wrapping text over one or more lines.
If you navigate to Window -> Preferences -> Java -> Code Style -> Formatter and then click "Edit" next to the active profile, you can edit all of the auto formatting options. Choose the Line Wrapping tab, and then select all of the options in the list that contains "Class Declarations", "Constructor Declarations" and so on. Underneath the list, set the Line wrapping policy to "Do not wrap".
I tested this in my version and it seems to work.
Try setting Line Wrapping -> Maximum line width to something larger.

Checkstyle : always receive File contains tab characters (this is the first instance)

I'm using Checkstyle for Java in Eclipse IDE. I don't know in every java file, at second line, I always receive warning in Checkstyle : (although I'm pretty sure that I don't press tab key)
File contains tab characters (this is the first instance).
public class Percolation {
private boolean[][] grid;
...
}
When I have typed : public class Percolation {, press enter, Eclipse will make an indent for me, after that, I type private boolean[][] grid;. But, I still receive this warning.
Please tell me how to fix this. (I don't want to turn off this warning, because this is a must. Another people will check again my file).
Thanks :)
step 1 In eclipse, Preference > Java > Code Style > Formatter. Edit the Active profile.(If you don't wish to edit the built-in profile , create a new profile). Under "Active profile:" there is a drop down dialogue box .. beside it, click "Edit..." which opens up the edit dialog box. Under the "Indention" tab, you'll find Tab Policy. Change the Tab Policy from Mixed to Space only & apply the changes.
step 2 Back to your Eclipse Perspective, navigate via the menu bar: Source > Format Element (not "Format") and save.
Run checkstyle you won't find "File Tab Character: File contains tab characters (this is the first instance)." warning anymore.
To visualize the difference by enabling whitespace character that you'll find in tool bar.
In eclipse, go to Preferences > General > Editors > Text Editors
and check the box for "Insert spaces for tabs". Then it will indent with spaces instead of tabs.
another possibility is find and replace:
- copy a tab
- Got to Find and Replace Window
find: paste the tab
replace: type 4 whitespaces (or number of whitespaces a tab consists of)
-> replaceAll
For Java code:
step 1 In eclipse, Preference > Java > Code Style > Formatter. Create a new profile and initialize it with default profile. There is a drop down dialogue box .. beside it, click "Edit..." which opens up the edit dialog box. Under the "Indention" tab, you'll find Tab Policy. Change the Tab Policy from Mixed to "Space only" & apply the changes.
step 2 Correct Indentation of your code by ctrl+I. It will replace Tab with Space.
To fix this Navigate to Preferences > Java > Code Style > Formatter.
Then click on New > give name for the formatter > click on ok.
Once you complete this step, on the automatically popped up window which is the formatter you created, click on indentation > tap policy > select space only.
Or if you come back later time you could click on edit and follow the same step.
in the indentation size I prefer to put 4 > click apply and close.
After that right click on the class file you want to format > source > format.
This will cause the indentation to be spaces instead of tabs.
I was also facing the problem but I got the solutions.
Step 1 : Go to Window > Preferences > Checkstyle.
Step 2 : In the right side You will see the Global Check Configurations. There you will find that Two configurations are available. Select the configuration with Eclipse and click on Set as Default button and click on OK.
Hope this will solve your problem.
For 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.
For other editors refer here How do I change Eclipse to use spaces instead of tabs?
Simply fixed that style issue (triggered by scalaStyle) by removing TAB with SPACE indent :)

Categories