EA-105522, KT-20256: Fix TextRange.<init> "Invalid range specified"

Check when caret inside template, and don't try to create invalid
range then

 EA-105522 fixed
 #KT-20256 fixed
This commit is contained in:
Simon Ogorodnik
2017-10-21 03:38:24 +03:00
parent 31a1fb916a
commit cace6624e6
3 changed files with 16 additions and 0 deletions
@@ -25,10 +25,15 @@ import org.jetbrains.kotlin.psi.KtSimpleNameStringTemplateEntry
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.psi.psiUtil.prevLeaf
import org.jetbrains.kotlin.psi.psiUtil.startOffset
class EnableAutopopupInStringTemplate : CompletionConfidence() {
override fun shouldSkipAutopopup(contextElement: PsiElement, psiFile: PsiFile, offset: Int): ThreeState {
val stringTemplate = contextElement.prevLeaf()?.getParentOfType<KtSimpleNameStringTemplateEntry>(strict = false) ?: return ThreeState.UNSURE
// "$<caret>nameRef" stringTemplate here is "$nameRef", so offset are inside template, we should show lookup
if (offset in stringTemplate.startOffset until stringTemplate.endOffset) return ThreeState.NO
val textRange = TextRange.create(stringTemplate.endOffset, offset)
val containsWhitespaces = textRange.substring(psiFile.text).any { it.isWhitespace() }
return if (containsWhitespaces) ThreeState.UNSURE else ThreeState.NO
@@ -0,0 +1,7 @@
fun foo() {
val aaa = ""
val y = "$a<caret>aaa"
}
// ELEMENT: aaa
@@ -68,6 +68,10 @@ public class KotlinConfidenceTest extends LightCompletionTestCase {
doTest();
}
public void testAutoPopupInStringTemplateAfterDollar() {
doTest();
}
public void testNoAutoPopupInStringTemplateAfterSpace() {
doTest();
}