Community
Participate
Working Groups
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
Additionally, when toggling comment on, the blank lines in the selected region should be skipped, to have the least changed lines.
Created attachment 175373 [details] Patch for ToggleCommentAction.java
Created attachment 175374 [details] Patch for JavaSourceViewerConfiguration.java
Created attachment 175375 [details] Patch for TextViewer.java
Created attachment 175383 [details] Patch for ToggleCommentAction.java Patch for org.eclipse.jdt.ui/src/org/eclipse/jdt/internal/ui/javaeditor/ToggleCommentAction.java
Created attachment 175384 [details] Patch for TextViewer.java Patch for org.eclipse.jface.text/src/org/eclipse/jface/text/TextViewer.java
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
>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.
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.
(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.
There are no plans to change the current behavior.
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
(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'.
(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!