Eliminate redundant descriptorToDeclaration call from factory for "Change signature" quick fixes

This commit is contained in:
Pavel V. Talanov
2013-10-15 17:22:50 +04:00
parent 8c95884ad2
commit f032b6e236
9 changed files with 93 additions and 18 deletions
@@ -44,12 +44,11 @@ public class AddFunctionParametersFix extends ChangeFunctionSignatureFix {
private final boolean hasTypeMismatches; private final boolean hasTypeMismatches;
public AddFunctionParametersFix( public AddFunctionParametersFix(
@NotNull PsiElement declaration,
@NotNull JetCallElement callElement, @NotNull JetCallElement callElement,
@NotNull FunctionDescriptor functionDescriptor, @NotNull FunctionDescriptor functionDescriptor,
boolean hasTypeMismatches boolean hasTypeMismatches
) { ) {
super(declaration, callElement, functionDescriptor); super(callElement, functionDescriptor);
this.callElement = callElement; this.callElement = callElement;
this.hasTypeMismatches = hasTypeMismatches; this.hasTypeMismatches = hasTypeMismatches;
} }
@@ -43,7 +43,7 @@ public class ChangeFunctionLiteralSignatureFix extends ChangeFunctionSignatureFi
@NotNull JetFunctionLiteral functionLiteral, @NotNull JetFunctionLiteral functionLiteral,
@NotNull FunctionDescriptor functionDescriptor, @NotNull FunctionDescriptor functionDescriptor,
@NotNull List<JetType> parameterTypes) { @NotNull List<JetType> parameterTypes) {
super(functionLiteral, functionLiteral, functionDescriptor); super(functionLiteral, functionDescriptor);
this.parameterTypes = parameterTypes; this.parameterTypes = parameterTypes;
} }
@@ -30,7 +30,6 @@ import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters1;
import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters2; import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters2;
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.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.checker.JetTypeChecker; import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
@@ -47,11 +46,10 @@ public abstract class ChangeFunctionSignatureFix extends JetIntentionAction<PsiE
protected final FunctionDescriptor functionDescriptor; protected final FunctionDescriptor functionDescriptor;
public ChangeFunctionSignatureFix( public ChangeFunctionSignatureFix(
@NotNull PsiElement element,
@NotNull PsiElement context, @NotNull PsiElement context,
@NotNull FunctionDescriptor functionDescriptor @NotNull FunctionDescriptor functionDescriptor
) { ) {
super(element); super(context);
this.context = context; this.context = context;
this.functionDescriptor = functionDescriptor; this.functionDescriptor = functionDescriptor;
} }
@@ -99,9 +97,7 @@ public abstract class ChangeFunctionSignatureFix extends JetIntentionAction<PsiE
BindingContext bindingContext BindingContext bindingContext
) { ) {
for (int i = 0; i < parameters.size(); i++) { for (int i = 0; i < parameters.size(); i++) {
assert i < assert i < arguments .size(); // number of parameters must not be greater than the number of arguments (it's called only for TOO_MANY_ARGUMENTS error)
arguments
.size(); // number of parameters must not be greater than the number of arguments (it's called only for TOO_MANY_ARGUMENTS error)
JetExpression argumentExpression = arguments.get(i).getArgumentExpression(); JetExpression argumentExpression = arguments.get(i).getArgumentExpression();
JetType argumentType = JetType argumentType =
argumentExpression != null ? bindingContext.get(BindingContext.EXPRESSION_TYPE, argumentExpression) : null; argumentExpression != null ? bindingContext.get(BindingContext.EXPRESSION_TYPE, argumentExpression) : null;
@@ -192,13 +188,8 @@ public abstract class ChangeFunctionSignatureFix extends JetIntentionAction<PsiE
} }
BindingContext bindingContext = BindingContext bindingContext =
AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) context.getContainingFile()).getBindingContext(); AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) context.getContainingFile()).getBindingContext();
PsiElement declaration = BindingContextUtils.descriptorToDeclaration(bindingContext, functionDescriptor);
if (declaration == null) {
return null;
}
if (descriptor instanceof ValueParameterDescriptor) { if (descriptor instanceof ValueParameterDescriptor) {
return new RemoveFunctionParametersFix(declaration, context, functionDescriptor, (ValueParameterDescriptor) descriptor); return new RemoveFunctionParametersFix(context, functionDescriptor, (ValueParameterDescriptor) descriptor);
} }
else { else {
List<ValueParameterDescriptor> parameters = functionDescriptor.getValueParameters(); List<ValueParameterDescriptor> parameters = functionDescriptor.getValueParameters();
@@ -206,7 +197,7 @@ public abstract class ChangeFunctionSignatureFix extends JetIntentionAction<PsiE
if (arguments.size() > parameters.size()) { if (arguments.size() > parameters.size()) {
boolean hasTypeMismatches = hasTypeMismatches(parameters, arguments, bindingContext); boolean hasTypeMismatches = hasTypeMismatches(parameters, arguments, bindingContext);
return new AddFunctionParametersFix(declaration, callElement, functionDescriptor, hasTypeMismatches); return new AddFunctionParametersFix(callElement, functionDescriptor, hasTypeMismatches);
} }
} }
@@ -37,12 +37,11 @@ public class RemoveFunctionParametersFix extends ChangeFunctionSignatureFix {
private final ValueParameterDescriptor parameterToRemove; private final ValueParameterDescriptor parameterToRemove;
public RemoveFunctionParametersFix( public RemoveFunctionParametersFix(
@NotNull PsiElement element,
@NotNull PsiElement context, @NotNull PsiElement context,
@NotNull FunctionDescriptor functionDescriptor, @NotNull FunctionDescriptor functionDescriptor,
@NotNull ValueParameterDescriptor parameterToRemove @NotNull ValueParameterDescriptor parameterToRemove
) { ) {
super(element, context, functionDescriptor); super(context, functionDescriptor);
this.parameterToRemove = parameterToRemove; this.parameterToRemove = parameterToRemove;
} }
@@ -0,0 +1,19 @@
// "Add parameter to function 'f'" "true"
trait Z {
fun f(i: Int)
}
trait ZZ {
fun f(i: Int)
}
trait ZZZ: Z, ZZ {
}
trait ZZZZ : ZZZ {
override fun f(i: Int)
}
fun usage(z: ZZZ) {
z.f(3)
}
@@ -0,0 +1,19 @@
// "Remove parameter 'i'" "true"
trait Z {
fun f()
}
trait ZZ {
fun f()
}
trait ZZZ: Z, ZZ {
}
trait ZZZZ : ZZZ {
override fun f()
}
fun usage(z: ZZZ) {
z.f()
}
@@ -0,0 +1,19 @@
// "Add parameter to function 'f'" "true"
trait Z {
fun f()
}
trait ZZ {
fun f()
}
trait ZZZ: Z, ZZ {
}
trait ZZZZ : ZZZ {
override fun f()
}
fun usage(z: ZZZ) {
z.f(<caret>3)
}
@@ -0,0 +1,19 @@
// "Remove parameter 'i'" "true"
trait Z {
fun f(i: Int)
}
trait ZZ {
fun f(i: Int)
}
trait ZZZ: Z, ZZ {
}
trait ZZZZ : ZZZ {
override fun f(i: Int)
}
fun usage(z: ZZZ) {
z.f(<caret>)
}
@@ -375,6 +375,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest("idea/testData/quickfix/changeSignature/beforeAddFunctionParameterAndChangeTypes.kt"); doTest("idea/testData/quickfix/changeSignature/beforeAddFunctionParameterAndChangeTypes.kt");
} }
@TestMetadata("beforeAddParameterToFakeOverride.kt")
public void testAddParameterToFakeOverride() throws Exception {
doTest("idea/testData/quickfix/changeSignature/beforeAddParameterToFakeOverride.kt");
}
public void testAllFilesPresentInChangeSignature() throws Exception { public void testAllFilesPresentInChangeSignature() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/changeSignature"), Pattern.compile("^before(\\w+)\\.kt$"), true); JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/changeSignature"), Pattern.compile("^before(\\w+)\\.kt$"), true);
} }
@@ -434,6 +439,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest("idea/testData/quickfix/changeSignature/beforeRemoveNamedParameter.kt"); doTest("idea/testData/quickfix/changeSignature/beforeRemoveNamedParameter.kt");
} }
@TestMetadata("beforeRemoveParameterFromFakeOverride.kt")
public void testRemoveParameterFromFakeOverride() throws Exception {
doTest("idea/testData/quickfix/changeSignature/beforeRemoveParameterFromFakeOverride.kt");
}
@TestMetadata("beforeRemoveUnusedParameter.kt") @TestMetadata("beforeRemoveUnusedParameter.kt")
public void testRemoveUnusedParameter() throws Exception { public void testRemoveUnusedParameter() throws Exception {
doTest("idea/testData/quickfix/changeSignature/beforeRemoveUnusedParameter.kt"); doTest("idea/testData/quickfix/changeSignature/beforeRemoveUnusedParameter.kt");