From c7c51a3d6bc873bd3351562cbcfc395211359fe4 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Wed, 12 Dec 2018 17:30:53 +0300 Subject: [PATCH] Refactoring: drop MoveDeclarationsOutHelper object --- .../surroundWith/MoveDeclarationsOutHelper.kt | 176 +++++++++--------- .../KotlinFunctionLiteralSurrounder.java | 4 +- .../statement/KotlinIfSurrounderBase.java | 4 +- .../statement/KotlinTrySurrounderBase.java | 4 +- 4 files changed, 93 insertions(+), 95 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/MoveDeclarationsOutHelper.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/MoveDeclarationsOutHelper.kt index ee23d664826..74c8f9e35d5 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/MoveDeclarationsOutHelper.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/MoveDeclarationsOutHelper.kt @@ -32,104 +32,102 @@ import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.isError import java.util.* -object MoveDeclarationsOutHelper { - fun move(container: PsiElement, statements: Array, generateDefaultInitializers: Boolean): Array { - if (statements.isEmpty()) { - return statements - } +fun move(container: PsiElement, statements: Array, generateDefaultInitializers: Boolean): Array { + if (statements.isEmpty()) { + return statements + } - val project = container.project + val project = container.project - val resultStatements = ArrayList() - val propertiesDeclarations = ArrayList() + val resultStatements = ArrayList() + val propertiesDeclarations = ArrayList() - // Dummy element to add new declarations at the beginning - val psiFactory = KtPsiFactory(project) - val dummyFirstStatement = container.addBefore(psiFactory.createExpression("dummyStatement"), statements[0]) + // Dummy element to add new declarations at the beginning + val psiFactory = KtPsiFactory(project) + val dummyFirstStatement = container.addBefore(psiFactory.createExpression("dummyStatement"), statements[0]) - try { - val scope = LocalSearchScope(container) - val lastStatementOffset = statements[statements.size - 1].textRange.endOffset + try { + val scope = LocalSearchScope(container) + val lastStatementOffset = statements[statements.size - 1].textRange.endOffset - for (statement in statements) { - if (needToDeclareOut(statement, lastStatementOffset, scope)) { - val property = statement as? KtProperty - if (property?.initializer != null) { - var declaration = createVariableDeclaration(property, generateDefaultInitializers) - declaration = container.addBefore(declaration, dummyFirstStatement) as KtProperty - propertiesDeclarations.add(declaration) - container.addAfter(psiFactory.createNewLine(), declaration) + for (statement in statements) { + if (needToDeclareOut(statement, lastStatementOffset, scope)) { + val property = statement as? KtProperty + if (property?.initializer != null) { + var declaration = createVariableDeclaration(property, generateDefaultInitializers) + declaration = container.addBefore(declaration, dummyFirstStatement) as KtProperty + propertiesDeclarations.add(declaration) + container.addAfter(psiFactory.createNewLine(), declaration) - val assignment = createVariableAssignment(property) - resultStatements.add(property.replace(assignment)) - } else { - val newStatement = container.addBefore(statement, dummyFirstStatement) - container.addAfter(psiFactory.createNewLine(), newStatement) - container.deleteChildRange(statement, statement) - } + val assignment = createVariableAssignment(property) + resultStatements.add(property.replace(assignment)) } else { - resultStatements.add(statement) - } - } - } finally { - dummyFirstStatement.delete() - } - - ShortenReferences.DEFAULT.process(propertiesDeclarations) - - return PsiUtilCore.toPsiElementArray(resultStatements) - } - - private fun createVariableAssignment(property: KtProperty): KtBinaryExpression { - val propertyName = property.name ?: error("Property should have a name " + property.text) - val assignment = KtPsiFactory(property).createExpression("$propertyName = x") as KtBinaryExpression - val right = assignment.right ?: error("Created binary expression should have a right part " + assignment.text) - val initializer = property.initializer ?: error("Initializer should exist for property " + property.text) - right.replace(initializer) - return assignment - } - - private fun createVariableDeclaration(property: KtProperty, generateDefaultInitializers: Boolean): KtProperty { - val propertyType = getPropertyType(property) - var defaultInitializer: String? = null - if (generateDefaultInitializers && property.isVar) { - defaultInitializer = CodeInsightUtils.defaultInitializer(propertyType) - } - return createProperty(property, propertyType, defaultInitializer) - } - - private fun getPropertyType(property: KtProperty): KotlinType { - val variableDescriptor = property.resolveToDescriptorIfAny(BodyResolveMode.PARTIAL) - ?: error("Couldn't resolve property to property descriptor " + property.text) - return variableDescriptor.type - } - - private fun createProperty(property: KtProperty, propertyType: KotlinType, initializer: String?): KtProperty { - val typeRef = property.typeReference - var typeString: String? = null - if (typeRef != null) { - typeString = typeRef.text - } else if (!propertyType.isError) { - typeString = IdeDescriptorRenderers.SOURCE_CODE.renderType(propertyType) - } - - return KtPsiFactory(property).createProperty(property.name!!, typeString, property.isVar, initializer) - } - - private fun needToDeclareOut(element: PsiElement, lastStatementOffset: Int, scope: SearchScope): Boolean { - if (element is KtProperty || - element is KtClassOrObject || - element is KtFunction - ) { - val refs = ReferencesSearch.search(element, scope, false).toArray(PsiReference.EMPTY_ARRAY) - if (refs.isNotEmpty()) { - val lastRef = refs[refs.size - 1] - if (lastRef.element.textOffset > lastStatementOffset) { - return true + val newStatement = container.addBefore(statement, dummyFirstStatement) + container.addAfter(psiFactory.createNewLine(), newStatement) + container.deleteChildRange(statement, statement) } + } else { + resultStatements.add(statement) } } - - return false + } finally { + dummyFirstStatement.delete() } + + ShortenReferences.DEFAULT.process(propertiesDeclarations) + + return PsiUtilCore.toPsiElementArray(resultStatements) +} + +private fun createVariableAssignment(property: KtProperty): KtBinaryExpression { + val propertyName = property.name ?: error("Property should have a name " + property.text) + val assignment = KtPsiFactory(property).createExpression("$propertyName = x") as KtBinaryExpression + val right = assignment.right ?: error("Created binary expression should have a right part " + assignment.text) + val initializer = property.initializer ?: error("Initializer should exist for property " + property.text) + right.replace(initializer) + return assignment +} + +private fun createVariableDeclaration(property: KtProperty, generateDefaultInitializers: Boolean): KtProperty { + val propertyType = getPropertyType(property) + var defaultInitializer: String? = null + if (generateDefaultInitializers && property.isVar) { + defaultInitializer = CodeInsightUtils.defaultInitializer(propertyType) + } + return createProperty(property, propertyType, defaultInitializer) +} + +private fun getPropertyType(property: KtProperty): KotlinType { + val variableDescriptor = property.resolveToDescriptorIfAny(BodyResolveMode.PARTIAL) + ?: error("Couldn't resolve property to property descriptor " + property.text) + return variableDescriptor.type +} + +private fun createProperty(property: KtProperty, propertyType: KotlinType, initializer: String?): KtProperty { + val typeRef = property.typeReference + var typeString: String? = null + if (typeRef != null) { + typeString = typeRef.text + } else if (!propertyType.isError) { + typeString = IdeDescriptorRenderers.SOURCE_CODE.renderType(propertyType) + } + + return KtPsiFactory(property).createProperty(property.name!!, typeString, property.isVar, initializer) +} + +private fun needToDeclareOut(element: PsiElement, lastStatementOffset: Int, scope: SearchScope): Boolean { + if (element is KtProperty || + element is KtClassOrObject || + element is KtFunction + ) { + val refs = ReferencesSearch.search(element, scope, false).toArray(PsiReference.EMPTY_ARRAY) + if (refs.isNotEmpty()) { + val lastRef = refs[refs.size - 1] + if (lastRef.element.textOffset > lastStatementOffset) { + return true + } + } + } + + return false } diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinFunctionLiteralSurrounder.java b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinFunctionLiteralSurrounder.java index 4629c6858a3..f42d4218b9b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinFunctionLiteralSurrounder.java +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinFunctionLiteralSurrounder.java @@ -24,14 +24,14 @@ import com.intellij.psi.PsiElement; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.idea.KotlinBundle; import org.jetbrains.kotlin.idea.codeInsight.surroundWith.KotlinSurrounderUtils; -import org.jetbrains.kotlin.idea.codeInsight.surroundWith.MoveDeclarationsOutHelper; +import org.jetbrains.kotlin.idea.codeInsight.surroundWith.MoveDeclarationsOutHelperKt; import org.jetbrains.kotlin.psi.*; public class KotlinFunctionLiteralSurrounder extends KotlinStatementsSurrounder { @Nullable @Override protected TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) { - statements = MoveDeclarationsOutHelper.INSTANCE.move(container, statements, true); + statements = MoveDeclarationsOutHelperKt.move(container, statements, true); if (statements.length == 0) { KotlinSurrounderUtils.showErrorHint(project, editor, KotlinSurrounderUtils.SURROUND_WITH_ERROR); diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinIfSurrounderBase.java b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinIfSurrounderBase.java index c6ae6d716f4..8f3cf46d838 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinIfSurrounderBase.java +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinIfSurrounderBase.java @@ -25,7 +25,7 @@ import com.intellij.psi.PsiElement; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.idea.codeInsight.surroundWith.KotlinSurrounderUtils; -import org.jetbrains.kotlin.idea.codeInsight.surroundWith.MoveDeclarationsOutHelper; +import org.jetbrains.kotlin.idea.codeInsight.surroundWith.MoveDeclarationsOutHelperKt; import org.jetbrains.kotlin.psi.KtBlockExpression; import org.jetbrains.kotlin.psi.KtExpression; import org.jetbrains.kotlin.psi.KtIfExpression; @@ -41,7 +41,7 @@ public abstract class KotlinIfSurrounderBase extends KotlinStatementsSurrounder @Nullable @Override protected TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) { - statements = MoveDeclarationsOutHelper.INSTANCE.move(container, statements, isGenerateDefaultInitializers()); + statements = MoveDeclarationsOutHelperKt.move(container, statements, isGenerateDefaultInitializers()); if (statements.length == 0) { KotlinSurrounderUtils.showErrorHint(project, editor, KotlinSurrounderUtils.SURROUND_WITH_ERROR); diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinTrySurrounderBase.java b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinTrySurrounderBase.java index d6ac0936551..160f39de9b6 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinTrySurrounderBase.java +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinTrySurrounderBase.java @@ -24,7 +24,7 @@ import com.intellij.psi.PsiElement; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.idea.codeInsight.surroundWith.KotlinSurrounderUtils; -import org.jetbrains.kotlin.idea.codeInsight.surroundWith.MoveDeclarationsOutHelper; +import org.jetbrains.kotlin.idea.codeInsight.surroundWith.MoveDeclarationsOutHelperKt; import org.jetbrains.kotlin.psi.*; public abstract class KotlinTrySurrounderBase extends KotlinStatementsSurrounder { @@ -37,7 +37,7 @@ public abstract class KotlinTrySurrounderBase extends KotlinStatementsSurrounder @Nullable @Override protected TextRange surroundStatements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement container, @NotNull PsiElement[] statements) { - statements = MoveDeclarationsOutHelper.INSTANCE.move(container, statements, true); + statements = MoveDeclarationsOutHelperKt.move(container, statements, true); if (statements.length == 0) { KotlinSurrounderUtils.showErrorHint(project, editor, KotlinSurrounderUtils.SURROUND_WITH_ERROR);