KT-6330 When completion for boolean method/property is finished by typing '!' symbol it should insert selected item with negation

#KT-6330 Fixed
This commit is contained in:
Valentin Kipyatkov
2015-05-08 14:19:05 +03:00
parent 892b2b0f83
commit 5ecf33c88e
18 changed files with 188 additions and 17 deletions
@@ -79,7 +79,7 @@ public class KotlinCompletionCharFilter() : CharFilter() {
Result.HIDE_LOOKUP
}
',', ' ', '(', '=' -> Result.SELECT_ITEM_AND_FINISH_LOOKUP
',', ' ', '(', '=', '!' -> Result.SELECT_ITEM_AND_FINISH_LOOKUP
else -> CharFilter.Result.HIDE_LOOKUP
}
@@ -142,13 +142,18 @@ class LookupElementsCollector(
getDelegate().handleInsert(context)
if (context.shouldAddCompletionChar() && !isJustTyping(context, this)) {
val handler = when (context.getCompletionChar()) {
',' -> WithTailInsertHandler.commaTail()
'=' -> WithTailInsertHandler.eqTail()
else -> null
when (context.getCompletionChar()) {
',' -> WithTailInsertHandler.commaTail().postHandleInsert(context, getDelegate())
'=' -> WithTailInsertHandler.eqTail().postHandleInsert(context, getDelegate())
'!' -> {
WithExpressionPrefixInsertHandler("!").postHandleInsert(context)
context.setAddCompletionChar(false)
}
}
handler?.postHandleInsert(context, getDelegate())
}
}
})
}
@@ -0,0 +1,55 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.completion.handlers
import com.intellij.codeInsight.completion.InsertHandler
import com.intellij.codeInsight.completion.InsertionContext
import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.psi.PsiDocumentManager
import org.jetbrains.kotlin.psi.JetCallExpression
import org.jetbrains.kotlin.psi.JetDotQualifiedExpression
import org.jetbrains.kotlin.psi.JetExpression
import org.jetbrains.kotlin.psi.JetSimpleNameExpression
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
class WithExpressionPrefixInsertHandler(val prefix: String) : InsertHandler<LookupElement> {
override fun handleInsert(context: InsertionContext, item: LookupElement) {
item.handleInsert(context)
postHandleInsert(context)
}
fun postHandleInsert(context: InsertionContext) {
PsiDocumentManager.getInstance(context.getProject()).commitAllDocuments()
val offset = context.getStartOffset()
val token = context.getFile().findElementAt(offset)!!
var expression = token.getStrictParentOfType<JetExpression>() ?: return
if (expression is JetSimpleNameExpression) {
var parent = expression.getParent()
if (parent is JetCallExpression && expression == parent.getCalleeExpression()) {
expression = parent
parent = parent.getParent()
}
if (parent is JetDotQualifiedExpression && expression == parent.getSelectorExpression()) {
expression = parent
}
}
context.getDocument().insertString(expression.getTextRange().getStartOffset(), prefix)
}
}
@@ -26,8 +26,8 @@ import com.intellij.psi.PsiDocumentManager
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.idea.caches.resolve.ResolutionFacade
import org.jetbrains.kotlin.idea.completion.*
import org.jetbrains.kotlin.idea.completion.handlers.WithExpressionPrefixInsertHandler
import org.jetbrains.kotlin.idea.completion.handlers.WithTailInsertHandler
import org.jetbrains.kotlin.idea.core.completion.DeclarationDescriptorLookupObject
import org.jetbrains.kotlin.idea.util.*
import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.psi.JetValueArgument
@@ -99,16 +99,7 @@ fun LookupElement.withOptions(options: ItemOptions): LookupElement {
}
override fun handleInsert(context: InsertionContext) {
getDelegate().handleInsert(context)
PsiDocumentManager.getInstance(context.getProject()).commitAllDocuments()
val offset = context.getStartOffset()
val token = context.getFile().findElementAt(offset)!!
val argument = token.getStrictParentOfType<JetValueArgument>()
if (argument != null) {
context.getDocument().insertString(argument.getArgumentExpression()!!.getTextRange().getStartOffset(), "*")
}
WithExpressionPrefixInsertHandler("*").handleInsert(context, getDelegate())
}
}
}
@@ -0,0 +1,6 @@
fun foo(flag: Boolean) {
if (<caret>)
}
// ELEMENT: flag
// CHAR: '!'
@@ -0,0 +1,6 @@
fun foo(flag: Boolean) {
if (!flag<caret>)
}
// ELEMENT: flag
// CHAR: '!'
@@ -0,0 +1,8 @@
fun String.checkIt(s: String): Boolean = true
fun foo(s: String) {
if (s.<caret>)
}
// ELEMENT: checkIt
// CHAR: '!'
@@ -0,0 +1,8 @@
fun String.checkIt(s: String): Boolean = true
fun foo(s: String) {
if (!s.checkIt(<caret>))
}
// ELEMENT: checkIt
// CHAR: '!'
@@ -0,0 +1,8 @@
class C(val flag: Boolean)
fun foo(c: C) {
if (c.<caret>)
}
// ELEMENT: flag
// CHAR: '!'
@@ -0,0 +1,8 @@
class C(val flag: Boolean)
fun foo(c: C) {
if (!c.flag<caret>)
}
// ELEMENT: flag
// CHAR: '!'
@@ -0,0 +1,8 @@
fun flag(): Boolean{}
fun foo() {
if (<caret>)
}
// ELEMENT: flag
// CHAR: '!'
@@ -0,0 +1,8 @@
fun flag(): Boolean{}
fun foo() {
if (!flag()<caret>)
}
// ELEMENT: flag
// CHAR: '!'
@@ -0,0 +1,6 @@
fun foo() {
if (<caret>)
}
// ELEMENT: true
// CHAR: '!'
@@ -0,0 +1,6 @@
fun foo() {
if (!true<caret>)
}
// ELEMENT: true
// CHAR: '!'
@@ -0,0 +1,6 @@
fun foo(flag: Boolean) {
if (<caret>)
}
// ELEMENT: flag
// CHAR: '!'
@@ -0,0 +1,6 @@
fun foo(flag: Boolean) {
if (!flag)<caret>
}
// ELEMENT: flag
// CHAR: '!'
@@ -47,6 +47,36 @@ public class BasicCompletionHandlerTestGenerated extends AbstractBasicCompletion
doTest(fileName);
}
@TestMetadata("ExclChar1.kt")
public void testExclChar1() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/ExclChar1.kt");
doTest(fileName);
}
@TestMetadata("ExclChar2.kt")
public void testExclChar2() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/ExclChar2.kt");
doTest(fileName);
}
@TestMetadata("ExclChar3.kt")
public void testExclChar3() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/ExclChar3.kt");
doTest(fileName);
}
@TestMetadata("ExclChar4.kt")
public void testExclChar4() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/ExclChar4.kt");
doTest(fileName);
}
@TestMetadata("ExclChar5.kt")
public void testExclChar5() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/ExclChar5.kt");
doTest(fileName);
}
@TestMetadata("ExtensionReceiverTypeArg.kt")
public void testExtensionReceiverTypeArg() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/ExtensionReceiverTypeArg.kt");
@@ -323,6 +323,12 @@ public class SmartCompletionHandlerTestGenerated extends AbstractSmartCompletion
doTest(fileName);
}
@TestMetadata("ExclChar.kt")
public void testExclChar() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/smart/ExclChar.kt");
doTest(fileName);
}
@TestMetadata("ForLoopRange.kt")
public void testForLoopRange() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/smart/ForLoopRange.kt");