ReplaceJavaStaticMethodWithKotlinAnalogInspection: add CommentSaver for case with transform to extension
This commit is contained in:
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.idea.inspections.jdk2k
|
|||||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference
|
import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference
|
||||||
import org.jetbrains.kotlin.idea.core.ShortenReferences
|
import org.jetbrains.kotlin.idea.core.ShortenReferences
|
||||||
import org.jetbrains.kotlin.idea.core.replaced
|
import org.jetbrains.kotlin.idea.core.replaced
|
||||||
|
import org.jetbrains.kotlin.idea.util.CommentSaver
|
||||||
import org.jetbrains.kotlin.idea.util.ImportInsertHelper
|
import org.jetbrains.kotlin.idea.util.ImportInsertHelper
|
||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.psi.KtCallExpression
|
import org.jetbrains.kotlin.psi.KtCallExpression
|
||||||
@@ -45,9 +46,15 @@ object ToExtensionFunctionWithNonNullableReceiver : Transformation {
|
|||||||
?.run { if (this is KtOperationExpression) "($text)" else text }
|
?.run { if (this is KtOperationExpression) "($text)" else text }
|
||||||
?: valueArguments.first().text
|
?: valueArguments.first().text
|
||||||
val argumentsText = valueArguments.drop(1).joinToString(separator = ", ") { it.text }
|
val argumentsText = valueArguments.drop(1).joinToString(separator = ", ") { it.text }
|
||||||
callExpression.getQualifiedExpressionForSelectorOrThis().replaced(
|
|
||||||
|
val oldExpression = callExpression.getQualifiedExpressionForSelectorOrThis()
|
||||||
|
val commentSaver = CommentSaver(oldExpression)
|
||||||
|
|
||||||
|
val replaced = oldExpression.replaced(
|
||||||
psiFactory.createExpression("$receiverText.${replacement.kotlinFunctionShortName}$typeArguments($argumentsText)")
|
psiFactory.createExpression("$receiverText.${replacement.kotlinFunctionShortName}$typeArguments($argumentsText)")
|
||||||
)
|
)
|
||||||
|
commentSaver.restore(replaced)
|
||||||
|
|
||||||
file.resolveImportReference(FqName(replacement.kotlinFunctionFqName)).firstOrNull()?.let {
|
file.resolveImportReference(FqName(replacement.kotlinFunctionFqName)).firstOrNull()?.let {
|
||||||
ImportInsertHelper.getInstance(callExpression.project).importDescriptor(file, it)
|
ImportInsertHelper.getInstance(callExpression.project).importDescriptor(file, it)
|
||||||
}
|
}
|
||||||
|
|||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
import java.util.Arrays
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val a = arrayOf(1, 2, 3)
|
||||||
|
val b = arrayOf(1, 2, 3)
|
||||||
|
/* comment */Arrays.<caret>equals(a, /* comment2 */b)
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
import java.util.Arrays
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val a = arrayOf(1, 2, 3)
|
||||||
|
val b = arrayOf(1, 2, 3)
|
||||||
|
/* comment *//* comment2 */a.contentEquals(b)
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
import java.util.Arrays
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val a = arrayOf(1, 2, 3)
|
||||||
|
val b = arrayOf(1, 2, 3)
|
||||||
|
// comment
|
||||||
|
Arrays.<caret>equals(a, b) //comment2
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
import java.util.Arrays
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val a = arrayOf(1, 2, 3)
|
||||||
|
val b = arrayOf(1, 2, 3)
|
||||||
|
// comment
|
||||||
|
a.contentEquals(b) //comment2
|
||||||
|
}
|
||||||
Vendored
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
import java.lang.Math.abs
|
||||||
|
|
||||||
|
fun x() {
|
||||||
|
/*dd*/<caret>abs(5/*s*/)
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
import java.lang.Math.abs
|
||||||
|
|
||||||
|
fun x() {
|
||||||
|
/*dd*/kotlin.math.abs(5/*s*/)
|
||||||
|
}
|
||||||
Vendored
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
import java.lang.Math.abs
|
||||||
|
|
||||||
|
fun x() {
|
||||||
|
// comment
|
||||||
|
<caret>abs(5)
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
import java.lang.Math.abs
|
||||||
|
|
||||||
|
fun x() {
|
||||||
|
// comment
|
||||||
|
kotlin.math.abs(5)
|
||||||
|
}
|
||||||
Vendored
+5
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
fun x() {
|
||||||
|
// comment
|
||||||
|
Math.<caret>abs(5)
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
import kotlin.math.abs
|
||||||
|
|
||||||
|
// WITH_RUNTIME
|
||||||
|
fun x() {
|
||||||
|
// comment
|
||||||
|
abs(5)
|
||||||
|
}
|
||||||
+25
@@ -9101,6 +9101,16 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equals2.kt");
|
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equals2.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("equalsWithBlockComment.kt")
|
||||||
|
public void testEqualsWithBlockComment() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithBlockComment.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("equalsWithEOLComment.kt")
|
||||||
|
public void testEqualsWithEOLComment() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithEOLComment.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("equalsWithNullOther.kt")
|
@TestMetadata("equalsWithNullOther.kt")
|
||||||
public void testEqualsWithNullOther() throws Exception {
|
public void testEqualsWithNullOther() throws Exception {
|
||||||
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithNullOther.kt");
|
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/collections/equalsWithNullOther.kt");
|
||||||
@@ -9291,6 +9301,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/abs4.kt");
|
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/abs4.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("absWithBlockComment.kt")
|
||||||
|
public void testAbsWithBlockComment() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithBlockComment.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("absWithEOLComment.kt")
|
||||||
|
public void testAbsWithEOLComment() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("absWithEOLComment2.kt")
|
||||||
|
public void testAbsWithEOLComment2() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/absWithEOLComment2.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("acos.kt")
|
@TestMetadata("acos.kt")
|
||||||
public void testAcos() throws Exception {
|
public void testAcos() throws Exception {
|
||||||
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/acos.kt");
|
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/acos.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user