eclipse-jdt / eclipse-jdt/eclipse.jdt.core
Formatter - function call wrapping issue when only one invocation
- Dominant language
- Java
- Stars
- 237
- Forks
- 195
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 47
Description
I cannot get the formatting settings of "Line Wrapping > Wrapping settings > Function Calls > Qualified invocations" to work the way I want.
Previously (I believe before Eclipse 4.8) it was possible to get the code formatted like this:
```java
public class FormattingIssue {
static void example() {
var x = new StringBuilder()
.append("These invocations ")
.append("cause the line to exceed ")
.append("80 characters")
.toString();
x.indexOf("This function call causes the line to exceed 80 characters",
0);
}
}
```
Now, however, the closest I am able to get are the following two alternatives (none of which are satisfactory):
* Alternative 1. Chosen option: _Wrap all elements, every element on a new line_
```java
public class FormattingIssue {
static void example() {
var x = new StringBuilder()
.append("These invocations ")
.append("cause the line to exceed ")
.append("80 characters")
.toString();
x
.indexOf("This function call causes the line to exceed 80 characters",
0);
}
}
```
The formatting of `x.indexOf(...)` seems odd; the line wrapping, which is done to prevent the line length from exceeding some chosen limit, causes the line length to be even larger than it would have been if the line had not been wrapped at all, in addition to looking rather awkward.
* Alternative 2. Chosen option: _Wrap all elements, except first element if not necessary_
```java
public class FormattingIssue {
static void example() {
var x = new StringBuilder().append("These invocations ")
.append("cause the line to exceed ")
.append("80 characters")
.toString();
x.indexOf("This function call causes the line to exceed 80 characters",
0);
}
}
```
I think chained method calls are generally easier to read when all method calls are aligned (with a few exceptions, such as when using `Stream.of(...)`).
I suggest adding an option to not wrap the method call if it is only a single method call (i.e. no chained method calls). This seems to be how IntelliJ handles it. This would, however, increase the risk of having the method arguments squished to the right if e.g. the variable name and/or the method name is very long, which IntelliJ seems to have solved by pushing the arguments to the left if the combined length of the variable name, method name and first argument exceeds the maximum line length:
```java
public class FormattingIssue {
static void example() {
var someveryveryverylongvariablename = new FormattingIssue();
someveryveryverylongvariablename.someveryveryveryverylongmethodname(
"foo",
"bar");
}
void someveryveryveryverylongmethodname(String arg1, String arg2) {}
}
```
Here are my formatting settings: [eclipse-formatter-rules.txt](https://github.com/eclipse-jdt/eclipse.jdt.core/files/9787966/eclipse-formatter-rules.txt)
Contributor guide
Assessment
This issue has not been assessed yet.