prettier-java icon indicating copy to clipboard operation
prettier-java copied to clipboard

Print width not obeyed on assignments

Open jtkiesel opened this issue 3 years ago • 2 comments

Prettier-Java 1.6.1

# Options:
--print-width 80
--tab-width 4

Input:

package org.prettier.java;

public class MyReallyLongClassName {

    public void method() {
        MyReallyLongClassName myReallyLongVariableName = new MyReallyLongClassName();
        MyReallyLongClassName myReallyLongVariableName2 = myReallyLongMethodName();
    }

    public MyReallyLongClassName myReallyLongMethodName() {
        return new MyReallyLongClassName();
    }
}

Output:

package org.prettier.java;

public class MyReallyLongClassName {

    public void method() {
        MyReallyLongClassName myReallyLongVariableName = new MyReallyLongClassName();
        MyReallyLongClassName myReallyLongVariableName2 = myReallyLongMethodName();
    }

    public MyReallyLongClassName myReallyLongMethodName() {
        return new MyReallyLongClassName();
    }
}

Expected behavior:

package org.prettier.java;

public class MyReallyLongClassName {

    public void method() {
        MyReallyLongClassName myReallyLongVariableName =
            new MyReallyLongClassName();
        MyReallyLongClassName myReallyLongVariableName2 =
            myReallyLongMethodName();
    }

    public MyReallyLongClassName myReallyLongMethodName() {
        return new MyReallyLongClassName();
    }
}

I expect lines that exceed the configured print width to be broken after =.

This used to be an issue for Prettier with JavaScript as well, see https://github.com/prettier/prettier/issues/2482. However, https://github.com/prettier/prettier/pull/10222 seems to have fixed most of the issues (at least for the two examples I have given, of instantiation and method return value assignment).

jtkiesel avatar Feb 08 '22 20:02 jtkiesel

Thanks for reporting this ! It is an issue only when the first element of the assignment is a new expression. Unfortunately, this part of the code is particularly complex due to the way the Java syntax is build around expressions. It should be possible to handle this use case, but I may need some time to investigate

clementdessoude avatar Mar 21 '22 07:03 clementdessoude

as it's a bug, let's start with this bounty. If it's more work than expected, I'll increase it

pascalgrimaud avatar Mar 21 '22 08:03 pascalgrimaud

In November I created a PR (#564) that fixes this, but the primary maintainer (@clementdessoude) seems to have been inactive on GitHub since then. I'm not sure who all of the maintainers of this project are, or who else may be able to review the PR (and hopefully merge it and create a new release). @pascalgrimaud Any advice?

jtkiesel avatar Feb 26 '23 22:02 jtkiesel

I'll try to contact personaly @clementdessoude and let you know if no answer, I'll have a look to your PR, but I never work on this project :)

pascalgrimaud avatar Feb 27 '23 08:02 pascalgrimaud

Hi @jtkiesel ! I'm so sorry, I had a few roughs months, I put this project aside for a moment... I will have a look right now ! Thanks for notifying @pascalgrimaud !

clementdessoude avatar Feb 27 '23 10:02 clementdessoude

@clementdessoude No worries at all! I am sorry if I came off as impatient or rude. To be honest, this is my first contribution to OSS, so I wasn't sure of the etiquette. I hope that life is treating you better! Thank you and @pascalgrimaud both!

jtkiesel avatar Feb 27 '23 15:02 jtkiesel