28

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;
}
Lesmana
  • 25,663
  • 9
  • 82
  • 87
  • 2
    Instead of answering (I don't have an answer) can I just try to convince you that Eclipse's way is far better? It visually differentiates line comments from commented out code, and it is more friendly for people with advanced text editors like vim, because they can use a macro or a visual block delete to comment/uncomment the code. – Mark Peters Nov 19 '10 at 15:03
  • @MarkPeters. I assure you that I am aware of the problems about commenting out code. I do not intend to ship such code out in any way. I only want this for my own usage, to quickly cut out code blocks when i look for bugs or test some other code. – Lesmana Nov 20 '10 at 08:43
  • 1
    Huh? I never made any argument against commenting out code, nor even about shipping it as such. In fact, I was arguing that Eclipse's way makes it *easier* to comment/uncomment code than your suggested alternative. Both your question and my comment were purely about the *formatting* configuration of commented out code, so I don't really understand your comment. And I don't understand why you think that your way will make it easier to "cut out code blocks...". The current toggle comment feature does that perfectly. It's just a difference in foramtting. – Mark Peters Nov 20 '10 at 20:24
  • @MarkPeters. I have misunderstood your first comment. I apologize. You are right, the question is strictly about the formatting and I meant it that way. What I meant with cutting out code blocks is when I am in the process of testing out different approaches to a problem: I comment one, test, comment the other, test, and so on. I actually find it easier on the eyes when the comment characters are indented instead of at the beginning of the line. – Lesmana Nov 21 '10 at 19:38
  • see the UPDATE part in my answer. – dira Nov 23 '10 at 12:46
  • For the record, catering to the needs of everyone (read: VIM) in terms of formatting, is absurd. It's a matter of preference. TextMate, for example, comments at the indent level for JavaScript. I actually find the indent level much more readable. – arxpoetica Mar 01 '12 at 23:38
  • Related to the original question... I'm having the same problem with Scala in Eclipse, but the solutions below are Java-specific, as the Scala code formatter tab has different options. Is there a way to turn off comment-reformatting for Scala? – Glenn Strycker Oct 31 '14 at 18:45

5 Answers5

9

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.

dira
  • 30,304
  • 14
  • 54
  • 69
  • It works in having the commented lines indented with the surrounding block, but it flattens the indentation inside of the commented block. Also, when uncommenting, the Eclipse autoformatter does not restore the previous indentation, it applies it's own formatting rules instead. – Lesmana Nov 23 '10 at 12:04
  • I workaround this problem by selecting the code and pressing `Shift-Tab` until the left edge of code hits the editor edge _(be careful not to `Shift-Tab` any further or you will lose indentation)_, then `Ctrl-/`, then pressing `Tab` until code returns to the original position. When I uncomment _(simply press `Ctrl-/` again, no tabbing necessary)_, it preserves the original indentation exactly as it was. – ADTC May 07 '13 at 07:20
  • I'm having the same problem with Scala in Eclipse, but the solutions below are Java-specific, as the Scala code formatter tab has different options. Is there a way to turn off comment-reformatting for Scala? – Glenn Strycker Oct 31 '14 at 18:45
8

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

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

Lesmana
  • 25,663
  • 9
  • 82
  • 87
  • Thanks for the link, they seem to think its a feature ...sigh. I would suggest folks who want this changed should go the issue and vote (and hope). – studgeek Nov 16 '12 at 18:05
2

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?

shycha
  • 440
  • 5
  • 13
  • This seems like a viable workaround. I am not using eclipse anymore at the moment so cannot test. Can someone verify? Then I will accept this answer. – Lesmana Jul 03 '19 at 22:30
2

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.

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
-1

Select the code to be commented

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

enter image description here

S Krishna
  • 1,255
  • 13
  • 9