2.6.5. Blank Lines

Lets you control the insertion of blank lines to separate statements or declarations with different functions or meanings.

2.6.5.1. General

Lets you specify the blank lines sizes for different Java source elements.

Figure 2.43. Blank Lines settings page

Blank Lines settings page
Declaration section

Lets you control how many blank lines should be printed before a new declaration section. A declaration section means an arbitary amount of similar declarations, like e.g. instance initializers, or methods or enum declarations.

This option is only meaningful when code sorting is enabled for declarations. Refer to Section 2.6.6.1, "Sort Declarations" for more information about the code sorting feature.

Since 1.4

Example 2.644. 2 blank lines before code section

...

Constructor(Foo foo) {
}
¶
¶
public static void method(Foo foo) {
}

...
Package statement

Lets you control how many blank lines should be printed after the package statement.

Example 2.645. 3 blank lines after package statement

package com.triemax.jalopy.printer;
¶
¶
¶
import antlr.collections.AST;

import com.triemax.jalopy.parser.JavaAST;
import com.triemax.jalopy.parser.JavaTokenTypes;

...
Imports

Lets you control how many blank lines should be printed after the last import statement.

Example 2.646. 4 blank lines after last import statement

package com.triemax.jalopy.printer;

import antlr.collections.AST;

import com.triemax.jalopy.parser.JavaAST;
import com.triemax.jalopy.parser.JavaTokenTypes;
¶
¶
¶
¶
public class Printer {
    ...
}
Classes

Lets you control how many blank lines should be printed before the first top level class declaration of a compilation unit and between two class declarations.

Example 2.647. 1 blank line before class declaration

class Foo {
    ...
}

The blank lines before setting is only meaningful if the class declaration is the first top level declaration of a compilation unit and not preceded by a package or import statement.

Example 2.648. 2 blank lines between two class declarations

class One {
    ...
}
¶
¶
class Two {
    ...
}
Interfaces

Lets you control how many blank lines should be printed before the first top level interface declaration of a compilation unit and between two interface declarations.

Example 2.649. 2 blank lines before first interface declarations

¶
¶
interface Fooable {
    ...
}

The blank lines before setting is only meaningful if the interface declaration is the first top level declaration of a compilation unit and not preceded by a package or import statement.

Example 2.650. 3 blank lines between two interface declarations

interface One {
    ...
}
¶
¶
¶
interface Two {
    ...
}
Enums

Lets you control how many blank lines should be printed before the first top level enum declaration of a compilation unit and between two enum declarations.

The blank lines before setting is only meaningful if the enum declaration is the first top level declaration of a compilation unit and not preceded by a package or import statement.

Example 2.651. 3 blank lines between two enum declarations

public enum Season {
    WINTER, SPRING, SUMMER, FALL
}
¶
¶
¶
public enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

Since 1.1

Annotations

Lets you control how many blank lines should be printed before the first top level annotation declaration of a compilation unit and between two annotation declarations.

The blank lines before setting is only meaningful if the annotation declaration is the first top level declaration of a compilation unit and not preceded by a package or import statement.

Example 2.652. 2 blank lines between two enum declarations

public @interface Name {
    String first();
    String last();
}
¶
¶
public @interface Endorsers {
    String[] value();
}

Since 1.1

Methods

Lets you control how many blank lines should be printed between two method/constructor declarations.

Example 2.653. 3 blank lines between two method declarations

public static Printer getInstance() {
    return INSTANCE;
}
¶
¶
¶
public void print(AST node, ASTWriter out)
           throws IOException {
    ...
}
Variables

Lets you control how many blank lines should be printed before and after variable declarations.

Example 2.654. 1 blank line before variable declarations

System.out.println();
¶
int a = 1;
int b = 2;

Example 2.655. 2 blank lines after variable declarations

int a = 1;
int b = 2;
¶
¶
System.out.println();
Left curly brace endline

Forces the given number of blank lines after left curly braces that are printed at the end of the line beginning the compound statement (a.k.a. Sun brace style). When enabled, this option overrides all other blank line settings.

Example 2.656. Blank lines before blocks=1

public void foo() {
¶
    if (condition()) {
¶
        if (anotherCondition()) {
            doSomething();
        }
    }
}

Example 2.657. Blank lines before blocks=1, Blank lines after left curly braces=0

public void foo() {
    if (condition()) {
        if (anotherCondition()) {
            doSomething();
        }
    }
}
Left curly brace endline newline

Forces the given number of blank lines after left curly braces that are printed at the beginning of lines (a.k.a. C brace style). When enabled, this option overrides all other blank line settings.

Since 1.7

Example 2.658. Blank lines before blocks=1

public void foo()
{
¶
    if (condition())
    {
¶
        if (anotherCondition())
        {
            doSomething();
        }
    }
}

Example 2.659. Blank lines before blocks=1, Blank lines after left curly braces=0

public void foo()
{
    if (condition())
    {
        if (anotherCondition())
        {
            doSomething();
        }
    }
}
Right curly brace

