0

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.

1
  • Which Eclipse Edition / Distribution are you using? Normally it does not auto-format. Commented Oct 23, 2015 at 8:24

5 Answers 5

4

Source code formatting on save can be limited to edited lines only:

enter image description here

The other save actions however are applied to the whole file.

Sign up to request clarification or add additional context in comments.

1 Comment

I want the extra parenthesis in my code (in this case, the 11th method), to be removed or unnecessary casts, unused variables, etc. Can it be possible in Eclipse Mars for it to be removed only from the 11th method, which I made? Not removing from all the other 10 methods which aren't written by me.
0

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.

Comments

0

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.

Comments

0

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.

Comments

0

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:

enter image description here

enter image description here

As far as I know this has been in Eclipse since version 3.6.

8 Comments

I want the extra parenthesis in my code (in this case, the 11th method), to be removed or unnecessary casts, unused variables, etc. Can it be possible, by the procedure you told, for it to be removed only from the 11th method, which I made? Not removing from all the other 10 methods which aren't written by me.
@abhi As I don't have your code: why not try this on your own ? Just enter the tags and see what happens. If it does not work as intended: just undo.
@abhi Does the exclusion work when you format manually and not on save ? Please be more specific what you tried and what does "not work"
For the code which I want to be left untouched, I kept the formatter off. Then added the method of my own with the formatter tag on, before writing the method. I can format by selecting the new code itself and then, Ctrl+Shift+F. What I need is that extra useless parenthesis, useless casting, unused local variables etc, be removed ONLY FROM the method I WROTE. Not the ones I did not write. It's someone else's code. On save actions, I get the task done, but the other people's code too gets changed, which I really DON'T want. Is there any tool or feature for Mars to achieve it?
@abhi What I meant is: is the exclusion working when you format manually without selecting anything ? There might be a bug which causes the tags only to work in manual mode and not in on save mode. And perhaps you should provide a minimal example for others to try.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.