Reformat: DeprecatedSymbolUsageFixBase

This commit is contained in:
Mikhail Glukhikh
2018-05-22 17:56:33 +03:00
parent 7cb4c91277
commit 0655e153c3
@@ -67,14 +67,13 @@ abstract class DeprecatedSymbolUsageFixBase(
final override fun invoke(project: Project, editor: Editor?, file: KtFile) { final override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val element = element ?: return val element = element ?: return
val strategy = buildUsageReplacementStrategy(element, replaceWith, recheckAnnotation = false, reformat = true, editor = editor) ?: return val strategy = buildUsageReplacementStrategy(
element, replaceWith, recheckAnnotation = false, reformat = true, editor = editor
) ?: return
invoke(strategy, project, editor) invoke(strategy, project, editor)
} }
protected abstract fun invoke( protected abstract fun invoke(replacementStrategy: UsageReplacementStrategy, project: Project, editor: Editor?)
replacementStrategy: UsageReplacementStrategy,
project: Project,
editor: Editor?)
companion object { companion object {
fun fetchReplaceWithPattern(descriptor: DeclarationDescriptor, project: Project): ReplaceWith? { fun fetchReplaceWithPattern(descriptor: DeclarationDescriptor, project: Project): ReplaceWith? {
@@ -88,8 +87,10 @@ abstract class DeprecatedSymbolUsageFixBase(
val imports = importValues.map { (it as StringValue).value } val imports = importValues.map { (it as StringValue).value }
// should not be available for descriptors with optional parameters if we cannot fetch default values for them (currently for library with no sources) // 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 && if (descriptor is CallableDescriptor && descriptor.valueParameters.any {
descriptor.valueParameters.any { it.hasDefaultValue() && OptionalParametersHelper.defaultParameterValue(it, project) == null }) return null it.hasDefaultValue() && OptionalParametersHelper.defaultParameterValue(it, project) == null
}
) return null
return ReplaceWith(pattern, imports) return ReplaceWith(pattern, imports)
} }
@@ -147,7 +148,9 @@ abstract class DeprecatedSymbolUsageFixBase(
is CallableDescriptor -> { is CallableDescriptor -> {
val resolvedCall = element.getResolvedCall(bindingContext) ?: return null val resolvedCall = element.getResolvedCall(bindingContext) ?: return null
if (!resolvedCall.isReallySuccess()) return null if (!resolvedCall.isReallySuccess()) return null
val replacement = ReplaceWithAnnotationAnalyzer.analyzeCallableReplacement(replaceWith, target, resolutionFacade, reformat) ?: return null val replacement = ReplaceWithAnnotationAnalyzer.analyzeCallableReplacement(
replaceWith, target, resolutionFacade, reformat
) ?: return null
return CallableUsageReplacementStrategy(replacement, inlineSetter = false) return CallableUsageReplacementStrategy(replacement, inlineSetter = false)
} }
@@ -162,14 +165,18 @@ abstract class DeprecatedSymbolUsageFixBase(
?.getStrictParentOfType<KtTypeAlias>() ?.getStrictParentOfType<KtTypeAlias>()
if (typeAlias != null) { if (typeAlias != null) {
val usedConstructorWithOwnReplaceWith = usedConstructorsWithOwnReplaceWith( val usedConstructorWithOwnReplaceWith = usedConstructorsWithOwnReplaceWith(
element.project, target, typeAlias) element.project, target, typeAlias
)
if (usedConstructorWithOwnReplaceWith != null) { if (usedConstructorWithOwnReplaceWith != null) {
val constructorStr = DescriptorRenderer.ONLY_NAMES_WITH_SHORT_TYPES.render(usedConstructorWithOwnReplaceWith) val constructorStr = DescriptorRenderer.ONLY_NAMES_WITH_SHORT_TYPES.render(
usedConstructorWithOwnReplaceWith
)
HintManager.getInstance().showErrorHint( HintManager.getInstance().showErrorHint(
editor, editor,
"There is own 'ReplaceWith' on '$constructorStr' that is used through this alias. " + "There is own 'ReplaceWith' on '$constructorStr' that is used through this alias. " +
"Please replace usages first.") "Please replace usages first."
)
return null return null
} }
} }
@@ -180,7 +187,12 @@ abstract class DeprecatedSymbolUsageFixBase(
} }
target is ClassDescriptor -> { target is ClassDescriptor -> {
val constructor = target.unsubstitutedPrimaryConstructor ?: return null val constructor = target.unsubstitutedPrimaryConstructor ?: return null
val replacementExpression = ReplaceWithAnnotationAnalyzer.analyzeCallableReplacement(replaceWith, constructor, resolutionFacade, reformat) ?: return null val replacementExpression = ReplaceWithAnnotationAnalyzer.analyzeCallableReplacement(
replaceWith,
constructor,
resolutionFacade,
reformat
) ?: return null
ClassUsageReplacementStrategy(null, replacementExpression, element.project) ClassUsageReplacementStrategy(null, replacementExpression, element.project)
} }
else -> null else -> null
@@ -192,7 +204,8 @@ abstract class DeprecatedSymbolUsageFixBase(
} }
private fun usedConstructorsWithOwnReplaceWith( private fun usedConstructorsWithOwnReplaceWith(
project: Project, classifier: ClassifierDescriptorWithTypeParameters, typeAlias: PsiElement): ConstructorDescriptor? { project: Project, classifier: ClassifierDescriptorWithTypeParameters, typeAlias: PsiElement
): ConstructorDescriptor? {
val specialReplaceWithForConstructor = classifier.constructors.filter { val specialReplaceWithForConstructor = classifier.constructors.filter {
DeprecatedSymbolUsageFixBase.fetchReplaceWithPattern(it, project) != null DeprecatedSymbolUsageFixBase.fetchReplaceWithPattern(it, project) != null
}.toSet() }.toSet()