Completion: escaping of named arguments
This commit is contained in:
@@ -36,7 +36,8 @@ import com.intellij.psi.filters.ClassFilter
|
||||
import org.jetbrains.jet.plugin.util.FirstChildInParentFilter
|
||||
import org.jetbrains.jet.lang.psi.psiUtil.getCallNameExpression
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.jet.plugin.completion.handlers.BaseDeclarationInsertHandler
|
||||
import com.intellij.codeInsight.completion.InsertHandler
|
||||
import org.jetbrains.jet.lang.resolve.name.Name
|
||||
|
||||
object NamedParametersCompletion {
|
||||
private val positionFilter = AndFilter(
|
||||
@@ -83,13 +84,14 @@ object NamedParametersCompletion {
|
||||
val usedArguments = QuickFixUtil.getUsedParameters(callElement, valueArgument, funDescriptor)
|
||||
|
||||
for (parameter in funDescriptor.getValueParameters()) {
|
||||
val name = parameter.getName().asString()
|
||||
if (name !in usedArguments) {
|
||||
val lookupElement = LookupElementBuilder.create(parameter, name)
|
||||
.withPresentableText("$name = ")
|
||||
.withTailText("${DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(parameter.getType())}")
|
||||
val name = parameter.getName()
|
||||
val nameString = name.asString()
|
||||
if (nameString !in usedArguments) {
|
||||
val text = "$nameString ="
|
||||
val lookupElement = LookupElementBuilder.create(text)
|
||||
.withTailText(" ${DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(parameter.getType())}")
|
||||
.withIcon(JetIcons.PARAMETER)
|
||||
.withInsertHandler(NamedParameterInsertHandler)
|
||||
.withInsertHandler(NamedParameterInsertHandler(name))
|
||||
.assignPriority(ItemPriority.NAMED_PARAMETER)
|
||||
lookupElement.putUserData(JetCompletionCharFilter.ACCEPT_EQ, true);
|
||||
|
||||
@@ -99,7 +101,7 @@ object NamedParametersCompletion {
|
||||
}
|
||||
}
|
||||
|
||||
private object NamedParameterInsertHandler : BaseDeclarationInsertHandler() {
|
||||
private class NamedParameterInsertHandler(val parameterName: Name) : InsertHandler<LookupElement> {
|
||||
override fun handleInsert(context: InsertionContext, item: LookupElement) {
|
||||
val ch = context.getCompletionChar()
|
||||
if (ch == '=' || ch == ' ') {
|
||||
@@ -107,10 +109,9 @@ object NamedParametersCompletion {
|
||||
}
|
||||
|
||||
val editor = context.getEditor()
|
||||
val tailOffset = context.getTailOffset()
|
||||
|
||||
editor.getDocument().insertString(tailOffset, " = ")
|
||||
editor.getCaretModel().moveToOffset(tailOffset + 3)
|
||||
val text = DescriptorRenderer.SOURCE_CODE.renderName(parameterName) + " = "
|
||||
editor.getDocument().replaceString(context.getStartOffset(), context.getTailOffset(), text)
|
||||
editor.getCaretModel().moveToOffset(context.getStartOffset() + text.length)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,4 +3,4 @@ fun small(param: Int) {
|
||||
|
||||
fun test() = small(<caret>)
|
||||
|
||||
// EXIST: {"lookupString":"param","tailText":"Int","itemText":"param = "}
|
||||
// EXIST: {"lookupString":"param =","tailText":" Int","itemText":"param ="}
|
||||
@@ -3,5 +3,5 @@ fun small(first: Int, second: Int) {
|
||||
|
||||
fun test() = small(12, <caret>)
|
||||
|
||||
// ABSENT: first
|
||||
// EXIST: second
|
||||
// ABSENT: "first ="
|
||||
// EXIST: "second ="
|
||||
@@ -7,5 +7,5 @@ fun small(paramFirst: ArrayList<String>, paramSecond: Comparable<java.lang.Runti
|
||||
|
||||
fun test() = small(<caret>)
|
||||
|
||||
// EXIST: {"lookupString":"paramSecond","tailText":"Comparable<RuntimeException>","itemText":"paramSecond = "}
|
||||
// EXIST: {"lookupString":"paramFirst","tailText":"ArrayList<String>","itemText":"paramFirst = "}
|
||||
// EXIST: {"lookupString":"paramSecond =","tailText":" Comparable<RuntimeException>","itemText":"paramSecond ="}
|
||||
// EXIST: {"lookupString":"paramFirst =","tailText":" ArrayList<String>","itemText":"paramFirst ="}
|
||||
@@ -6,6 +6,6 @@ fun small(paramFirst: Int, paramSecond: Int) {
|
||||
fun test() = small(paramFirst = param<caret>)
|
||||
|
||||
// EXIST: paramTest
|
||||
// ABSENT: {"lookupString":"paramFirst","tailText":"Int","itemText":"paramFirst = "}
|
||||
// ABSENT: paramSecond
|
||||
// ABSENT: {"lookupString":"paramFirst =","tailText":" Int","itemText":"paramFirst ="}
|
||||
// ABSENT: "paramSecond ="
|
||||
// NUMBER: 1
|
||||
+5
-5
@@ -10,11 +10,11 @@ fun other() {
|
||||
foo(n<caret>)
|
||||
}
|
||||
|
||||
// EXIST: nFirst
|
||||
// EXIST: nSecond
|
||||
// EXIST: nThird
|
||||
// EXIST: "nFirst ="
|
||||
// EXIST: "nSecond ="
|
||||
// EXIST: "nThird ="
|
||||
// EXIST: nLocal
|
||||
|
||||
// todo - should exist
|
||||
// ABSENT: nClassParam
|
||||
// ABSENT: nClassField
|
||||
// ABSENT: "nClassParam ="
|
||||
// ABSENT: "nClassField ="
|
||||
@@ -6,5 +6,5 @@ fun other() {
|
||||
Foo(p<caret>)
|
||||
}
|
||||
|
||||
// EXIST: {"lookupString":"pFirst","tailText":"String?","itemText":"pFirst = "}
|
||||
// EXIST: {"lookupString":"pSecond","tailText":"Int","itemText":"pSecond = "}
|
||||
// EXIST: {"lookupString":"pFirst =","tailText":" String?","itemText":"pFirst ="}
|
||||
// EXIST: {"lookupString":"pSecond =","tailText":" Int","itemText":"pSecond ="}
|
||||
@@ -4,8 +4,8 @@ fun test() {
|
||||
foo("str", paramThird = "test", param<caret>)
|
||||
}
|
||||
|
||||
// ABSENT: paramFirst
|
||||
// EXIST: paramSecond
|
||||
// ABSENT: paramThird
|
||||
// EXIST: paramFourth
|
||||
// ABSENT: "paramFirst ="
|
||||
// EXIST: "paramSecond ="
|
||||
// ABSENT: "paramThird ="
|
||||
// EXIST: "paramFourth ="
|
||||
|
||||
|
||||
+4
-4
@@ -5,7 +5,7 @@ fun test() {
|
||||
foo(12, param<caret>)
|
||||
}
|
||||
|
||||
// ABSENT: paramFirst
|
||||
// EXIST: {"lookupString":"paramSecond","tailText":"Int","itemText":"paramSecond = "}
|
||||
// ABSENT: paramThird
|
||||
// ABSENT: paramFourth
|
||||
// ABSENT: "paramFirst ="
|
||||
// EXIST: {"lookupString":"paramSecond =","tailText":" Int","itemText":"paramSecond ="}
|
||||
// ABSENT: "paramThird ="
|
||||
// ABSENT: "paramFourth ="
|
||||
@@ -4,6 +4,6 @@ fun foo(first: Int, second: Int, third: String) {
|
||||
fun test(p: Int) = foo(12, <caret>, third = "")
|
||||
|
||||
// EXIST: p
|
||||
// ABSENT: first
|
||||
// EXIST: third
|
||||
// EXIST: second
|
||||
// ABSENT: "first ="
|
||||
// EXIST: "third ="
|
||||
// EXIST: "second ="
|
||||
|
||||
@@ -4,6 +4,6 @@ fun foo(first: Int, second: Int, third: String) {
|
||||
fun test(p: Int) = foo(<caret>second = 3)
|
||||
|
||||
// EXIST: p
|
||||
// EXIST: first
|
||||
// EXIST: second
|
||||
// EXIST: third
|
||||
// EXIST: "first ="
|
||||
// EXIST: "second ="
|
||||
// EXIST: "third ="
|
||||
|
||||
@@ -4,7 +4,7 @@ fun foo(first: Int, second: Int, third: String) {
|
||||
fun test(p: Int) = foo(12, third = "", <caret>)
|
||||
|
||||
// ABSENT: p
|
||||
// ABSENT: first
|
||||
// ABSENT: third
|
||||
// EXIST: second
|
||||
// ABSENT: "first ="
|
||||
// ABSENT: "third ="
|
||||
// EXIST: "second ="
|
||||
// NUMBER: 1
|
||||
@@ -5,8 +5,8 @@ fun small(paramFirst: Int, paramSecond: Int) {
|
||||
|
||||
fun test() = small(param<caret>First = 12)
|
||||
|
||||
// EXIST: paramFirst
|
||||
// EXIST: paramSecond
|
||||
// EXIST: "paramFirst ="
|
||||
// EXIST: "paramSecond ="
|
||||
// EXIST: paramTest
|
||||
|
||||
// NUMBER: 3
|
||||
@@ -2,5 +2,5 @@ import lib.KotlinClass
|
||||
|
||||
fun test() = KotlinClass().foo(<caret>)
|
||||
|
||||
// ABSENT: p0
|
||||
// EXIST: paramName
|
||||
// ABSENT: "p0 ="
|
||||
// EXIST: "paramName ="
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo(`class`: Int) {}
|
||||
|
||||
fun test() {
|
||||
foo(<caret>)
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo(`class`: Int) {}
|
||||
|
||||
fun test() {
|
||||
foo(`class` = <caret>)
|
||||
}
|
||||
@@ -18,5 +18,5 @@ fun test(listParam: Int) {
|
||||
// ORDER: listImportedVal
|
||||
// ORDER: listThisFileFun
|
||||
// ORDER: listImportedFun
|
||||
// ORDER: listMatch
|
||||
// ORDER: listNew
|
||||
// ORDER: "listMatch ="
|
||||
// ORDER: "listNew ="
|
||||
|
||||
@@ -48,7 +48,9 @@ public class BasicCompletionHandlerTest : CompletionHandlerTestBase(){
|
||||
|
||||
fun testNamedParametersCompletion() = doTest()
|
||||
|
||||
fun testNamedParametersCompletionOnEqual() = doTest(0, "paramTest", null, '=')
|
||||
fun testNamedParametersCompletionOnEqual() = doTest(0, "paramTest =", null, '=')
|
||||
|
||||
fun testNamedParameterKeywordName() = doTest(1, "class =", null, '\n')
|
||||
|
||||
fun testInsertJavaClassImport() = doTest()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user