|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.java.format; |
| 17 | + |
| 18 | +import lombok.EqualsAndHashCode; |
| 19 | +import lombok.Value; |
| 20 | +import org.jspecify.annotations.Nullable; |
| 21 | +import org.openrewrite.Cursor; |
| 22 | +import org.openrewrite.internal.ListUtils; |
| 23 | +import org.openrewrite.java.JavaIsoVisitor; |
| 24 | +import org.openrewrite.java.service.SourcePositionService; |
| 25 | +import org.openrewrite.java.style.SpacesStyle; |
| 26 | +import org.openrewrite.java.style.WrappingAndBracesStyle; |
| 27 | +import org.openrewrite.java.tree.J; |
| 28 | +import org.openrewrite.java.tree.JRightPadded; |
| 29 | +import org.openrewrite.java.tree.JavaSourceFile; |
| 30 | +import org.openrewrite.java.tree.Space; |
| 31 | +import org.openrewrite.style.LineWrapSetting; |
| 32 | + |
| 33 | +import java.util.Objects; |
| 34 | + |
| 35 | +@Value |
| 36 | +@EqualsAndHashCode(callSuper = false) |
| 37 | +public class WrapMethodDeclarationParameters<P> extends JavaIsoVisitor<P> { |
| 38 | + |
| 39 | + SpacesStyle spacesStyle; |
| 40 | + WrappingAndBracesStyle style; |
| 41 | + |
| 42 | + @Override |
| 43 | + public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, P ctx) { |
| 44 | + J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx); |
| 45 | + |
| 46 | + try { |
| 47 | + // styles are parent loaded, so the getters may or may not be present and they may or may not return null |
| 48 | + if (style != null && style.getMethodDeclarationParameters() != null && style.getMethodDeclarationParameters().getWrap() != LineWrapSetting.DoNotWrap) { |
| 49 | + if (m.getParameters().size() <= 1) { |
| 50 | + return m; |
| 51 | + } |
| 52 | + JavaSourceFile sourceFile = getCursor().firstEnclosing(JavaSourceFile.class); |
| 53 | + if (style.getMethodDeclarationParameters().getWrap() == LineWrapSetting.ChopIfTooLong) { |
| 54 | + if (sourceFile == null) { |
| 55 | + return m; |
| 56 | + } |
| 57 | + Cursor minimized = minimize(getCursor(), ctx); |
| 58 | + if (sourceFile.service(SourcePositionService.class).positionOf(minimized, ((J.MethodDeclaration)minimized.getValue()).getPadding().getParameters()).getMaxColumn() <= style.getHardWrapAt()) { |
| 59 | + return m; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if (style.getMethodDeclarationParameters().getCloseNewLine()) { |
| 64 | + m = m.getPadding().withParameters(m.getPadding().getParameters().getPadding().withElements(ListUtils.mapLast(m.getPadding().getParameters().getPadding().getElements(), rightPaddedParam -> { |
| 65 | + Space after = rightPaddedParam.getAfter(); |
| 66 | + if (after.getWhitespace().contains("\n")) { |
| 67 | + return rightPaddedParam; |
| 68 | + } |
| 69 | + if (after.getComments().isEmpty()) { |
| 70 | + after = after.withWhitespace("\n"); |
| 71 | + } else { |
| 72 | + after = after.withComments(ListUtils.mapLast(after.getComments(), comment -> comment.withSuffix("\n"))); |
| 73 | + } |
| 74 | + return rightPaddedParam.withAfter(after); |
| 75 | + }))); |
| 76 | + } |
| 77 | + |
| 78 | + m = m.withParameters(ListUtils.map(m.getParameters(), (paramIndex, param) -> { |
| 79 | + if (param instanceof J.Empty) { |
| 80 | + return param; |
| 81 | + } |
| 82 | + Space prefix = param.getPrefix(); |
| 83 | + if (prefix.getComments().isEmpty()) { |
| 84 | + if ((paramIndex != 0 || style.getMethodDeclarationParameters().getOpenNewLine()) && !prefix.getWhitespace().contains("\n")) { |
| 85 | + prefix = prefix.withWhitespace("\n"); |
| 86 | + } |
| 87 | + } else { |
| 88 | + int index = -1; |
| 89 | + for (int i = prefix.getComments().size() - 1; i >= 0; i--) { |
| 90 | + if (prefix.getComments().get(i).getSuffix().contains("\n")) { |
| 91 | + index = i; |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + if (index == -1 && (paramIndex != 0 || style.getMethodDeclarationParameters().getOpenNewLine())) { |
| 96 | + if (!prefix.getWhitespace().contains("\n")) { |
| 97 | + prefix = prefix.withWhitespace("\n"); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + return param.withPrefix(prefix); |
| 102 | + })); |
| 103 | + } |
| 104 | + } catch (NoSuchMethodError | NoSuchFieldError ignore) { |
| 105 | + // Styles are parent-first loaded and this can happen if the style is from a older version of the runtime. Can be removed in future releases. |
| 106 | + } |
| 107 | + |
| 108 | + return m; |
| 109 | + } |
| 110 | + |
| 111 | + public Cursor minimize(Cursor cursor, P ctx) { |
| 112 | + if (cursor.getValue() instanceof J) { |
| 113 | + J cursorValue = cursor.getValue(); |
| 114 | + J j = new JavaIsoVisitor<P>() { |
| 115 | + @Override |
| 116 | + public @Nullable <T> JRightPadded<T> visitRightPadded(@Nullable JRightPadded<T> right, JRightPadded.Location loc, P ctx) { |
| 117 | + switch (loc) { |
| 118 | + case METHOD_DECLARATION_PARAMETER: |
| 119 | + case RECORD_STATE_VECTOR: { |
| 120 | + if (right != null && right.getElement() instanceof J) { |
| 121 | + //noinspection unchecked |
| 122 | + right = right |
| 123 | + .withAfter(minimized(right.getAfter())) |
| 124 | + .withElement(((J) right.getElement()).withPrefix(minimized(((J) right.getElement()).getPrefix()))); |
| 125 | + } |
| 126 | + break; |
| 127 | + } |
| 128 | + } |
| 129 | + return super.visitRightPadded(right, loc, ctx); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public Space visitSpace(@Nullable Space space, Space.Location loc, P ctx) { |
| 134 | + if (space == null) { |
| 135 | + return super.visitSpace(space, loc, ctx); |
| 136 | + } |
| 137 | + if (space == cursorValue.getPrefix()) { |
| 138 | + return space; |
| 139 | + } |
| 140 | + switch (loc) { |
| 141 | + case BLOCK_PREFIX: |
| 142 | + case MODIFIER_PREFIX: |
| 143 | + case METHOD_DECLARATION_PARAMETER_SUFFIX: |
| 144 | + case METHOD_DECLARATION_PARAMETERS: |
| 145 | + case METHOD_SELECT_SUFFIX: |
| 146 | + case METHOD_INVOCATION_ARGUMENTS: |
| 147 | + case METHOD_INVOCATION_ARGUMENT_SUFFIX: |
| 148 | + case METHOD_INVOCATION_NAME: |
| 149 | + case RECORD_STATE_VECTOR_SUFFIX: { |
| 150 | + space = minimized(space); |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + return super.visitSpace(space, loc, ctx); |
| 155 | + } |
| 156 | + |
| 157 | + //IntelliJ does not format when comments are present. |
| 158 | + private Space minimized(Space space) { |
| 159 | + if (space.getComments().isEmpty()) { |
| 160 | + return space.getWhitespace().isEmpty() ? space : Space.EMPTY; |
| 161 | + } |
| 162 | + return space; |
| 163 | + } |
| 164 | + }.visit(cursorValue, ctx); |
| 165 | + if (j != cursor.getValue()) { |
| 166 | + j = new MinimumViableSpacingVisitor<>(null).visit(j, ctx); |
| 167 | + j = new SpacesVisitor<>(spacesStyle, null, null).visit(j, ctx); |
| 168 | + } |
| 169 | + return new Cursor(cursor.getParent(), Objects.requireNonNull(j)); |
| 170 | + } |
| 171 | + throw new IllegalArgumentException("Can only minimize J elements."); |
| 172 | + } |
| 173 | +} |
0 commit comments