Implemented "Complete statement" for completion

#KT-18663 fixed
This commit is contained in:
Max Medvedev
2018-12-20 14:38:35 +03:00
parent e8c52e0a8f
commit a9362d844a
2 changed files with 145 additions and 6 deletions
@@ -93,15 +93,18 @@ sealed class KotlinFunctionInsertHandler(callType: CallType<*>) : KotlinCallable
val project = context.project
val chars = document.charsSequence
val insertLambda = lambdaInfo != null && completionChar != '(' && !(completionChar == '\t' && chars.isCharAt(offset, '('))
val isSmartEnterCompletion = completionChar == Lookup.COMPLETE_STATEMENT_SELECT_CHAR
val isReplaceCompletion = completionChar == Lookup.REPLACE_SELECT_CHAR
val isNormalCompletion = completionChar == Lookup.NORMAL_SELECT_CHAR
val insertLambda = lambdaInfo != null && completionChar != '(' && !(isReplaceCompletion && chars.isCharAt(offset, '('))
val openingBracket = if (insertLambda) '{' else '('
val closingBracket = if (insertLambda) '}' else ')'
var insertTypeArguments =
inputTypeArguments && (completionChar == '\n' || completionChar == '\r' || completionChar == Lookup.REPLACE_SELECT_CHAR)
var insertTypeArguments = inputTypeArguments && (isNormalCompletion || isReplaceCompletion || isSmartEnterCompletion)
if (completionChar == Lookup.REPLACE_SELECT_CHAR) {
if (isReplaceCompletion) {
val offset1 = chars.skipSpaces(offset)
if (offset1 < chars.length) {
if (chars[offset1] == '<') {
@@ -149,12 +152,16 @@ sealed class KotlinFunctionInsertHandler(callType: CallType<*>) : KotlinCallable
document.insertString(offset, " {}")
}
} else {
document.insertString(offset, "()")
if (isSmartEnterCompletion) {
document.insertString(offset, "(")
} else {
document.insertString(offset, "()")
}
}
PsiDocumentManager.getInstance(project).commitDocument(document)
openingBracketOffset = chars.indexOfSkippingSpace(openingBracket, offset)!!
closeBracketOffset = chars.indexOfSkippingSpace(closingBracket, openingBracketOffset + 1)!!
closeBracketOffset = chars.indexOfSkippingSpace(closingBracket, openingBracketOffset + 1)
}
if (insertLambda && lambdaInfo!!.explicitParameters) {
@@ -0,0 +1,132 @@
/*
* Copyright 2010-2019 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.completion.test
import com.intellij.codeInsight.lookup.Lookup
import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
class SmartEnterCompletionTest : KotlinLightCodeInsightFixtureTestCase() {
fun testBasic() {
doTest(
"""
fun foo(p:String) {}
fun test() {
val s = ""
fo<caret>s
}
""", """
fun foo(p:String) {}
fun test() {
val s = ""
foo(s)<caret>
}
"""
)
}
fun testEncloseCall() {
doTest(
"""
fun foo(p:String) {}
fun bar(p:String): String = p
fun test() {
val s = ""
fo<caret>bar(s)
}
""", """
fun foo(p:String) {}
fun bar(p:String): String = p
fun test() {
val s = ""
foo(bar(s))<caret>
}
"""
)
}
fun testFinishOuterCall() {
doTest(
"""
fun foo(p:String) {}
fun bar(p:String): String = p
fun test() {
val s = ""
foo(ba<caret>s)
}
""", """
fun foo(p:String) {}
fun bar(p:String): String = p
fun test() {
val s = ""
foo(bar(s))<caret>
}
"""
)
}
fun testIf() {
doTest(
"""
fun foo(p:String): Boolean = false
fun test() {
val s = ""
if (fo<caret>s
}
""", """
fun foo(p:String): Boolean = false
fun test() {
val s = ""
if (foo(s)) {
<caret>
}
}
"""
)
}
fun testTwoLineBinaryExpr() {
doTest(
"""
fun foo(p:Int) {}
fun test() {
fo<caret>1 +
2
}
""", """
fun foo(p:Int) {}
fun test() {
foo(1 +
2)<caret>
}
"""
)
}
fun doTest(before: String, after: String) {
with(myFixture) {
configureByText(KotlinFileType.INSTANCE, before)
completeBasic()
type(Lookup.COMPLETE_STATEMENT_SELECT_CHAR)
checkResult(after)
}
}
override fun getProjectDescriptor(): LightProjectDescriptor = LightCodeInsightFixtureTestCase.JAVA_LATEST
}