diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/KotlinFunctionInsertHandler.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/KotlinFunctionInsertHandler.kt index 64f1dcbc758..abd5ce245e5 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/KotlinFunctionInsertHandler.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/KotlinFunctionInsertHandler.kt @@ -92,7 +92,7 @@ class KotlinFunctionInsertHandler(val caretPosition: CaretPosition, val lambdaIn val document = context.getDocument() val chars = document.getCharsSequence() - val insertLambda = lambdaInfo != null && completionChar != '(' && !(completionChar == '\t' && chars.charAt(offset) == '(') + val insertLambda = lambdaInfo != null && completionChar != '(' && !(completionChar == '\t' && chars.isCharAt(offset, '(')) val openingBracket = if (insertLambda) '{' else '(' val closingBracket = if (insertLambda) '}' else ')' diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/WithTailInsertHandler.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/WithTailInsertHandler.kt index 05310874db6..0272f7cdba1 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/WithTailInsertHandler.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/WithTailInsertHandler.kt @@ -16,14 +16,14 @@ package org.jetbrains.kotlin.idea.completion.handlers -import com.intellij.codeInsight.completion.* +import com.intellij.codeInsight.AutoPopupController +import com.intellij.codeInsight.completion.InsertHandler +import com.intellij.codeInsight.completion.InsertionContext +import com.intellij.codeInsight.lookup.Lookup import com.intellij.codeInsight.lookup.LookupElement import com.intellij.psi.PsiDocumentManager -import com.intellij.codeInsight.AutoPopupController -import com.intellij.codeInsight.lookup.Lookup -import org.jetbrains.kotlin.idea.completion.smart.SmartCompletion import org.jetbrains.kotlin.idea.completion.KEEP_OLD_ARGUMENT_LIST_ON_TAB_KEY -import com.intellij.openapi.util.TextRange +import org.jetbrains.kotlin.idea.completion.smart.SmartCompletion class WithTailInsertHandler(val tailText: String, val spaceBefore: Boolean, @@ -55,16 +55,13 @@ class WithTailInsertHandler(val tailText: String, //TODO: analyze parenthesis balance to decide whether to replace or not var insert = true if (overwriteText) { - fun isCharAt(offset: Int, c: Char) = offset < document.getTextLength() && document.getCharsSequence().charAt(offset) == c - fun isTextAt(offset: Int, text: String) = offset + text.length() <= document.getTextLength() && document.getText(TextRange(offset, offset + text.length())) == text - var offset = document.charsSequence.skipSpacesAndLineBreaks(tailOffset) - if (isTextAt(offset, tailText)) { + if (document.isTextAt(offset, tailText)) { insert = false offset += tailText.length() tailOffset = offset - if (spaceAfter && isCharAt(offset, ' ')) { + if (spaceAfter && document.charsSequence.isCharAt(offset, ' ')) { document.deleteString(offset, offset + 1) } } diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/handlerUtils.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/handlerUtils.kt index 01f571f731b..6d3810d3f89 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/handlerUtils.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/handlerUtils.kt @@ -16,6 +16,9 @@ package org.jetbrains.kotlin.idea.completion.handlers +import com.intellij.openapi.editor.Document +import com.intellij.openapi.util.TextRange + fun CharSequence.indexOfSkippingSpace(c: Char, startIndex: Int): Int? { for (i in startIndex..this.length() - 1) { val currentChar = this[i] @@ -31,3 +34,6 @@ fun CharSequence.skipSpaces(index: Int): Int fun CharSequence.skipSpacesAndLineBreaks(index: Int): Int = (index..length() - 1).firstOrNull { val c = this[it]; c != ' ' && c != '\t' && c != '\n' && c != '\r' } ?: this.length() +fun CharSequence.isCharAt(offset: Int, c: Char) = offset < length() && charAt(offset) == c + +fun Document.isTextAt(offset: Int, text: String) = offset + text.length() <= getTextLength() && getText(TextRange(offset, offset + text.length())) == text diff --git a/idea/idea-completion/testData/handlers/basic/EA70229.kt b/idea/idea-completion/testData/handlers/basic/EA70229.kt new file mode 100644 index 00000000000..2654d511dc4 --- /dev/null +++ b/idea/idea-completion/testData/handlers/basic/EA70229.kt @@ -0,0 +1,6 @@ +// ELEMENT: foo +// CHAR: '\t' + +fun foo(handler: () -> Unit){} + +val v = \ No newline at end of file diff --git a/idea/idea-completion/testData/handlers/basic/EA70229.kt.after b/idea/idea-completion/testData/handlers/basic/EA70229.kt.after new file mode 100644 index 00000000000..21fbbc5d12c --- /dev/null +++ b/idea/idea-completion/testData/handlers/basic/EA70229.kt.after @@ -0,0 +1,6 @@ +// ELEMENT: foo +// CHAR: '\t' + +fun foo(handler: () -> Unit){} + +val v = foo { } \ No newline at end of file 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 219da9fa438..ddba423aacb 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 @@ -47,6 +47,12 @@ public class BasicCompletionHandlerTestGenerated extends AbstractBasicCompletion doTest(fileName); } + @TestMetadata("EA70229.kt") + public void testEA70229() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/EA70229.kt"); + doTest(fileName); + } + @TestMetadata("ExtensionReceiverTypeArg.kt") public void testExtensionReceiverTypeArg() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/ExtensionReceiverTypeArg.kt");