Pull Up: Drop default parameter values in function which becomes overriding. Disable function with default values if target class is the Java one
#KT-9833 Fixed
This commit is contained in:
@@ -21,6 +21,7 @@ import com.intellij.navigation.ItemPresentation;
|
|||||||
import com.intellij.navigation.ItemPresentationProviders;
|
import com.intellij.navigation.ItemPresentationProviders;
|
||||||
import com.intellij.psi.PsiElement;
|
import com.intellij.psi.PsiElement;
|
||||||
import com.intellij.psi.tree.TokenSet;
|
import com.intellij.psi.tree.TokenSet;
|
||||||
|
import com.intellij.psi.util.PsiTreeUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens;
|
import org.jetbrains.kotlin.lexer.KtTokens;
|
||||||
@@ -64,6 +65,11 @@ public class KtParameter extends KtNamedDeclarationStub<KotlinParameterStub> imp
|
|||||||
return findChildByType(KtTokens.COLON);
|
return findChildByType(KtTokens.COLON);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public PsiElement getEqualsToken() {
|
||||||
|
return findChildByType(KtTokens.EQ);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean hasDefaultValue() {
|
public boolean hasDefaultValue() {
|
||||||
KotlinParameterStub stub = getStub();
|
KotlinParameterStub stub = getStub();
|
||||||
if (stub != null) {
|
if (stub != null) {
|
||||||
@@ -75,20 +81,10 @@ public class KtParameter extends KtNamedDeclarationStub<KotlinParameterStub> imp
|
|||||||
@Nullable
|
@Nullable
|
||||||
public KtExpression getDefaultValue() {
|
public KtExpression getDefaultValue() {
|
||||||
KotlinParameterStub stub = getStub();
|
KotlinParameterStub stub = getStub();
|
||||||
if (stub != null && !stub.hasDefaultValue()) {
|
if (stub != null && !stub.hasDefaultValue()) return null;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
boolean passedEQ = false;
|
|
||||||
ASTNode child = getNode().getFirstChildNode();
|
|
||||||
while (child != null) {
|
|
||||||
if (child.getElementType() == KtTokens.EQ) passedEQ = true;
|
|
||||||
if (passedEQ && child.getPsi() instanceof KtExpression) {
|
|
||||||
return (KtExpression) child.getPsi();
|
|
||||||
}
|
|
||||||
child = child.getTreeNext();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
PsiElement equalsToken = getEqualsToken();
|
||||||
|
return equalsToken != null ? PsiTreeUtil.getNextSiblingOfType(equalsToken, KtExpression.class) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isMutable() {
|
public boolean isMutable() {
|
||||||
|
|||||||
@@ -191,4 +191,10 @@ fun KtSecondaryConstructor.getOrCreateBody(): KtBlockExpression {
|
|||||||
val anchor = if (delegationCall.isImplicit) valueParameterList else delegationCall
|
val anchor = if (delegationCall.isImplicit) valueParameterList else delegationCall
|
||||||
val newBody = KtPsiFactory(this).createEmptyBody()
|
val newBody = KtPsiFactory(this).createEmptyBody()
|
||||||
return addAfter(newBody, anchor) as KtBlockExpression
|
return addAfter(newBody, anchor) as KtBlockExpression
|
||||||
|
}
|
||||||
|
|
||||||
|
fun KtParameter.dropDefaultValue() {
|
||||||
|
val from = equalsToken ?: return
|
||||||
|
val to = defaultValue ?: from
|
||||||
|
deleteChildRange(from, to)
|
||||||
}
|
}
|
||||||
@@ -33,6 +33,7 @@ import org.jetbrains.kotlin.asJava.unwrapped
|
|||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
import org.jetbrains.kotlin.idea.KotlinLanguage
|
import org.jetbrains.kotlin.idea.KotlinLanguage
|
||||||
import org.jetbrains.kotlin.idea.codeInsight.shorten.addToShorteningWaitSet
|
import org.jetbrains.kotlin.idea.codeInsight.shorten.addToShorteningWaitSet
|
||||||
|
import org.jetbrains.kotlin.idea.core.dropDefaultValue
|
||||||
import org.jetbrains.kotlin.idea.core.refactoring.createJavaField
|
import org.jetbrains.kotlin.idea.core.refactoring.createJavaField
|
||||||
import org.jetbrains.kotlin.idea.core.refactoring.createJavaMethod
|
import org.jetbrains.kotlin.idea.core.refactoring.createJavaMethod
|
||||||
import org.jetbrains.kotlin.idea.core.refactoring.createPrimaryConstructorIfAbsent
|
import org.jetbrains.kotlin.idea.core.refactoring.createPrimaryConstructorIfAbsent
|
||||||
@@ -360,6 +361,7 @@ class KotlinPullUpHelper(
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
member.addModifierWithSpace(KtTokens.OVERRIDE_KEYWORD)
|
member.addModifierWithSpace(KtTokens.OVERRIDE_KEYWORD)
|
||||||
|
(member as? KtNamedFunction)?.valueParameters?.forEach { it.dropDefaultValue() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
package org.jetbrains.kotlin.idea.refactoring.pullUp
|
package org.jetbrains.kotlin.idea.refactoring.pullUp
|
||||||
|
|
||||||
import com.intellij.psi.PsiClass
|
import com.intellij.psi.PsiClass
|
||||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
|
||||||
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
||||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||||
import org.jetbrains.kotlin.idea.codeInsight.shorten.addToShorteningWaitSet
|
import org.jetbrains.kotlin.idea.codeInsight.shorten.addToShorteningWaitSet
|
||||||
@@ -46,7 +45,7 @@ fun KtNamedDeclaration.canMoveMemberToJavaClass(targetClass: PsiClass): Boolean
|
|||||||
if (accessors.isNotEmpty() || delegateExpression != null) return false
|
if (accessors.isNotEmpty() || delegateExpression != null) return false
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
is KtNamedFunction -> true
|
is KtNamedFunction -> valueParameters.all { it.defaultValue == null }
|
||||||
else -> false
|
else -> false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
class A {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
class A {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
class <caret>B : A() {
|
||||||
|
// INFO: {"checked": "true", "toAbstract": "true"}
|
||||||
|
fun foo(n: Int = 1) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
B().foo()
|
||||||
|
B().foo(2)
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
class B : A() {
|
||||||
|
// INFO: {"checked": "true", "toAbstract": "true"}
|
||||||
|
fun foo(n: Int = 1) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
B().foo()
|
||||||
|
B().foo(2)
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
open class A {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class <caret>B : A() {
|
||||||
|
// INFO: {"checked": "true", "toAbstract": "true"}
|
||||||
|
fun foo(n: Int = 1) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
B().foo()
|
||||||
|
B().foo(2)
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
abstract class A {
|
||||||
|
// INFO: {"checked": "true", "toAbstract": "true"}
|
||||||
|
abstract fun foo(n: Int = 1)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class B : A() {
|
||||||
|
// INFO: {"checked": "true", "toAbstract": "true"}
|
||||||
|
override fun foo(n: Int) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
B().foo()
|
||||||
|
B().foo(2)
|
||||||
|
}
|
||||||
@@ -49,6 +49,12 @@ public class PullUpTestGenerated extends AbstractPullUpTest {
|
|||||||
doKotlinTest(fileName);
|
doKotlinTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("defaultValuesInOverride.kt")
|
||||||
|
public void testDefaultValuesInOverride() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/pullUp/k2k/defaultValuesInOverride.kt");
|
||||||
|
doKotlinTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("fromClassToClass.kt")
|
@TestMetadata("fromClassToClass.kt")
|
||||||
public void testFromClassToClass() throws Exception {
|
public void testFromClassToClass() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/pullUp/k2k/fromClassToClass.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/pullUp/k2k/fromClassToClass.kt");
|
||||||
@@ -232,6 +238,12 @@ public class PullUpTestGenerated extends AbstractPullUpTest {
|
|||||||
KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File("idea/testData/refactoring/pullUp/k2j"), Pattern.compile("^(.+)\\.kt$"));
|
KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File("idea/testData/refactoring/pullUp/k2j"), Pattern.compile("^(.+)\\.kt$"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("defaultValuesInOverride.kt")
|
||||||
|
public void testDefaultValuesInOverride() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/pullUp/k2j/defaultValuesInOverride.kt");
|
||||||
|
doKotlinTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("fromClassToClass.kt")
|
@TestMetadata("fromClassToClass.kt")
|
||||||
public void testFromClassToClass() throws Exception {
|
public void testFromClassToClass() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/pullUp/k2j/fromClassToClass.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/pullUp/k2j/fromClassToClass.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user