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

2

16

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
Mistletoe answered 29/5, 2015 at 14:52 Comment(10)
I know it doesn't help anything, but why?Professed
@Trengot I've been looking for something like this for ages, it helps alot with readability. I like to know the answer tooFiacre
@Trengot - as nafas says, natural readability.Mistletoe
Ok, personal preference I guess. Looks really confusing to me.Professed
@Trengot - Out of curiosity, is it the rule enforcement structure or the resulting code output that appears confusing to you, and how so?Mistletoe
@JoshDM, The resulting code layout. I find having loads of extra blank lines within code blocks breaks the code up and makes it harder to see each block as a coherent 'lump' of code.Professed
The blank lines aren't internal to the blocks themselves, if a block is defined as a run of code enclosed within braces. There is nothing breaking up those "blocks". The blank line basically follows or precedes a jump in indentation.Mistletoe
@Mistletoe I'm guessing you haven't found an answer for this yet... :(Fiacre
Sorry @nafas, no. Apparently everyone hates my spacing desires though, according to the high number of votes Trengot's comment has gained.Mistletoe
@Mistletoe well, right now I'm doing what you are asking manually and I hate it, it would have been great if it could be automated.Fiacre
S
1

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

Stafani answered 3/6, 2015 at 13:19 Comment(1)
Was hoping for something without a cost attached.Mistletoe
K
1

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

http://uncrustify.sourceforge.net/

Kirbie answered 8/6, 2015 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.