Skip to content

Conversation

Jenson3210
Copy link
Contributor

Also support ChopIfTooLong for the recently added WrapMethodChains formatter.

@Jenson3210 Jenson3210 requested a review from sambsnyd October 8, 2025 18:41
@Jenson3210 Jenson3210 self-assigned this Oct 8, 2025
@Jenson3210 Jenson3210 added the enhancement New feature or request label Oct 8, 2025
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Oct 8, 2025
@timtebeek timtebeek marked this pull request as draft October 10, 2025 15:40
# Conflicts:
#	rewrite-java/src/main/java/org/openrewrite/java/format/WrapMethodChains.java
Jenson3210 and others added 4 commits October 16, 2025 13:14
…hodChains.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@openrewrite openrewrite deleted a comment from github-actions bot Oct 16, 2025
@openrewrite openrewrite deleted a comment from github-actions bot Oct 16, 2025
@Jenson3210 Jenson3210 marked this pull request as ready for review October 16, 2025 18:03
@Jenson3210 Jenson3210 requested a review from timtebeek October 16, 2025 18:04
Comment on lines +27 to +41
class LengthCalculator {

public static int computeTreeLineLength(J tree, Cursor cursor) {
Object cursorValue = cursor.getValue();
if (cursorValue instanceof J) {
J j = (J) cursorValue;
boolean hasNewLine = j.getPrefix().getWhitespace().contains("\n") || j.getComments().stream().anyMatch(c -> c.getSuffix().contains("\n"));
Cursor parent = cursor.getParentTreeCursor();
boolean isCompilationUnit = parent.getValue() instanceof J.CompilationUnit;
if (!hasNewLine && !isCompilationUnit) {
return computeTreeLineLength(parent.getValue(), parent);
}
} else {
throw new RuntimeException("Unable to calculate length due to unexpected cursor value: " + cursorValue.getClass());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a service mechanism for supplying language-specific implementations of concepts that appear in multiple languages. This strikes me as a good candidate for that pattern. See JavaSourceFile.service().
This is how a JavaVisitor can visit a kotlin source file and have maybeAddImport() use the kotlin version of AddImport to do it.

I see this as the default SourcePositionService. While you don't need to do it right now, it could be a reasonable place to put a getLineNumber() or getStartColumn()/getEndColumn() methods which are sometimes requested. Methods like these could be relevant in any language or data format.

There could be Groovy/Kotlin variations which, for example, return false more often in needsSemicolon as those languages only require them to separate multiple statements on the same line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took this approach from ColumnPositionCalculator already present. Is that then also a good candidate?

This method though does not getLineNumber but gets the length of the current element (including its newlines) starting from the first element that starts on it's line.

Counting the prefix (indentation only) of that element + the length. I struggled to find an explanatory name for that. Any suggestions?

Comment on lines 102 to 105
if (method.getName().getComments().isEmpty() && method.getName().getPrefix().getWhitespace().contains("\n")) {
return method.withName(method.getName().withPrefix(Space.EMPTY));
}
return super.visitMethodInvocation(method, p);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A method invocation meeting this condition could be contained within another method invocation which meets this condition. In that circumstance the inner method would not be reformatted as no traversal into sub-elements happens when this condition is matched.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

2 participants