Minor: Generify KtClassOrObject.addDeclarationXXX()
This commit is contained in:
@@ -63,20 +63,20 @@ abstract class KtClassOrObject :
|
||||
|
||||
fun getBody(): KtClassBody? = getStubOrPsiChild(KtStubElementTypes.CLASS_BODY)
|
||||
|
||||
fun addDeclaration(declaration: KtDeclaration): KtDeclaration {
|
||||
inline fun <reified T: KtDeclaration> addDeclaration(declaration: T): T {
|
||||
val body = getOrCreateBody()
|
||||
val anchor = PsiTreeUtil.skipSiblingsBackward(body.rBrace ?: body.lastChild!!, PsiWhiteSpace::class.java)
|
||||
return body.addAfter(declaration, anchor) as KtDeclaration
|
||||
return body.addAfter(declaration, anchor) as T
|
||||
}
|
||||
|
||||
fun addDeclarationAfter(declaration: KtDeclaration, anchor: PsiElement?): KtDeclaration {
|
||||
inline fun <reified T: KtDeclaration> addDeclarationAfter(declaration: T, anchor: PsiElement?): T {
|
||||
val anchorBefore = anchor ?: declarations.lastOrNull() ?: return addDeclaration(declaration)
|
||||
return getOrCreateBody().addAfter(declaration, anchorBefore) as KtDeclaration
|
||||
return getOrCreateBody().addAfter(declaration, anchorBefore) as T
|
||||
}
|
||||
|
||||
fun addDeclarationBefore(declaration: KtDeclaration, anchor: PsiElement?): KtDeclaration {
|
||||
inline fun <reified T: KtDeclaration> addDeclarationBefore(declaration: T, anchor: PsiElement?): T {
|
||||
val anchorAfter = anchor ?: declarations.firstOrNull() ?: return addDeclaration(declaration)
|
||||
return getOrCreateBody().addBefore(declaration, anchorAfter) as KtDeclaration
|
||||
return getOrCreateBody().addBefore(declaration, anchorAfter) as T
|
||||
}
|
||||
|
||||
fun isTopLevel(): Boolean = stub?.isTopLevel() ?: (parent is KtFile)
|
||||
|
||||
Reference in New Issue
Block a user