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.
Related
In Eclipse, when i select part of a text and press any key, the whole selection is replaced.
I would like to know how to make it, instead of replacing the text, add the key pressed to both ends of the selection, as it happens on visual studio code.
Example:
System.out.println(HelloWorld);
Select Hello World
System.out.println(HelloWorld);
Then press the " button. The code will turn to
System.out.println(");
Can i config the IDE so it will instead change it to
System.out.println("HelloWorld");
There is a already answered question like this one, but considering that one as 7 years and a lot of version old, and also considering that the answer on that one is kind of a work around, i would like to know if there's any new plugin or configuration that make it work better
From what I gather, you are wanting to do something like this?
Go to Java > Editor > Templates and add a new template, e. g. called quote, as follows:
"${word_selection}"${cursor}
Then, in the editor, write a text you want to quote, select it, press Ctrl+Space, type quote and hit Return. The highlighted text should be quoted now.
If you don't get template proposals when pressing Ctrl+Space, make sure you have them checked in Java > Editor > Content Assist > Advanced.
Just tried it and it works for me!
You can use this method for other things, like (${word_selection})${cursor} for parenthesis or '${word_selection}'${cursor} for single quote.
While editing java code in eclipse, may be I pressed some key combo, or something else, but strings, code and comments in my entire file is now split into multiple lines. The newline is inserted at every object property/method.
Ex.:
object.prop1.method1()
has become:
object
.prop1
.method1()
What can I do to revert back???
Here is one small screenshot:
Thanx in advance...
You might have hit the auto formatter with Ctrl + Shift + F. Although the formatting seems strange and would definetly not be the default. Try hitting it again and see what happens.
You can configure the formatter in the preferences under Java=>Code Style=>Formatter
Ctrl+ Shfit + F - the short cut for format.
If have saved and ctrl + z do not work than only a new roll out from repository. If you do not have any then you can change change your format settings and perform operation one more time, but this time on your rules .
To set the format go to:
Options > Preferences > Java > Code Style > Formatter
If you press Ctrl+Shift+F your code gets formatted. (This must be what you pressed)
The formatter follows certain rules. You can find those rules if you go in Eclipse:
Window->Preferences->Java->Code Style->Formatter
There you must edit the current profile so that you can have more characters per line. This option is in tab
Line Wrapping -> Maximum line width
If you set this value large enough and press again Ctrl+Shift+F everything is going to be restored.
Thnx all for the suggestions....it worked by using code formatter and changing a few options
Thanx again...
By default, pressing the Comment out shortcut keys Ctrl + /, has behavior like this:
Notice that the // get added at the left-most position of the row.
Is there a way to set it so that the // appear at the same level of indention as the code that is being commented out?
e.g.
Perhaps a Python habit, but I find it much easier to parse when everything is at the same indention level.
Is there a way to change where the slashes show up?
Yes, it's possible.
go to this link for detailed info
First go to Preferences, then
On the left side of the box, expand the Java option, then expand Code
Style and finally click Formatter
You'll land on the Java-Profile page:
The other, temporary way is via CTRL+SHIFT+F shortcut ( Eclipse: Change the block comment style of ctrl+shift+/ ) . So you'd highlight those comments, and press the combo.
I would then also use a shortcut for CTRL+SHIFT+F , like ALT+F5 (press CTRL+3 , 'keys' to get the preferences page.
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 ..
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.