From 7ab2d0c951eb00f5ff718e68d18b01424256ec6e Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Thu, 13 Oct 2016 21:00:01 +0300 Subject: [PATCH] KT-13810 Kotlin code completion missing last character #KT-13810 Fixed --- .../handlers/KotlinCallableInsertHandler.kt | 5 ++++- .../testData/handlers/basic/KT14130.kt | 16 ++++++++++++++++ .../testData/handlers/basic/KT14130.kt.after | 16 ++++++++++++++++ .../handlers/AbstractCompletionHandlerTests.kt | 15 +++++++++++---- .../BasicCompletionHandlerTestGenerated.java | 6 ++++++ 5 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 idea/idea-completion/testData/handlers/basic/KT14130.kt create mode 100644 idea/idea-completion/testData/handlers/basic/KT14130.kt.after diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/KotlinCallableInsertHandler.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/KotlinCallableInsertHandler.kt index 9216a335753..1684fe599a1 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/KotlinCallableInsertHandler.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/KotlinCallableInsertHandler.kt @@ -59,7 +59,10 @@ abstract class KotlinCallableInsertHandler(val callType: CallType<*>) : BaseDecl psiDocumentManager.doPostponedOperationsAndUnblockDocument(context.document) - context.document.deleteString(context.tailOffset - 1, context.tailOffset) // delete space + // delete space + if (context.document.isTextAt(context.tailOffset - 1, " ")) { // sometimes space can be lost because of reformatting + context.document.deleteString(context.tailOffset - 1, context.tailOffset) + } } } } diff --git a/idea/idea-completion/testData/handlers/basic/KT14130.kt b/idea/idea-completion/testData/handlers/basic/KT14130.kt new file mode 100644 index 00000000000..272332ec9e3 --- /dev/null +++ b/idea/idea-completion/testData/handlers/basic/KT14130.kt @@ -0,0 +1,16 @@ +// CODE_STYLE_SETTING: ALIGN_MULTILINE_PARAMETERS_IN_CALLS = true +package test + +interface Foo { + companion object { + val EMPTY = object : Foo {} + } +} + +fun test(foo: Foo, bar: String) {} + +fun test2() { + test(, "") +} + +// ELEMENT: EMPTY \ No newline at end of file diff --git a/idea/idea-completion/testData/handlers/basic/KT14130.kt.after b/idea/idea-completion/testData/handlers/basic/KT14130.kt.after new file mode 100644 index 00000000000..4d3ab296c56 --- /dev/null +++ b/idea/idea-completion/testData/handlers/basic/KT14130.kt.after @@ -0,0 +1,16 @@ +// CODE_STYLE_SETTING: ALIGN_MULTILINE_PARAMETERS_IN_CALLS = true +package test + +interface Foo { + companion object { + val EMPTY = object : Foo {} + } +} + +fun test(foo: Foo, bar: String) {} + +fun test2() { + test(Foo.EMPTY, "") +} + +// ELEMENT: EMPTY \ No newline at end of file diff --git a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/handlers/AbstractCompletionHandlerTests.kt b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/handlers/AbstractCompletionHandlerTests.kt index 33abbac5dd8..160bc93abb7 100644 --- a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/handlers/AbstractCompletionHandlerTests.kt +++ b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/handlers/AbstractCompletionHandlerTests.kt @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea.completion.test.handlers import com.intellij.codeInsight.completion.CompletionType import com.intellij.openapi.util.io.FileUtil import com.intellij.psi.codeStyle.CodeStyleSettingsManager +import org.jetbrains.kotlin.idea.KotlinLanguage import org.jetbrains.kotlin.idea.completion.test.ExpectedCompletionUtils import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor @@ -57,15 +58,21 @@ abstract class AbstractCompletionHandlerTest(private val defaultCompletionType: val completionType = ExpectedCompletionUtils.getCompletionType(fileText) ?: defaultCompletionType - val codeStyleSettings = KotlinCodeStyleSettings.getInstance(project) + val kotlinStyleSettings = KotlinCodeStyleSettings.getInstance(project) + val commonStyleSettings = CodeStyleSettingsManager.getSettings(project).getCommonSettings(KotlinLanguage.INSTANCE) for (line in InTextDirectivesUtils.findLinesWithPrefixesRemoved(fileText, CODE_STYLE_SETTING_PREFIX)) { val index = line.indexOfOrNull('=') ?: error("Invalid code style setting '$line': '=' expected") val settingName = line.substring(0, index).trim() val settingValue = line.substring(index + 1).trim() - val field = codeStyleSettings.javaClass.getDeclaredField(settingName) + val (field, settings) = try { + kotlinStyleSettings.javaClass.getDeclaredField(settingName) to kotlinStyleSettings + } + catch (e: NoSuchFieldException) { + commonStyleSettings.javaClass.getDeclaredField(settingName) to commonStyleSettings + } when (field.type.name) { - "boolean" -> field.setBoolean(codeStyleSettings, settingValue.toBoolean()) - "int" -> field.setInt(codeStyleSettings, settingValue.toInt()) + "boolean" -> field.setBoolean(settings, settingValue.toBoolean()) + "int" -> field.setInt(settings, settingValue.toInt()) else -> error("Unsupported setting type: ${field.type}") } } diff --git a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/handlers/BasicCompletionHandlerTestGenerated.java b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/handlers/BasicCompletionHandlerTestGenerated.java index 3927779c982..5cc6e06501c 100644 --- a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/handlers/BasicCompletionHandlerTestGenerated.java +++ b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/handlers/BasicCompletionHandlerTestGenerated.java @@ -125,6 +125,12 @@ public class BasicCompletionHandlerTestGenerated extends AbstractBasicCompletion doTest(fileName); } + @TestMetadata("KT14130.kt") + public void testKT14130() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/KT14130.kt"); + doTest(fileName); + } + @TestMetadata("NestedTypeArg.kt") public void testNestedTypeArg() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/NestedTypeArg.kt");