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

No linebreak at single variables

Open koppor opened this issue 5 months ago • 3 comments

This refs https://github.com/jhipster/prettier-java/issues/626#issuecomment-1949566463.

Prettier-Java 2.7.5

Input:

            git.remoteAdd()
               .setName(remoteName)
               .setUri(new URIish(headRepoCloneUrl))
               .call();

Output:

            git
                .remoteAdd()
                .setName(remoteName)
                .setUri(new URIish(headRepoCloneUrl))
                .call();

Expected behavior:

Dots are aligned - and first method call directly after variable.


Similar:

-                .filter(r -> r.getURIs().stream().anyMatch(uri -> uri.toString().contains("JabRef/jabref")))
+                .filter(r ->
+                    r
+                        .getURIs()
+                        .stream()
+                        .anyMatch(uri ->
+                            uri.toString().contains("JabRef/jabref")
+                        )
+                )

It should be r.getURIs() and then .stream() in the next line.

koppor avatar Sep 13 '25 09:09 koppor

Maybe, I understand the intention:

Methods remoteAdd, setName are all applied to git, therefore they form a "new block", which should be indented and be on a new line - because the first method more belongs to the chain than to the git object itself.


The second example only looks bad, because the variable name is too short :)

koppor avatar Sep 13 '25 09:09 koppor

However, the idea feels strange at following diff:

-            PagedIterator<GHPullRequest> prIterator = repo.queryPullRequests().direction(GHDirection.DESC).state(GHIssueState.ALL).list().iterator();
+            PagedIterator<GHPullRequest> prIterator = repo
+                .queryPullRequests()
+                .direction(GHDirection.DESC)
+                .state(GHIssueState.ALL)
+                .list()
+                .iterator();

the .,queryPullRequests() does not belong to PagedIterator.

Thus, the consequence would be to have

           PagedIterator<GHPullRequest> prIterator =
               repo
                   .queryPullRequests()
                   .direction(GHDirection.DESC)
                   .state(GHIssueState.ALL)
                   .list()
                   .iterator();

But this "feels" too verbose. Can't we accept that the first call belongs to the variable?

           PagedIterator<GHPullRequest> prIterator =
               repo.queryPullRequests()
                   .direction(GHDirection.DESC)
                   .state(GHIssueState.ALL)
                   .list()
                   .iterator();

Update This seems to be similar to palantir-java-format when reading their motivation: https://github.com/palantir/palantir-java-format#motivation--examples

koppor avatar Sep 13 '25 09:09 koppor

To chip in, I personally prefer the way it currently is for sake of accessibility. Now I understand using tabs as indentation is commonly not done but being able to adjust the indentation width is an accessibility feature (Reference https://adamtuttle.codes/blog/2021/tabs-vs-spaces-its-an-accessibility-issue/ )

While

            git
                .remoteAdd()
                .setName(remoteName)
                .setUri(new URIish(headRepoCloneUrl))
                .call();

still reads legible with double width tabs

                        git
                                .remoteAdd()
                                .setName(remoteName)
                                .setUri(new URIish(headRepoCloneUrl))
                                .call();

The former option

            git.remoteAdd()
               .setName(remoteName)
               .setUri(new URIish(headRepoCloneUrl))
               .call();

only properly works with exactly this level of indentation, otherwise nothing lines up:

                        git.remoteAdd()
                              .setName(remoteName)
                              .setUri(new URIish(headRepoCloneUrl))
                              .call();

this gets continuously more extreme the smaller your default tab-width vs the users-tab width is e.g. from 2-space-tabs to 6-space-tabs

      git.remoteAdd()
                      .setName(remoteName)
                      .setUri(new URIish(headRepoCloneUrl))
                      .call();

spthiel avatar Nov 04 '25 14:11 spthiel