Create from Usage: Generate secondary constructors by delegation calls
This commit is contained in:
@@ -258,6 +258,9 @@ public class QuickFixRegistrar {
|
||||
QuickFixes.factories.put(TOO_MANY_ARGUMENTS, CreateCallableFromCallActionFactory.INSTANCE$);
|
||||
QuickFixes.factories.put(EXPRESSION_EXPECTED_PACKAGE_FOUND, CreateCallableFromCallActionFactory.INSTANCE$);
|
||||
|
||||
QuickFixes.factories.put(NO_VALUE_FOR_PARAMETER, CreateConstructorFromDelegationCallActionFactory.INSTANCE$);
|
||||
QuickFixes.factories.put(TOO_MANY_ARGUMENTS, CreateConstructorFromDelegationCallActionFactory.INSTANCE$);
|
||||
|
||||
QuickFixes.factories.put(UNRESOLVED_REFERENCE_WRONG_RECEIVER, CreateClassFromConstructorCallActionFactory.INSTANCE$);
|
||||
QuickFixes.factories.put(UNRESOLVED_REFERENCE, CreateClassFromConstructorCallActionFactory.INSTANCE$);
|
||||
QuickFixes.factories.put(EXPRESSION_EXPECTED_PACKAGE_FOUND, CreateClassFromConstructorCallActionFactory.INSTANCE$);
|
||||
|
||||
+9
-5
@@ -32,12 +32,16 @@ import java.util.Collections
|
||||
import java.util.HashSet
|
||||
|
||||
public class CreateCallableFromUsageFix(
|
||||
originalExpression: JetExpression,
|
||||
originalElement: JetElement,
|
||||
val callableInfos: List<CallableInfo>,
|
||||
val isExtension: Boolean
|
||||
) : CreateFromUsageFixBase(originalExpression) {
|
||||
{
|
||||
assert (callableInfos.isNotEmpty(), "No CallableInfos: ${JetPsiUtil.getElementTextWithContext(originalExpression)}")
|
||||
) : CreateFromUsageFixBase(originalElement) {
|
||||
constructor(originalExpression: JetElement,
|
||||
callableInfo: CallableInfo,
|
||||
isExtension: Boolean) : this(originalExpression, Collections.singletonList(callableInfo), isExtension) { }
|
||||
|
||||
init {
|
||||
assert (callableInfos.isNotEmpty(), "No CallableInfos: ${JetPsiUtil.getElementTextWithContext(originalElement)}")
|
||||
if (callableInfos.size > 1) {
|
||||
val receiverSet = callableInfos.mapTo(HashSet<TypeInfo>()) { it.receiverTypeInfo }
|
||||
if (receiverSet.size > 1) throw AssertionError("All functions must have common receiver: $receiverSet")
|
||||
@@ -103,7 +107,7 @@ public class CreateCallableFromUsageFix(
|
||||
val callableInfo = callableInfos.first()
|
||||
|
||||
val callableBuilder =
|
||||
CallableBuilderConfiguration(callableInfos, element as JetExpression, file!!, editor!!, isExtension).createBuilder()
|
||||
CallableBuilderConfiguration(callableInfos, element as JetElement, file!!, editor!!, isExtension).createBuilder()
|
||||
|
||||
fun runBuilder(placement: CallablePlacement) {
|
||||
callableBuilder.placement = placement
|
||||
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.quickfix.createFromUsage.createCallable
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction
|
||||
import com.intellij.psi.PsiClass
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
|
||||
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
|
||||
import org.jetbrains.kotlin.idea.quickfix.JetSingleIntentionActionFactory
|
||||
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.ParameterInfo
|
||||
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.SecondaryConstructorInfo
|
||||
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.TypeInfo
|
||||
import org.jetbrains.kotlin.idea.refactoring.canRefactor
|
||||
import org.jetbrains.kotlin.psi.JetClass
|
||||
import org.jetbrains.kotlin.psi.JetConstructorDelegationCall
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
|
||||
object CreateConstructorFromDelegationCallActionFactory : JetSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||
val delegationCall = diagnostic.getPsiElement().getStrictParentOfType<JetConstructorDelegationCall>() ?: return null
|
||||
val calleeExpression = delegationCall.getCalleeExpression() ?: return null
|
||||
val currentClass = delegationCall.getStrictParentOfType<JetClass>() ?: return null
|
||||
|
||||
val project = currentClass.getProject()
|
||||
|
||||
val classDescriptor = currentClass.resolveToDescriptor() as? ClassDescriptor ?: return null
|
||||
|
||||
val targetClass = if (calleeExpression.isThis()) {
|
||||
currentClass
|
||||
}
|
||||
else {
|
||||
val superClassDescriptor = DescriptorUtils.getSuperclassDescriptors(classDescriptor)
|
||||
.singleOrNull { it.getKind() == ClassKind.CLASS } ?: return null
|
||||
DescriptorToSourceUtilsIde.getAnyDeclaration(project, superClassDescriptor) ?: return null
|
||||
}
|
||||
if (!(targetClass.canRefactor() && (targetClass is JetClass || targetClass is PsiClass))) return null
|
||||
|
||||
val anyType = KotlinBuiltIns.getInstance().getNullableAnyType()
|
||||
val parameters = delegationCall.getValueArguments().map {
|
||||
ParameterInfo(
|
||||
it.getArgumentExpression()?.let { TypeInfo(it, Variance.IN_VARIANCE) } ?: TypeInfo(anyType, Variance.IN_VARIANCE),
|
||||
it.getArgumentName()?.getReferenceExpression()?.getReferencedName()
|
||||
)
|
||||
}
|
||||
|
||||
return CreateCallableFromUsageFix(delegationCall, SecondaryConstructorInfo(parameters, targetClass), false)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// "Create secondary constructor" "true"
|
||||
|
||||
open class A {
|
||||
constructor(i: Int) {
|
||||
//To change body of created constructors use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class B: A {
|
||||
constructor(): super(1) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Create secondary constructor" "true"
|
||||
|
||||
class A {
|
||||
constructor(): this(1) {
|
||||
|
||||
}
|
||||
|
||||
constructor(i: Int) {
|
||||
//To change body of created constructors use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Create secondary constructor" "true"
|
||||
|
||||
open class A {
|
||||
|
||||
}
|
||||
|
||||
class B: A {
|
||||
constructor(): super(<caret>1) {
|
||||
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Create secondary constructor" "false"
|
||||
// ERROR: Too many arguments for public constructor Any() defined in kotlin.Any
|
||||
|
||||
trait T {
|
||||
|
||||
}
|
||||
|
||||
class A: T {
|
||||
constructor(): super(<caret>1) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// "Create secondary constructor" "true"
|
||||
|
||||
class A {
|
||||
constructor(): this(<caret>1) {
|
||||
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
class J {
|
||||
|
||||
public J(int i) {
|
||||
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Create secondary constructor" "true"
|
||||
// ERROR: Too many arguments for public/*package*/ constructor J() defined in J
|
||||
|
||||
class B: J {
|
||||
constructor(): super(1) {
|
||||
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Create secondary constructor" "true"
|
||||
// ERROR: Too many arguments for public/*package*/ constructor J() defined in J
|
||||
|
||||
class B: J {
|
||||
constructor(): super(<caret>1) {
|
||||
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
class J {
|
||||
|
||||
}
|
||||
@@ -776,6 +776,12 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createSecondaryConstructor/javaConstructor.before.Main.kt");
|
||||
doTestWithExtraFile(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("superCallJavaClass.before.Main.kt")
|
||||
public void testSuperCallJavaClass() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createSecondaryConstructor/superCallJavaClass.before.Main.kt");
|
||||
doTestWithExtraFile(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/createFromUsage/createVariable")
|
||||
|
||||
@@ -2299,6 +2299,24 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeSuperCall.kt")
|
||||
public void testSuperCall() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createSecondaryConstructor/beforeSuperCall.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeSuperCallNoClass.kt")
|
||||
public void testSuperCallNoClass() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createSecondaryConstructor/beforeSuperCallNoClass.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeThisCall.kt")
|
||||
public void testThisCall() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createSecondaryConstructor/beforeThisCall.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeWrongExpectedType.kt")
|
||||
public void testWrongExpectedType() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createSecondaryConstructor/beforeWrongExpectedType.kt");
|
||||
|
||||
Reference in New Issue
Block a user