Refactoring: Extract JetClassOrObject.getOrCreateBody() function
This commit is contained in:
@@ -44,6 +44,8 @@ abstract public class JetClassOrObject : JetTypeParameterListOwnerStub<KotlinCla
|
||||
|
||||
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 isLocal(): Boolean = getStub()?.isLocal() ?: JetPsiUtil.isLocal(this)
|
||||
|
||||
+1
-6
@@ -137,12 +137,7 @@ public abstract class OverrideImplementMethodsHandler : LanguageCodeInsightActio
|
||||
|
||||
public fun generateMethods(editor: Editor, classOrObject: JetClassOrObject, selectedElements: List<DescriptorClassMember>) {
|
||||
runWriteAction {
|
||||
var body = classOrObject.getBody()
|
||||
if (body == null) {
|
||||
val psiFactory = JetPsiFactory(classOrObject)
|
||||
classOrObject.add(psiFactory.createWhiteSpace())
|
||||
body = classOrObject.add(psiFactory.createEmptyClassBody()) as JetClassBody
|
||||
}
|
||||
val body = classOrObject.getOrCreateBody()
|
||||
|
||||
var afterAnchor = findInsertAfterAnchor(editor, body)
|
||||
|
||||
|
||||
@@ -86,12 +86,7 @@ public class JetAddFunctionToClassifierAction implements QuestionAction {
|
||||
@Override
|
||||
public void run() {
|
||||
JetPsiFactory psiFactory = JetPsiFactory(classifierDeclaration);
|
||||
JetClassBody body = classifierDeclaration.getBody();
|
||||
if (body == null) {
|
||||
PsiElement whitespaceBefore = classifierDeclaration.add(psiFactory.createWhiteSpace());
|
||||
body = (JetClassBody) classifierDeclaration.addAfter(psiFactory.createEmptyClassBody(), whitespaceBefore);
|
||||
classifierDeclaration.addAfter(psiFactory.createNewLine(), body);
|
||||
}
|
||||
JetClassBody body = classifierDeclaration.getOrCreateBody();
|
||||
|
||||
String functionBody = "";
|
||||
if (typeDescriptor.getKind() != ClassKind.INTERFACE && functionDescriptor.getModality() != Modality.ABSTRACT) {
|
||||
|
||||
+4
-10
@@ -566,7 +566,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
|
||||
containingElement is PsiClass -> {
|
||||
if (declaration is JetSecondaryConstructor) {
|
||||
val wrappingClass = psiFactory.createClass("class ${containingElement.getName()} {\n}")
|
||||
addDeclarationToClassOrObject(wrappingClass, declaration, psiFactory)
|
||||
addDeclarationToClassOrObject(wrappingClass, declaration)
|
||||
(jetFileToEdit.add(wrappingClass) as JetClass).getDeclarations().first() as JetNamedDeclaration
|
||||
}
|
||||
else {
|
||||
@@ -575,7 +575,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
|
||||
}
|
||||
|
||||
containingElement is JetClassOrObject -> {
|
||||
addDeclarationToClassOrObject(containingElement, declaration, psiFactory)
|
||||
addDeclarationToClassOrObject(containingElement, declaration)
|
||||
}
|
||||
else -> throw AssertionError("Invalid containing element: ${containingElement.getText()}")
|
||||
}
|
||||
@@ -593,14 +593,8 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
|
||||
}
|
||||
|
||||
private fun addDeclarationToClassOrObject(classOrObject: JetClassOrObject,
|
||||
declaration: JetNamedDeclaration,
|
||||
psiFactory: JetPsiFactory): JetNamedDeclaration {
|
||||
var classBody = classOrObject.getBody()
|
||||
if (classBody == null) {
|
||||
classBody = classOrObject.add(psiFactory.createEmptyClassBody()) as JetClassBody
|
||||
classOrObject.addBefore(psiFactory.createWhiteSpace(), classBody)
|
||||
}
|
||||
|
||||
declaration: JetNamedDeclaration): JetNamedDeclaration {
|
||||
val classBody = classOrObject.getOrCreateBody()
|
||||
return if (declaration is JetNamedFunction) {
|
||||
val anchor = PsiTreeUtil.skipSiblingsBackward(
|
||||
classBody.getRBrace() ?: classBody.getLastChild()!!,
|
||||
|
||||
Reference in New Issue
Block a user