Insert line break in editor - java

I'm trying to add a line break with the Find function in eclipse - What I want to do is put a line after each method in all my classes [I liked them all close together but now its hard to read through]. so I figure theres 2 ways to do this.
1. Find all with "public" and change to "\npublic", \n being the new line in a string to give you the idea of what I mean.
2. A convenient eclipse shortcut that separates all the methods in a class with an empty line.
Any ideas on how to achieve this? I'll be happy as long as it works and I don't have to go through every method in the project myself and push enter .-.

For #2, you can set the Eclipse auto-formatter to do so for you.
Look under the Preferences panel; in the left side (tree), select Java Code Style (click the arrow to expand the section), then Formatter. You can then decide to use a single formatting style for all of your projects, or check the box to apply a special set of formatting rules to just the current project. Some of the options include where to locate whitespace and newlines.
You can also use the “Save Actions” panel to re-apply the same formatting rules every time you save your files. Personally, I use (and team members as well) this method, to ensure that the formatting for the files is always consistent. By mechanically reformatting in this way, changes to the file will always show up “properly” in Subversion/Git — I don't accidentally have whitespace-only changes being introduced into files showing up as “false positive” changes.

Check box 'Regular Expressions' and use '\R' for Linebreak in the 'Replace with' box in eclipse
configure the auto-formatter its a bit of work for the first time but its worth, when ever you get ugly formatted code you can fix this in seconds ..

Related

Disable Intellij IDEA append space after code autocomplete

An example: When I want to type public, I type pub and press tab, IDEA automatically append a space after public, how can I turn off it.
Because I am used to tapping space after autocompletion, so every time after that, there are 2 spaces and I have to delete one. Other IDEs or text editors I've used seem not to append that space automatically.
You cannot disable this particular piece of autocomplete behavior in IDEA short of disabling autocompletion feature altogether.
You can, however, use the 'Reformat' action to apply single default format to your whole file (Ctrl+Alt+L) or 'Complete Current Statement' action after you are done typing a construct (highlight the statement and press Ctrl+Shift+Enter). Conformed to the default formatting settings, double spaces should be replaced with single ones.
Assuming that this is for Java code (although the general mechanism is true for most file types), you can modify when/how spaces are used in code.
Go to File | Settings | Code Style | Java.
If you then click on the Spaces tab you can specify the code layout you want. After you've done this if you reformat your code it should format according to your preferences.
You can specify this for other types too (General, CSS, JavaScript and so on).

Format specific part of Java code in Eclipse Mars

I am able to format my Java code by configuring it in Save Actions.
What I get after Saving the file is that the whole code gets formatted according to my settings. What I need is, that only a part of code gets formatted according to the settings.
Say, there are 10 methods in my code and I add one more to it. What I want is that only my 11th method gets formatted and the previous 10 gets untouched.
Is that possible? I am using Eclipse Mars
NOTE:
The format includes, removing unnecessary casts, parenthesis, etc.
Source code formatting on save can be limited to edited lines only:
The other save actions however are applied to the whole file.
Select the text you want to format, and press Ctrl + Shift + F to format the selection.
Alternatively you can do Ctrl + I on the selected text to just correct the indentation.
I always use CTRL + SHIFT + F to format my code. In eclipse you can just drag your mouse and select the code you want, then press CTRL + SHIFT + F. That way it will just format the selected code.
Normal the Ctrl+Shift+F will format the current class. Or when a selection is made, only that selected part will be formated.
To make a quick selection, from the current place, use Ctrl+Shift+Arrow-Up to make it bigger. Or Ctrl+Shift+Arrow-Down to make it smaller again.
Telling Eclipse "manually" which parts of your code to format and which not can be a daunting task. Especially if others work on the same project and don't know which parts of the code have to be left out.
Because of this Eclipse allows you to set "markers", simply surround your code with these tags:
// #formatter:off
Here goes your code
// #formatter:on
In order to make this work you have to adjust the Java Code Formatter (it can be reached via Window / Preferences) settings like this:
As far as I know this has been in Eclipse since version 3.6.

Changing the priority of markers in eclipse

