EA-70229 - IOOBE (KotlinFunctionInsertHandler): ImmutableText.findLeaf

#EA-70229 Fixed
This commit is contained in:
Valentin Kipyatkov
2015-08-14 18:37:17 +03:00
parent b7c1bed809
commit 4ba1f423e1
6 changed files with 32 additions and 11 deletions
@@ -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 ')'
@@ -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)
}
}
@@ -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
@@ -0,0 +1,6 @@
// ELEMENT: foo
// CHAR: '\t'
fun foo(handler: () -> Unit){}
val v = <caret>
@@ -0,0 +1,6 @@
// ELEMENT: foo
// CHAR: '\t'
fun foo(handler: () -> Unit){}
val v = foo { <caret> }
@@ -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");