Skip to content

Formatting code with intellij plugin and using the jar gives different results #566

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

Open
Allc9001 opened this issue Feb 19, 2021 · 8 comments
Assignees
Labels

Comments

@Allc9001
Copy link

Allc9001 commented Feb 19, 2021

If I format the following file using the intellij plugin (IDE version: Ultimate 2020.2.4 / plugin version: google-java-format 1.9.0.0. ) I get different result when compared to using the jar on the command line.

original example file:

package x;

public class test2 {

  static final String LONG_STRING = "This is a long string that makes the total line length for this line over 100 characters";

  static final String ANOTHER_LONG_STRING =
      "This is another long string but this line is under 100 characters "
      + "and this line is also under 100 characters "
      + "but this line is over 100 characters and will not be reformatted to under 100 characters using the intellij plugin but will be reformatted to under 100 characters using the jar.";

  public test2() {
    int x=1;if(x==1){x=2;}else{x=3;}
  }

}

after formatting using intellij plugin (using menu code / reformat code) I get this:

package x;

public class test2 {

  static final String LONG_STRING =
      "This is a long string that makes the total line length for this line over 100 characters";

  static final String ANOTHER_LONG_STRING =
      "This is another long string but this line is under 100 characters "
          + "and this line is also under 100 characters "
          + "but this line is over 100 characters and will not be reformatted to under 100 characters using the intellij plugin but will be reformatted to under 100 characters using the jar.";

  public test2() {
    int x = 1;
    if (x == 1) {
      x = 2;
    } else {
      x = 3;
    }
  }
}

And after formatting on the command line:

/tmp $ java -version
openjdk version "11.0.9" 2020-10-20
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.9+11)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.9+11, mixed mode)
/tmp $ cksum ~/lib/google-java-format-1.9-all-deps.jar
2002751029 3404610 ~/lib/google-java-format-1.9-all-deps.jar
 /tmp $ java -jar ~/lib/google-java-format-1.9-all-deps.jar ./test2.java

I get this:

package x;

public class test2 {

  static final String LONG_STRING =
      "This is a long string that makes the total line length for this line over 100 characters";

  static final String ANOTHER_LONG_STRING =
      "This is another long string but this line is under 100 characters and this line is also"
          + " under 100 characters but this line is over 100 characters and will not be"
          + " reformatted to under 100 characters using the intellij plugin but will be"
          + " reformatted to under 100 characters using the jar.";

  public test2() {
    int x = 1;
    if (x == 1) {
      x = 2;
    } else {
      x = 3;
    }
  }
}
@cies
Copy link

cies commented Aug 30, 2021

Possibly related to #642

@cushon
Copy link
Collaborator

cushon commented Aug 30, 2021

The CLI has a separate pass that reflows long strings literals (corresponding to --skip-reflowing-long-strings):

formatted = StringWrapper.wrap(Formatter.MAX_LINE_LENGTH, formatted, formatter);

The IntelliJ plugin currently just runs the core formatter logic, not the string reflowing part.

@eShorko
Copy link

eShorko commented Jun 1, 2022

Hi,
we want to start using the plugin in IntelliJ and Eclipse IDE and format code on Save Actions and also in GitHub workflows using the axel-op/googlejavaformat-action. But, it gives different results and our GitHub workflow fails due to unformatted code, even if all code is formatted in the IDE.
Is there any workaround for this in IntelliJ and Eclipse plugin or maybe a fix coming?

@Allc9001
Copy link
Author

@eShorko don't hold your breath. I've added git hooks to format all code with the cli on commit to keep things consistent and force the same formatting for everything that is committed.

@mdhirsch
Copy link

mdhirsch commented Sep 7, 2022

This is causing me grief at work as well.

@ssh352
Copy link

ssh352 commented Nov 8, 2023

too bad IntelliJ plugin still cannot line break
cc #800

@dweiss
Copy link
Contributor

dweiss commented Sep 13, 2024

So... this has been a problem for me too and after I debugged the source of the problem I found my way here.

Is there any reason why the IntelliJ plugin doesn't reflow long strings by default, @cushon ? Seems like an easy fix to add:

formatted = StringWrapper.wrap(formatted, this);

to the plugin. One could even make it configurable, much like the formatting style. I can produce a patch but wanted to make sure it's not some kind of policy to not make any structural changes to the code in the plugin (I don't see the reason for it).

@zaneduffield
Copy link

This is giving me grief as well.

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

No branches or pull requests

9 participants