Forces the given number of blank lines before closing curly braces, no matter what other blank lines settings dictate.

Example 2.660. Blank lines before blocks=1

public void foo() {
    if (condititon()) {
        if (anotherCondition()) {
            doSomething();
¶
        }
¶
    }
¶
}
Blocks

Lets you control how many blank lines should be printed before and after statement blocks (if-else , for, while, do-while, switch, try-catch-finally, synchronized). Note that the 'Blank Lines After' setting also applies for anonymous inner classes.

Example 2.661. 2 blank lines before and after blocks

AST type = null;
¶
¶
switch (next.getType()) {
    case JavaTokenTypes.LPAREN :
        type = PrinterUtils.advanceToFirstNonParen(next);
        break;
    default :
        type = next;
        break;
}
¶
¶
AST ident = type.getFirstChild();
Case blocks

Lets you control how many blank lines should be printed before each case block of a switch expression.

Example 2.662. 3 blank lines before case blocks

switch (next.getType()) {
¶
¶
¶
    case JavaTokenTypes.LPAREN :
        type = PrinterUtils.advanceToFirstNonParen(next);
        break;
¶
¶
¶
    default :
        type = next;
        break;
}
Control statements

Lets you control how many blank lines should be printed before the statements return, break and continue.

Example 2.663. 2 blank lines before case control statements

switch (next.getType()) {
    case JavaTokenTypes.LPAREN :
        type = PrinterUtils.advanceToFirstNonParen(next);
¶
¶
        break;

    default :
        type = next;
¶
¶
        break;
}

Note that this setting does not apply when a control statement appears directly after the case or default keyword or when the statement is the single member of a statement block without curly braces.

Example 2.664. Setting takes no effect before case control statements

switch (next.getType()) {
    case JavaTokenTypes.LPAREN :
        break;

    default :
        continue;
}

Example 2.665. Setting takes no effect for single statements in blocks

if (isClean())
    return;
Single-line comments

Lets you control how many blank lines should be printed before single-line comments.

Example 2.666. 1 blank line before single-line comment

System.out.println("ERROR");
¶
// XXX use log4j logger
ex.printStackTrace();
Multi-line comments

Lets you control how many blank lines should be printed before multi-line comments.

Example 2.667. 2 blank lines before multi-line comment

System.out.println("ERROR");
¶
¶
/* XXX use log4j logger */
ex.printStackTrace();
Javadoc comments

Lets you control how many blank lines should be printed before Javadoc comments.

Separator comments

Lets you control how many blank lines should be printed before and after separator comments.

Since 1.7

Example 2.668. 2 blank lines before/after separator comment

protected Foo instance;
¶
¶
//~ Constructors -----------------------------------------------------------
¶
¶
public Demo ( ) {}
public Demo (  Foo anotherFoo ) {}
Header

Lets you control how many blank lines should be printed before and after headers.

Example 2.669. No blank lines after header

// Copyright 1998-2000, Foo, Inc. All Rights Reserved.
// Confidential and Proprietary Information of Foo, Inc.
// Protected by or for use under one or more of the following patents:
// U.S. Patent Nos. X,XXX,XXX. Other Patents Pending.
package com.foobar;

...

Example 2.670. 2 blank lines after header

// Copyright 1998-2000, Foo, Inc. All Rights Reserved.
// Confidential and Proprietary Information of Foo, Inc.
// Protected by or for use under one or more of the following patents:
// U.S. Patent Nos. X,XXX,XXX. Other Patents Pending.
¶
¶
package com.foobar;

...
Footer

Lets you control how many blank lines should be printed before and after footers.

SQLJ clauses

Lets you control how many blank lines should be printed before and after SQLJ clauses.

Example 2.671. 2 blank lines before/after SQLJ clause

Integer salesRepID   = new Integer(358);
String  salesRepName = "Jouni Seppanen";
Date    dateSold     = new Date(97,11,6);
¶
¶
#sql { INSERT INTO SALES VALUES( :itemID,:itemName,:dateSold,:totalCost,
      :salesRepID,:salesRepName) };
¶
¶
SalesRecs sales;
Assignment section

Lets you control how many blank lines are printed after a section with several (at least two) consecutive assignment expressions.

Since 1.4

Example 2.672. 1 blank line after assignment section

c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0, 0, 0, 0);
¶
middlePanel.add(buttonsPanel, c);

2.6.5.2. Misc

Lets you control miscellaneous separation settings.

Figure 2.44. Blank Lines Misc settings page

Blank Lines Misc settings page
2.6.5.2.1. Misc
Keep blank lines up to

If enabled, retains up to the given number of blank lines found in the original source. Note that Jalopy still takes your other blank lines settings into account.

Note that if you disable this option, all original blank lines will be ignored!

Example 2.673. Source code with blank lines to separate declaration sections

