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:
+5
@@ -25,10 +25,15 @@ import org.jetbrains.kotlin.psi.KtSimpleNameStringTemplateEntry
|
|||||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.prevLeaf
|
import org.jetbrains.kotlin.psi.psiUtil.prevLeaf
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
|
|
||||||
class EnableAutopopupInStringTemplate : CompletionConfidence() {
|
class EnableAutopopupInStringTemplate : CompletionConfidence() {
|
||||||
override fun shouldSkipAutopopup(contextElement: PsiElement, psiFile: PsiFile, offset: Int): ThreeState {
|
override fun shouldSkipAutopopup(contextElement: PsiElement, psiFile: PsiFile, offset: Int): ThreeState {
|
||||||
val stringTemplate = contextElement.prevLeaf()?.getParentOfType<KtSimpleNameStringTemplateEntry>(strict = false) ?: return ThreeState.UNSURE
|
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 textRange = TextRange.create(stringTemplate.endOffset, offset)
|
||||||
val containsWhitespaces = textRange.substring(psiFile.text).any { it.isWhitespace() }
|
val containsWhitespaces = textRange.substring(psiFile.text).any { it.isWhitespace() }
|
||||||
return if (containsWhitespaces) ThreeState.UNSURE else ThreeState.NO
|
return if (containsWhitespaces) ThreeState.UNSURE else ThreeState.NO
|
||||||
|
|||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
fun foo() {
|
||||||
|
val aaa = ""
|
||||||
|
val y = "$a<caret>aaa"
|
||||||
|
}
|
||||||
|
|
||||||
|
// ELEMENT: aaa
|
||||||
+4
@@ -68,6 +68,10 @@ public class KotlinConfidenceTest extends LightCompletionTestCase {
|
|||||||
doTest();
|
doTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testAutoPopupInStringTemplateAfterDollar() {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
public void testNoAutoPopupInStringTemplateAfterSpace() {
|
public void testNoAutoPopupInStringTemplateAfterSpace() {
|
||||||
doTest();
|
doTest();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user