diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/addRemoveModifier.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/addRemoveModifier.kt index 364029d39fc..5fa89c44cab 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/addRemoveModifier.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/addRemoveModifier.kt @@ -88,14 +88,20 @@ internal fun addModifier(modifierList: KtModifierList, modifier: KtModifierKeywo fun placeAfter(child: PsiElement): Boolean { if (child is PsiWhiteSpace) return false - if (child is KtAnnotation) return true // place modifiers after annotations + if (child is KtAnnotation || child is KtAnnotationEntry) return true // place modifiers after annotations val elementType = child.node!!.elementType val order = MODIFIERS_ORDER.indexOf(elementType) return newModifierOrder > order } val lastChild = modifierList.lastChild - val anchor = lastChild?.siblings(forward = false)?.firstOrNull(::placeAfter) + val anchor = lastChild?.siblings(forward = false)?.firstOrNull(::placeAfter).let { + when { + it?.nextSibling is PsiWhiteSpace && (it is KtAnnotation || it is KtAnnotationEntry) -> it.nextSibling + it == null && modifierList.firstChild is PsiWhiteSpace -> modifierList.firstChild + else -> it + } + } modifierList.addAfter(newModifier, anchor) if (anchor == lastChild) { // add line break if needed, otherwise visibility keyword may appear on previous line diff --git a/idea/testData/intentions/changeVisibility/private/annotated.kt b/idea/testData/intentions/changeVisibility/private/annotated.kt new file mode 100644 index 00000000000..bd705ce5298 --- /dev/null +++ b/idea/testData/intentions/changeVisibility/private/annotated.kt @@ -0,0 +1,4 @@ +annotation class Ann + +@Ann +annotation class My \ No newline at end of file diff --git a/idea/testData/intentions/changeVisibility/private/annotated.kt.after b/idea/testData/intentions/changeVisibility/private/annotated.kt.after new file mode 100644 index 00000000000..b8b32391d51 --- /dev/null +++ b/idea/testData/intentions/changeVisibility/private/annotated.kt.after @@ -0,0 +1,4 @@ +annotation class Ann + +@Ann +private annotation class My \ No newline at end of file diff --git a/idea/testData/multiModuleQuickFix/annotationOptionalExpectation/jvm/Ann.kt.after b/idea/testData/multiModuleQuickFix/annotationOptionalExpectation/jvm/Ann.kt.after index 885fca77516..af8498d9247 100644 --- a/idea/testData/multiModuleQuickFix/annotationOptionalExpectation/jvm/Ann.kt.after +++ b/idea/testData/multiModuleQuickFix/annotationOptionalExpectation/jvm/Ann.kt.after @@ -1,5 +1,4 @@ package kotlin // Ann: to be implemented -actual -annotation class Ann \ No newline at end of file +actual annotation class Ann \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 287ad63150e..3d5cb6ea31f 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -3508,6 +3508,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/changeVisibility/private"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); } + @TestMetadata("annotated.kt") + public void testAnnotated() throws Exception { + runTest("idea/testData/intentions/changeVisibility/private/annotated.kt"); + } + @TestMetadata("hasModifier1.kt") public void testHasModifier1() throws Exception { runTest("idea/testData/intentions/changeVisibility/private/hasModifier1.kt");