Format only selected code in eclipse
Asked Answered
F

1

12

As the title says, I want to format only selected lines of code. I know by pressing Ctrl + Shift +F formats whole file.

Formatting whole file is not good options as it will create lots of conflicts when I will commit the file. I changed a portion of the file (copied some code from internet etc.). Is there any easy way to format selected lines in eclipse. I have searched a lot but found no solution. I am working with PHP.

Fauch answered 17/4, 2013 at 7:53 Comment(0)
C
17

Highlight (left click, drag, left release) the lines you want to format, and press Ctrl + Shift + F, or right click and select Source -> Format.

For Java code, you have to highlight a complete piece of code (statement(s), method).

For PHP, I imagine you would still have to highlight a complete piece of code.

Tested on Windows version of Eclipse 4.2.

Croquet answered 17/4, 2013 at 8:53 Comment(4)
This was first thing I tried, but it didn't worked for me. I am using eclipse version 3.7.2 platform ubuntu 12.04.Fauch
@Anf: I didn't have any PHP on Eclipse handy at home. Highlighting and formatting worked selectively on PHP on Windows Eclipse 3.6.Croquet
It does not work again on 4.5.1 (Mars) on OS X. Or is there any configuration for this behavior? (In case it is useful, I am working with Java.)Elephus
Oh, I figured out what happened. It is just not allowed to format only part of a javadoc block (e.g. only for @param tags). I can still format only a single documentation block anyway so not a big deal.Elephus

© 2022 - 2024 — McMap. All rights reserved.