diff --git a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java index 5dddde28d..8bde65a88 100644 --- a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java +++ b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java @@ -790,6 +790,7 @@ public J visitParameter(KtParameter parameter, ExecutionContext data) { List modifiers = new ArrayList<>(); TypeTree typeExpression = null; List> vars = new ArrayList<>(1); + Set consumedSpaces = preConsumedInfix(parameter); if (!parameter.getAnnotations().isEmpty()) { throw new UnsupportedOperationException("TODO"); @@ -801,7 +802,7 @@ public J visitParameter(KtParameter parameter, ExecutionContext data) { if (valOrVarOffset < modifierOffset) { if (parameter.getValOrVarKeyword() != null) { - modifiers.add(mapModifier(parameter.getValOrVarKeyword(), Collections.emptyList())); + modifiers.add(mapModifier(parameter.getValOrVarKeyword(), Collections.emptyList(), consumedSpaces)); } if (parameter.getModifierList() != null) { @@ -813,7 +814,7 @@ public J visitParameter(KtParameter parameter, ExecutionContext data) { } if (parameter.getValOrVarKeyword() != null) { - modifiers.add(mapModifier(parameter.getValOrVarKeyword(), Collections.emptyList())); + modifiers.add(mapModifier(parameter.getValOrVarKeyword(), Collections.emptyList(), consumedSpaces)); } } @@ -872,6 +873,7 @@ public J visitPrimaryConstructor(KtPrimaryConstructor constructor, ExecutionCont List leadingAnnotations = new ArrayList<>(); List modifiers = new ArrayList<>(); + Set consumedSpaces = preConsumedInfix(constructor); if (constructor.getModifierList() != null) { KtModifierList ktModifierList = constructor.getModifierList(); @@ -879,7 +881,7 @@ public J visitPrimaryConstructor(KtPrimaryConstructor constructor, ExecutionCont } if (constructor.getConstructorKeyword() != null) { - modifiers.add(mapModifier(constructor.getConstructorKeyword(), Collections.emptyList())); + modifiers.add(mapModifier(constructor.getConstructorKeyword(), Collections.emptyList(), consumedSpaces)); } JavaType.Method type = methodDeclarationType(constructor); @@ -1083,7 +1085,7 @@ public J visitSecondaryConstructor(KtSecondaryConstructor constructor, Execution List leadingAnnotations = new ArrayList<>(); List lastAnnotations = new ArrayList<>(); List modifiers = mapModifiers(constructor.getModifierList(), leadingAnnotations, lastAnnotations, data); - modifiers.add(mapModifier(constructor.getConstructorKeyword(), emptyList())); + modifiers.add(mapModifier(constructor.getConstructorKeyword(), emptyList(), preConsumedInfix(constructor))); JavaType.Method type = methodDeclarationType(constructor); J.Identifier name = createIdentifier(requireNonNull(constructor.getName()), prefix(constructor.getConstructorKeyword()), type) @@ -2905,7 +2907,7 @@ private List mapModifiers(@Nullable KtModifierList modifierList, } else if (isKeyword) { isLeadingAnnotation = false; - modifiers.add(mapModifier(child, new ArrayList<>(annotations))); + modifiers.add(mapModifier(child, new ArrayList<>(annotations), null)); annotations.clear(); } } @@ -3617,8 +3619,8 @@ private J.VariableDeclarations mapDestructuringDeclaration(KtDestructuringDeclar ); } - private J.Modifier mapModifier(PsiElement modifier, List annotations) { - Space prefix = prefix(modifier); + private J.Modifier mapModifier(PsiElement modifier, List annotations, @Nullable Set consumedSpaces) { + Space prefix = prefix(modifier, consumedSpaces); J.Modifier.Type type; String keyword = null; switch (modifier.getText()) { diff --git a/src/test/java/org/openrewrite/kotlin/tree/CommentTest.java b/src/test/java/org/openrewrite/kotlin/tree/CommentTest.java index 6ad09db30..a80ead84e 100644 --- a/src/test/java/org/openrewrite/kotlin/tree/CommentTest.java +++ b/src/test/java/org/openrewrite/kotlin/tree/CommentTest.java @@ -195,4 +195,19 @@ fun method(){} // END ) ); } + + @Test + void kdocBeforeValueParameter() { + rewriteRun( + kotlin( + """ + class CodeGenConfig( + var kotlinAllFieldsOptional: Boolean = false, + /** If enabled, the names of the classes available via the DgsConstant class will be snake cased.*/ + var snakeCaseConstantNames: Boolean = false + ) + """ + ) + ); + } }