开发者

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

开发者 https://www.devze.com 2023-01-06 18:40 出处:网络
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?

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.


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


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消