Complete statement for class declaration: add '()' to supertype

#KT-31668 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-05-29 10:40:14 +02:00
committed by Vladimir Dolzhenko
parent e9a7be4a46
commit 5a6cf19c68
2 changed files with 76 additions and 1 deletions
@@ -19,8 +19,12 @@ package org.jetbrains.kotlin.idea.editor.fixers
import com.intellij.lang.SmartEnterProcessorWithFixers
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.caches.resolve.allowResolveInDispatchThread
import org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtSuperTypeCallEntry
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespaceAndComments
@@ -28,7 +32,7 @@ class KotlinClassBodyFixer : SmartEnterProcessorWithFixers.Fixer<KotlinSmartEnte
override fun apply(editor: Editor, processor: KotlinSmartEnterHandler, psiElement: PsiElement) {
if (psiElement !is KtClassOrObject) return
val body = psiElement.getBody()
val body = psiElement.body
if (!body?.text.isNullOrBlank()) return
var endOffset = psiElement.range.end
@@ -39,6 +43,17 @@ class KotlinClassBodyFixer : SmartEnterProcessorWithFixers.Fixer<KotlinSmartEnte
}
}
val notInitializedSuperType = allowResolveInDispatchThread {
psiElement.superTypeListEntries.firstOrNull {
if (it is KtSuperTypeCallEntry) return@firstOrNull false
(it.typeAsUserType?.referenceExpression?.mainReference?.resolve() as? KtClass)?.isInterface() != true
}
}
if (notInitializedSuperType != null) {
editor.document.insertString(notInitializedSuperType.endOffset, "()")
endOffset += 2
}
editor.caretModel.moveToOffset(endOffset - 1)
// Insert '\n' to force a multiline body, otherwise there will be an empty body on one line and a caret on the next one.
@@ -1399,6 +1399,66 @@ class SmartEnterTest : KotlinLightCodeInsightFixtureTestCase() {
"""
)
fun testClassBodyHasInitializedSuperType() = doFileTest(
"""
open class A
class B : A()<caret>
"""
,
"""
open class A
class B : A() {
<caret>
}
"""
)
fun testClassBodyHasNotInitializedSuperType() = doFileTest(
"""
open class A
class B : A<caret>
"""
,
"""
open class A
class B : A() {
<caret>
}
"""
)
fun testClassBodyHasNotInitializedSuperType2() = doFileTest(
"""
sealed class A(val s: String)
class B : A<caret>
"""
,
"""
sealed class A(val s: String)
class B : A() {
<caret>
}
"""
)
fun testClassBodyHasNotInitializedSuperType3() = doFileTest(
"""
interface I
interface J
abstract class A
class B : I, A, J<caret>
"""
,
"""
interface I
interface J
abstract class A
class B : I, A(), J {
<caret>
}
"""
)
fun testEmptyLine() = doFileTest(
"""fun foo() {}
<caret>""",