diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinSmartEnterHandler.kt b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinSmartEnterHandler.kt index c88311cadd5..f14ec9f53b6 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinSmartEnterHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinSmartEnterHandler.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. + * Copyright 2010-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,7 +58,9 @@ class KotlinSmartEnterHandler: SmartEnterProcessorWithFixers() { KotlinLastLambdaParameterFixer(), - KotlinClassInitializerFixer() + KotlinClassInitializerFixer(), + + KotlinClassBodyFixer() ) addEnterProcessors(KotlinPlainEnterProcessor()) @@ -71,13 +73,13 @@ class KotlinSmartEnterHandler: SmartEnterProcessorWithFixers() { while (atCaret != null) { when { - atCaret.isKotlinStatement() == true -> return atCaret + atCaret.isKotlinStatement() -> return atCaret atCaret.parent is KtFunctionLiteral -> return atCaret atCaret is KtDeclaration -> { val declaration = atCaret when { declaration is KtParameter && !declaration.isInLambdaExpression() -> {/* proceed to function declaration */} - declaration.getParent() is KtForExpression -> {/* skip variable declaration in 'for' expression */} + declaration.parent is KtForExpression -> {/* skip variable declaration in 'for' expression */} else -> return atCaret } } diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinClassBodyFixer.kt b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinClassBodyFixer.kt new file mode 100644 index 00000000000..03f8f3e3480 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/editor/fixers/KotlinClassBodyFixer.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +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.editor.KotlinSmartEnterHandler +import org.jetbrains.kotlin.psi.KtClassOrObject +import org.jetbrains.kotlin.psi.psiUtil.endOffset + +class KotlinClassBodyFixer : SmartEnterProcessorWithFixers.Fixer() { + override fun apply(editor: Editor, processor: KotlinSmartEnterHandler, psiElement: PsiElement) { + if (psiElement !is KtClassOrObject) return + if (psiElement.getBody() != null) return + + val doc = editor.document + val endOffset = psiElement.range.end + + editor.caretModel.moveToOffset(psiElement.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. + doc.insertString(endOffset, "{\n}") + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/smartEnter/SmartEnterTest.kt b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/smartEnter/SmartEnterTest.kt index 83b80a5676b..d26a729e017 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/smartEnter/SmartEnterTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/smartEnter/SmartEnterTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. + * Copyright 2010-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -1356,6 +1356,30 @@ class SmartEnterTest : KotlinLightCodeInsightFixtureTestCase() { """ ) + fun testClassBody1() = doFileTest( + """ + class Foo + """ + , + """ + class Foo { + + } + """ + ) + + fun testClassBody2() = doFileTest( + """ + class Foo + """ + , + """ + class Foo { + + } + """ + ) + fun doFunTest(before: String, after: String) { fun String.withFunContext(): String { val bodyText = "//----\n${this.trimIndent()}\n//----"