eclipse-jdt / eclipse-jdt/eclipse.jdt.core

Formatter enhancements for fluent builder method chaining

Open
#951 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

I would like to have more control over the formatting options for method chaining, particularly when using fluent builders or stream processing. Specifically, controlling how line wrapping works, e.g. based on the number of chained methods, total line length etc. The formatter should ideally recognize nested chaining, and adapt the wrapping/indentation rules accordingly.

For example, a rule might be to only line wrap when 3 or more methods are chained, so that you get:

```
// 2 chained methods remain on single line
List matches = records.stream().sorted().collect(Collectors.toList());
// 4 chained methods are wrapped one per line
List matches = records.stream()
.filter(MySubClass.class::isInstance)
.map(MySubClass.class::cast)
.sorted()
.collect(Collectors.toList());
```

Some fluent builders allow for nested builders, so a rule to detect when the return type changes and add nested indentation would be useful. When the return type reverts back to the parent, the indentation would go back to the parent level.

For example, a real-world example of a Camel route builder:

```
from("seda:a")
.choice()
.when(header("foo").isEqualTo("bar"))
.to("seda:b")
.when(header("foo").isEqualTo("cheese"))
.to("seda:c")
.otherwise()
.to("seda:d");
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.