When I set a break point on a row which already has a marker(The value of the local variable is not used) on it, I cannot see the break point marker; because the yellow lamp marker covers the break point marker.
Is it possible to change the priority of the markers so that the break point sits on top of the lamp marker?
As long as #Mena answer is ok, as he sais...
Use at your own risk :)
This can be dangerous in a big project.
To avoid problems with hided warnings due configurations, what I usually do is:
Leave config as is, to see warnings.
Remove all really unnecessary lines causing warnings
Add an annotation to method still having warnings but needing breakpoint at same line. You can annotate class if necessary when massive warnings or breakpoints needed.
#SuppressWarning("unused")
public void yourMethod() {
}
This will allow you to handle warnings individually and see breakpoints when necessary.
I find that incredibly annoying too.
The only solution I've found is to go to the Java editor preferences, and disable the checkbox "Report problems as you type".
This will not change the priority, it will disable the hint and only show the breakpoint.
Here's a picture to illustrate:
Use at your own risk :)
Finally a TRUE relief after years of a so annoying and tiny thing:
1) go to your eclipse install path and look for all files (filename) that contain the word "warn" and are of the format .gif or .png (because they contain transparency data), here there were 5 files whose icon/image matched, most were named as: quickfix_warning_obj.gif (or.png)
2) rename all of them to *.DISABLED like "quickfix_warning_obj.gif.DISABLED", and as soon you restart eclipse, this will force it to restore the right one as soon you open an editor that uses it and has some warning on it.
3) having detected the right file, replace it with one of identical size (width and height in pixels), I would like to suggest this image I just created, so simple and not annoying at all and still is useful!
4) restart eclipse again and clap with a smiling face!
PS.: you can try to just replace this file also, that was the right one here for Eclipse Luna:
$ECLIPSE_INSTALL_PATH/configuration/org.eclipse.osgi/478/0/.cp/icons/full/obj16/quickfix_warning_obj.gif
Obs.:
Of course you can replace all of them that look the same, change other annoying icons and so on, and experiment with your own gif/png size and image, just that these steps is what worked perfectly here.
The image I supplied is almost invisible some times, but they were never really useful to me as I let the code with warning become underlined as a highlight. A better image is welcome :)
And, of course, this is a workaround, like a theme change, that does not involve any coding/recompiling of eclipse, therefore the priority is not changed at all, but the results are good enough to me at least.
EDIT: here one for the search results: searchm_obj.gif at configuration/org.eclipse.osgi/331/0/.cp/icons/full/obj16/searchm_obj.gif on Luna

Why does my source code (written in Eclipse) look different in other text editors?

I've been using Eclipse to do my CS assignments, as recommended by my professor. However, I've noticed that if I open my source code in a different text editor, my beautiful, perfect formatting looks wrong. I believe the problem lies in tabs. A tab character seems to take up less space in eclipse than in other text editors.
A good chunk of our grade is determined by the neatness of our code. I'm not sure if our programs are graded in eclipse or not, so I'd like to figure out how to make source code have the same formatting regardless of text editor.
Is this a problem with Eclipse? Are there settings I can fiddle with?
This is probably due to your settings for the tab symbol. If you really want to indent using tab, make sure it is set to 8 spaces everywhere.
From the Java coding convention:
Four spaces should be used as the unit of indentation. The exact construction of the indentation (spaces vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4).
Personally I always use spaces to indent my code due to the fact that some people have their tab symbol set to show as 4 spaces.
To set Eclipse to always use spaces, go to
Window -> Preferences -> Java -> Code style -> Formatter -> Edit
and set Tab policy to Spaces Only.
The tab size is probably different in other editors. It still is in its raw form a \t. Depending on what the editor will display when it encounters one is probably found in the preference. Either way it should be consistent in size all the way across the file.
You could also convert tabs to spaces so it is always the same regardless of editor.
The tab character does not have a defined display width. In notepad, it is displayed as the same width as 8 spaces wide. All code editors should allow the viewer to change the displayed tab width. The convention for code is normally a width equivalent to 4 spaces.
If you're desperate, you could replace all tabs with 4 spaces. However, this is frowned upon by some, and may lose you marks. I'm pretty sure that the advised java coding style advocates the use of tabs, not spaces.
At the end of the day, the marker is a fool if he'll turn down syntax highlighting, and the code editor that comes with it.

how to turn off break line in eclipse

Does anybody know how to turn line breaking in eclipse after you press CTRL + SHIFT + F (code format), ex :
System.err
.println("Incorrect file name, make sure you include .extension with your file name");
Go to Window->Preferences->Java->Code Style->Formatter. Create new formatter. Click on edit and then pick tab Line Wrapping and set Line Wrapping policy to Do not wrap.
The default line wrapping in Eclipse is really rather irritating and the main reason why is that the 'line width' is set as 80 characters, clearly a hangover from the nineteen eighties. This really should be updated now that everyone uses high resolution monitors instead of 80 char wide punch cards.
You probably don't want to turn it off entirely but instead you want to change the 'line width' to something more sensible (otherwise when you use the auto formatter you may get lines of code that go on forever).
I would suggest a value of 140, which seems to work nicely for most of us now using 1080p monitors.
In order to do this you need to...
With-in Preferences, navigate to...
> Java
> Code Style
> Formatter
You can't edit the default profiles so you have to hit New... and type a name like 'Eclipse 140 Line Width' for your profile and OK.
Then Edit the profile
Change to the Line Wrapping tab
Change the Maximum Line width from 80 to 140.
Then OK your way out.
If you then want to fix up your existing code, select your target code and use the Source > Format menu option.
In Helios, set 'Line Wrapping/Function Call/Indentation Policy' to 'Indent on Column' for all cases. This seems to stop the pathological case where it splits a line to try and fit within a fixed width but ends up with it further to the right than it would have been unsplit.
Or, as suggested above, disable line wrapping, or set a very wide line width. But I find it better to have it wrap and get it right than not to wrap at all.
There is an option "never join wrapped lines". This will help to keep our custom wrapping unchanged. Great Feature.

Categories

Resources