google-java-format
google-java-format copied to clipboard
Failed reformatting on simultaneous declarations
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
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.
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.