refactoring: do not create dialog in ChangeSignatureTests
This commit is contained in:
+30
-32
@@ -16,27 +16,20 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.refactoring.changeSignature
|
package org.jetbrains.kotlin.idea.refactoring.changeSignature
|
||||||
|
|
||||||
import com.intellij.ide.IdeBundle
|
|
||||||
import com.intellij.openapi.application.ApplicationManager
|
import com.intellij.openapi.application.ApplicationManager
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
import com.intellij.openapi.diagnostic.Logger
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.ui.Messages
|
|
||||||
import com.intellij.openapi.util.Disposer
|
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
import com.intellij.refactoring.changeSignature.ChangeSignatureHandler
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
|
||||||
import org.jetbrains.kotlin.idea.JetBundle
|
|
||||||
import org.jetbrains.kotlin.renderer.DescriptorRenderer
|
|
||||||
import java.util.*
|
|
||||||
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor.Kind.*
|
|
||||||
import org.jetbrains.annotations.TestOnly
|
import org.jetbrains.annotations.TestOnly
|
||||||
|
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||||
|
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||||
|
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||||
|
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||||
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
|
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
|
||||||
import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil
|
|
||||||
import com.intellij.CommonBundle
|
|
||||||
import com.intellij.refactoring.RefactoringBundle
|
|
||||||
import org.jetbrains.kotlin.resolve.OverrideResolver
|
|
||||||
import org.jetbrains.kotlin.idea.refactoring.CallableRefactoring
|
import org.jetbrains.kotlin.idea.refactoring.CallableRefactoring
|
||||||
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
|
import org.jetbrains.kotlin.resolve.OverrideResolver
|
||||||
|
|
||||||
public trait JetChangeSignatureConfiguration {
|
public trait JetChangeSignatureConfiguration {
|
||||||
fun configure(originalDescriptor: JetMethodDescriptor, bindingContext: BindingContext): JetMethodDescriptor
|
fun configure(originalDescriptor: JetMethodDescriptor, bindingContext: BindingContext): JetMethodDescriptor
|
||||||
@@ -75,24 +68,32 @@ public class JetChangeSignature(project: Project,
|
|||||||
"Function descriptors expected: " + descriptorsForChange.joinToString(separator = "\n")
|
"Function descriptors expected: " + descriptorsForChange.joinToString(separator = "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
[suppress("UNCHECKED_CAST")]
|
@suppress("UNCHECKED_CAST")
|
||||||
val dialog = createChangeSignatureDialog(descriptorsForChange as Collection<FunctionDescriptor>)
|
val adjustedDescriptor = adjustDescriptor(descriptorsForChange as Collection<FunctionDescriptor>)
|
||||||
if (dialog == null) return
|
if (adjustedDescriptor == null) return
|
||||||
|
|
||||||
val affectedFunctions = dialog.getMethodDescriptor().affectedFunctions.map { it.getElement() }.filterNotNull()
|
val affectedFunctions = adjustedDescriptor.affectedFunctions.map { it.getElement() }.filterNotNull()
|
||||||
|
|
||||||
if (affectedFunctions.any { !checkModifiable(it) }) return
|
if (affectedFunctions.any { !checkModifiable(it) }) return
|
||||||
|
|
||||||
if (configuration.performSilently(affectedFunctions)
|
if (configuration.performSilently(affectedFunctions)
|
||||||
|| ApplicationManager.getApplication()!!.isUnitTestMode()) {
|
|| ApplicationManager.getApplication()!!.isUnitTestMode()) {
|
||||||
performRefactoringSilently(dialog)
|
JetChangeSignatureDialog.createRefactoringProcessor(
|
||||||
|
project,
|
||||||
|
commandName ?: ChangeSignatureHandler.REFACTORING_NAME,
|
||||||
|
adjustedDescriptor,
|
||||||
|
defaultValueContext
|
||||||
|
|
||||||
|
).run()
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
val dialog = JetChangeSignatureDialog(project, adjustedDescriptor, defaultValueContext, commandName)
|
||||||
|
|
||||||
dialog.show()
|
dialog.show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createChangeSignatureDialog(descriptorsForSignatureChange: Collection<FunctionDescriptor>): JetChangeSignatureDialog? {
|
fun adjustDescriptor(descriptorsForSignatureChange: Collection<FunctionDescriptor>): JetMethodDescriptor? {
|
||||||
val baseDescriptor = preferContainedInClass(descriptorsForSignatureChange)
|
val baseDescriptor = preferContainedInClass(descriptorsForSignatureChange)
|
||||||
val functionDeclaration = DescriptorToSourceUtilsIde.getAnyDeclaration(project, baseDescriptor)
|
val functionDeclaration = DescriptorToSourceUtilsIde.getAnyDeclaration(project, baseDescriptor)
|
||||||
if (functionDeclaration == null) {
|
if (functionDeclaration == null) {
|
||||||
@@ -105,15 +106,7 @@ public class JetChangeSignature(project: Project,
|
|||||||
}
|
}
|
||||||
|
|
||||||
val originalDescriptor = JetChangeSignatureData(baseDescriptor, functionDeclaration, descriptorsForSignatureChange)
|
val originalDescriptor = JetChangeSignatureData(baseDescriptor, functionDeclaration, descriptorsForSignatureChange)
|
||||||
val adjustedDescriptor = configuration.configure(originalDescriptor, bindingContext)
|
return configuration.configure(originalDescriptor, bindingContext)
|
||||||
return JetChangeSignatureDialog(project, adjustedDescriptor, defaultValueContext, commandName)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun performRefactoringSilently(dialog: JetChangeSignatureDialog) {
|
|
||||||
runWriteAction {
|
|
||||||
dialog.createRefactoringProcessor().run()
|
|
||||||
Disposer.dispose(dialog.getDisposable())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun preferContainedInClass(descriptorsForSignatureChange: Collection<FunctionDescriptor>): FunctionDescriptor {
|
private fun preferContainedInClass(descriptorsForSignatureChange: Collection<FunctionDescriptor>): FunctionDescriptor {
|
||||||
@@ -128,11 +121,16 @@ public class JetChangeSignature(project: Project,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TestOnly public fun getChangeSignatureDialog(project: Project,
|
TestOnly public fun createChangeInfo(project: Project,
|
||||||
functionDescriptor: FunctionDescriptor,
|
functionDescriptor: FunctionDescriptor,
|
||||||
configuration: JetChangeSignatureConfiguration,
|
configuration: JetChangeSignatureConfiguration,
|
||||||
bindingContext: BindingContext,
|
bindingContext: BindingContext,
|
||||||
defaultValueContext: PsiElement): JetChangeSignatureDialog? {
|
defaultValueContext: PsiElement): JetChangeInfo? {
|
||||||
val jetChangeSignature = JetChangeSignature(project, functionDescriptor, configuration, bindingContext, defaultValueContext, null)
|
val jetChangeSignature = JetChangeSignature(project, functionDescriptor, configuration, bindingContext, defaultValueContext, null)
|
||||||
return jetChangeSignature.createChangeSignatureDialog(OverrideResolver.getDeepestSuperDeclarations(functionDescriptor))
|
val declarations = OverrideResolver.getDeepestSuperDeclarations(functionDescriptor)
|
||||||
|
|
||||||
|
val adjustedDescriptor = jetChangeSignature.adjustDescriptor(declarations) ?: return null
|
||||||
|
|
||||||
|
val processor = JetChangeSignatureDialog.createRefactoringProcessor(project, "", adjustedDescriptor, defaultValueContext) as JetChangeSignatureProcessor
|
||||||
|
return processor.getChangeInfo()
|
||||||
}
|
}
|
||||||
|
|||||||
+71
-13
@@ -50,6 +50,7 @@ import com.intellij.util.ui.table.JBTableRow;
|
|||||||
import com.intellij.util.ui.table.JBTableRowEditor;
|
import com.intellij.util.ui.table.JBTableRowEditor;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.annotations.TestOnly;
|
||||||
import org.jetbrains.kotlin.descriptors.Visibilities;
|
import org.jetbrains.kotlin.descriptors.Visibilities;
|
||||||
import org.jetbrains.kotlin.descriptors.Visibility;
|
import org.jetbrains.kotlin.descriptors.Visibility;
|
||||||
import org.jetbrains.kotlin.idea.JetFileType;
|
import org.jetbrains.kotlin.idea.JetFileType;
|
||||||
@@ -94,21 +95,31 @@ public class JetChangeSignatureDialog extends ChangeSignatureDialogBase<
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected JetCallableParameterTableModel createParametersInfoModel(JetMethodDescriptor descriptor) {
|
protected JetCallableParameterTableModel createParametersInfoModel(JetMethodDescriptor descriptor) {
|
||||||
|
return createParametersInfoModel(descriptor, myDefaultValueContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static JetCallableParameterTableModel createParametersInfoModel(JetMethodDescriptor descriptor, PsiElement defaultValueContext) {
|
||||||
switch (descriptor.getKind()) {
|
switch (descriptor.getKind()) {
|
||||||
case FUNCTION:
|
case FUNCTION:
|
||||||
return new JetFunctionParameterTableModel(descriptor, myDefaultValueContext);
|
return new JetFunctionParameterTableModel(descriptor, defaultValueContext);
|
||||||
case PRIMARY_CONSTRUCTOR:
|
case PRIMARY_CONSTRUCTOR:
|
||||||
return new JetPrimaryConstructorParameterTableModel(descriptor, myDefaultValueContext);
|
return new JetPrimaryConstructorParameterTableModel(descriptor, defaultValueContext);
|
||||||
case SECONDARY_CONSTRUCTOR:
|
case SECONDARY_CONSTRUCTOR:
|
||||||
return new JetSecondaryConstructorParameterTableModel(descriptor, myDefaultValueContext);
|
return new JetSecondaryConstructorParameterTableModel(descriptor, defaultValueContext);
|
||||||
}
|
}
|
||||||
throw new AssertionError("Invalid kind: " + descriptor.getKind());
|
throw new AssertionError("Invalid kind: " + descriptor.getKind());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected PsiCodeFragment createReturnTypeCodeFragment() {
|
protected PsiCodeFragment createReturnTypeCodeFragment() {
|
||||||
return JetPsiFactory(myProject).createTypeCodeFragment(
|
return createReturnTypeCodeFragment(myProject, myMethod);
|
||||||
ChangeSignaturePackage.renderOriginalReturnType(myMethod), myMethod.getBaseDeclaration()
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static PsiCodeFragment createReturnTypeCodeFragment(@NotNull Project project, @NotNull JetMethodDescriptor method) {
|
||||||
|
return JetPsiFactory(project).createTypeCodeFragment(
|
||||||
|
ChangeSignaturePackage.renderOriginalReturnType(method), method.getBaseDeclaration()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -398,7 +409,14 @@ public class JetChangeSignatureDialog extends ChangeSignatureDialogBase<
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String calculateSignature() {
|
protected String calculateSignature() {
|
||||||
JetChangeInfo changeInfo = evaluateChangeInfo();
|
JetChangeInfo changeInfo = evaluateChangeInfo(
|
||||||
|
myParametersTableModel,
|
||||||
|
myReturnTypeCodeFragment,
|
||||||
|
getMethodDescriptor(),
|
||||||
|
getVisibility(),
|
||||||
|
getMethodName(),
|
||||||
|
myDefaultValueContext
|
||||||
|
);
|
||||||
return changeInfo.getNewSignature(getMethodDescriptor().getOriginalPrimaryFunction());
|
return changeInfo.getNewSignature(getMethodDescriptor().getOriginalPrimaryFunction());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -444,7 +462,36 @@ public class JetChangeSignatureDialog extends ChangeSignatureDialogBase<
|
|||||||
@Override
|
@Override
|
||||||
@NotNull
|
@NotNull
|
||||||
protected BaseRefactoringProcessor createRefactoringProcessor() {
|
protected BaseRefactoringProcessor createRefactoringProcessor() {
|
||||||
return new JetChangeSignatureProcessor(myProject, evaluateChangeInfo(), commandName != null ? commandName : getTitle());
|
String commandName1 = commandName != null ? commandName : getTitle();
|
||||||
|
JetChangeInfo changeInfo = evaluateChangeInfo(
|
||||||
|
myParametersTableModel,
|
||||||
|
myReturnTypeCodeFragment,
|
||||||
|
getMethodDescriptor(),
|
||||||
|
getVisibility(),
|
||||||
|
getMethodName(),
|
||||||
|
myDefaultValueContext
|
||||||
|
);
|
||||||
|
return new JetChangeSignatureProcessor(myProject, changeInfo, commandName1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestOnly
|
||||||
|
public static BaseRefactoringProcessor createRefactoringProcessor(
|
||||||
|
@NotNull Project project,
|
||||||
|
@NotNull String commandName,
|
||||||
|
@NotNull JetMethodDescriptor method,
|
||||||
|
@NotNull PsiElement defaultValueContext
|
||||||
|
) {
|
||||||
|
JetCallableParameterTableModel parameterTableModel = createParametersInfoModel(method, defaultValueContext);
|
||||||
|
parameterTableModel.setParameterInfos(method.getParameters());
|
||||||
|
JetChangeInfo changeInfo = evaluateChangeInfo(
|
||||||
|
parameterTableModel,
|
||||||
|
createReturnTypeCodeFragment(project, method),
|
||||||
|
method,
|
||||||
|
method.getVisibility(),
|
||||||
|
method.getName(),
|
||||||
|
defaultValueContext
|
||||||
|
);
|
||||||
|
return new JetChangeSignatureProcessor(project, changeInfo, commandName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -452,12 +499,22 @@ public class JetChangeSignatureDialog extends ChangeSignatureDialogBase<
|
|||||||
return myMethod;
|
return myMethod;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JetChangeInfo evaluateChangeInfo() {
|
private static JetChangeInfo evaluateChangeInfo(
|
||||||
List<JetParameterInfo> parameters = getParameters();
|
@NotNull JetCallableParameterTableModel myParametersTableModel,
|
||||||
|
@Nullable PsiCodeFragment myReturnTypeCodeFragment,
|
||||||
|
@NotNull JetMethodDescriptor myMethod,
|
||||||
|
@Nullable Visibility visibility,
|
||||||
|
@NotNull String methodName,
|
||||||
|
@NotNull PsiElement myDefaultValueContext
|
||||||
|
) {
|
||||||
|
List<JetParameterInfo> parameters = new ArrayList<JetParameterInfo>(myParametersTableModel.getRowCount());
|
||||||
|
|
||||||
for (int i = 0; i < parameters.size(); i++) {
|
for (int i = 0; i < myParametersTableModel.getItems().size(); i++) {
|
||||||
JetParameterInfo parameter = parameters.get(i);
|
JetParameterInfo parameter = myParametersTableModel.getItems().get(i).parameter;
|
||||||
parameter.setCurrentTypeText(myParametersTableModel.getItems().get(i).typeCodeFragment.getText().trim());
|
parameter.setCurrentTypeText(myParametersTableModel.getItems().get(i).typeCodeFragment.getText().trim());
|
||||||
|
|
||||||
|
parameters.add(parameter);
|
||||||
|
|
||||||
JetExpressionCodeFragment codeFragment =
|
JetExpressionCodeFragment codeFragment =
|
||||||
(JetExpressionCodeFragment) myParametersTableModel.getItems().get(i).defaultValueCodeFragment;
|
(JetExpressionCodeFragment) myParametersTableModel.getItems().get(i).defaultValueCodeFragment;
|
||||||
JetExpression oldDefaultValue = parameter.getDefaultValueForCall();
|
JetExpression oldDefaultValue = parameter.getDefaultValueForCall();
|
||||||
@@ -470,8 +527,9 @@ public class JetChangeSignatureDialog extends ChangeSignatureDialogBase<
|
|||||||
JetMethodDescriptor descriptor = myMethod instanceof JetMutableMethodDescriptor
|
JetMethodDescriptor descriptor = myMethod instanceof JetMutableMethodDescriptor
|
||||||
? ((JetMutableMethodDescriptor) myMethod).getOriginal()
|
? ((JetMutableMethodDescriptor) myMethod).getOriginal()
|
||||||
: myMethod;
|
: myMethod;
|
||||||
return new JetChangeInfo(descriptor, getMethodName(), getReturnType(), returnTypeText,
|
JetType returnType = getType((JetTypeCodeFragment) myReturnTypeCodeFragment);
|
||||||
getVisibility(), parameters, myParametersTableModel.getReceiver(), myDefaultValueContext);
|
return new JetChangeInfo(descriptor, methodName, returnType, returnTypeText,
|
||||||
|
visibility, parameters, myParametersTableModel.getReceiver(), myDefaultValueContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+3
-11
@@ -20,7 +20,6 @@ import com.intellij.codeInsight.TargetElementUtilBase;
|
|||||||
import com.intellij.openapi.editor.Editor;
|
import com.intellij.openapi.editor.Editor;
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
import com.intellij.openapi.projectRoots.Sdk;
|
import com.intellij.openapi.projectRoots.Sdk;
|
||||||
import com.intellij.openapi.util.Disposer;
|
|
||||||
import com.intellij.openapi.util.text.StringUtil;
|
import com.intellij.openapi.util.text.StringUtil;
|
||||||
import com.intellij.psi.*;
|
import com.intellij.psi.*;
|
||||||
import com.intellij.psi.search.GlobalSearchScope;
|
import com.intellij.psi.search.GlobalSearchScope;
|
||||||
@@ -36,10 +35,10 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
|
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
|
||||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
|
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
|
||||||
import org.jetbrains.kotlin.descriptors.Visibilities;
|
import org.jetbrains.kotlin.descriptors.Visibilities;
|
||||||
import org.jetbrains.kotlin.idea.test.KotlinCodeInsightTestCase;
|
|
||||||
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase;
|
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.ResolvePackage;
|
import org.jetbrains.kotlin.idea.caches.resolve.ResolvePackage;
|
||||||
import org.jetbrains.kotlin.idea.refactoring.JetRefactoringBundle;
|
import org.jetbrains.kotlin.idea.refactoring.JetRefactoringBundle;
|
||||||
|
import org.jetbrains.kotlin.idea.test.KotlinCodeInsightTestCase;
|
||||||
|
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase;
|
||||||
import org.jetbrains.kotlin.psi.JetElement;
|
import org.jetbrains.kotlin.psi.JetElement;
|
||||||
import org.jetbrains.kotlin.psi.JetPsiFactory;
|
import org.jetbrains.kotlin.psi.JetPsiFactory;
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext;
|
import org.jetbrains.kotlin.resolve.BindingContext;
|
||||||
@@ -49,8 +48,6 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static org.jetbrains.kotlin.idea.refactoring.changeSignature.ChangeSignaturePackage.getChangeSignatureDialog;
|
|
||||||
|
|
||||||
public class JetChangeSignatureTest extends KotlinCodeInsightTestCase {
|
public class JetChangeSignatureTest extends KotlinCodeInsightTestCase {
|
||||||
public void testBadSelection() throws Exception {
|
public void testBadSelection() throws Exception {
|
||||||
configureByFile(getTestName(false) + "Before.kt");
|
configureByFile(getTestName(false) + "Before.kt");
|
||||||
@@ -955,13 +952,8 @@ public class JetChangeSignatureTest extends KotlinCodeInsightTestCase {
|
|||||||
FunctionDescriptor functionDescriptor = JetChangeSignatureHandler.findDescriptor(element, project, editor, bindingContext);
|
FunctionDescriptor functionDescriptor = JetChangeSignatureHandler.findDescriptor(element, project, editor, bindingContext);
|
||||||
assertNotNull(functionDescriptor);
|
assertNotNull(functionDescriptor);
|
||||||
|
|
||||||
JetChangeSignatureDialog dialog = getChangeSignatureDialog(
|
return ChangeSignaturePackage.createChangeInfo(
|
||||||
project, functionDescriptor, JetChangeSignatureHandler.getConfiguration(), bindingContext, context);
|
project, functionDescriptor, JetChangeSignatureHandler.getConfiguration(), bindingContext, context);
|
||||||
assertNotNull(dialog);
|
|
||||||
|
|
||||||
dialog.canRun();
|
|
||||||
Disposer.register(getTestRootDisposable(), dialog.getDisposable());
|
|
||||||
return dialog.evaluateChangeInfo();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class JavaRefactoringProvider {
|
private class JavaRefactoringProvider {
|
||||||
|
|||||||
Reference in New Issue
Block a user