Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 321092 - [typing] Indentation should be preserved when comment toggled
Summary: [typing] Indentation should be preserved when comment toggled
Status: RESOLVED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 3.0   Edit
Hardware: PC All
: P3 normal with 2 votes (vote)
Target Milestone: ---   Edit
Assignee: JDT-Text-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-07-28 03:31 EDT by redstun CLA
Modified: 2012-11-24 11:59 EST (History)
2 users (show)

See Also:


Attachments
Patch for ToggleCommentAction.java (384 bytes, patch)
2010-07-28 04:02 EDT, redstun CLA
no flags Details | Diff
Patch for JavaSourceViewerConfiguration.java (145 bytes, patch)
2010-07-28 04:04 EDT, redstun CLA
no flags Details | Diff
Patch for TextViewer.java (2.24 KB, patch)
2010-07-28 04:06 EDT, redstun CLA
no flags Details | Diff
Patch for ToggleCommentAction.java (792 bytes, patch)
2010-07-28 05:11 EDT, redstun CLA
no flags Details | Diff
Patch for TextViewer.java (3.57 KB, patch)
2010-07-28 05:13 EDT, redstun CLA
no flags Details | Diff
Patch for JavaSourceViewerConfiguration.java (496 bytes, patch)
2010-07-28 05:20 EDT, redstun CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description redstun CLA 2010-07-28 03:31:22 EDT
Build Identifier: I20100608-0911

When Java comment prefix "//" is being added/removed, the indentation should be reserved to have the code keep well formatted. Instead of making me want to do a code format after toggling comments, which, in most cases incurs unfriendly format change, and causes problems for version control.

Sample code

package tp;
// Here we go
public static void main(String[] args) {
    if (true) {
        System.out.println("Hello world!");
    )
}

Actual comment result

package tp;
// Here we go
//public static void main(String[] args) {
//    if (true) {
//        System.out.println("Hello world!");
//    )
//}

Expected comment result

package tp;
// Here we go
// public static void main(String[] args) {
    // if (true) {
        // System.out.println("Hello world!");
    // )
// }

Note 
1. the original indentation are expected to be preserved
2. a space should be inserted, as this is the behavior we see the most

When the comment is toggled off, again the indentation should be preserved. that means the additional space after the comment prefix "//" should be removed as well

So for the code below

System.out.print


Reproducible: Always

Steps to Reproduce:
1. Select a block of Java code which have some level of indentation
2. Toggle comment on, observe the result indentation
3. Insert some spaces after the comment prefix "//", then toggle comment off, and observe the result indentation
Comment 1 redstun CLA 2010-07-28 03:57:27 EDT
Additionally, when toggling comment on, the blank lines in the selected region should be skipped, to have the least changed lines.
Comment 2 redstun CLA 2010-07-28 04:02:48 EDT
Created attachment 175373 [details]
Patch for ToggleCommentAction.java
Comment 3 redstun CLA 2010-07-28 04:04:29 EDT
Created attachment 175374 [details]
Patch for JavaSourceViewerConfiguration.java
Comment 4 redstun CLA 2010-07-28 04:06:44 EDT
Created attachment 175375 [details]
Patch for TextViewer.java
Comment 5 redstun CLA 2010-07-28 05:11:50 EDT
Created attachment 175383 [details]
Patch for ToggleCommentAction.java

Patch for org.eclipse.jdt.ui/src/org/eclipse/jdt/internal/ui/javaeditor/ToggleCommentAction.java
Comment 6 redstun CLA 2010-07-28 05:13:16 EDT
Created attachment 175384 [details]
Patch for TextViewer.java

Patch for org.eclipse.jface.text/src/org/eclipse/jface/text/TextViewer.java
Comment 7 redstun CLA 2010-07-28 05:20:24 EDT
Created attachment 175385 [details]
Patch for JavaSourceViewerConfiguration.java

Patch for org.eclipse.jdt.ui/src/org/eclipse/jdt/ui/text/JavaSourceViewerConfiguration.java

The obsolete patches was created with the plain diff command thus could not be applied in Eclipse. I then used a local svn repository so that I can make the working patches with command 'svn diff ...', therefore please ignore the revision numbers in the patch files as they are for my local svn repo.

the indentation settings of my environment seems different from what the Eclipse team use, sorry for this.

