From 8b0b135112d8ef8c839f6dc2eec85bbf72cfa500 Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Thu, 6 Jun 2019 17:11:07 +0700 Subject: [PATCH] Add new line after modifier list if last child is comment #KT-30804 Fixed --- .../jetbrains/kotlin/psi/addRemoveModifier.kt | 7 +++++++ .../kotlin/idea/quickfix/RemoveModifierFix.kt | 8 ++------ .../removeRedundantModifier3.kt.after | 2 +- .../withAnnotationAndBlockComment.kt | 5 +++++ .../withAnnotationAndBlockComment.kt.after | 5 +++++ .../modifiers/withAnnotationAndEolComment.kt | 4 ++++ .../withAnnotationAndEolComment.kt.after | 4 ++++ .../modifiers/withAnnotationAndEolComment2.kt | 6 ++++++ .../withAnnotationAndEolComment2.kt.after | 6 ++++++ .../modifiers/withAnnotationAndEolComment3.kt | 6 ++++++ .../withAnnotationAndEolComment3.kt.after | 6 ++++++ .../redundantLateinit/simple.kt.after | 2 +- .../removeRedundantProjection1.kt | 2 +- .../removeRedundantProjection2.kt | 2 +- .../idea/quickfix/QuickFixTestGenerated.java | 20 +++++++++++++++++++ 15 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 idea/testData/quickfix/modifiers/withAnnotationAndBlockComment.kt create mode 100644 idea/testData/quickfix/modifiers/withAnnotationAndBlockComment.kt.after create mode 100644 idea/testData/quickfix/modifiers/withAnnotationAndEolComment.kt create mode 100644 idea/testData/quickfix/modifiers/withAnnotationAndEolComment.kt.after create mode 100644 idea/testData/quickfix/modifiers/withAnnotationAndEolComment2.kt create mode 100644 idea/testData/quickfix/modifiers/withAnnotationAndEolComment2.kt.after create mode 100644 idea/testData/quickfix/modifiers/withAnnotationAndEolComment3.kt create mode 100644 idea/testData/quickfix/modifiers/withAnnotationAndEolComment3.kt.after diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/addRemoveModifier.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/addRemoveModifier.kt index 5fa89c44cab..49668760f8c 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/addRemoveModifier.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/addRemoveModifier.kt @@ -22,6 +22,7 @@ import com.intellij.psi.PsiWhiteSpace import org.jetbrains.kotlin.lexer.KtModifierKeywordToken import org.jetbrains.kotlin.lexer.KtTokens.* import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespace import org.jetbrains.kotlin.psi.psiUtil.siblings private fun KtModifierListOwner.addModifierList(newModifierList: KtModifierList): KtModifierList { @@ -119,6 +120,12 @@ fun removeModifier(owner: KtModifierListOwner, modifier: KtModifierKeywordToken) it.getModifier(modifier)?.delete() if (it.firstChild == null) { it.delete() + return + } + + val lastChild = it.lastChild + if (lastChild is PsiComment) { + it.addAfter(KtPsiFactory(owner).createNewLine(), lastChild) } } } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveModifierFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveModifierFix.kt index 34a4780a903..68b9eacd84d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveModifierFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveModifierFix.kt @@ -49,18 +49,14 @@ class RemoveModifierFix( override fun getText() = text - override fun isAvailableImpl(project: Project, editor: Editor?, file: PsiFile) = (element?.hasModifier(modifier) ?: false) + override fun isAvailableImpl(project: Project, editor: Editor?, file: PsiFile) = element?.hasModifier(modifier) == true override fun invokeImpl(project: Project, editor: Editor?, file: PsiFile) { invoke() } fun invoke() { - val element = element ?: return - //TODO: without this copy&replace we get bad formatting on removing last modifier - val newElement = element.copy() as KtModifierListOwner - newElement.removeModifier(modifier) - element.replace(newElement) + element?.removeModifier(modifier) } companion object { diff --git a/idea/testData/quickfix/modifiers/removeRedundantModifier3.kt.after b/idea/testData/quickfix/modifiers/removeRedundantModifier3.kt.after index 8dea6adeadc..2f6b03af29c 100644 --- a/idea/testData/quickfix/modifiers/removeRedundantModifier3.kt.after +++ b/idea/testData/quickfix/modifiers/removeRedundantModifier3.kt.after @@ -4,5 +4,5 @@ abstract class B() { } abstract class A() : B() { - abstract override fun foo() + abstract override fun foo() } diff --git a/idea/testData/quickfix/modifiers/withAnnotationAndBlockComment.kt b/idea/testData/quickfix/modifiers/withAnnotationAndBlockComment.kt new file mode 100644 index 00000000000..b62e80d58d3 --- /dev/null +++ b/idea/testData/quickfix/modifiers/withAnnotationAndBlockComment.kt @@ -0,0 +1,5 @@ +// "Remove 'final' modifier" "true" + +@Deprecated("") +/* some comment */ +final val x: Int = 42 \ No newline at end of file diff --git a/idea/testData/quickfix/modifiers/withAnnotationAndBlockComment.kt.after b/idea/testData/quickfix/modifiers/withAnnotationAndBlockComment.kt.after new file mode 100644 index 00000000000..b0b1a528500 --- /dev/null +++ b/idea/testData/quickfix/modifiers/withAnnotationAndBlockComment.kt.after @@ -0,0 +1,5 @@ +// "Remove 'final' modifier" "true" + +@Deprecated("") +/* some comment */ +val x: Int = 42 \ No newline at end of file diff --git a/idea/testData/quickfix/modifiers/withAnnotationAndEolComment.kt b/idea/testData/quickfix/modifiers/withAnnotationAndEolComment.kt new file mode 100644 index 00000000000..297048a649c --- /dev/null +++ b/idea/testData/quickfix/modifiers/withAnnotationAndEolComment.kt @@ -0,0 +1,4 @@ +// "Remove 'final' modifier" "true" + +@Deprecated("") //some comment +final val x: Int = 42 \ No newline at end of file diff --git a/idea/testData/quickfix/modifiers/withAnnotationAndEolComment.kt.after b/idea/testData/quickfix/modifiers/withAnnotationAndEolComment.kt.after new file mode 100644 index 00000000000..fad2b93404b --- /dev/null +++ b/idea/testData/quickfix/modifiers/withAnnotationAndEolComment.kt.after @@ -0,0 +1,4 @@ +// "Remove 'final' modifier" "true" + +@Deprecated("") //some comment +val x: Int = 42 \ No newline at end of file diff --git a/idea/testData/quickfix/modifiers/withAnnotationAndEolComment2.kt b/idea/testData/quickfix/modifiers/withAnnotationAndEolComment2.kt new file mode 100644 index 00000000000..65a22ffd5cd --- /dev/null +++ b/idea/testData/quickfix/modifiers/withAnnotationAndEolComment2.kt @@ -0,0 +1,6 @@ +// "Remove 'final' modifier" "true" + +class A() { + @Deprecated("") // wd + final constructor(i: Int): this() +} \ No newline at end of file diff --git a/idea/testData/quickfix/modifiers/withAnnotationAndEolComment2.kt.after b/idea/testData/quickfix/modifiers/withAnnotationAndEolComment2.kt.after new file mode 100644 index 00000000000..df38aaabde9 --- /dev/null +++ b/idea/testData/quickfix/modifiers/withAnnotationAndEolComment2.kt.after @@ -0,0 +1,6 @@ +// "Remove 'final' modifier" "true" + +class A() { + @Deprecated("") // wd + constructor(i: Int): this() +} \ No newline at end of file diff --git a/idea/testData/quickfix/modifiers/withAnnotationAndEolComment3.kt b/idea/testData/quickfix/modifiers/withAnnotationAndEolComment3.kt new file mode 100644 index 00000000000..a3ff44a807a --- /dev/null +++ b/idea/testData/quickfix/modifiers/withAnnotationAndEolComment3.kt @@ -0,0 +1,6 @@ +// "Remove 'final' modifier" "true" + +class A @Deprecated("") // ds +final constructor() { + +} \ No newline at end of file diff --git a/idea/testData/quickfix/modifiers/withAnnotationAndEolComment3.kt.after b/idea/testData/quickfix/modifiers/withAnnotationAndEolComment3.kt.after new file mode 100644 index 00000000000..63111261913 --- /dev/null +++ b/idea/testData/quickfix/modifiers/withAnnotationAndEolComment3.kt.after @@ -0,0 +1,6 @@ +// "Remove 'final' modifier" "true" + +class A @Deprecated("") // ds +constructor() { + +} \ No newline at end of file diff --git a/idea/testData/quickfix/redundantLateinit/simple.kt.after b/idea/testData/quickfix/redundantLateinit/simple.kt.after index b1d590b2ada..6de607c4763 100644 --- a/idea/testData/quickfix/redundantLateinit/simple.kt.after +++ b/idea/testData/quickfix/redundantLateinit/simple.kt.after @@ -1,5 +1,5 @@ // "Remove 'lateinit' modifier" "true" class Test { - private var foo: String = "" + private var foo: String = "" } \ No newline at end of file diff --git a/idea/testData/quickfix/typeProjection/removeRedundantProjection1.kt b/idea/testData/quickfix/typeProjection/removeRedundantProjection1.kt index c0d6c5b4a7a..f3727a9c259 100644 --- a/idea/testData/quickfix/typeProjection/removeRedundantProjection1.kt +++ b/idea/testData/quickfix/typeProjection/removeRedundantProjection1.kt @@ -3,6 +3,6 @@ class Foo { val x = 0 } -fun bar(x : Foo< out Any>) { +fun bar(x : Foo Any>) { } diff --git a/idea/testData/quickfix/typeProjection/removeRedundantProjection2.kt b/idea/testData/quickfix/typeProjection/removeRedundantProjection2.kt index 22f4e728a9e..ddda58f8601 100644 --- a/idea/testData/quickfix/typeProjection/removeRedundantProjection2.kt +++ b/idea/testData/quickfix/typeProjection/removeRedundantProjection2.kt @@ -3,6 +3,6 @@ class Foo { val x = 0 } -fun bar(x : Foo< in Any>) { +fun bar(x : Foo Any>) { } diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index bfbc72c3f13..d85d1318ca2 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -8622,6 +8622,26 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { runTest("idea/testData/quickfix/modifiers/visibilityModiferParameter.kt"); } + @TestMetadata("withAnnotationAndBlockComment.kt") + public void testWithAnnotationAndBlockComment() throws Exception { + runTest("idea/testData/quickfix/modifiers/withAnnotationAndBlockComment.kt"); + } + + @TestMetadata("withAnnotationAndEolComment.kt") + public void testWithAnnotationAndEolComment() throws Exception { + runTest("idea/testData/quickfix/modifiers/withAnnotationAndEolComment.kt"); + } + + @TestMetadata("withAnnotationAndEolComment2.kt") + public void testWithAnnotationAndEolComment2() throws Exception { + runTest("idea/testData/quickfix/modifiers/withAnnotationAndEolComment2.kt"); + } + + @TestMetadata("withAnnotationAndEolComment3.kt") + public void testWithAnnotationAndEolComment3() throws Exception { + runTest("idea/testData/quickfix/modifiers/withAnnotationAndEolComment3.kt"); + } + @TestMetadata("idea/testData/quickfix/modifiers/addOpenToClassDeclaration") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)