Implement Smart Enter handler for value argument list (KT-18807)
#KT-18807 Fixed
This commit is contained in:
committed by
Nikolay Krasko
parent
25cc85241f
commit
2547612d54
@@ -50,7 +50,9 @@ class KotlinSmartEnterHandler : SmartEnterProcessorWithFixers() {
|
|||||||
|
|
||||||
KotlinClassInitializerFixer(),
|
KotlinClassInitializerFixer(),
|
||||||
|
|
||||||
KotlinClassBodyFixer()
|
KotlinClassBodyFixer(),
|
||||||
|
|
||||||
|
KotlinValueArgumentListFixer()
|
||||||
)
|
)
|
||||||
|
|
||||||
addEnterProcessors(KotlinPlainEnterProcessor())
|
addEnterProcessors(KotlinPlainEnterProcessor())
|
||||||
@@ -133,6 +135,8 @@ class KotlinSmartEnterHandler : SmartEnterProcessorWithFixers() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun doEnter(atCaret: PsiElement, file: PsiFile?, editor: Editor, modified: Boolean): Boolean {
|
override fun doEnter(atCaret: PsiElement, file: PsiFile?, editor: Editor, modified: Boolean): Boolean {
|
||||||
|
if (modified && atCaret is KtCallExpression) return true
|
||||||
|
|
||||||
val block = getControlStatementBlock(editor.caretModel.offset, atCaret) as? KtBlockExpression
|
val block = getControlStatementBlock(editor.caretModel.offset, atCaret) as? KtBlockExpression
|
||||||
if (block != null) {
|
if (block != null) {
|
||||||
val firstElement = block.firstChild?.nextSibling
|
val firstElement = block.firstChild?.nextSibling
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||||
|
* that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.editor.fixers
|
||||||
|
|
||||||
|
import com.intellij.lang.SmartEnterProcessorWithFixers
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
|
import com.intellij.psi.util.PsiUtilCore
|
||||||
|
import org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler
|
||||||
|
import org.jetbrains.kotlin.psi.KtValueArgumentList
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespace
|
||||||
|
|
||||||
|
class KotlinValueArgumentListFixer : SmartEnterProcessorWithFixers.Fixer<KotlinSmartEnterHandler>() {
|
||||||
|
|
||||||
|
override fun apply(editor: Editor, processor: KotlinSmartEnterHandler, element: PsiElement) {
|
||||||
|
if (element !is KtValueArgumentList || element.rightParenthesis != null) return
|
||||||
|
val lPar = element.leftParenthesis ?: return
|
||||||
|
|
||||||
|
val lastArgument = element.arguments.lastOrNull()
|
||||||
|
if (lastArgument != null && PsiUtilCore.hasErrorElementChild(lastArgument)) {
|
||||||
|
val prev = lastArgument.getPrevSiblingIgnoringWhitespace() ?: lPar
|
||||||
|
val offset = prev.endOffset
|
||||||
|
if (prev == lPar) {
|
||||||
|
editor.document.insertString(offset, ")")
|
||||||
|
editor.caretModel.moveToOffset(offset)
|
||||||
|
} else {
|
||||||
|
editor.document.insertString(offset, " )")
|
||||||
|
editor.caretModel.moveToOffset(offset + 1)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
val offset = element.endOffset
|
||||||
|
editor.document.insertString(offset, ")")
|
||||||
|
editor.caretModel.moveToOffset(offset + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1405,6 +1405,81 @@ class SmartEnterTest : KotlinLightCodeInsightFixtureTestCase() {
|
|||||||
<caret>"""
|
<caret>"""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
fun testValueArgumentList1() = doFileTest(
|
||||||
|
"""
|
||||||
|
fun foo(i: Int) = 1
|
||||||
|
fun test1() {
|
||||||
|
foo(1<caret>
|
||||||
|
}
|
||||||
|
""",
|
||||||
|
"""
|
||||||
|
fun foo(i: Int) = 1
|
||||||
|
fun test1() {
|
||||||
|
foo(1)<caret>
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
fun testValueArgumentList2() = doFileTest(
|
||||||
|
"""
|
||||||
|
fun foo(i: Int) = 1
|
||||||
|
fun test2() {
|
||||||
|
foo(foo(1<caret>
|
||||||
|
}
|
||||||
|
""",
|
||||||
|
"""
|
||||||
|
fun foo(i: Int) = 1
|
||||||
|
fun test2() {
|
||||||
|
foo(foo(1))<caret>
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
fun testValueArgumentList3() = doFileTest(
|
||||||
|
"""
|
||||||
|
fun foo(i: Int) = 1
|
||||||
|
fun test3() {
|
||||||
|
foo(<caret>
|
||||||
|
}
|
||||||
|
""",
|
||||||
|
"""
|
||||||
|
fun foo(i: Int) = 1
|
||||||
|
fun test3() {
|
||||||
|
foo(<caret>)
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
fun testValueArgumentList4() = doFileTest(
|
||||||
|
"""
|
||||||
|
fun foo(i: Int) = 1
|
||||||
|
fun test4() {
|
||||||
|
foo(1,<caret>
|
||||||
|
}
|
||||||
|
""",
|
||||||
|
"""
|
||||||
|
fun foo(i: Int) = 1
|
||||||
|
fun test4() {
|
||||||
|
foo(1, <caret>)
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
fun testValueArgumentList5() = doFileTest(
|
||||||
|
"""
|
||||||
|
class Foo(i: Int)
|
||||||
|
fun test5() {
|
||||||
|
Foo(1<caret>
|
||||||
|
}
|
||||||
|
""",
|
||||||
|
"""
|
||||||
|
class Foo(i: Int)
|
||||||
|
fun test5() {
|
||||||
|
Foo(1)<caret>
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
fun doFunTest(before: String, after: String) {
|
fun doFunTest(before: String, after: String) {
|
||||||
fun String.withFunContext(): String {
|
fun String.withFunContext(): String {
|
||||||
val bodyText = "//----\n${this.trimIndent()}\n//----"
|
val bodyText = "//----\n${this.trimIndent()}\n//----"
|
||||||
|
|||||||
Reference in New Issue
Block a user