Best Regards
Comment 8 Dani Megert CLA 2010-07-28 06:19:51 EDT
>Expected comment result
>
>package tp;
>// Here we go
>// public static void main(String[] args) {
>    // if (true) {
>        // System.out.println("Hello world!");
>    // )
>// }
This won't happen: 'Toggle Comment' is to quickly and temporarily comment out (and later uncomment) a section of code. Therefore the comment always has to start at the first column.

>in most cases incurs unfriendly
>format change, and causes problems for version control.
This is only an issue if spaces are used for indentation.
Comment 9 redstun CLA 2010-07-29 23:16:23 EDT
Probably the sample code I previously used is not helping, but is a negative case not adding the persuasiveness.

Regardless the bad sample, my original intent is for the most common cases I encountered, select just one or a few lines of code, which normally belong to the same code block, thus have the same indentation level.

A simplification would be that, for the selected code block, once commented, the indentation level of the first line in the block should be used. instead of each line preserve it's original indentation level.

I don't think 
> to quickly and temporarily comment out 
> (and later uncomment) a section of code 
is the reason that the comment should always start in the first column.
Comment 10 Dani Megert CLA 2010-07-30 01:53:23 EDT
(In reply to comment #9)
> Probably the sample code I previously used is not helping, but is a negative
> case not adding the persuasiveness.
> 
> Regardless the bad sample, my original intent is for the most common cases I
> encountered, select just one or a few lines of code, which normally belong to
> the same code block, thus have the same indentation level.
I assume you use spaces for indentation, right? Otherwise there's no issue.

> I don't think 
> > to quickly and temporarily comment out 
> > (and later uncomment) a section of code 
> is the reason that the comment should always start in the first column.
It has been designed that way because having all "//" at the same column lets you quickly detect the disabled code. We won't change that.
Comment 11 Dani Megert CLA 2010-08-09 05:54:52 EDT
There are no plans to change the current behavior.
Comment 12 David Rees CLA 2012-11-16 13:11:38 EST
I really think this should be driven by the user's Code Style settings rather than the Eclipse developer's personal style preferences.

If the team feels strongly in having a third type of comment (eclipse toggled line vs block or line), then perhaps an additional setting should be added to the code style preferences so developers can decide for themselves how they want it displayed?

Note that solving this had six votes on stackoverflow - http://stackoverflow.com/questions/4225951/eclipse-toggle-comment-indented
Comment 13 Dani Megert CLA 2012-11-23 07:05:30 EST
(In reply to comment #12)
> I really think this should be driven by the user's Code Style settings
> rather than the Eclipse developer's personal style preferences.
> 
> If the team feels strongly in having a third type of comment (eclipse
> toggled line vs block or line), then perhaps an additional setting should be
> added to the code style preferences so developers can decide for themselves
> how they want it displayed?
> 
> Note that solving this had six votes on stackoverflow -
> http://stackoverflow.com/questions/4225951/eclipse-toggle-comment-indented

If you look at the stackoverflow example you can see that it is not the same as what's requested in comment 0. If you want to add comments like those mentioned on stackoverflow, then you can switch into block selection mode, mark the lines and type "//". Same approach can be used to delete them later.

The reason why we won't change this is not just style. Our implementation allows the formatter to decide which comments are part of the source and which ones are due to 'Toggle Comment'.
Comment 14 David Rees CLA 2012-11-24 11:59:45 EST

(In reply to comment #13)
> (In reply to comment #12)
> > Note that solving this had six votes on stackoverflow -
> > http://stackoverflow.com/questions/4225951/eclipse-toggle-comment-indented
> 
> If you look at the stackoverflow example you can see that it is not the same
> as what's requested in comment 0. If you want to add comments like those
> mentioned on stackoverflow, then you can switch into block selection mode,
> mark the lines and type "//". Same approach can be used to delete them later.
> 
Thank you for the tip.

Though I'm not sure how you can say the SO request isn't for the same thing when it explicitly says "Can I make Eclipse to toggle comment like this instead?" and then shows an example with indented //s.

> The reason why we won't change this is not just style. Our implementation
> allows the formatter to decide which comments are part of the source and
> which ones are due to 'Toggle Comment'.

I do understand that is the purpose, I just personally don't agree with the need for this third type of "toggled comment" and have projects whose coding styles don't agree either. I would be happier if it just did normal block commenting with indents.

But I'm just one guy, and if others are not voting then I guess its not a big enough deal to spend more time/discussion on. I'll just remap my keys not to use this.  Thanks!