DeprecatedSymbolUsageFix to not be available for functions with optional parameters and no sources

This commit is contained in:
Valentin Kipyatkov
2015-05-28 20:52:48 +03:00
parent 6fbd530eee
commit 0e69162628
3 changed files with 9 additions and 4 deletions
@@ -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)
}
@@ -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<CompileTimeConstant<String>>)?.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())
}
@@ -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")
}