Refactoring: Extract JetClassOrObject.getOrCreateBody() function

This commit is contained in:
Alexey Sedunov
2015-07-13 15:58:46 +03:00
parent 83e28b360f
commit 3c36795984
4 changed files with 8 additions and 22 deletions
@@ -44,6 +44,8 @@ abstract public class JetClassOrObject : JetTypeParameterListOwnerStub<KotlinCla
public fun getBody(): JetClassBody? = getStubOrPsiChild(JetStubElementTypes.CLASS_BODY) public fun getBody(): JetClassBody? = getStubOrPsiChild(JetStubElementTypes.CLASS_BODY)
public fun getOrCreateBody(): JetClassBody = getBody() ?: add(JetPsiFactory(this).createEmptyClassBody()) as JetClassBody
public fun isTopLevel(): Boolean = getStub()?.isTopLevel() ?: (getParent() is JetFile) public fun isTopLevel(): Boolean = getStub()?.isTopLevel() ?: (getParent() is JetFile)
public fun isLocal(): Boolean = getStub()?.isLocal() ?: JetPsiUtil.isLocal(this) public fun isLocal(): Boolean = getStub()?.isLocal() ?: JetPsiUtil.isLocal(this)
@@ -137,12 +137,7 @@ public abstract class OverrideImplementMethodsHandler : LanguageCodeInsightActio
public fun generateMethods(editor: Editor, classOrObject: JetClassOrObject, selectedElements: List<DescriptorClassMember>) { public fun generateMethods(editor: Editor, classOrObject: JetClassOrObject, selectedElements: List<DescriptorClassMember>) {
runWriteAction { runWriteAction {
var body = classOrObject.getBody() val body = classOrObject.getOrCreateBody()
if (body == null) {
val psiFactory = JetPsiFactory(classOrObject)
classOrObject.add(psiFactory.createWhiteSpace())
body = classOrObject.add(psiFactory.createEmptyClassBody()) as JetClassBody
}
var afterAnchor = findInsertAfterAnchor(editor, body) var afterAnchor = findInsertAfterAnchor(editor, body)
@@ -86,12 +86,7 @@ public class JetAddFunctionToClassifierAction implements QuestionAction {
@Override @Override
public void run() { public void run() {
JetPsiFactory psiFactory = JetPsiFactory(classifierDeclaration); JetPsiFactory psiFactory = JetPsiFactory(classifierDeclaration);
JetClassBody body = classifierDeclaration.getBody(); JetClassBody body = classifierDeclaration.getOrCreateBody();
if (body == null) {
PsiElement whitespaceBefore = classifierDeclaration.add(psiFactory.createWhiteSpace());
body = (JetClassBody) classifierDeclaration.addAfter(psiFactory.createEmptyClassBody(), whitespaceBefore);
classifierDeclaration.addAfter(psiFactory.createNewLine(), body);
}
String functionBody = ""; String functionBody = "";
if (typeDescriptor.getKind() != ClassKind.INTERFACE && functionDescriptor.getModality() != Modality.ABSTRACT) { if (typeDescriptor.getKind() != ClassKind.INTERFACE && functionDescriptor.getModality() != Modality.ABSTRACT) {
@@ -566,7 +566,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
containingElement is PsiClass -> { containingElement is PsiClass -> {
if (declaration is JetSecondaryConstructor) { if (declaration is JetSecondaryConstructor) {
val wrappingClass = psiFactory.createClass("class ${containingElement.getName()} {\n}") val wrappingClass = psiFactory.createClass("class ${containingElement.getName()} {\n}")
addDeclarationToClassOrObject(wrappingClass, declaration, psiFactory) addDeclarationToClassOrObject(wrappingClass, declaration)
(jetFileToEdit.add(wrappingClass) as JetClass).getDeclarations().first() as JetNamedDeclaration (jetFileToEdit.add(wrappingClass) as JetClass).getDeclarations().first() as JetNamedDeclaration
} }
else { else {
@@ -575,7 +575,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
} }
containingElement is JetClassOrObject -> { containingElement is JetClassOrObject -> {
addDeclarationToClassOrObject(containingElement, declaration, psiFactory) addDeclarationToClassOrObject(containingElement, declaration)
} }
else -> throw AssertionError("Invalid containing element: ${containingElement.getText()}") else -> throw AssertionError("Invalid containing element: ${containingElement.getText()}")
} }
@@ -593,14 +593,8 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
} }
private fun addDeclarationToClassOrObject(classOrObject: JetClassOrObject, private fun addDeclarationToClassOrObject(classOrObject: JetClassOrObject,
declaration: JetNamedDeclaration, declaration: JetNamedDeclaration): JetNamedDeclaration {
psiFactory: JetPsiFactory): JetNamedDeclaration { val classBody = classOrObject.getOrCreateBody()
var classBody = classOrObject.getBody()
if (classBody == null) {
classBody = classOrObject.add(psiFactory.createEmptyClassBody()) as JetClassBody
classOrObject.addBefore(psiFactory.createWhiteSpace(), classBody)
}
return if (declaration is JetNamedFunction) { return if (declaration is JetNamedFunction) {
val anchor = PsiTreeUtil.skipSiblingsBackward( val anchor = PsiTreeUtil.skipSiblingsBackward(
classBody.getRBrace() ?: classBody.getLastChild()!!, classBody.getRBrace() ?: classBody.getLastChild()!!,