Refactored DeclarationUtils
This commit is contained in:
@@ -226,8 +226,6 @@ transform.if.statement.with.assignments.to.expression=Transform 'if' statement w
|
|||||||
transform.assignment.with.if.expression.to.statement=Transform assignment with 'if' expression to statement
|
transform.assignment.with.if.expression.to.statement=Transform assignment with 'if' expression to statement
|
||||||
transform.if.statement.with.assignments.to.expression.family=Transform 'if' Statement with Assignments to Expression
|
transform.if.statement.with.assignments.to.expression.family=Transform 'if' Statement with Assignments to Expression
|
||||||
transform.assignment.with.if.expression.to.statement.family=Transform Assignment with 'if' Expression to Statement
|
transform.assignment.with.if.expression.to.statement.family=Transform Assignment with 'if' Expression to Statement
|
||||||
split.property.declaration=Split property declaration
|
|
||||||
split.property.declaration.family=Split Property Declaration
|
|
||||||
change.function.signature.action.single=Change function signature to ''{0}''
|
change.function.signature.action.single=Change function signature to ''{0}''
|
||||||
change.function.signature.action.multiple=Change function signature...
|
change.function.signature.action.multiple=Change function signature...
|
||||||
change.function.signature.family=Change function signature
|
change.function.signature.family=Change function signature
|
||||||
|
|||||||
+2
-2
@@ -20,7 +20,7 @@ import com.intellij.openapi.editor.Editor
|
|||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
|
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
|
||||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedUnfoldingUtils
|
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedUnfoldingUtils
|
||||||
import org.jetbrains.kotlin.idea.intentions.declarations.DeclarationUtils
|
import org.jetbrains.kotlin.idea.intentions.declarations.splitPropertyDeclaration
|
||||||
import org.jetbrains.kotlin.psi.JetIfExpression
|
import org.jetbrains.kotlin.psi.JetIfExpression
|
||||||
import org.jetbrains.kotlin.psi.JetProperty
|
import org.jetbrains.kotlin.psi.JetProperty
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ public class UnfoldPropertyToIfIntention : JetSelfTargetingRangeIntention<JetPro
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: JetProperty, editor: Editor) {
|
override fun applyTo(element: JetProperty, editor: Editor) {
|
||||||
val assignment = DeclarationUtils.splitPropertyDeclaration(element)
|
val assignment = splitPropertyDeclaration(element)
|
||||||
BranchedUnfoldingUtils.unfoldAssignmentToIf(assignment, editor)
|
BranchedUnfoldingUtils.unfoldAssignmentToIf(assignment, editor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+2
-2
@@ -20,7 +20,7 @@ import com.intellij.openapi.editor.Editor
|
|||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
|
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
|
||||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedUnfoldingUtils
|
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedUnfoldingUtils
|
||||||
import org.jetbrains.kotlin.idea.intentions.declarations.DeclarationUtils
|
import org.jetbrains.kotlin.idea.intentions.declarations.splitPropertyDeclaration
|
||||||
import org.jetbrains.kotlin.psi.JetProperty
|
import org.jetbrains.kotlin.psi.JetProperty
|
||||||
import org.jetbrains.kotlin.psi.JetPsiUtil
|
import org.jetbrains.kotlin.psi.JetPsiUtil
|
||||||
import org.jetbrains.kotlin.psi.JetWhenExpression
|
import org.jetbrains.kotlin.psi.JetWhenExpression
|
||||||
@@ -34,7 +34,7 @@ public class UnfoldPropertyToWhenIntention : JetSelfTargetingRangeIntention<JetP
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: JetProperty, editor: Editor) {
|
override fun applyTo(element: JetProperty, editor: Editor) {
|
||||||
val assignment = DeclarationUtils.splitPropertyDeclaration(element)
|
val assignment = splitPropertyDeclaration(element)
|
||||||
BranchedUnfoldingUtils.unfoldAssignmentToWhen(assignment, editor)
|
BranchedUnfoldingUtils.unfoldAssignmentToWhen(assignment, editor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -16,71 +16,33 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.intentions.declarations
|
package org.jetbrains.kotlin.idea.intentions.declarations
|
||||||
|
|
||||||
import com.intellij.openapi.project.Project
|
|
||||||
import com.intellij.psi.PsiDocumentManager
|
import com.intellij.psi.PsiDocumentManager
|
||||||
import com.intellij.psi.PsiElement
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
import com.intellij.psi.PsiFile
|
import org.jetbrains.kotlin.idea.intentions.setType
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.*
|
import org.jetbrains.kotlin.psi.JetBinaryExpression
|
||||||
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
|
import org.jetbrains.kotlin.psi.JetProperty
|
||||||
import org.jetbrains.kotlin.idea.util.ShortenReferences
|
import org.jetbrains.kotlin.psi.JetPsiFactory
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.createExpressionByPattern
|
||||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
|
||||||
import org.jetbrains.kotlin.types.JetType
|
|
||||||
|
|
||||||
public object DeclarationUtils {
|
// returns assignment which replaces initializer
|
||||||
|
public fun splitPropertyDeclaration(property: JetProperty): JetBinaryExpression {
|
||||||
|
val parent = property.getParent()!!
|
||||||
|
|
||||||
private fun assertNotNull(value: Any?) {
|
val initializer = property.getInitializer()!!
|
||||||
assert(value != null, "Expression must be checked before applying transformation")
|
|
||||||
|
val explicitTypeToSet = if (property.getTypeReference() != null) null else initializer.analyze().getType(initializer)
|
||||||
|
|
||||||
|
val psiFactory = JetPsiFactory(property)
|
||||||
|
var assignment = psiFactory.createExpressionByPattern("$0 = $1", property.getName()!!, initializer)
|
||||||
|
|
||||||
|
assignment = parent.addAfter(assignment, property) as JetBinaryExpression
|
||||||
|
parent.addAfter(psiFactory.createNewLine(), property)
|
||||||
|
|
||||||
|
property.setInitializer(null)
|
||||||
|
|
||||||
|
if (explicitTypeToSet != null) {
|
||||||
|
property.setType(explicitTypeToSet)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun checkSplitProperty(property: JetProperty): Boolean {
|
return assignment
|
||||||
return property.hasInitializer() && property.isLocal()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getPropertyTypeIfNeeded(property: JetProperty): JetType? {
|
|
||||||
if (property.getTypeReference() != null) return null
|
|
||||||
|
|
||||||
val initializer = property.getInitializer()
|
|
||||||
val type = if (initializer != null) property.analyze(BodyResolveMode.FULL).getType(initializer) else null
|
|
||||||
return if (type == null || type.isError()) null else type
|
|
||||||
}
|
|
||||||
|
|
||||||
// returns assignment which replaces initializer
|
|
||||||
public fun splitPropertyDeclaration(property: JetProperty): JetBinaryExpression {
|
|
||||||
var property = property
|
|
||||||
val parent = property.getParent()
|
|
||||||
assertNotNull(parent)
|
|
||||||
|
|
||||||
//noinspection unchecked
|
|
||||||
val initializer = property.getInitializer()
|
|
||||||
assertNotNull(initializer)
|
|
||||||
|
|
||||||
val psiFactory = JetPsiFactory(property)
|
|
||||||
//noinspection ConstantConditions, unchecked
|
|
||||||
var newInitializer = psiFactory.createExpressionByPattern("$0 = $1", property.getName(), initializer)
|
|
||||||
|
|
||||||
newInitializer = parent.addAfter(newInitializer, property) as JetBinaryExpression
|
|
||||||
parent.addAfter(psiFactory.createNewLine(), property)
|
|
||||||
|
|
||||||
val project = newInitializer.getProject()
|
|
||||||
val file = parent.getContainingFile()
|
|
||||||
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(PsiDocumentManager.getInstance(project).getDocument(file))
|
|
||||||
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
val inferredType = getPropertyTypeIfNeeded(property)
|
|
||||||
|
|
||||||
val typeStr = if (inferredType != null)
|
|
||||||
IdeDescriptorRenderers.SOURCE_CODE.renderType(inferredType)
|
|
||||||
else
|
|
||||||
JetPsiUtil.getNullableText(property.getTypeReference())
|
|
||||||
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
property = property.replace(psiFactory.createProperty(property.getNameIdentifier()!!.getText(), typeStr, property.isVar())) as JetProperty
|
|
||||||
|
|
||||||
if (inferredType != null) {
|
|
||||||
ShortenReferences.DEFAULT.process(property.getTypeReference())
|
|
||||||
}
|
|
||||||
|
|
||||||
return newInitializer
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -20,10 +20,10 @@ import com.intellij.openapi.editor.Editor
|
|||||||
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
|
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
|
||||||
import org.jetbrains.kotlin.psi.JetProperty
|
import org.jetbrains.kotlin.psi.JetProperty
|
||||||
|
|
||||||
public class SplitPropertyDeclarationIntention : JetSelfTargetingOffsetIndependentIntention<JetProperty>("split.property.declaration", javaClass()) {
|
public class SplitPropertyDeclarationIntention : JetSelfTargetingOffsetIndependentIntention<JetProperty>(javaClass(), "Split property declaration") {
|
||||||
override fun isApplicableTo(element: JetProperty): Boolean = DeclarationUtils.checkSplitProperty(element)
|
override fun isApplicableTo(element: JetProperty): Boolean = element.hasInitializer() && element.isLocal()
|
||||||
|
|
||||||
override fun applyTo(element: JetProperty, editor: Editor) {
|
override fun applyTo(element: JetProperty, editor: Editor) {
|
||||||
DeclarationUtils.splitPropertyDeclaration(element)
|
splitPropertyDeclaration(element)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user