Title: Keep a Mutiny group call on one line with the call that completes it · Issue #25 · openjavaformat/open-java-format · GitHub
Open Graph Title: Keep a Mutiny group call on one line with the call that completes it · Issue #25 · openjavaformat/open-java-format
X Title: Keep a Mutiny group call on one line with the call that completes it · Issue #25 · openjavaformat/open-java-format
Description: Motivation SmallRye Mutiny is designed so that a chain reads as a sentence. A "group" call selects what you are talking about, and the next call says what to do: onItem().transform(…), onFailure(IOException.class).recoverWithItem(…), sub...
Open Graph Description: Motivation SmallRye Mutiny is designed so that a chain reads as a sentence. A "group" call selects what you are talking about, and the next call says what to do: onItem().transform(…), onFailure(IO...
X Description: Motivation SmallRye Mutiny is designed so that a chain reads as a sentence. A "group" call selects what you are talking about, and the next call says what to do: onItem().transform(…), on...
Opengraph URL: https://github.com/openjavaformat/open-java-format/issues/25
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Keep a Mutiny group call on one line with the call that completes it","articleBody":"### Motivation\n\n[SmallRye Mutiny](https://smallrye.io/smallrye-mutiny/) is designed so that a chain reads as a sentence. A \"group\" call selects what you are talking about, and the next call says what to do: `onItem().transform(…)`, `onFailure(IOException.class).recoverWithItem(…)`, `subscribe().with(…)`. The formatter puts every call on its own line, which cuts each phrase in two.\n\nToday, 2.98.0.1:\n\n```java\nrequest.ifNoItem()\n .after(ofMillis(100))\n .failWith(() -> new TooSlowException(\"boom\"))\n .onFailure(IOException.class)\n .recoverWithItem(fail -> \"fallback\")\n .subscribe()\n .with(item -> log(\"ok \" + item), err -> log(err.getMessage()));\n```\n\nWanted:\n\n```java\nrequest.ifNoItem().after(ofMillis(100)).failWith(() -> new TooSlowException(\"boom\"))\n .onFailure(IOException.class).recoverWithItem(fail -> \"fallback\")\n .subscribe().with(item -> log(\"ok \" + item), err -> log(err.getMessage()));\n```\n\n### It fits the current design\n\nThe chain layout already knows a few method names. `LOG_METHODS` keeps Flogger's `logger.atInfo().log(…)` together, and `handleStream` keeps `stream()`, `parallelStream()` and `toBuilder()` with the expression before them.\n\nBreaks in a chain are emitted in two loops, in `visitRegularDot` and `visitDotWithPrefix`: one `breakOp` before every dot. Keeping two calls together means not emitting that break before the second one. Nothing in `doc/` has to change.\n\nThe formatter has no type information, so it cannot know that a receiver is a `Uni`. The rule therefore has to go by method names, and to keep it from firing on unrelated code it can be switched on only in a compilation unit that imports `io.smallrye.mutiny.*`. `visitCompilationUnit` sees the imports before anything else.\n\n### Prototype\n\nA spike on top of `7f06ca16` does exactly that: a set of group method names, a `gluedToPrevious(items, i)` check in both loops, and the import gate. It is one file, +41 −9 lines. A group call counts only when it has no arguments, so that `collect(Collectors.toList())` on a stream is left alone, with `onFailure(…)` and `after(…)` as the exceptions.\n\nResults:\n\n- the example above comes out as the \"wanted\" block;\n- `uni.onItem().transform(String::toUpperCase)` and `multi.collect().asList()` style phrases stay on one line, and a plain `stream()` chain in the same file is unchanged;\n- formatting the result again changes nothing;\n- the same code without the Mutiny import is byte-identical to 2.98.0.1;\n- 116 files of this repository, about 25,000 lines of output, format identically with the release and with the spike.\n\n### To design properly\n\n- **The list.** Which calls are groups, including the second-level ones. The spike leaves `.onFailure().retry()` on one line and `.atMost(3)` on the next, because `retry()` is a group too. `ifNull()`, `ifNotNull()` and `delayIt()` are in the same family.\n- **A phrase that does not fit.** The spike never breaks inside a phrase, so an over-long one breaks inside its arguments instead, which looks worse than today. The break should stay available but be taken only when the phrase does not fit on the line, that is, a level of its own with an independent break instead of no break at all.\n- **The gate.** A file that uses Mutiny without importing it, for example through `var`, keeps today's layout. That seems an acceptable fallback.\n- **Built-in knowledge or configuration.** The formatter has no options, and this should not become the first one. A small built-in table of import prefix and group method names would also cover other libraries with the same grammar, if any turn up.\n\n### Compatibility\n\nFiles that do not import Mutiny are unaffected. Files that do will format differently from palantir-java-format 2.98.0, so under the 2.x promise this is a 3.0 change unless decided otherwise.\n\n\u003cdetails>\n\u003csummary>The spike, as a diff\u003c/summary>\n\n```diff\ndiff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java b/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java\nindex 3917911e..6e8b1876 100644\n--- a/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java\n+++ b/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java\n@@ -345,6 +345,8 @@ public class JavaInputAstVisitor extends TreePathScanner\u003cVoid, Void> {\n \n @Override\n public Void visitCompilationUnit(CompilationUnitTree node, Void unused) {\n+ fluentGroupsEnabled = node.getImports().stream()\n+ .anyMatch(i -> i.getQualifiedIdentifier().toString().startsWith(\"io.smallrye.mutiny.\"));\n boolean first = true;\n if (node.getPackageName() != null) {\n markForPartialFormat();\n@@ -1610,6 +1612,33 @@ public class JavaInputAstVisitor extends TreePathScanner\u003cVoid, Void> {\n \"withCause\",\n \"withStackTrace\");\n \n+ /** SPIKE: Mutiny \"group\" calls read as one phrase with the call that follows them. */\n+ private boolean fluentGroupsEnabled = false;\n+\n+ private static final ImmutableSet\u003cString> FLUENT_GROUP_METHODS = ImmutableSet.of(\n+ \"onItem\", \"onFailure\", \"onItemOrFailure\", \"onSubscription\", \"onTermination\", \"onCancellation\",\n+ \"onCompletion\", \"onOverflow\", \"onRequest\", \"ifNoItem\", \"subscribe\", \"await\", \"collect\", \"select\",\n+ \"skip\", \"group\", \"broadcast\", \"memoize\", \"convert\", \"after\");\n+\n+ /** Whether items[i] must stay on the line of items[i - 1]. */\n+ private boolean gluedToPrevious(List\u003cExpressionTree> items, int i) {\n+ if (!fluentGroupsEnabled || i == 0 || !(items.get(i) instanceof MethodInvocationTree)) {\n+ return false;\n+ }\n+ ExpressionTree previous = items.get(i - 1);\n+ if (!(previous instanceof MethodInvocationTree)) {\n+ return false;\n+ }\n+ MethodInvocationTree call = (MethodInvocationTree) previous;\n+ String name = getMethodName(call).toString();\n+ if (!FLUENT_GROUP_METHODS.contains(name)) {\n+ return false;\n+ }\n+ // `collect(toList())` on a stream is an action; Mutiny's groups take no arguments,\n+ // except onFailure(Class) and ifNoItem().after(Duration).\n+ return call.getArguments().isEmpty() || name.equals(\"onFailure\") || name.equals(\"after\");\n+ }\n+\n private static List\u003cLong> handleStream(List\u003cExpressionTree> parts) {\n return indexes(parts.stream(), p -> {\n if (!(p instanceof MethodInvocationTree)) {\n@@ -2857,9 +2886,10 @@ public class JavaInputAstVisitor extends TreePathScanner\u003cVoid, Void> {\n // chain starts with another expression\n int minLength = indentMultiplier * 4;\n int length = needDot0 ? minLength : 0;\n- for (ExpressionTree e : items) {\n+ for (int itemIndex = 0; itemIndex \u003c items.size(); itemIndex++) {\n+ ExpressionTree e = items.get(itemIndex);\n if (needDot) {\n- if (length > minLength) {\n+ if (length > minLength && !gluedToPrevious(items, itemIndex)) {\n builder.breakOp(Break.builder()\n .fillMode(FillMode.UNIFIED)\n .flat(\"\")\n@@ -2966,13 +2996,15 @@ public class JavaInputAstVisitor extends TreePathScanner\u003cVoid, Void> {\n fillMode = FillMode.UNIFIED;\n }\n \n- builder.breakOp(Break.builder()\n- .fillMode(fillMode)\n- .flat(\"\")\n- .plusIndent(ZERO)\n- .optTag(Optional.of(nameTag))\n- .hasColumnLimit(shouldHaveColumnLimit(e))\n- .build());\n+ if (!gluedToPrevious(items, i)) {\n+ builder.breakOp(Break.builder()\n+ .fillMode(fillMode)\n+ .flat(\"\")\n+ .plusIndent(ZERO)\n+ .optTag(Optional.of(nameTag))\n+ .hasColumnLimit(shouldHaveColumnLimit(e))\n+ .build());\n+ }\n token(\".\");\n }\n BreakTag tyargTag = new BreakTag();\n```\n\n\u003c/details>\n","author":{"url":"https://github.com/abashev","@type":"Person","name":"abashev"},"datePublished":"2026-09-21T14:09:35Z","url":"https://github.com/openjavaformat/open-java-format/issues/25"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:859b86fb-246d-3b04-e696-4c083306f6b9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 826A:D1712:5E9588:7DA1C1:6AFBC735 |
| html-safe-nonce | 89173416e07c53e068c4a57047f2d94376be3e380354bcefeca4d09ccc16d42b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MjZBOkQxNzEyOjVFOTU4ODo3REExQzE6NkFGQkM3MzUiLCJ2aXNpdG9yX2lkIjoiNDIxODUyMjg0MTM0MDY5MjI3NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | eef5d754d3626688599eb833bdab4d80fadb819eb304a8294c33e99c145661e5 |
| hovercard-subject-tag | issue:5528110041 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/openjavaformat/open-java-format/25/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7b6bb31daba8a562554e19a51e0cb71a25bea36e54774a735deca89c8f2be295/openjavaformat/open-java-format/issues/25 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7b6bb31daba8a562554e19a51e0cb71a25bea36e54774a735deca89c8f2be295/openjavaformat/open-java-format/issues/25 |
| og:image:alt | Motivation SmallRye Mutiny is designed so that a chain reads as a sentence. A "group" call selects what you are talking about, and the next call says what to do: onItem().transform(…), onFailure(IO... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | abashev |
| hostname | github.com |
| expected-hostname | github.com |
| None | d440bda0d812801778b27bfe9f7c1ce9ff22420c73f6f95150c4fcbca311c50b |
| turbo-cache-control | no-preview |
| go-import | github.com/openjavaformat/open-java-format git https://github.com/openjavaformat/open-java-format.git |
| octolytics-dimension-user_id | 325177626 |
| octolytics-dimension-user_login | openjavaformat |
| octolytics-dimension-repository_id | 1288055459 |
| octolytics-dimension-repository_nwo | openjavaformat/open-java-format |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | true |
| octolytics-dimension-repository_parent_id | 214273098 |
| octolytics-dimension-repository_parent_nwo | palantir/palantir-java-format |
| octolytics-dimension-repository_network_root_id | 214273098 |
| octolytics-dimension-repository_network_root_nwo | palantir/palantir-java-format |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2c43021aed8695990c86c0eaaedc600bfeffec36 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width