google-java-format icon indicating copy to clipboard operation
google-java-format copied to clipboard

Failed reformatting on simultaneous declarations

Open CharlesZ-Chen opened this issue 8 years ago • 3 comments

Hi there,

I meet a problem when trying to use google-java-format to re-format on simultaneous declarations in a java file:

According to Google Java Style Guide 4.8.2-variable-declarations:

Every variable declaration (field or local) declares only one variable: declarations such as int a, b; are not used.

However, given java file:

public class ReplaceColor {
    private int      oldRed, oldGreen, oldBlue;
    private IntRange oldRedRange, oldGreenRange, oldBlueRange;
    private boolean  isRange;
}

using google-java-format would not re-format the simultaneous declarations in above file.

Is this a defect that google-java-format forget to consider about?

If not, could you guys help me a bit on pointing out where should I getting started to hack this project to get this feature in my own fork?

Many thanks & best wishes,

Charles

CharlesZ-Chen avatar Dec 06 '16 02:12 CharlesZ-Chen

This is related to #51 - the formatter doesn't currently make non-whitespace changes, and there are some difficulties to doing that we haven't addressed yet. Fixing imports and modifier order are the only exceptions, and that doesn't happen in the core formatting code.

cushon avatar Jan 13 '17 01:01 cushon

Just write this as a note for others who also meet the "multiple declarations" problem:

I've searched a bit on existing refactor tools that could refactor multiple declarations in one statement. However I didn't find anyone works.

I've filed an issue on google-java-format here:

google/google-java-format#104

But at the moment nobody gives a response on this issue.

Thus, as a workaround, I write a simple and ugly refactor that specifically refactor multiple declarations:

https://github.com/CharlesZ-Chen/multiDeclRefactor

This refactor could work as a pre-processor before doing annotation insertion on a project. However, since I developed it in a catch-the-DDL way, the code is a bit unordered and not good for maintenance. It should be just a temporary workaround, and hopefully later on some mature & professional refactor tools could solve this problem wonderfully.

CharlesZ-Chen avatar Feb 22 '18 03:02 CharlesZ-Chen