Move member to companion object: reformat

This commit is contained in:
Mikhail Glukhikh
2018-11-23 13:28:12 +03:00
parent 88cea0a88c
commit d59ea4c087
@@ -70,8 +70,10 @@ import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitReceiver
import org.jetbrains.kotlin.util.findCallableMemberBySignature
class MoveMemberToCompanionObjectIntention : SelfTargetingRangeIntention<KtNamedDeclaration>(KtNamedDeclaration::class.java,
"Move to companion object") {
class MoveMemberToCompanionObjectIntention : SelfTargetingRangeIntention<KtNamedDeclaration>(
KtNamedDeclaration::class.java,
"Move to companion object"
) {
override fun applicabilityRange(element: KtNamedDeclaration): TextRange? {
if (element !is KtNamedFunction && element !is KtProperty && element !is KtClassOrObject) return null
if (element is KtEnumEntry) return null
@@ -107,9 +109,9 @@ class MoveMemberToCompanionObjectIntention : SelfTargetingRangeIntention<KtNamed
}
private fun runTemplateForInstanceParam(
declaration: KtNamedDeclaration,
nameSuggestions: List<String>,
editor: Editor?
declaration: KtNamedDeclaration,
nameSuggestions: List<String>,
editor: Editor?
) {
if (nameSuggestions.isNotEmpty() && editor != null) {
val restoredElement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(declaration)
@@ -140,21 +142,24 @@ class MoveMemberToCompanionObjectIntention : SelfTargetingRangeIntention<KtNamed
is KtSimpleNameExpression -> {
val call = refElement.parent as? KtCallExpression ?: return
val psiFactory = KtPsiFactory(refElement)
val argumentList = call.valueArgumentList
?: call.addAfter(psiFactory.createCallArguments("()"), call.typeArgumentList ?: refElement) as KtValueArgumentList
val argumentList =
call.valueArgumentList
?: call.addAfter(psiFactory.createCallArguments("()"), call.typeArgumentList ?: refElement) as KtValueArgumentList
val receiver = call.getQualifiedExpressionForSelector()?.receiverExpression
val receiverArg = receiver?.let { psiFactory.createArgument(it) }
?: psiFactory.createArgument(psiFactory.createExpression("this@${classFqName.shortName().asString()}"))
?: psiFactory.createArgument(psiFactory.createExpression("this@${classFqName.shortName().asString()}"))
argumentList.addArgumentBefore(receiverArg, argumentList.arguments.firstOrNull())
}
}
}
private fun doMove(element: KtNamedDeclaration,
externalUsages: SmartList<UsageInfo>,
outerInstanceUsages: SmartList<UsageInfo>,
editor: Editor?) {
private fun doMove(
element: KtNamedDeclaration,
externalUsages: SmartList<UsageInfo>,
outerInstanceUsages: SmartList<UsageInfo>,
editor: Editor?
) {
val project = element.project
val containingClass = element.containingClassOrObject as KtClass
@@ -180,7 +185,9 @@ class MoveMemberToCompanionObjectIntention : SelfTargetingRangeIntention<KtNamed
nameSuggestions = getNameSuggestionsForOuterInstance(element)
val newParam = parameterList.addParameterBefore(
ktPsiFactory.createParameter("${nameSuggestions.first()}: ${IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_NO_ANNOTATIONS.renderType(newParamType)}"),
ktPsiFactory.createParameter(
"${nameSuggestions.first()}: ${IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_NO_ANNOTATIONS.renderType(newParamType)}"
),
parameters.firstOrNull()
)
@@ -196,8 +203,7 @@ class MoveMemberToCompanionObjectIntention : SelfTargetingRangeIntention<KtNamed
}
}
}
}
else {
} else {
nameSuggestions = emptyList()
}
@@ -231,14 +237,14 @@ class MoveMemberToCompanionObjectIntention : SelfTargetingRangeIntention<KtNamed
is ImplicitReceiverUsageInfo -> {
usage.callExpression
.let { it.replaced(ktPsiFactory.createExpressionByPattern("$0.$1", ktCompanionRef, it)) }
.let {
val qualifiedCall = it as KtQualifiedExpression
elementsToShorten += qualifiedCall.receiverExpression
if (hasInstanceArg) {
elementsToShorten += (qualifiedCall.selectorExpression as KtCallExpression).valueArguments.first()
}
.let { it.replaced(ktPsiFactory.createExpressionByPattern("$0.$1", ktCompanionRef, it)) }
.let {
val qualifiedCall = it as KtQualifiedExpression
elementsToShorten += qualifiedCall.receiverExpression
if (hasInstanceArg) {
elementsToShorten += (qualifiedCall.selectorExpression as KtCallExpression).valueArguments.first()
}
}
}
}
}
@@ -264,19 +270,22 @@ class MoveMemberToCompanionObjectIntention : SelfTargetingRangeIntention<KtNamed
val containingClass = element.containingClassOrObject as KtClass
if (element is KtClassOrObject) {
val nameSuggestions = if (traverseOuterInstanceReferences(element, true)) getNameSuggestionsForOuterInstance(element) else emptyList()
val nameSuggestions =
if (traverseOuterInstanceReferences(element, true)) getNameSuggestionsForOuterInstance(element) else emptyList()
val outerInstanceName = nameSuggestions.firstOrNull()
var movedClass: KtClassOrObject? = null
val mover = object: Mover {
val mover = object : Mover {
override fun invoke(originalElement: KtNamedDeclaration, targetContainer: KtElement): KtNamedDeclaration {
return Mover.Default(originalElement, targetContainer).apply { movedClass = this as KtClassOrObject }
}
}
val moveDescriptor = MoveDeclarationsDescriptor(project,
MoveSource(element),
KotlinMoveTargetForCompanion(containingClass),
MoveDeclarationsDelegate.NestedClass(null, outerInstanceName),
moveCallback = MoveCallback { runTemplateForInstanceParam(movedClass!!, nameSuggestions, editor) })
val moveDescriptor = MoveDeclarationsDescriptor(
project,
MoveSource(element),
KotlinMoveTargetForCompanion(containingClass),
MoveDeclarationsDelegate.NestedClass(null, outerInstanceName),
moveCallback = MoveCallback { runTemplateForInstanceParam(movedClass!!, nameSuggestions, editor) }
)
MoveKotlinDeclarationsProcessor(moveDescriptor, mover).run()
return
}
@@ -284,11 +293,15 @@ class MoveMemberToCompanionObjectIntention : SelfTargetingRangeIntention<KtNamed
val description = RefactoringUIUtil.getDescription(element, false).capitalize()
if (HierarchySearchRequest(element, element.useScope, false).searchOverriders().any()) {
return CommonRefactoringUtil.showErrorHint(project, editor, "$description is overridden by declaration(s) in a subclass", text, null)
return CommonRefactoringUtil.showErrorHint(
project, editor, "$description is overridden by declaration(s) in a subclass", text, null
)
}
if (hasTypeParameterReferences(containingClass, element)) {
return CommonRefactoringUtil.showErrorHint(project, editor, "$description references type parameters of the containing class", text, null)
return CommonRefactoringUtil.showErrorHint(
project, editor, "$description references type parameters of the containing class", text, null
)
}
val externalUsages = SmartList<UsageInfo>()
@@ -309,8 +322,7 @@ class MoveMemberToCompanionObjectIntention : SelfTargetingRangeIntention<KtNamed
if (outerInstanceReferences.isNotEmpty()) {
if (element is KtProperty) {
conflicts.putValue(element, "Usages of outer class instance inside of property '${element.name}' won't be processed")
}
else {
} else {
outerInstanceReferences.filterNotTo(outerInstanceUsages) { it.reportConflictIfAny(conflicts) }
}
}
@@ -329,16 +341,17 @@ class MoveMemberToCompanionObjectIntention : SelfTargetingRangeIntention<KtNamed
val extensionReceiver = resolvedCall.extensionReceiver
if (extensionReceiver != null && extensionReceiver !is ImplicitReceiver) {
conflicts.putValue(callExpression,
"Calls with explicit extension receiver won't be processed: ${callExpression.text}")
conflicts.putValue(
callExpression,
"Calls with explicit extension receiver won't be processed: ${callExpression.text}"
)
return@mapNotNullTo null
}
val dispatchReceiver = resolvedCall.dispatchReceiver ?: return@mapNotNullTo null
if (dispatchReceiver is ExpressionReceiver) {
ExplicitReceiverUsageInfo(refExpr, dispatchReceiver.expression)
}
else {
} else {
ImplicitReceiverUsageInfo(refExpr, callExpression)
}
}