KT-9388 Code completion inserts use-site annotation target with superfluous chars

#KT-9388 Fixed
This commit is contained in:
Valentin Kipyatkov
2015-10-21 17:37:32 +03:00
parent fabf3e5e84
commit 6cfb30bb81
10 changed files with 56 additions and 10 deletions
@@ -32,8 +32,10 @@ import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget.* import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget.*
import org.jetbrains.kotlin.idea.completion.handlers.KotlinFunctionInsertHandler import org.jetbrains.kotlin.idea.completion.handlers.KotlinFunctionInsertHandler
import org.jetbrains.kotlin.idea.completion.handlers.KotlinKeywordInsertHandler import org.jetbrains.kotlin.idea.completion.handlers.KotlinKeywordInsertHandler
import org.jetbrains.kotlin.idea.completion.handlers.UseSiteAnnotationTargetInsertHandler
import org.jetbrains.kotlin.lexer.KtKeywordToken import org.jetbrains.kotlin.lexer.KtKeywordToken
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.lexer.KtTokens.* import org.jetbrains.kotlin.lexer.KtTokens.*
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.* import org.jetbrains.kotlin.psi.psiUtil.*
@@ -83,12 +85,16 @@ object KeywordCompletion {
if (!parserFilter(keywordToken)) continue if (!parserFilter(keywordToken)) continue
val insertHandler = if (position.prevLeaf()?.node?.elementType == KtTokens.AT)
UseSiteAnnotationTargetInsertHandler
else if (keywordToken !in FUNCTION_KEYWORDS)
KotlinKeywordInsertHandler
else
KotlinFunctionInsertHandler.Normal(inputTypeArguments = false, inputValueArguments = false)
val element = LookupElementBuilder.create(KeywordLookupObject(), keyword) val element = LookupElementBuilder.create(KeywordLookupObject(), keyword)
.bold() .bold()
.withInsertHandler(if (keywordToken !in FUNCTION_KEYWORDS) .withInsertHandler(insertHandler)
KotlinKeywordInsertHandler
else
KotlinFunctionInsertHandler.Normal(inputTypeArguments = false, inputValueArguments = false))
consumer(element) consumer(element)
} }
} }
@@ -42,12 +42,15 @@ object KotlinKeywordInsertHandler : InsertHandler<LookupElement> {
DYNAMIC_KEYWORD).map { it.getValue() } + "companion object" DYNAMIC_KEYWORD).map { it.getValue() } + "companion object"
override fun handleInsert(context: InsertionContext, item: LookupElement) { override fun handleInsert(context: InsertionContext, item: LookupElement) {
val keyword = item.getLookupString() val keyword = item.lookupString
if (keyword == FILE_KEYWORD.getValue()) { if (keyword !in NO_SPACE_AFTER) {
WithTailInsertHandler.colonTail().postHandleInsert(context, item)
}
else if (keyword !in NO_SPACE_AFTER) {
WithTailInsertHandler.spaceTail().postHandleInsert(context, item) WithTailInsertHandler.spaceTail().postHandleInsert(context, item)
} }
} }
} }
object UseSiteAnnotationTargetInsertHandler : InsertHandler<LookupElement> {
override fun handleInsert(context: InsertionContext, item: LookupElement) {
WithTailInsertHandler(":", spaceBefore = false, spaceAfter = false).postHandleInsert(context, item)
}
}
@@ -93,6 +93,5 @@ class WithTailInsertHandler(val tailText: String,
fun elseTail() = WithTailInsertHandler("else", spaceBefore = true, spaceAfter = true) fun elseTail() = WithTailInsertHandler("else", spaceBefore = true, spaceAfter = true)
fun eqTail() = WithTailInsertHandler("=", spaceBefore = true, spaceAfter = true) /*TODO: use code style options*/ fun eqTail() = WithTailInsertHandler("=", spaceBefore = true, spaceAfter = true) /*TODO: use code style options*/
fun spaceTail() = WithTailInsertHandler(" ", spaceBefore = false, spaceAfter = false, overwriteText = false) fun spaceTail() = WithTailInsertHandler(" ", spaceBefore = false, spaceAfter = false, overwriteText = false)
fun colonTail() = WithTailInsertHandler(":", spaceBefore = false, spaceAfter = true)
} }
} }
@@ -0,0 +1,3 @@
class C(@<caret> val p: String)
// ELEMENT: get
@@ -0,0 +1,3 @@
class C(@get:<caret> val p: String)
// ELEMENT: get
@@ -0,0 +1,3 @@
class C(@<caret>Ann var p: String)
// ELEMENT: set
@@ -0,0 +1,3 @@
class C(@set:<caret>Ann var p: String)
// ELEMENT: set
@@ -0,0 +1,4 @@
class C(@<caret>get:Ann var p: String)
// ELEMENT: set
// CHAR: '\t'
@@ -0,0 +1,4 @@
class C(@set:<caret>Ann var p: String)
// ELEMENT: set
// CHAR: '\t'
@@ -124,4 +124,22 @@ public class KeywordCompletionHandlerTestGenerated extends AbstractKeywordComple
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/keywords/SpaceAfterImport.kt"); String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/keywords/SpaceAfterImport.kt");
doTest(fileName); doTest(fileName);
} }
@TestMetadata("UseSiteAnnotationTarget1.kt")
public void testUseSiteAnnotationTarget1() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/keywords/UseSiteAnnotationTarget1.kt");
doTest(fileName);
}
@TestMetadata("UseSiteAnnotationTarget2.kt")
public void testUseSiteAnnotationTarget2() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/keywords/UseSiteAnnotationTarget2.kt");
doTest(fileName);
}
@TestMetadata("UseSiteAnnotationTarget3.kt")
public void testUseSiteAnnotationTarget3() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/keywords/UseSiteAnnotationTarget3.kt");
doTest(fileName);
}
} }