committed by
Dmitry Jemerov
parent
e6a7808d8e
commit
96d0f91afc
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<KotlinSmartEnterHandler>() {
|
||||
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}")
|
||||
}
|
||||
}
|
||||
@@ -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<caret>
|
||||
"""
|
||||
,
|
||||
"""
|
||||
class Foo {
|
||||
<caret>
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
fun testClassBody2() = doFileTest(
|
||||
"""
|
||||
class <caret>Foo
|
||||
"""
|
||||
,
|
||||
"""
|
||||
class Foo {
|
||||
<caret>
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
fun doFunTest(before: String, after: String) {
|
||||
fun String.withFunContext(): String {
|
||||
val bodyText = "//----\n${this.trimIndent()}\n//----"
|
||||
|
||||
Reference in New Issue
Block a user