JoshDM
JoshDM

Reputation: 5052

How do I enforce blank lines around non-orphaned braces in Eclipse?

The Eclipse format tool (Luna) does not account for

  1. Enforcing 1 blank line before and after a non-orphaned brace on non-function declaration.

for (...) {, if (...) {, } else {, } catch (...) {

  1. Enforcing 0 blank lines before an orphaned closing brace }.

  2. Enforcing 1 blank line after an orphaned closing brace, except when followed by another orphaned closing brace (rule 2 trumping rule 3).

Terminology:

Example:

                ...
                someCall();
            } // ORPHAN CLOSE BRACE - NO BLANK LINE BEFORE OR AFTER
        } // ORPHAN CLOSE BRACE – BLANK LINE AFTER 

        someOtherCall();
    } // ORPHAN CLOSE BRACE – NO BLANK LINE BEFORE

    ...
    String result;
    int foo = 1000;
                                           // a blank line
    for (int x = 0; x < foo; x++) {
                                           // a blank line
        if (x < value) {
                                           // a blank line
            try {
                                           // a blank line
                result = methodCall(x);
                result.setBar(outtaReach);
                handleResult(x, result);
                x = z % 5;
                                           // a blank line
            } catch (Exception ex) {
                                           // a blank line
                doSomething(x, ex);        // no blank line - orphan
            }
                                           // a blank line
        } else {
                                           // a blank line
            otherCall(x);                  // no blank line - orphan
        }                                  // no blank line - orphan
    }
                                           // a blank line
    ...

My reasons for this are to enhance code readability; I'll let the JVM handle optimization. Opinions regarding my desired formatting style aside, is there a way to enforce this particular blank line style within Eclipse? Perhaps there is a plug-in to do this that I haven't yet found, or would I have to code one?

The "Blank Lines" section of the Java Code Formatter of Eclipse does not provide the ability to enforce or apply this. Of note, I don't want to force blank lines between code blocks that do not have braces (see the String | int and result | handleResult() code blocks above as examples).

Closest settings I have to this is:

Blank lines in compilation unit

  • Before package declaration: 0
  • After package declaration: 1
  • Before import declaration: 1
  • Between import groups: 0
  • After import declaration: 1
  • Between class declarations: 1

Blank lines within class declarations

  • Before first declaration: 1
  • Before declarations of the same kind: 1
  • Before member class declarations: 1
  • Before field declarations: 0
  • Before method declarations: 1
  • At beginning of method body: 1

Existing blank lines

  • Number of empty lines to preserve: 1

Upvotes: 16

Views: 1152

Answers (2)

Shaun
Shaun

Reputation: 3895

Uncrustify should be able to do this. Unfortunately it's not available as an Eclipse plugin

http://uncrustify.sourceforge.net/

Upvotes: 1

E-Riz
E-Riz

Reputation: 32905

Have a look at the JIndent plugin; it's commercial but it might be able to achieve the complex rules you're looking for.

Upvotes: 1

Related Questions