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