aMVString = new MultiValueString("abc");
¶
System.out.println("MV = "+aMVString);
¶
System.out.println("MV0 = "+aMVString.extract(0));
System.out.println("MV1 = "+aMVString.extract(1));
System.out.println("MV2 = "+aMVString.extract(2));
System.out.println("");

If this feature is left disabled, Jalopy will print the individual lines according to the current blank lines settings but won't try to keep any blank lines.

Keep blank lines in headers up to

If enabled, retains up to the given number of blank lines found in the original source file between header (and footer) comments.

This option is only signficant when you enable the header/footer feature and specify a multi-header template, without enabling the override mode.

Since 1.7

Example 2.674. Source code with blank lines to separate headers

/*
 * Foo.java
 * 
 * $Header: //depot/jalopy_main/jalopy-doc/src/xdocs/manual/core-printer-separation.xml#12 $
 *//*
 * Copyright (c) 2002 FooBar, Inc. All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of FooBar, Inc. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you
 * entered into with FooBar, Inc.
 */
package com.foobar.lemon;
Remove blank lines for method prototypes

When enabled, blank lines around method declarations in interfaces and abstract classes are removed. If left disabled, blank lines will be printed according to the blank lines settings for methods (see "Blank lines after methods").

Since 1.2

Example 2.675. Method prototypes

interface foo {

    public void method1();

    public void method2();

    public void method3();
}

Example 2.676. Method prototypes without blank lines

interface foo {
    public void method1();
    public void method2();
    public void method3();
}
Ignore control statements option for break in switch

When enabled, the "Blank Lines before control statements" option is ignored for break statements within switch blocks.

Since 1.3

Example 2.677. 1 blank lines before control statements

switch (number) {
    case 1:
        System.out.println();

        break;

    default:
        System.out.println();

        break;
}

Example 2.678. 1 blank lines before control statements, but option ignored

switch (number) {
    case 1:
        System.out.println();
        break;

    default:
        System.out.println();
        break;
}
IIgnore blocks option in switch

When enabled, the "Blank Lines before blocks" option is ignored within switch blocks.

Since 1.3

Example 2.679. 1 blank lines before blocks

switch (number) {
    case 1:

        if (DEBUG)
            System.out.println("FIRST NUMBER");

        break;

    default:

        while (true)
            perform();

        break;
}

Example 2.680. 1 blank lines before blocks, but option ignored

switch (number) {
    case 1:
        if (DEBUG)
            System.out.println("FIRST NUMBER");

        break;

    default:
        while (true)
            perform();

        break;
}
2.6.5.2.2. Chunks

Lets you define what makes a chunk: a section of associated statements. With "Variable identifiers" and/or "Align assignments" enabled, Jalopy determines what statements can be aligned using these options.

By comments

If enabled, a statement with a comment before is recognized as the start of a new chunk.

Example 2.681. Aligned assignments/identifiers

String        text  = "text";
int           a     = -1;

// create a new entry
History.Entry entry = new History.Entry(text);

Example 2.682. Aligned assignments/identifiers, chunks detected by comments

String text  = "text";
int    a     = -1;

// create a new entry
History.Entry entry = new History.Entry(text);
By blank lines

If enabled, a statement which has one or more blank lines before is recognized as the start of a new chunk.

Example 2.683. Aligned assignments/identifiers

String        text  = "text";
int           a     = -1;

History.Entry entry = new History.Entry(text);

Example 2.684. Aligned assignments/identifiers, chunks detected by blank lines

String text  = "text";
int    a     = -1;

History.Entry entry = new History.Entry(text);
By line wrap

If enabled, a statement that takes more than just one line to print is recognized as the start of a new chunk.

With standard indentation, it is recommended to habe this option enabled, because otherwise wrapped expressions might obscure aligned assignments.

Since 1.3

Example 2.685. Aligned assignments

int labelR_x      = Math_min(iconR.x, textR.x);
int labelR_with   = Math_max(iconR.x + iconR.width, textRrr.x) -
    labelR_x;
int labelR_width  = Math_max(iconR.x + iconR.width, textRrr.x) -
    labelR_x;
int labelR_y      = Math_min(iconR.y, textR.y);
int labelR_height = Math_max(iconR.y + iconR.height,
        textR.y + textR.height);
int labelR_x      = Math_min(iconR.x, textR.x);
int lab           = Math_min(iconR.x, textR.x);

Example 2.686. Aligned assignments, chunks detected by line wrap, prefer wrap after assign

int labelR_x = Math_min(iconR.x, textR.x);
int labelR_with =
    Math_max(iconR.x + iconR.width, textRrr.x) - labelR_x;
int labelR_width =
    Math_max(iconR.x + iconR.width, textRrr.x) - labelR_x;
int labelR_y = Math_min(iconR.y, textR.y);
int labelR_height =
    Math_max(iconR.y + iconR.height, textR.y + textR.height);
int labelR_x = Math_min(iconR.x, textR.x);
int lab      = Math_min(iconR.x, textR.x);