Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed reformatting on simultaneous declarations #104

Open
CharlesZ-Chen opened this issue Dec 6, 2016 · 3 comments
Open

Failed reformatting on simultaneous declarations #104

CharlesZ-Chen opened this issue Dec 6, 2016 · 3 comments

Comments

@CharlesZ-Chen
Copy link

CharlesZ-Chen commented Dec 6, 2016

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

@cushon
Copy link
Collaborator

cushon commented Jan 13, 2017

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.

@CharlesZ-Chen
Copy link
Author

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:

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

@romani
Copy link

romani commented Aug 31, 2024

from style guide https://google.github.io/styleguide/javaguide.html#s4.8.2-variable-declarations

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

we detected the same in testing of checkstyle checkstyle/checkstyle#15587 (comment)
while we wait fix for formatter users can rely on checkstyle to hint/highlight such problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants