Minor: refactoring ConvertMemberToExtensionIntention

This commit is contained in:
Dmitry Gridin
2019-03-20 22:42:38 +07:00
parent e11072b8c2
commit e8eb9c3d58
@@ -37,14 +37,19 @@ import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.getReturnTypeReference import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.getReturnTypeReference
import org.jetbrains.kotlin.idea.refactoring.withExpectedActuals import org.jetbrains.kotlin.idea.refactoring.withExpectedActuals
import org.jetbrains.kotlin.idea.references.KtReference import org.jetbrains.kotlin.idea.references.KtReference
import org.jetbrains.kotlin.idea.util.* import org.jetbrains.kotlin.idea.util.ImportInsertHelper
import org.jetbrains.kotlin.idea.util.actualsForExpected
import org.jetbrains.kotlin.idea.util.application.runWriteAction import org.jetbrains.kotlin.idea.util.application.runWriteAction
import org.jetbrains.kotlin.idea.util.isExpectDeclaration
import org.jetbrains.kotlin.idea.util.liftToExpected
import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.* import org.jetbrains.kotlin.psi.psiUtil.*
import org.jetbrains.kotlin.utils.addIfNotNull import org.jetbrains.kotlin.utils.addIfNotNull
class ConvertMemberToExtensionIntention : SelfTargetingRangeIntention<KtCallableDeclaration>(KtCallableDeclaration::class.java, "Convert member to extension"), LowPriorityAction { class ConvertMemberToExtensionIntention :
SelfTargetingRangeIntention<KtCallableDeclaration>(KtCallableDeclaration::class.java, "Convert member to extension"),
LowPriorityAction {
private fun isApplicable(element: KtCallableDeclaration): Boolean { private fun isApplicable(element: KtCallableDeclaration): Boolean {
val classBody = element.parent as? KtClassBody ?: return false val classBody = element.parent as? KtClassBody ?: return false
if (classBody.parent !is KtClass) return false if (classBody.parent !is KtClass) return false
@@ -87,14 +92,13 @@ class ConvertMemberToExtensionIntention : SelfTargetingRangeIntention<KtCallable
val parent = bodyToSelect.parent val parent = bodyToSelect.parent
val lastSibling = val lastSibling =
if (parent is KtBlockExpression) if (parent is KtBlockExpression)
parent.rBrace?.siblings(forward = false, withItself = false)?.first { it !is PsiWhiteSpace } parent.rBrace?.siblings(forward = false, withItself = false)?.first { it !is PsiWhiteSpace }
else else
bodyToSelect.siblings(forward = true, withItself = false).lastOrNull() bodyToSelect.siblings(forward = true, withItself = false).lastOrNull()
val endOffset = lastSibling?.endOffset ?: range.endOffset val endOffset = lastSibling?.endOffset ?: range.endOffset
selectionModel.setSelection(range.startOffset, endOffset) selectionModel.setSelection(range.startOffset, endOffset)
} } else {
else {
moveCaret(extension.textOffset, ScrollType.CENTER) moveCaret(extension.textOffset, ScrollType.CENTER)
} }
} }
@@ -102,8 +106,8 @@ class ConvertMemberToExtensionIntention : SelfTargetingRangeIntention<KtCallable
} }
private fun processSingleDeclaration( private fun processSingleDeclaration(
element: KtCallableDeclaration, element: KtCallableDeclaration,
allowExpected: Boolean allowExpected: Boolean
): Pair<KtCallableDeclaration, KtExpression?> { ): Pair<KtCallableDeclaration, KtExpression?> {
val descriptor = element.unsafeResolveToDescriptor() val descriptor = element.unsafeResolveToDescriptor()
val containingClass = descriptor.containingDeclaration as ClassDescriptor val containingClass = descriptor.containingDeclaration as ClassDescriptor
@@ -142,8 +146,7 @@ class ConvertMemberToExtensionIntention : SelfTargetingRangeIntention<KtCallable
if (typeParameterList != null) { if (typeParameterList != null) {
if (extension.typeParameterList != null) { if (extension.typeParameterList != null) {
extension.typeParameterList!!.replace(typeParameterList) extension.typeParameterList!!.replace(typeParameterList)
} } else {
else {
extension.addBefore(typeParameterList, extension.receiverTypeReference) extension.addBefore(typeParameterList, extension.receiverTypeReference)
extension.addBefore(psiFactory.createWhiteSpace(), extension.receiverTypeReference) extension.addBefore(psiFactory.createWhiteSpace(), extension.receiverTypeReference)
} }
@@ -168,11 +171,11 @@ class ConvertMemberToExtensionIntention : SelfTargetingRangeIntention<KtCallable
} }
val bodyText = getFunctionBodyTextFromTemplate( val bodyText = getFunctionBodyTextFromTemplate(
project, project,
if (extension is KtFunction) TemplateKind.FUNCTION else TemplateKind.PROPERTY_INITIALIZER, if (extension is KtFunction) TemplateKind.FUNCTION else TemplateKind.PROPERTY_INITIALIZER,
extension.name, extension.name,
extension.getReturnTypeReference()?.text ?: "Unit", extension.getReturnTypeReference()?.text ?: "Unit",
extension.containingClassOrObject?.fqName extension.containingClassOrObject?.fqName
) )
when (extension) { when (extension) {
@@ -196,8 +199,7 @@ class ConvertMemberToExtensionIntention : SelfTargetingRangeIntention<KtCallable
getter = extension.addAfter(templateGetter, extension.typeReference) as KtPropertyAccessor getter = extension.addAfter(templateGetter, extension.typeReference) as KtPropertyAccessor
extension.addBefore(psiFactory.createNewLine(), getter) extension.addBefore(psiFactory.createNewLine(), getter)
selectBody(getter) selectBody(getter)
} } else if (!getter.hasBody()) {
else if (!getter.hasBody()) {
getter = getter.replace(templateGetter) as KtPropertyAccessor getter = getter.replace(templateGetter) as KtPropertyAccessor
selectBody(getter) selectBody(getter)
} }
@@ -208,8 +210,7 @@ class ConvertMemberToExtensionIntention : SelfTargetingRangeIntention<KtCallable
setter = extension.addAfter(templateSetter, getter) as KtPropertyAccessor setter = extension.addAfter(templateSetter, getter) as KtPropertyAccessor
extension.addBefore(psiFactory.createNewLine(), setter) extension.addBefore(psiFactory.createNewLine(), setter)
selectBody(setter) selectBody(setter)
} } else if (!setter.hasBody()) {
else if (!setter.hasBody()) {
setter = setter.replace(templateSetter) as KtPropertyAccessor setter = setter.replace(templateSetter) as KtPropertyAccessor
selectBody(setter) selectBody(setter)
} }
@@ -248,15 +249,15 @@ class ConvertMemberToExtensionIntention : SelfTargetingRangeIntention<KtCallable
} }
return Messages.showYesNoDialog( return Messages.showYesNoDialog(
"Do you want to make new extension an expected declaration?", "Do you want to make new extension an expected declaration?",
text, text,
Messages.getQuestionIcon() Messages.getQuestionIcon()
) == Messages.YES ) == Messages.YES
} }
private fun createExtensionCallableAndPrepareBodyToSelect( private fun createExtensionCallableAndPrepareBodyToSelect(
element: KtCallableDeclaration, element: KtCallableDeclaration,
allowExpected: Boolean = true allowExpected: Boolean = true
): Pair<KtCallableDeclaration, KtExpression?> { ): Pair<KtCallableDeclaration, KtExpression?> {
val expectedDeclaration = element.liftToExpected() as? KtCallableDeclaration val expectedDeclaration = element.liftToExpected() as? KtCallableDeclaration
if (expectedDeclaration != null) { if (expectedDeclaration != null) {