Show error hint when trying to refactor synthesized function

This commit is contained in:
Pavel V. Talanov
2013-10-15 18:04:53 +04:00
parent f032b6e236
commit 66cc9b2cc7
8 changed files with 54 additions and 8 deletions
@@ -21,10 +21,7 @@ import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.psi.util.PsiTreeUtil; 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.jet.lang.descriptors.CallableDescriptor; import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.diagnostics.Diagnostic; import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters1; import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters1;
import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters2; import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters2;
@@ -41,6 +38,8 @@ import org.jetbrains.jet.plugin.refactoring.changeSignature.JetParameterInfo;
import java.util.List; import java.util.List;
import static org.jetbrains.jet.lang.descriptors.CallableMemberDescriptor.Kind.SYNTHESIZED;
public abstract class ChangeFunctionSignatureFix extends JetIntentionAction<PsiElement> { public abstract class ChangeFunctionSignatureFix extends JetIntentionAction<PsiElement> {
protected final PsiElement context; protected final PsiElement context;
protected final FunctionDescriptor functionDescriptor; protected final FunctionDescriptor functionDescriptor;
@@ -186,6 +185,11 @@ public abstract class ChangeFunctionSignatureFix extends JetIntentionAction<PsiE
if (functionDescriptor == null) { if (functionDescriptor == null) {
return null; return null;
} }
if (functionDescriptor.getKind() == SYNTHESIZED) {
return null;
}
BindingContext bindingContext = BindingContext bindingContext =
AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) context.getContainingFile()).getBindingContext(); AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) context.getContainingFile()).getBindingContext();
if (descriptor instanceof ValueParameterDescriptor) { if (descriptor instanceof ValueParameterDescriptor) {
@@ -9,6 +9,7 @@ cannot.refactor.namespace.expression=Cannot intoduce namespace reference
cannot.extract.method=Cannot find statements to extract cannot.extract.method=Cannot find statements to extract
cannot.find.class.to.extract=Cannot find class to extract method cannot.find.class.to.extract=Cannot find class to extract method
cannot.refactor.expression.should.have.inferred.type=Expression should have inferred type cannot.refactor.expression.should.have.inferred.type=Expression should have inferred type
cannot.refactor.synthesized.function=Cannot refactor synthesized function ''{0}''
error.wrong.caret.position.function.or.constructor.name=The caret should be positioned at the name of the function or constructor to be refactored. error.wrong.caret.position.function.or.constructor.name=The caret should be positioned at the name of the function or constructor to be refactored.
error.cant.refactor.vararg.functions=Can't refactor the function with variable arguments error.cant.refactor.vararg.functions=Can't refactor the function with variable arguments
@@ -32,10 +32,7 @@ import com.intellij.refactoring.util.CommonRefactoringUtil;
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.annotations.TestOnly;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
@@ -182,6 +179,12 @@ public class JetChangeSignatureHandler implements ChangeSignatureHandler {
return null; return null;
} }
} }
if (((FunctionDescriptor) descriptor).getKind() == SYNTHESIZED) {
String message = JetRefactoringBundle.message("cannot.refactor.synthesized.function", descriptor.getName());
CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.CHANGE_SIGNATURE);
return null;
}
return (FunctionDescriptor) descriptor; return (FunctionDescriptor) descriptor;
} }
@@ -0,0 +1,8 @@
// "Add parameter to function 'component1'" "false"
//ERROR: Too many arguments for internal final fun component1(): jet.Int defined in Data
data class Data(val i: Int) {}
fun usage(d: Data) {
d.component1(2)
}
@@ -0,0 +1,8 @@
// "Add parameter to function 'component1'" "false"
//ERROR: Too many arguments for internal final fun component1(): jet.Int defined in Data
data class Data(val i: Int) {}
fun usage(d: Data) {
d.component1(<caret>2)
}
@@ -0,0 +1,5 @@
data class Data(val i: Int) {}
fun usage(d: Data) {
d.<caret>component1()
}
@@ -414,6 +414,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest("idea/testData/quickfix/changeSignature/beforeLinearHierarchy.kt"); doTest("idea/testData/quickfix/changeSignature/beforeLinearHierarchy.kt");
} }
@TestMetadata("beforeNotAvailableForSynthesized.kt")
public void testNotAvailableForSynthesized() throws Exception {
doTest("idea/testData/quickfix/changeSignature/beforeNotAvailableForSynthesized.kt");
}
@TestMetadata("beforeRemoveConstructorParameter.kt") @TestMetadata("beforeRemoveConstructorParameter.kt")
public void testRemoveConstructorParameter() throws Exception { public void testRemoveConstructorParameter() throws Exception {
doTest("idea/testData/quickfix/changeSignature/beforeRemoveConstructorParameter.kt"); doTest("idea/testData/quickfix/changeSignature/beforeRemoveConstructorParameter.kt");
@@ -34,6 +34,7 @@ import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.plugin.PluginTestCaseBase; import org.jetbrains.jet.plugin.PluginTestCaseBase;
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
import org.jetbrains.jet.plugin.refactoring.JetRefactoringBundle;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
@@ -80,6 +81,17 @@ public class JetChangeSignatureTest extends LightCodeInsightTestCase {
doTest(changeInfo); doTest(changeInfo);
} }
public void testSynthesized() throws Exception {
try {
getChangeInfo();
}
catch (CommonRefactoringUtil.RefactoringErrorHintException e) {
assertEquals(JetRefactoringBundle.message("cannot.refactor.synthesized.function", "component1"), e.getMessage());
return;
}
fail();
}
public void testPreferContainedInClass() throws Exception { public void testPreferContainedInClass() throws Exception {
JetChangeInfo changeInfo = getChangeInfo(); JetChangeInfo changeInfo = getChangeInfo();
assertEquals("param", changeInfo.getNewParameters()[0].getName()); assertEquals("param", changeInfo.getNewParameters()[0].getName());