From 0e691626284f7d4c9d3a5d9b9dd0bb60e83e9a92 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Thu, 28 May 2015 20:52:48 +0300 Subject: [PATCH] DeprecatedSymbolUsageFix to not be available for functions with optional parameters and no sources --- .../kotlin/idea/quickfix/DeprecatedSymbolUsageFix.kt | 2 +- .../kotlin/idea/quickfix/DeprecatedSymbolUsageFixBase.kt | 9 +++++++-- .../quickfix/DeprecatedSymbolUsageInWholeProjectFix.kt | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageFix.kt index a3fb9114bb5..7951e13622e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageFix.kt @@ -68,7 +68,7 @@ public class DeprecatedSymbolUsageFix( override fun createAction(diagnostic: Diagnostic): IntentionAction? { val nameExpression = diagnostic.getPsiElement() as? JetSimpleNameExpression ?: return null val descriptor = Errors.DEPRECATED_SYMBOL_WITH_MESSAGE.cast(diagnostic).getA() - val replacement = DeprecatedSymbolUsageFixBase.replaceWithPattern(descriptor) ?: return null + val replacement = DeprecatedSymbolUsageFixBase.replaceWithPattern(descriptor, nameExpression.getProject()) ?: return null return DeprecatedSymbolUsageFix(nameExpression, replacement) } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageFixBase.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageFixBase.kt index 127fa47c059..0116250b7e8 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageFixBase.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageFixBase.kt @@ -76,7 +76,7 @@ public abstract class DeprecatedSymbolUsageFixBase( val resolvedCall = element.getResolvedCall(element.analyze()) ?: return false if (!resolvedCall.getStatus().isSuccess()) return false val descriptor = resolvedCall.getResultingDescriptor() - if (replaceWithPattern(descriptor) != replaceWith) return false + if (replaceWithPattern(descriptor, project) != replaceWith) return false try { JetPsiFactory(project).createExpression(replaceWith.expression) @@ -105,7 +105,7 @@ public abstract class DeprecatedSymbolUsageFixBase( editor: Editor?) companion object { - public fun replaceWithPattern(descriptor: DeclarationDescriptor): ReplaceWith? { + public fun replaceWithPattern(descriptor: DeclarationDescriptor, project: Project): ReplaceWith? { val annotationClass = descriptor.builtIns.getDeprecatedAnnotation() val annotation = descriptor.getAnnotations().findAnnotation(DescriptorUtils.getFqNameSafe(annotationClass)) ?: return null //TODO: code duplication @@ -118,6 +118,11 @@ public abstract class DeprecatedSymbolUsageFixBase( if (pattern.isEmpty()) return null val argument = replaceWithValue.getAllValueArguments().entrySet().singleOrNull { it.key.getName().asString() == "imports"/*TODO*/ }?.value val imports = (argument?.getValue() as? List>)?.map { it.getValue() } ?: emptyList() + + // should not be available for descriptors with optional parameters if we cannot fetch default values for them (currently for library with no sources) + if (descriptor is CallableDescriptor && + descriptor.getValueParameters().any { it.hasDefaultValue() && OptionalParametersHelper.defaultParameterValue(it, project) == null }) return null + return ReplaceWith(pattern, *imports.toTypedArray()) } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageInWholeProjectFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageInWholeProjectFix.kt index e1329103e6c..905dff21510 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageInWholeProjectFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageInWholeProjectFix.kt @@ -164,7 +164,7 @@ public class DeprecatedSymbolUsageInWholeProjectFix( override fun createAction(diagnostic: Diagnostic): IntentionAction? { val nameExpression = diagnostic.getPsiElement() as? JetSimpleNameExpression ?: return null val descriptor = Errors.DEPRECATED_SYMBOL_WITH_MESSAGE.cast(diagnostic).getA() - val replacement = DeprecatedSymbolUsageFixBase.replaceWithPattern(descriptor) ?: return null + val replacement = DeprecatedSymbolUsageFixBase.replaceWithPattern(descriptor, nameExpression.getProject()) ?: return null val descriptorName = RENDERER.render(descriptor) return DeprecatedSymbolUsageInWholeProjectFix(nameExpression, replacement, "Replace usages of '$descriptorName' in whole project") }