Skip to content

Commit

Permalink
refactor: Operator wrapping on end of line
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek and TeamModerne committed Sep 21, 2024
1 parent cc8b9fb commit 16d0ec3
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/openrewrite/kotlin/AddImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String

if (alias == null) {
// No need to add imports if the class to import is implicitly imported, or if the classes are within the same package
if ((IMPLICITLY_IMPORTED_PACKAGES.contains(packageName) && StringUtils.isBlank(member))
|| (cu.getPackageDeclaration() != null && packageName.equals(cu.getPackageDeclaration().getExpression().printTrimmed(getCursor())))) {
if ((IMPLICITLY_IMPORTED_PACKAGES.contains(packageName) && StringUtils.isBlank(member)) ||
(cu.getPackageDeclaration() != null && packageName.equals(cu.getPackageDeclaration().getExpression().printTrimmed(getCursor())))) {
return cu;
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/org/openrewrite/kotlin/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ public static UncheckedConsumer<SourceSpec<?>> spaceConscious() {
}

private static void assertValidTypes(TypeValidation typeValidation, J sf) {
if (typeValidation.identifiers() || typeValidation.methodInvocations() || typeValidation.methodDeclarations() || typeValidation.classDeclarations()
|| typeValidation.constructorInvocations()) {
if (typeValidation.identifiers() || typeValidation.methodInvocations() || typeValidation.methodDeclarations() || typeValidation.classDeclarations() ||
typeValidation.constructorInvocations()) {
List<FindMissingTypes.MissingTypeResult> missingTypeResults = findMissingTypes(sf);
missingTypeResults = missingTypeResults.stream()
.filter(missingType -> {
Expand Down Expand Up @@ -463,10 +463,10 @@ public J.ParameterizedType visitParameterizedType(J.ParameterizedType type, Exec
}

private boolean isAllowedToHaveNullType(J.Identifier ident) {
return inPackageDeclaration() || inImport() || isClassName()
|| isMethodName() || isMethodInvocationName() || isFieldAccess(ident) || isBeingDeclared(ident) || isParameterizedType(ident)
|| isNewClass(ident) || isTypeParameter() || isMemberReference(ident) || isCaseLabel() || isLabel() || isAnnotationField(ident)
|| isInJavaDoc(ident) || isWhenLabel() || isUseSite();
return inPackageDeclaration() || inImport() || isClassName() ||
isMethodName() || isMethodInvocationName() || isFieldAccess(ident) || isBeingDeclared(ident) || isParameterizedType(ident) ||
isNewClass(ident) || isTypeParameter() || isMemberReference(ident) || isCaseLabel() || isLabel() || isAnnotationField(ident) ||
isInJavaDoc(ident) || isWhenLabel() || isUseSite();
}

private boolean inPackageDeclaration() {
Expand Down Expand Up @@ -494,8 +494,8 @@ private boolean isMethodInvocationName() {

private boolean isFieldAccess(J.Identifier ident) {
Tree value = getCursor().getParentTreeCursor().getValue();
return value instanceof J.FieldAccess
&& (ident == ((J.FieldAccess) value).getName() ||
return value instanceof J.FieldAccess &&
(ident == ((J.FieldAccess) value).getName() ||
ident == ((J.FieldAccess) value).getTarget() && !((J.FieldAccess) value).getSimpleName().equals("class"));
}

Expand All @@ -515,8 +515,8 @@ private boolean isNewClass(J.Identifier ident) {
}

private boolean isTypeParameter() {
return getCursor().getParent() != null
&& getCursor().getParent().getValue() instanceof J.TypeParameter;
return getCursor().getParent() != null &&
getCursor().getParent().getValue() instanceof J.TypeParameter;
}

private boolean isMemberReference(J.Identifier ident) {
Expand Down Expand Up @@ -551,8 +551,8 @@ private boolean isLabel() {

private boolean isAnnotationField(J.Identifier ident) {
Cursor parent = getCursor().getParent();
return parent != null && parent.getValue() instanceof J.Assignment
&& (ident == ((J.Assignment) parent.getValue()).getVariable() && getCursor().firstEnclosing(J.Annotation.class) != null);
return parent != null && parent.getValue() instanceof J.Assignment &&
(ident == ((J.Assignment) parent.getValue()).getVariable() && getCursor().firstEnclosing(J.Annotation.class) != null);
}

private boolean isValidated(J.Identifier i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public String getDisplayName() {

@Override
public String getDescription() {
return "Replace the usage of the deprecated `Char#toInt()` with `Char#code`. "
+ "Please ensure that your Kotlin version is 1.5 or later to support the `Char#code` property. "
+ "Note that the current implementation does not perform a Kotlin version check.";
return "Replace the usage of the deprecated `Char#toInt()` with `Char#code`. " +
"Please ensure that your Kotlin version is 1.5 or later to support the `Char#code` property. " +
"Note that the current implementation does not perform a Kotlin version check.";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@ private List<JRightPadded<Statement>> visitStatementList(List<JRightPadded<State
return ListUtils.map(statements,
(i, st) -> {
Statement element = st.getElement();
if (i == 0
|| element.getPrefix().getWhitespace().contains("\n")
|| element.getPrefix().getLastWhitespace().contains("\n")
|| statements.get(i - 1).getMarkers().findFirst(Semicolon.class).isPresent()) {
if (i == 0 ||
element.getPrefix().getWhitespace().contains("\n") ||
element.getPrefix().getLastWhitespace().contains("\n") ||
statements.get(i - 1).getMarkers().findFirst(Semicolon.class).isPresent()) {
return st;
}
return st.withElement(element.withPrefix(addNewline(element.getPrefix())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -964,8 +964,8 @@ public J.Lambda visitLambda(J.Lambda lambda, P p) {
JRightPadded<J> lastParam = parameters.get(parameters.size() - 1);
Space after = lastParam.getAfter();
trailingComma = lastParam.getMarkers().findFirst(TrailingComma.class).isPresent();
lastParamHasSpace = after.getComments().isEmpty() && onlySpacesAndNotEmpty(after.getWhitespace())
|| lastParam.getMarkers().findFirst(TrailingComma.class).map(t -> onlySpacesAndNotEmpty(t.getSuffix().getWhitespace())).orElse(false);
lastParamHasSpace = after.getComments().isEmpty() && onlySpacesAndNotEmpty(after.getWhitespace()) ||
lastParam.getMarkers().findFirst(TrailingComma.class).map(t -> onlySpacesAndNotEmpty(t.getSuffix().getWhitespace())).orElse(false);
useSpaceBeforeLambdaArrow &= !trailingComma;
} else {
l = l.withArrow(Space.EMPTY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public TabsAndIndentsVisitor(TabsAndIndentsStyle style, WrappingAndBracesStyle w
tree instanceof J.Case ||
tree instanceof J.EnumValueSet ||
(tree instanceof J.Ternary && !wrappingStyle.getElvisExpressions().getUseContinuationIndent()) ||
(tree instanceof J.FieldAccess || tree instanceof J.MethodInvocation)
&& !wrappingStyle.getChainedFunctionCalls().getUseContinuationIndent() ||
(tree instanceof J.FieldAccess || tree instanceof J.MethodInvocation) &&
!wrappingStyle.getChainedFunctionCalls().getUseContinuationIndent() ||
tree instanceof J.Annotation
) {
getCursor().putMessage("indentType", IndentType.INDENT);
Expand Down Expand Up @@ -163,8 +163,8 @@ public Space visitSpace(Space space, Space.Location loc, P p) {
!(getCursor().getParentOrThrow().getValue() instanceof J.Annotation);

if ((loc == Space.Location.CLASS_KIND ||
loc == Space.Location.METHOD_DECLARATION_PREFIX)
&& value instanceof J.ClassDeclaration) {
loc == Space.Location.METHOD_DECLARATION_PREFIX) &&
value instanceof J.ClassDeclaration) {
J.ClassDeclaration c = (J.ClassDeclaration) value;
if (!c.getLeadingAnnotations().isEmpty()) {
alignToAnnotation = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ public String print() {
}

// print current visiting element
String typeName = tree instanceof J
? tree.getClass().getCanonicalName().substring(tree.getClass().getPackage().getName().length() + 1)
: tree.getClass().getCanonicalName();
String typeName = tree instanceof J ?
tree.getClass().getCanonicalName().substring(tree.getClass().getPackage().getName().length() + 1) :
tree.getClass().getCanonicalName();

if (skipUnvisitedElement) {
boolean leftPadded = diffPos >= 0;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/openrewrite/kotlin/style/Autodetect.java
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,8 @@ public Expression visitExpression(Expression expression, IndentStatistics stats)
// (newline-separated) annotations on some common target are not continuations
boolean isContinuation = !(expression instanceof J.Annotation && !(
// ...but annotations which are *arguments* to other annotations can be continuations
getCursor().getParentTreeCursor().getValue() instanceof J.Annotation
|| getCursor().getParentTreeCursor().getValue() instanceof J.NewArray
getCursor().getParentTreeCursor().getValue() instanceof J.Annotation ||
getCursor().getParentTreeCursor().getValue() instanceof J.NewArray
));
countIndents(expression.getPrefix().getWhitespace(), isContinuation, stats);

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/openrewrite/kotlin/tree/K.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ public Space getPrefix() {

@Override
public <J2 extends J> J2 withPrefix(Space space) {
return (J2) (annotations.isEmpty() ? withExpression(expression.withPrefix(space))
: withAnnotations(ListUtils.mapFirst(annotations, a -> a.withPrefix(space))));
return (J2) (annotations.isEmpty() ? withExpression(expression.withPrefix(space)) :
withAnnotations(ListUtils.mapFirst(annotations, a -> a.withPrefix(space))));
}

@Override
Expand Down Expand Up @@ -1262,8 +1262,8 @@ public static class Padding {
}

public FunctionType withParameters(@Nullable JContainer<TypeTree> parameters) {
return t.parameters == parameters ? t
: new FunctionType(t.id, t.prefix, t.markers, t.leadingAnnotations, t.modifiers, t.receiver, parameters, t.arrow, t.returnType);
return t.parameters == parameters ? t :
new FunctionType(t.id, t.prefix, t.markers, t.leadingAnnotations, t.modifiers, t.receiver, parameters, t.arrow, t.returnType);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/openrewrite/kotlin/tree/WhenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ fun method() {
spec -> spec.afterRecipe(cu -> {
K.When when = (K.When)((J.MethodDeclaration)(cu.getStatements().get(0))).getBody().getStatements().get(1);
boolean allBranchesHasLiteralBody = when.getBranches().getStatements().stream().map(K.WhenBranch.class::cast).allMatch(
branch -> branch.getBody() instanceof J.Literal
|| branch.getBody() instanceof J.Parentheses<?> && ((J.Parentheses<?>) branch.getBody()).getTree() instanceof J.Literal
branch -> branch.getBody() instanceof J.Literal ||
branch.getBody() instanceof J.Parentheses<?> && ((J.Parentheses<?>) branch.getBody()).getTree() instanceof J.Literal
);
assertThat(allBranchesHasLiteralBody).isTrue();
})
Expand Down

0 comments on commit 16d0ec3

Please sign in to comment.