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

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 :)

Related

Intellij IDE for Windows 7

I am using Intellij IDE on Windows 7. Here Shift + Up / Shift + Down is not working for Line selection operation. How to get this, as it is default way in other editors.
Thanks in advance.
You can change the key mapping in the settings (File -> Settings). Type in the search box (upper left) keymap. When on the keymap settings page find in the tree Main menu -> Code. There you find the Move Line Up and Move Line Down. Make a right click and select add keyboard shortcut to assign what you want. Most probably SHIFT-UP is already assigned to some action. It's up to you if you want to reassign it.

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!

Eclipse - Only Auto-Indent On Typing

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.

Eclipse: How do you change the highlight color of the currently selected method/expression?

In eclipse, when your cursor is placed on a method (or other things), other places the method exists are highlighted. I'd like to change the color of this highlight, but after scouring the eclipse preferences many times in all 3 places, I have yet to find it.
Any of you Eclipse gurus know where to find this option?
After running around in the Preferences dialog, the following is the location at which the highlight color for "occurrences" can be changed:
General -> Editors -> Text Editors -> Annotations
Look for Occurences from the Annotation types list.
Then, be sure that Text as highlighted is selected, then choose the desired color.
And, a picture is worth a thousand words...
(source: coobird.net)
(source: coobird.net)
For those working in Titanium Studio, the item is a little different: It's under the "Titanium Studio" Themes tab.
The color to change is the "Selection" one in the top right.
1 - right click the highlight whose color you want to change
2 - select "Properties" in the popup menu
3 - choose the new color (as coobird suggested)
This solution is easy because you dont have to search for the highlight by its name ("Ocurrence" or "Write Ocurrence" etc), just right click and the appropriate window is shown.
If you're using eclipse with PHP package and want to change highlighted colour then there is slight difference to above answer.
Right click on highlighted word
Select 'Preferences'
Go to General > Editors > Text Editors > Annotations. Now look for "PHP elements 'read' occurrences" and "PHP elements 'write' occurrences". You can select your desired colour there.
right click the highlight whose color you want to change
select "Preference"
->General->Editors->Text Editors->Annotations->Occurrences->Text as Hightlited->color.
Select "Preference ->java->Editor->Restore Defaults
in my case (Eclipse IDE 2021-06), dark theme, the issue was with the background color of "C/C++ Write Occurrences".
On each click over a variable being written in the code, the editor highlights all occurrences of that variable.
You can find this setting here:
General -> Editors -> Text Editors -> Annotations
and then select 'C/C++ Write Occurrences'.
And finally change che Color property.

Categories