Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • non-orphaned = paired with operator/keyword
  • non-function = not a class or method declaration

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
like image 317
JoshDM Avatar asked May 29 '15 14:05

JoshDM


2 Answers

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

like image 68
E-Riz Avatar answered Oct 12 '22 23:10

E-Riz


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

http://uncrustify.sourceforge.net/

like image 24
Shaun Avatar answered Oct 12 '22 23:10

Shaun