From 4abcc278fdeb29265ff48d2ebb28e5fc211aa143 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 20 Jun 2016 20:12:47 +0300 Subject: [PATCH] Can be constructor property: comment saver introduced to retain comments; unbound comments now added before, not after (cherry picked from commit 886cf21) --- .../org/jetbrains/kotlin/idea/util/CommentSaver.kt | 2 +- .../CanBePrimaryConstructorPropertyInspection.kt | 3 +++ .../simplifyAssertNotNull/commentsNoMessage.kt.after | 3 ++- .../abstractFunctionWithBodyWithComments2.kt.after | 3 ++- .../canBePrimaryConstructorProperty/commentAfter.kt | 4 ++++ .../commentAfter.kt.after | 4 ++++ .../canBePrimaryConstructorProperty/commentAhead.kt | 7 +++++++ .../commentAhead.kt.after | 7 +++++++ .../keepComments/dropArgument.kt.after | 2 +- .../kotlin/idea/quickfix/QuickFixTestGenerated.java | 12 ++++++++++++ 10 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 idea/testData/quickfix/canBePrimaryConstructorProperty/commentAfter.kt create mode 100644 idea/testData/quickfix/canBePrimaryConstructorProperty/commentAfter.kt.after create mode 100644 idea/testData/quickfix/canBePrimaryConstructorProperty/commentAhead.kt create mode 100644 idea/testData/quickfix/canBePrimaryConstructorProperty/commentAhead.kt.after diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/util/CommentSaver.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/util/CommentSaver.kt index 35cfed29943..890311d3a61 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/util/CommentSaver.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/util/CommentSaver.kt @@ -332,7 +332,7 @@ class CommentSaver(originalElements: PsiChildRange, private val saveLineBreaks: } } else { - restored = putAbandonedCommentsAfter.parent.addAfter(comment, putAbandonedCommentsAfter) as PsiComment + restored = putAbandonedCommentsAfter.parent.addBefore(comment, putAbandonedCommentsAfter) as PsiComment putAbandonedCommentsAfter = restored } diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/CanBePrimaryConstructorPropertyInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/CanBePrimaryConstructorPropertyInspection.kt index a95e75ebc08..87128b2feb2 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/CanBePrimaryConstructorPropertyInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/CanBePrimaryConstructorPropertyInspection.kt @@ -25,6 +25,7 @@ import com.intellij.psi.PsiElementVisitor import org.jetbrains.kotlin.descriptors.ConstructorDescriptor import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully +import org.jetbrains.kotlin.idea.util.CommentSaver import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.getAnnotationEntries import org.jetbrains.kotlin.resolve.BindingContext @@ -73,6 +74,7 @@ class CanBePrimaryConstructorPropertyInspection : AbstractKotlinInspection() { override fun getFamilyName() = "Move to constructor" override fun applyFix(project: Project, descriptor: ProblemDescriptor) { + val commentSaver = CommentSaver(original) val isVar = original.isVar val modifiers = original.modifierList?.text val factory = KtPsiFactory(project) @@ -83,6 +85,7 @@ class CanBePrimaryConstructorPropertyInspection : AbstractKotlinInspection() { parameter.addBefore(newModifiers, parameter.valOrVarKeyword) } original.delete() + commentSaver.restore(parameter) } } } \ No newline at end of file diff --git a/idea/testData/intentions/simplifyAssertNotNull/commentsNoMessage.kt.after b/idea/testData/intentions/simplifyAssertNotNull/commentsNoMessage.kt.after index e5621582c9b..a7558c264de 100644 --- a/idea/testData/intentions/simplifyAssertNotNull/commentsNoMessage.kt.after +++ b/idea/testData/intentions/simplifyAssertNotNull/commentsNoMessage.kt.after @@ -2,7 +2,8 @@ // WITH_RUNTIME fun foo(p: Array) { - val v = p[0]!!/* null */ + /* null */ + val v = p[0]!! // now let's check it for null // 'v' should not be null } \ No newline at end of file diff --git a/idea/testData/quickfix/abstract/abstractFunctionWithBodyWithComments2.kt.after b/idea/testData/quickfix/abstract/abstractFunctionWithBodyWithComments2.kt.after index d847c57383b..cb337570a03 100644 --- a/idea/testData/quickfix/abstract/abstractFunctionWithBodyWithComments2.kt.after +++ b/idea/testData/quickfix/abstract/abstractFunctionWithBodyWithComments2.kt.after @@ -1,4 +1,5 @@ // "Remove function body" "true" abstract class A() { - abstract fun foo() // 3/*1*/ + /*1*/ + abstract fun foo() // 3 } diff --git a/idea/testData/quickfix/canBePrimaryConstructorProperty/commentAfter.kt b/idea/testData/quickfix/canBePrimaryConstructorProperty/commentAfter.kt new file mode 100644 index 00000000000..5bfa1936a5b --- /dev/null +++ b/idea/testData/quickfix/canBePrimaryConstructorProperty/commentAfter.kt @@ -0,0 +1,4 @@ +// "Move to constructor" "true" +class Complex(x: Int, y: Double, z: String) { + val y: Double = y // Duplicating +} \ No newline at end of file diff --git a/idea/testData/quickfix/canBePrimaryConstructorProperty/commentAfter.kt.after b/idea/testData/quickfix/canBePrimaryConstructorProperty/commentAfter.kt.after new file mode 100644 index 00000000000..df5c48087d3 --- /dev/null +++ b/idea/testData/quickfix/canBePrimaryConstructorProperty/commentAfter.kt.after @@ -0,0 +1,4 @@ +// "Move to constructor" "true" +class Complex(x: Int, // Duplicating + val y: Double, z: String) { +} \ No newline at end of file diff --git a/idea/testData/quickfix/canBePrimaryConstructorProperty/commentAhead.kt b/idea/testData/quickfix/canBePrimaryConstructorProperty/commentAhead.kt new file mode 100644 index 00000000000..d362c581f5d --- /dev/null +++ b/idea/testData/quickfix/canBePrimaryConstructorProperty/commentAhead.kt @@ -0,0 +1,7 @@ +// "Move to constructor" "true" +class Complex(x: Int, y: Double, z: String) { + /** + * Very complex field x + */ + val x = x +} \ No newline at end of file diff --git a/idea/testData/quickfix/canBePrimaryConstructorProperty/commentAhead.kt.after b/idea/testData/quickfix/canBePrimaryConstructorProperty/commentAhead.kt.after new file mode 100644 index 00000000000..2d2629e8729 --- /dev/null +++ b/idea/testData/quickfix/canBePrimaryConstructorProperty/commentAhead.kt.after @@ -0,0 +1,7 @@ +// "Move to constructor" "true" +class Complex( + /** + * Very complex field x + */ + val x: Int, y: Double, z: String) { +} \ No newline at end of file diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/keepComments/dropArgument.kt.after b/idea/testData/quickfix/deprecatedSymbolUsage/keepComments/dropArgument.kt.after index b3481be3707..fc85c23faba 100644 --- a/idea/testData/quickfix/deprecatedSymbolUsage/keepComments/dropArgument.kt.after +++ b/idea/testData/quickfix/deprecatedSymbolUsage/keepComments/dropArgument.kt.after @@ -8,5 +8,5 @@ fun oldFun(p: Int) { fun newFun(){} fun foo() { - newFun()/* use zero */ + /* use zero */newFun() } diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 4b14a63d64c..0814ce00e7e 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -724,6 +724,18 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/canBePrimaryConstructorProperty"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); } + @TestMetadata("commentAfter.kt") + public void testCommentAfter() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/canBePrimaryConstructorProperty/commentAfter.kt"); + doTest(fileName); + } + + @TestMetadata("commentAhead.kt") + public void testCommentAhead() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/canBePrimaryConstructorProperty/commentAhead.kt"); + doTest(fileName); + } + @TestMetadata("protectedOpenVar.kt") public void testProtectedOpenVar() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/canBePrimaryConstructorProperty/protectedOpenVar.kt");