Can be constructor property: comment saver introduced to retain comments; unbound comments now added before, not after
(cherry picked from commit 886cf21)
This commit is contained in:
committed by
Mikhail Glukhikh
parent
4145e3438f
commit
4abcc278fd
@@ -332,7 +332,7 @@ class CommentSaver(originalElements: PsiChildRange, private val saveLineBreaks:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
restored = putAbandonedCommentsAfter.parent.addAfter(comment, putAbandonedCommentsAfter) as PsiComment
|
restored = putAbandonedCommentsAfter.parent.addBefore(comment, putAbandonedCommentsAfter) as PsiComment
|
||||||
putAbandonedCommentsAfter = restored
|
putAbandonedCommentsAfter = restored
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
@@ -25,6 +25,7 @@ import com.intellij.psi.PsiElementVisitor
|
|||||||
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||||
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
|
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully
|
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.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getAnnotationEntries
|
import org.jetbrains.kotlin.psi.psiUtil.getAnnotationEntries
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
@@ -73,6 +74,7 @@ class CanBePrimaryConstructorPropertyInspection : AbstractKotlinInspection() {
|
|||||||
override fun getFamilyName() = "Move to constructor"
|
override fun getFamilyName() = "Move to constructor"
|
||||||
|
|
||||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||||
|
val commentSaver = CommentSaver(original)
|
||||||
val isVar = original.isVar
|
val isVar = original.isVar
|
||||||
val modifiers = original.modifierList?.text
|
val modifiers = original.modifierList?.text
|
||||||
val factory = KtPsiFactory(project)
|
val factory = KtPsiFactory(project)
|
||||||
@@ -83,6 +85,7 @@ class CanBePrimaryConstructorPropertyInspection : AbstractKotlinInspection() {
|
|||||||
parameter.addBefore(newModifiers, parameter.valOrVarKeyword)
|
parameter.addBefore(newModifiers, parameter.valOrVarKeyword)
|
||||||
}
|
}
|
||||||
original.delete()
|
original.delete()
|
||||||
|
commentSaver.restore(parameter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,8 @@
|
|||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
|
||||||
fun foo(p: Array<String?>) {
|
fun foo(p: Array<String?>) {
|
||||||
val v = p[0]!!<caret>/* null */
|
/* null */
|
||||||
|
val v = p[0]!!
|
||||||
// now let's check it for null
|
// now let's check it for null
|
||||||
// 'v' should not be null
|
// 'v' should not be null
|
||||||
}
|
}
|
||||||
+2
-1
@@ -1,4 +1,5 @@
|
|||||||
// "Remove function body" "true"
|
// "Remove function body" "true"
|
||||||
abstract class A() {
|
abstract class A() {
|
||||||
<caret>abstract fun foo() // 3/*1*/
|
/*1*/
|
||||||
|
abstract fun foo() // 3
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
// "Move to constructor" "true"
|
||||||
|
class Complex(x: Int, y: Double, z: String) {
|
||||||
|
val <caret>y: Double = y // Duplicating
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
// "Move to constructor" "true"
|
||||||
|
class Complex(x: Int, // Duplicating
|
||||||
|
val y: Double, z: String) {
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// "Move to constructor" "true"
|
||||||
|
class Complex(x: Int, y: Double, z: String) {
|
||||||
|
/**
|
||||||
|
* Very complex field x
|
||||||
|
*/
|
||||||
|
val <caret>x = x
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// "Move to constructor" "true"
|
||||||
|
class Complex(
|
||||||
|
/**
|
||||||
|
* Very complex field x
|
||||||
|
*/
|
||||||
|
val x: Int, y: Double, z: String) {
|
||||||
|
}
|
||||||
+1
-1
@@ -8,5 +8,5 @@ fun oldFun(p: Int) {
|
|||||||
fun newFun(){}
|
fun newFun(){}
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
newFun()/* use zero */
|
/* use zero */newFun()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -724,6 +724,18 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/canBePrimaryConstructorProperty"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
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")
|
@TestMetadata("protectedOpenVar.kt")
|
||||||
public void testProtectedOpenVar() throws Exception {
|
public void testProtectedOpenVar() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/canBePrimaryConstructorProperty/protectedOpenVar.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/canBePrimaryConstructorProperty/protectedOpenVar.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user