Fix AssertionError on completing after template in string literal

#KT-16402 Fixed
This commit is contained in:
Toshiaki Kameyama
2017-09-18 15:26:41 +09:00
committed by Simon Ogorodnik
parent 0560ba7929
commit 27e7f13335
7 changed files with 52 additions and 3 deletions
@@ -333,9 +333,7 @@ class KotlinCompletionContributor : CompletionContributor() {
val psiDocumentManager = PsiDocumentManager.getInstance(context.project)
psiDocumentManager.commitAllDocuments()
assert(startOffset > 1 && document.charsSequence[startOffset - 1] == '.')
val token = context.file.findElementAt(startOffset - 2)!!
assert(token.node.elementType == KtTokens.IDENTIFIER || token.node.elementType == KtTokens.THIS_KEYWORD)
val token = getToken(context.file, document.charsSequence, startOffset)
val nameRef = token.parent as KtNameReferenceExpression
document.insertString(nameRef.startOffset, "{")
@@ -346,6 +344,15 @@ class KotlinCompletionContributor : CompletionContributor() {
super.handleInsert(context)
}
private fun getToken(file: PsiFile, charsSequence: CharSequence, startOffset: Int): PsiElement {
assert(startOffset > 1 && charsSequence[startOffset - 1] == '.')
val token = file.findElementAt(startOffset - 2)!!
return if (token.node.elementType == KtTokens.IDENTIFIER || token.node.elementType == KtTokens.THIS_KEYWORD)
token
else
getToken(file, charsSequence, token.startOffset + 1)
}
}
}
@@ -0,0 +1,8 @@
class Foo(val bar: Bar)
class Bar(val baz: String)
fun foo(foo: Foo) {
val s = "$foo.bar.<caret>"
}
// EXIST: baz
@@ -0,0 +1,8 @@
class Foo(val bar: Bar)
class Bar(val baz: String)
fun foo(foo: Foo) {
val s = "$foo.bar.<caret>"
}
// ELEMENT: baz
@@ -0,0 +1,8 @@
class Foo(val bar: Bar)
class Bar(val baz: String)
fun foo(foo: Foo) {
val s = "${foo.bar.baz}"
}
// ELEMENT: baz
@@ -1825,6 +1825,12 @@ public class JSBasicCompletionTestGenerated extends AbstractJSBasicCompletionTes
doTest(fileName);
}
@TestMetadata("StringTemplateDot2.kt")
public void testStringTemplateDot2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/basic/common/inStringLiterals/StringTemplateDot2.kt");
doTest(fileName);
}
@TestMetadata("StringTemplateDotSomething.kt")
public void testStringTemplateDotSomething() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/basic/common/inStringLiterals/StringTemplateDotSomething.kt");
@@ -1825,6 +1825,12 @@ public class JvmBasicCompletionTestGenerated extends AbstractJvmBasicCompletionT
doTest(fileName);
}
@TestMetadata("StringTemplateDot2.kt")
public void testStringTemplateDot2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/basic/common/inStringLiterals/StringTemplateDot2.kt");
doTest(fileName);
}
@TestMetadata("StringTemplateDotSomething.kt")
public void testStringTemplateDotSomething() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/basic/common/inStringLiterals/StringTemplateDotSomething.kt");
@@ -835,6 +835,12 @@ public class BasicCompletionHandlerTestGenerated extends AbstractBasicCompletion
doTest(fileName);
}
@TestMetadata("AfterDot4.kt")
public void testAfterDot4() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/stringTemplate/AfterDot4.kt");
doTest(fileName);
}
@TestMetadata("AfterThisDot.kt")
public void testAfterThisDot() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/stringTemplate/AfterThisDot.kt");