Refactoring: drop MoveDeclarationsOutHelper object
This commit is contained in:
+87
-89
@@ -32,104 +32,102 @@ import org.jetbrains.kotlin.types.KotlinType
|
|||||||
import org.jetbrains.kotlin.types.isError
|
import org.jetbrains.kotlin.types.isError
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
object MoveDeclarationsOutHelper {
|
fun move(container: PsiElement, statements: Array<PsiElement>, generateDefaultInitializers: Boolean): Array<PsiElement> {
|
||||||
fun move(container: PsiElement, statements: Array<PsiElement>, generateDefaultInitializers: Boolean): Array<PsiElement> {
|
if (statements.isEmpty()) {
|
||||||
if (statements.isEmpty()) {
|
return statements
|
||||||
return statements
|
}
|
||||||
}
|
|
||||||
|
|
||||||
val project = container.project
|
val project = container.project
|
||||||
|
|
||||||
val resultStatements = ArrayList<PsiElement>()
|
val resultStatements = ArrayList<PsiElement>()
|
||||||
val propertiesDeclarations = ArrayList<KtProperty>()
|
val propertiesDeclarations = ArrayList<KtProperty>()
|
||||||
|
|
||||||
// Dummy element to add new declarations at the beginning
|
// Dummy element to add new declarations at the beginning
|
||||||
val psiFactory = KtPsiFactory(project)
|
val psiFactory = KtPsiFactory(project)
|
||||||
val dummyFirstStatement = container.addBefore(psiFactory.createExpression("dummyStatement"), statements[0])
|
val dummyFirstStatement = container.addBefore(psiFactory.createExpression("dummyStatement"), statements[0])
|
||||||
|
|
||||||
try {
|
try {
|
||||||
val scope = LocalSearchScope(container)
|
val scope = LocalSearchScope(container)
|
||||||
val lastStatementOffset = statements[statements.size - 1].textRange.endOffset
|
val lastStatementOffset = statements[statements.size - 1].textRange.endOffset
|
||||||
|
|
||||||
for (statement in statements) {
|
for (statement in statements) {
|
||||||
if (needToDeclareOut(statement, lastStatementOffset, scope)) {
|
if (needToDeclareOut(statement, lastStatementOffset, scope)) {
|
||||||
val property = statement as? KtProperty
|
val property = statement as? KtProperty
|
||||||
if (property?.initializer != null) {
|
if (property?.initializer != null) {
|
||||||
var declaration = createVariableDeclaration(property, generateDefaultInitializers)
|
var declaration = createVariableDeclaration(property, generateDefaultInitializers)
|
||||||
declaration = container.addBefore(declaration, dummyFirstStatement) as KtProperty
|
declaration = container.addBefore(declaration, dummyFirstStatement) as KtProperty
|
||||||
propertiesDeclarations.add(declaration)
|
propertiesDeclarations.add(declaration)
|
||||||
container.addAfter(psiFactory.createNewLine(), declaration)
|
container.addAfter(psiFactory.createNewLine(), declaration)
|
||||||
|
|
||||||
val assignment = createVariableAssignment(property)
|
val assignment = createVariableAssignment(property)
|
||||||
resultStatements.add(property.replace(assignment))
|
resultStatements.add(property.replace(assignment))
|
||||||
} else {
|
|
||||||
val newStatement = container.addBefore(statement, dummyFirstStatement)
|
|
||||||
container.addAfter(psiFactory.createNewLine(), newStatement)
|
|
||||||
container.deleteChildRange(statement, statement)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
resultStatements.add(statement)
|
val newStatement = container.addBefore(statement, dummyFirstStatement)
|
||||||
}
|
container.addAfter(psiFactory.createNewLine(), newStatement)
|
||||||
}
|
container.deleteChildRange(statement, 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
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
resultStatements.add(statement)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
return false
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -24,14 +24,14 @@ import com.intellij.psi.PsiElement;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.kotlin.idea.KotlinBundle;
|
import org.jetbrains.kotlin.idea.KotlinBundle;
|
||||||
import org.jetbrains.kotlin.idea.codeInsight.surroundWith.KotlinSurrounderUtils;
|
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.*;
|
import org.jetbrains.kotlin.psi.*;
|
||||||
|
|
||||||
public class KotlinFunctionLiteralSurrounder extends KotlinStatementsSurrounder {
|
public class KotlinFunctionLiteralSurrounder extends KotlinStatementsSurrounder {
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
protected TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) {
|
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) {
|
if (statements.length == 0) {
|
||||||
KotlinSurrounderUtils.showErrorHint(project, editor, KotlinSurrounderUtils.SURROUND_WITH_ERROR);
|
KotlinSurrounderUtils.showErrorHint(project, editor, KotlinSurrounderUtils.SURROUND_WITH_ERROR);
|
||||||
|
|||||||
+2
-2
@@ -25,7 +25,7 @@ import com.intellij.psi.PsiElement;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.kotlin.idea.codeInsight.surroundWith.KotlinSurrounderUtils;
|
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.KtBlockExpression;
|
||||||
import org.jetbrains.kotlin.psi.KtExpression;
|
import org.jetbrains.kotlin.psi.KtExpression;
|
||||||
import org.jetbrains.kotlin.psi.KtIfExpression;
|
import org.jetbrains.kotlin.psi.KtIfExpression;
|
||||||
@@ -41,7 +41,7 @@ public abstract class KotlinIfSurrounderBase extends KotlinStatementsSurrounder
|
|||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
protected TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) {
|
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) {
|
if (statements.length == 0) {
|
||||||
KotlinSurrounderUtils.showErrorHint(project, editor, KotlinSurrounderUtils.SURROUND_WITH_ERROR);
|
KotlinSurrounderUtils.showErrorHint(project, editor, KotlinSurrounderUtils.SURROUND_WITH_ERROR);
|
||||||
|
|||||||
+2
-2
@@ -24,7 +24,7 @@ import com.intellij.psi.PsiElement;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.kotlin.idea.codeInsight.surroundWith.KotlinSurrounderUtils;
|
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.*;
|
import org.jetbrains.kotlin.psi.*;
|
||||||
|
|
||||||
public abstract class KotlinTrySurrounderBase extends KotlinStatementsSurrounder {
|
public abstract class KotlinTrySurrounderBase extends KotlinStatementsSurrounder {
|
||||||
@@ -37,7 +37,7 @@ public abstract class KotlinTrySurrounderBase extends KotlinStatementsSurrounder
|
|||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
protected TextRange surroundStatements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement container, @NotNull PsiElement[] statements) {
|
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) {
|
if (statements.length == 0) {
|
||||||
KotlinSurrounderUtils.showErrorHint(project, editor, KotlinSurrounderUtils.SURROUND_WITH_ERROR);
|
KotlinSurrounderUtils.showErrorHint(project, editor, KotlinSurrounderUtils.SURROUND_WITH_ERROR);
|
||||||
|
|||||||
Reference in New Issue
Block a user