Sam
Sam

Reputation: 8693

How to search lines which don't contain an empty line before the return statement

I would prefer to have an empty line prior to the last return statement of any Java method. How do I find and replace such lines using Eclipse?

public int foo() {
    ...
    ...

    System.out.println("end of foo");
    return 1;
}

In this case , I would prefer an empty line before the return statement, do note that there could be any statement before return. How do I find and replace in such occurences.

Upvotes: 1

Views: 207

Answers (2)

sholsapp
sholsapp

Reputation: 16070

You can go to Window -> Preferences and expand the trees Java -> Code Style -> Formatter and change/enforce many rules, but I don't think you can check/enforce a new line before 'return' as a built in option in Eclipse.

Instead you'll have to follow Josiah's advice and write your own nifty check-style tool (which are really fun to have).

Upvotes: 1

Josiah
Josiah

Reputation: 4854

You could use a regular expression to search for the pattern "; *\n[ \t]*return" and replace the \n with 2 \n characters

Check out this overview of regular expressions in java (only if you need it. You could be a regexp pro for all I know ;) ) http://www.regular-expressions.info/java.html

Upvotes: 2

Related Questions