Lesmana
Lesmana

Reputation: 27053

Eclipse toggle comment indented

I have following code:

public int doBam(int bam) {
    if(foo) {
        bam += 1;
        if(bar) {
            bam += 1;
        }
    }
    return bam;
}

I want to comment out the if(bar) ...

When I do toggle comment in Eclipse 3.6 I will get this:

public int doBam(int bam) {
    if(foo) {
        bam += 1;
//        if(bar) {
//            bam += 1;
//        }
    }
    return bam;
}

Can I make Eclipse to toggle comment like this instead?

public int doBam(int bam) {
    if(foo) {
        bam += 1;
        //if(bar) {
        //    bam += 1;
        //}
    }
    return bam;
}

Upvotes: 28

Views: 5597

Answers (5)

S Krishna
S Krishna

Reputation: 1333

Select the code to be commented

  • For toggle comment press Cntrl + /
  • For block comments press Cntrl + Shift + /

enter image description here

Upvotes: -1

shycha
shycha

Reputation: 462

The question seems to be little outdated, but anyway.

Block of code can be nicely commented-out (starting at Eclipse 3.5) using column selection mode [1]. Just press Alt + Shift + A to switch to column mode, then select the code using arrows, type // and press Alt + Shift + A again to turn off column mode.

[1] How do I enable the column selection mode in Eclipse?

Upvotes: 2

blahdiblah
blahdiblah

Reputation: 33991

For the bold, though the Eclipse team doesn't want to include this behavior, the bug report mentioned includes patches that would allow anyone building Eclipse from source to add the behavior into their copy.

Upvotes: 2

Lesmana
Lesmana

Reputation: 27053

seems like a real solution for this is not going to happen in eclipse

https://bugs.eclipse.org/bugs/show_bug.cgi?id=321092

Upvotes: 8

dira
dira

Reputation: 30594

Can I make Eclipse to toggle comment like this instead?

It's a three step process..

Step-1:- Select desired code.

if(bar) {
  bam += 1;
}

Step-2:- Hit Control + 7 OR Control + /

//    if(bar) {
//      bam += 1;
//    }

Step-3:- Hit Control + Shift + F

   // if(bar) {
   // bam += 1;
   // }

UPDATE

Also, when uncommenting, the Eclipse autoformatter does not restore the previous indentation

Like commenting, uncommenting is also three step process:-

  1. select
  2. Contorl + /
  3. Control + Shift + F

it applies it's own formatting rules instead.

You can change these formatting rules. Windows --> Preferences --> Java --> Code Style --> Formatter --> Edit --> Comments Tab.

Eclipse does not allow editing default built-in profile. Create new profile and inherit all the properties from built-in profile, then you can customize the newly created profile.

Upvotes: 9

Related Questions