Bug 6284 - Java formatter enhancements
Summary: Java formatter enhancements
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.0   Edit
Hardware: All All
: P5 enhancement (vote)
Target Milestone: 3.0 M4   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 27111 (view as bug list)
Depends on:
Blocks:
 
Reported: 2001-11-24 02:13 EST by Kirill Frolov CLA
Modified: 2004-04-19 10:09 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kirill Frolov CLA 2001-11-24 02:13:53 EST
I propose the following advanced Java code formatter features for 
implementation:

1. When the indentation level is X (or i-th column), formatter shouldn't 
indent after that level (column)

2. When the formatter continues some complex expression on the new line, the 
continuation indent should be configurable

3. User should be able to configure the following items:
   * Should formatter preserve empty lines or not (this preference is already 
present) and is it should then how many lines to preserve (to be added)
   * Should formatter preserve line breaks or not
   * Should formatter preserve spaces or not

4. Should formatter place statements on the new line (always/never/as is)

5. Should formatter place simple body on the same line (always/never/as is)

6. Should formatter place 'else' on the same line (always/never/as is) - 
always and never preference already implemented

7. Should formatter keep array initialization (yes/no/yes but indent)

8. Should formatter align initialization (yes/no/class attributes only)
   Example:
     int i     = 5; // aligned 
     int iiiii = 5; // aligned 
     int a     = 34; // aligned 
     k = t * r; // not aligned because it is not an initialization

9. Should formatter surround simple body with braces (always/never/if on the 
next line)

10. position on the break operator in the switch block (normal/deindent)

11. Should formatter break an expression either before or after a binary 
operator 

12. Should formatter make empty lines around the declarations and is yes then 
how many in each of the following situations:
    * number of empty lines before class declaration
    * number of empty lines before class method
    * number of empty lines before class attribute
    * number of empty lines after class declaration
    * number of empty lines after class method
    * number of empty lines after class attribute

13. Should formatter insert spaces in expressions or not
    * space before control expression (between if/while/switch etc. and the 
following opening bracket)
    * space around binary operator (c=a+b or c = a + b)
    * spaces within parentheses
       ((x*y)+5)/3 or 
       ( ( x*y ) +5 ) /3 (provided that the previous option is off)
       ( ( x * y ) + 5 ) / 3 (provided that the previous option is on)
    * spaces within square brackets

Also I propose the followig Java and Javadoc comments formatter features for 
implementation:

1. Should formatter preserve spaces in comments or not

2. Format of C-style (/* xxx */) comments should include the following 
preferences:
   * Number of empty lines before a comment
   * Number of empty lines after a comment
   * Should formatter preserve line breaks in comments or not
   * Should formatter preserve empty lines in comments or not
   * Should formatter format small comments to occupy one line or not
     Example: imean should formatter transform
       /*
        abcde
       */
      to
       /* abcde */
   * Should formatter format c-style comments (I mean split into lines 
according to maximum line length preference)

3. Format inline comments (// xxx) should include the following preferences:
   * Whether to format inline comments (and use the following options) or not 
(simply skip them)
   * Inline comment separatior (one space/tab/none/as is)
   * Should formatter locate inline comment on a new line (always/never/as is)

4. Format Javadoc comments (/** xxx */) should include the following 
preferences:
   * Number of empty lines before a comment
   * Number of empty lines after a comment (the value of this preference is 
questionable because of the current javadoc implementation which consideres 
javadoc comment to describing the following item only if there is NO empty 
lines between javadoc and the item being described. But the future 
implementations may change this behaviour. Besides we can customize the 
default doclet within eclipse as a subproject (by the way it may also collect 
metrics))
   * Should formatter preserve line breaks within javadoc comments or not
   * Should formatter preserve empty lines within javadoc comments or not
   * Should formatter use one asterisk (*)/two asterisks(**)/or nothing to 
begin the consequent javadoc comment lines
     Example:
       /**
        * This is some comment
        */
     or
       /**
        ** This is some comment
        **/
     or
       /**
        This is some comment
        */
     Note: I prefer the second formatting option and all the following 
examples will use two asterisks
   * Should formatter fill the first line of javadoc with some char/empty 
line/disabled (char should be user-defined)
     Example:
       1. char fill (for example, '-')
         /** ------------------------------------------------
          ** This is some comment
          **/
       2. empty line
         /**
          ** This is some comment
          **/
       3. disabled
         /** This is sime comment
          **/
   * Should fprmatter fill the last line of javadoc with some char/empty 
line/disabled (char should be user-defined)
     Example is the same as for previous preference but it changes the last 
line of a comment:
       ------------------------------------------- **/
       **/
       This is some comment **/
   * Should formatter format short comments to occupy one line or not
     Example: should formatter transform 
       /** ******************************
        ** This is some comment
        ****************************** **/
     to
       /** This is some comment **/
   * Should formatter format javadoc comments (should it use the maximum line 
length preference to split comments into lines)


As a professional programmer I DO BELIEVE that these enhancements will make 
readability of code much better for EVERY programmer regardless of his own 
habit and style.
And besides there are a lot of other applications of formatter (only to 
mention: COMPARABLE code metrics, corporate code style and so on)
Comment 1 Olivier Thomann CLA 2002-01-29 13:40:27 EST
Defer till post 2.0.
Comment 2 Philipe Mulet CLA 2002-07-25 06:30:41 EDT
Resurrecting formatter issues in 2.1 stream.
Comment 3 Philipe Mulet CLA 2002-07-25 06:32:33 EDT
Clearing resolution
Comment 4 Channing Walton CLA 2002-10-22 10:28:47 EDT
An additional option I would like is for the formatter to reorganise methods and
fields according to the Java Coding Guidelines:

1. Static variable field declarations
2. Instance variable field declarations
3. Static initializer
4. Static member inner class declarations
5. Static method declarations
6. Instance initializer
7. Instance constructor declarations
8. Instance member inner class declarations
9. Instance method declarations
Comment 5 Arni J Rognvaldsson CLA 2002-11-25 13:09:00 EST
*** Bug 27111 has been marked as a duplicate of this bug. ***
Comment 6 Olivier Thomann CLA 2003-01-15 13:23:04 EST
We need to clear 2.1 bug reports that won't be addressed before 2.1. The new 
implementation is still in the works. Therefore we cannot include it for 2.1. 
Not enough testing and we need to polish the preferences. This will be address 
for 2.2 as stated in the JDT/Core plan.
Comment 7 Philipe Mulet CLA 2003-06-12 06:35:36 EDT
Resurrecting for 3.0
Comment 8 Olivier Thomann CLA 2003-06-12 15:58:51 EDT
Reopen for 3.0 consideration.
Comment 9 Olivier Thomann CLA 2003-10-03 12:02:43 EDT
Please look at the new formatter options when they are exposed in the UI
(between M4 and M5).
If you believe that we should add more options, open one PR per request.
The sorting of compilation unit elements is already available. See Source>Sort
members. This is not a code formatter feature.
Comment 10 David Audel CLA 2003-10-14 07:35:04 EDT
Verified.
Comment 11 thomas menzel CLA 2004-04-19 09:57:54 EDT
i'd like this from the request to be implemented:

9. Should formatter surround simple body with braces (always/never/if on the 
next line)
Comment 12 thomas menzel CLA 2004-04-19 09:59:47 EDT
sorry didnt realize this bug is closed... reopen new one for this
Comment 13 Olivier Thomann CLA 2004-04-19 10:09:43 EDT
The rule we try to follow is that the code formatter doesn't change the code.
Adding braces is changing the code. So it won't be done.