Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2385,8 +2385,8 @@ void recordComponents() {
java(
"""
public record RenameRequest(
@NotBlank
@JsonProperty("name") String name) {
@NotBlank
@JsonProperty("name") String name) {
}
"""
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,30 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, P
)
);
}

if (c.getPrimaryConstructor() != null) {
c = c.withPrimaryConstructor(ListUtils.map(c.getPrimaryConstructor(), (ix, param) -> {
if (param.getPrefix().getLastWhitespace().contains("\n")) {
return param;
}
if (ix == 0 && param.getComments().isEmpty()) {
if (!style.getWithin().getRecordHeader()) {
return param.withPrefix(param.getPrefix().withWhitespace(""));
}
}
return param.withPrefix(param.getPrefix().withWhitespace(" "));
}));
JContainer.Padding<Statement> padding = c.getPadding().getPrimaryConstructor().getPadding();
c = c.getPadding().withPrimaryConstructor(
padding.withElements(ListUtils.mapLast(padding.getElements(), last -> {
if (!last.getAfter().getLastWhitespace().contains("\n")) {
return last.withAfter(last.getAfter().withWhitespace(!style.getWithin().getRecordHeader() ? "" : " "));
}
return last;
}
)));
}

return c;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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") ||
Expand Down Expand Up @@ -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 {
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().getAlignWhenMultiple())) {
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:
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
return -1;
}
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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ public TabsAndIndentsStyle getTabsAndIndentsStyle() {
continuationIndent,
false,
new TabsAndIndentsStyle.MethodDeclarationParameters(
multilineAlignedToFirstArgument >= multilineNotAlignedToFirstArgument)
multilineAlignedToFirstArgument >= multilineNotAlignedToFirstArgument),
new TabsAndIndentsStyle.RecordComponents(multilineAlignedToFirstArgument >= multilineNotAlignedToFirstArgument)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static ImportLayoutStyle importLayout() {

public static TabsAndIndentsStyle tabsAndIndents() {
return new TabsAndIndentsStyle(false, 4, 4, 8, false,
new TabsAndIndentsStyle.MethodDeclarationParameters(true));
new TabsAndIndentsStyle.MethodDeclarationParameters(true), new TabsAndIndentsStyle.RecordComponents(true));
}

public static BlankLinesStyle blankLines() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ public static class Within {
Boolean typeCastParentheses;
Boolean annotationParentheses;
Boolean angleBrackets;

// Records are a java 14 preview feature (and still a preview language feature in java 15)
// rewrite does not consult this style setting for now
Boolean recordHeader;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,27 @@ public class TabsAndIndentsStyle implements JavaStyle {
Boolean indentsRelativeToExpressionStart;

MethodDeclarationParameters methodDeclarationParameters;
RecordComponents recordComponents;

@Value
@With
public static class MethodDeclarationParameters {
Boolean alignWhenMultiple;
}

@Value
@With
public static class RecordComponents {
Boolean alignWhenMultiple;
}

@Override
public Style applyDefaults() {
return StyleHelper.merge(IntelliJ.tabsAndIndents(), this);
}

public RecordComponents getRecordComponents() {
//noinspection ConstantConditions
return recordComponents == null ? new RecordComponents(true) : recordComponents;
}
}
Loading