Skip to content

Commit

Permalink
Don't ask user to choose method for renaming parameter when it is cha…
Browse files Browse the repository at this point in the history
…nged in overridden function.
  • Loading branch information
Evgeny Gerashchenko authored and Evgeny Gerashchenko committed May 22, 2015
1 parent 24c533d commit a5da64b
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 3 deletions.
Expand Up @@ -844,7 +844,7 @@ private void checkNameAndDefaultForFakeOverrideParameter(
}
}

private static boolean shouldReportParameterNameOverrideWarning(
public static boolean shouldReportParameterNameOverrideWarning(
@NotNull ValueParameterDescriptor parameterFromSubclass,
@NotNull ValueParameterDescriptor parameterFromSuperclass
) {
Expand Down
Expand Up @@ -172,6 +172,11 @@ public boolean performSilently(@NotNull Collection<? extends PsiElement> affecte
PsiElement onlyFunction = affectedFunctions.iterator().next();
return !hasTypeMismatches && !isConstructor() && !hasOtherUsages(onlyFunction);
}

@Override
public boolean forcePerformForSelectedFunctionOnly() {
return false;
}
};
}

Expand Down
Expand Up @@ -86,6 +86,11 @@ public Unit invoke(JetMutableMethodDescriptor descriptor) {
public boolean performSilently(@NotNull Collection<? extends PsiElement> elements) {
return false;
}

@Override
public boolean forcePerformForSelectedFunctionOnly() {
return false;
}
}, bindingContext, context, getText());
}
}
Expand Up @@ -79,6 +79,11 @@ public Unit invoke(JetMutableMethodDescriptor descriptor) {
public boolean performSilently(@NotNull Collection<? extends PsiElement> elements) {
return false;
}

@Override
public boolean forcePerformForSelectedFunctionOnly() {
return false;
}
}, bindingContext, context, getText());
}
}
Expand Up @@ -50,6 +50,7 @@ import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.psi.JetDeclarationWithBody
import org.jetbrains.kotlin.psi.JetExpression
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.idea.refactoring.changeSignature.JetChangeSignature
import org.jetbrains.kotlin.psi.JetDeclaration

public abstract class CallableRefactoring<T: CallableDescriptor>(
Expand All @@ -61,6 +62,10 @@ public abstract class CallableRefactoring<T: CallableDescriptor>(

private val kind = (callableDescriptor as? CallableMemberDescriptor)?.getKind() ?: CallableMemberDescriptor.Kind.DECLARATION

protected open fun forcePerformForSelectedFunctionOnly(): Boolean {
return false
}

private fun getClosestModifiableDescriptors(): Set<CallableDescriptor> {
return when (kind) {
DECLARATION -> {
Expand Down Expand Up @@ -138,6 +143,11 @@ public abstract class CallableRefactoring<T: CallableDescriptor>(
}

val closestModifiableDescriptors = getClosestModifiableDescriptors()
if (forcePerformForSelectedFunctionOnly()) {
performRefactoring(closestModifiableDescriptors)
return true
}

assert(!closestModifiableDescriptors.isEmpty(), "Should contain original declaration or some of its super declarations")
val deepestSuperDeclarations =
(callableDescriptor as? CallableMemberDescriptor)?.let { OverrideResolver.getDeepestSuperDeclarations(it) }
Expand Down
Expand Up @@ -37,6 +37,10 @@ public trait JetChangeSignatureConfiguration {
fun performSilently(affectedFunctions: Collection<PsiElement>): Boolean {
return false
}

fun forcePerformForSelectedFunctionOnly(): Boolean {
return false
}
}

fun JetMethodDescriptor.modify(action: JetMutableMethodDescriptor.() -> Unit): JetMethodDescriptor {
Expand All @@ -63,6 +67,8 @@ public class JetChangeSignature(project: Project,

private val LOG = Logger.getInstance(javaClass<JetChangeSignature>())

override fun forcePerformForSelectedFunctionOnly() = configuration.forcePerformForSelectedFunctionOnly()

override fun performRefactoring(descriptorsForChange: Collection<CallableDescriptor>) {
assert (descriptorsForChange.all { it is FunctionDescriptor }) {
"Function descriptors expected: " + descriptorsForChange.joinToString(separator = "\n")
Expand Down
Expand Up @@ -154,6 +154,11 @@ public JetMethodDescriptor configure(@NotNull JetMethodDescriptor originalDescri
public boolean performSilently(@NotNull Collection<? extends PsiElement> elements) {
return false;
}

@Override
public boolean forcePerformForSelectedFunctionOnly() {
return false;
}
};
}

Expand Down
Expand Up @@ -29,6 +29,7 @@ import org.jetbrains.kotlin.psi.JetNamedFunction
import org.jetbrains.kotlin.psi.JetParameter
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.OverrideResolver

public class RenameKotlinParameterProcessor : RenameKotlinPsiProcessor() {
override fun canProcessElement(element: PsiElement): Boolean {
Expand All @@ -41,16 +42,23 @@ public class RenameKotlinParameterProcessor : RenameKotlinPsiProcessor() {
assert(paramIndex != -1, { "couldn't find parameter in parent ${element.getElementTextWithContext()}" })

val context = function.analyze()
val descriptor = context[BindingContext.DECLARATION_TO_DESCRIPTOR, function] as? FunctionDescriptor ?: return
val functionDescriptor = context[BindingContext.DECLARATION_TO_DESCRIPTOR, function] as? FunctionDescriptor ?: return
val parameterDescriptor = functionDescriptor.getValueParameters()[paramIndex]

val parameterNameChangedOnOverride = parameterDescriptor.getOverriddenDescriptors().any {
overriddenParameter -> OverrideResolver.shouldReportParameterNameOverrideWarning(parameterDescriptor, overriddenParameter)
}

val changeSignatureConfiguration = object : JetChangeSignatureConfiguration {
override fun configure(originalDescriptor: JetMethodDescriptor, bindingContext: BindingContext): JetMethodDescriptor {
return originalDescriptor.modify { renameParameter(paramIndex, newName) }
}

override fun performSilently(affectedFunctions: Collection<PsiElement>) = true

override fun forcePerformForSelectedFunctionOnly() = parameterNameChangedOnOverride
}

runChangeSignature(element.getProject(), descriptor, changeSignatureConfiguration, context, element, "Rename parameter")
runChangeSignature(element.getProject(), functionDescriptor, changeSignatureConfiguration, context, element, "Rename parameter")
}
}
Expand Up @@ -243,6 +243,11 @@ public JetMethodDescriptor configure(@NotNull JetMethodDescriptor originalDescri
public boolean performSilently(@NotNull Collection<? extends PsiElement> elements) {
return true;
}

@Override
public boolean forcePerformForSelectedFunctionOnly() {
return false;
}
};
BindingContext context = ResolvePackage.analyze(method, BodyResolveMode.FULL);

Expand Down

0 comments on commit a5da64b

Please sign in to comment.