Skip to content

Commit

Permalink
Add test case of kdocBeforeValueParameter and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kunli2 committed Nov 8, 2023
1 parent bfa942d commit bf7e772
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ public J visitParameter(KtParameter parameter, ExecutionContext data) {
List<J.Modifier> modifiers = new ArrayList<>();
TypeTree typeExpression = null;
List<JRightPadded<J.VariableDeclarations.NamedVariable>> vars = new ArrayList<>(1);
Set<PsiElement> consumedSpaces = preConsumedInfix(parameter);

if (!parameter.getAnnotations().isEmpty()) {
throw new UnsupportedOperationException("TODO");
Expand All @@ -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) {
Expand All @@ -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));
}
}

Expand Down Expand Up @@ -872,14 +873,15 @@ public J visitPrimaryConstructor(KtPrimaryConstructor constructor, ExecutionCont

List<J.Annotation> leadingAnnotations = new ArrayList<>();
List<J.Modifier> modifiers = new ArrayList<>();
Set<PsiElement> consumedSpaces = preConsumedInfix(constructor);

if (constructor.getModifierList() != null) {
KtModifierList ktModifierList = constructor.getModifierList();
modifiers.addAll(mapModifiers(ktModifierList, leadingAnnotations, emptyList(), data));
}

if (constructor.getConstructorKeyword() != null) {
modifiers.add(mapModifier(constructor.getConstructorKeyword(), Collections.emptyList()));
modifiers.add(mapModifier(constructor.getConstructorKeyword(), Collections.emptyList(), consumedSpaces));
}

JavaType.Method type = methodDeclarationType(constructor);
Expand Down Expand Up @@ -1083,7 +1085,7 @@ public J visitSecondaryConstructor(KtSecondaryConstructor constructor, Execution
List<J.Annotation> leadingAnnotations = new ArrayList<>();
List<J.Annotation> lastAnnotations = new ArrayList<>();
List<J.Modifier> 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)
Expand Down Expand Up @@ -2905,7 +2907,7 @@ private List<J.Modifier> 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();
}
}
Expand Down Expand Up @@ -3617,8 +3619,8 @@ private J.VariableDeclarations mapDestructuringDeclaration(KtDestructuringDeclar
);
}

private J.Modifier mapModifier(PsiElement modifier, List<J.Annotation> annotations) {
Space prefix = prefix(modifier);
private J.Modifier mapModifier(PsiElement modifier, List<J.Annotation> annotations, @Nullable Set<PsiElement> consumedSpaces) {
Space prefix = prefix(modifier, consumedSpaces);
J.Modifier.Type type;
String keyword = null;
switch (modifier.getText()) {
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/openrewrite/kotlin/tree/CommentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
"""
)
);
}
}

0 comments on commit bf7e772

Please sign in to comment.