-
Notifications
You must be signed in to change notification settings - Fork 552
Improved support for indentation formatting using records and single arg method declarations #6158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
e6b5d1f
238dae9
f729120
bf7f540
39695f2
8a43e5f
f9b94d4
c8b77a9
ef65db4
ee27e25
f470ef5
6c9e883
f07a178
7ea5489
25fcb20
ffbbd06
35de878
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| import java.util.Iterator; | ||
| import java.util.List; | ||
|
|
||
| import static java.util.Collections.emptyList; | ||
| import static org.openrewrite.java.format.ColumnPositionCalculator.computeColumnPosition; | ||
|
|
||
| public class TabsAndIndentsVisitor<P> extends JavaIsoVisitor<P> { | ||
|
|
@@ -201,6 +202,7 @@ public Space visitSpace(Space space, Space.Location loc, P p) { | |
| Space after; | ||
|
|
||
| int indent = getCursor().getNearestMessage("lastIndent", 0); | ||
| IndentType indentType = getCursor().getParent().getNearestMessage("indentType", IndentType.ALIGN); | ||
| if (right.getElement() instanceof J) { | ||
| J elem = (J) right.getElement(); | ||
| if ((right.getAfter().getLastWhitespace().contains("\n") || | ||
|
|
@@ -232,27 +234,52 @@ public Space visitSpace(Space space, Space.Location loc, P p) { | |
| JContainer<J> container = getCursor().getParentOrThrow().getValue(); | ||
| List<J> elements = container.getElements(); | ||
| J lastArg = elements.get(elements.size() - 1); | ||
| if (elements.size() > 1 && style.getMethodDeclarationParameters().getAlignWhenMultiple()) { | ||
| J.MethodDeclaration method = getCursor().firstEnclosing(J.MethodDeclaration.class); | ||
| if (method != null) { | ||
| int alignTo = computeFirstParameterColumn(method); | ||
| if (alignTo != -1) { | ||
| getCursor().getParentOrThrow().putMessage("lastIndent", alignTo - style.getContinuationIndent()); | ||
| elem = visitAndCast(elem, p); | ||
| getCursor().getParentOrThrow().putMessage("lastIndent", indent); | ||
| after = indentTo(right.getAfter(), t == lastArg ? indent : alignTo, loc.getAfterLocation()); | ||
| //noinspection ConstantConditions | ||
| J tree = null; | ||
| if (loc == JRightPadded.Location.METHOD_DECLARATION_PARAMETER) { | ||
| tree = getCursor().firstEnclosing(J.MethodDeclaration.class); | ||
| } else if (loc == JRightPadded.Location.RECORD_STATE_VECTOR) { | ||
|
Jenson3210 marked this conversation as resolved.
Outdated
|
||
| tree = getCursor().firstEnclosing(J.ClassDeclaration.class); | ||
| getCursor().getParentOrThrow().putMessage("indentType", IndentType.CONTINUATION_INDENT); | ||
| } | ||
| if (elements.size() > 1) { | ||
| try { | ||
| if ((loc == JRightPadded.Location.METHOD_DECLARATION_PARAMETER && style.getMethodDeclarationParameters().getAlignWhenMultiple()) || | ||
| ((loc == JRightPadded.Location.RECORD_STATE_VECTOR && style.getRecordComponents() != null && style.getRecordComponents().getAlignWhenMultiple()))) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as with the try/catch, we had NPE's, No SuchMethodErrors last time when introducing new style classes. So added like this to be safe. We should then later be able to remove the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it makes more sense for these temporary backwards compatibility null checks to exist within the relevant style class. You can always provide an implementation for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense in this occasion as we will introduce both the style and the default getter in a single go. It will hence be either a NoSuchMethod or a defaulted getter. Last time we also had the possibilitiy of an old (non-defaulted) getter being parent loaded. |
||
| if (tree != null) { | ||
| int alignTo = computeFirstParameterColumn(tree); | ||
| if (alignTo != -1) { | ||
| getCursor().getParentOrThrow().putMessage("lastIndent", alignTo - style.getContinuationIndent()); | ||
| elem = visitAndCast(elem, p); | ||
| getCursor().getParentOrThrow().putMessage("lastIndent", indent); | ||
| after = indentTo(right.getAfter(), t == lastArg ? indent : alignTo, loc.getAfterLocation()); | ||
| } else { | ||
| after = right.getAfter(); | ||
| } | ||
| } else { | ||
| after = right.getAfter(); | ||
| } | ||
| } else { | ||
| after = right.getAfter(); | ||
| elem = visitAndCast(elem, p); | ||
| after = indentTo(right.getAfter(), t == lastArg ? indent : style.getContinuationIndent(), loc.getAfterLocation()); | ||
| } | ||
| } else { | ||
| after = right.getAfter(); | ||
| } catch (NoSuchMethodError error) { | ||
| // style.getRecordComponents introduction might give NoSuchMethodError depending on the runtime, the lst build date... | ||
| elem = visitAndCast(elem, p); | ||
| after = indentTo(right.getAfter(), t == lastArg ? indent : style.getContinuationIndent(), loc.getAfterLocation()); | ||
| } | ||
| } else if (elements.size() > 1) { | ||
| elem = visitAndCast(elem, p); | ||
| after = indentTo(right.getAfter(), t == lastArg ? indent : style.getContinuationIndent(), loc.getAfterLocation()); | ||
| } else { | ||
| if (elem.getPrefix().getLastWhitespace().contains("\n") && tree != null) { | ||
| int alignTo = computeFirstParameterColumn(tree); | ||
| if (alignTo != -1) { | ||
| getCursor().getParentTreeCursor().putMessage("lastIndent", alignTo - style.getContinuationIndent()); | ||
| elem = visitAndCast(elem, p); | ||
| getCursor().getParentTreeCursor().putMessage("lastIndent", indent); | ||
| } | ||
| } | ||
| after = right.getAfter(); | ||
| } | ||
| getCursor().getParentOrThrow().putMessage("indentType", indentType); | ||
| break; | ||
| } | ||
| case METHOD_INVOCATION_ARGUMENT: | ||
|
|
@@ -339,6 +366,8 @@ public Space visitSpace(Space space, Space.Location loc, P p) { | |
| switch (loc) { | ||
| case NEW_CLASS_ARGUMENTS: | ||
| case METHOD_INVOCATION_ARGUMENT: | ||
| case RECORD_STATE_VECTOR: | ||
| case METHOD_DECLARATION_PARAMETER: | ||
| if (!elem.getPrefix().getLastWhitespace().contains("\n")) { | ||
| JContainer<J> args = getCursor().getParentOrThrow().getValue(); | ||
| boolean anyOtherArgOnOwnLine = false; | ||
|
|
@@ -389,23 +418,27 @@ public Space visitSpace(Space space, Space.Location loc, P p) { | |
| return (after == right.getAfter() && t == right.getElement()) ? right : new JRightPadded<>(t, after, right.getMarkers()); | ||
| } | ||
|
|
||
| private int computeFirstParameterColumn(J.MethodDeclaration method) { | ||
| List<JRightPadded<Statement>> arguments = method.getPadding().getParameters().getPadding().getElements(); | ||
| private int computeFirstParameterColumn(J tree) { | ||
| List<JRightPadded<Statement>> arguments; | ||
| if (tree instanceof J.MethodDeclaration) { | ||
| arguments = ((J.MethodDeclaration) tree).getPadding().getParameters().getPadding().getElements(); | ||
| } else if (tree instanceof J.ClassDeclaration) { | ||
| JContainer<Statement> primaryConstructorArgs = ((J.ClassDeclaration) tree).getPadding().getPrimaryConstructor(); | ||
| arguments = primaryConstructorArgs == null ? emptyList() : primaryConstructorArgs.getPadding().getElements(); | ||
| } else { | ||
| throw new IllegalStateException("Unexpected tree type: " + tree.getClass().getSimpleName()); | ||
|
Jenson3210 marked this conversation as resolved.
Outdated
|
||
| } | ||
| J firstArg = arguments.isEmpty() ? null : arguments.get(0).getElement(); | ||
| if (firstArg == null || firstArg instanceof J.Empty) { | ||
| return -1; | ||
| } | ||
|
|
||
| if (firstArg.getPrefix().getLastWhitespace().contains("\n")) { | ||
| int declPrefixLength = getLengthOfWhitespace(method.getPrefix().getLastWhitespace()); | ||
| int argPrefixLength = getLengthOfWhitespace(firstArg.getPrefix().getLastWhitespace()); | ||
| //noinspection ConstantConditions to be backwards compatible with older style versions | ||
| if (declPrefixLength >= argPrefixLength) { | ||
| return declPrefixLength + style.getContinuationIndent(); | ||
| } | ||
| return argPrefixLength; | ||
| int declPrefixLength = getLengthOfWhitespace(tree.getPrefix().getLastWhitespace()); | ||
|
|
||
| return declPrefixLength + style.getContinuationIndent(); | ||
| } else { | ||
| return computeColumnPosition(method, firstArg, getCursor()); | ||
| return computeColumnPosition(tree, firstArg, getCursor()); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.