KT-6534 Code completion of global function does not insert import in very specific context

#KT-6534 Fixed
This commit is contained in:
Valentin Kipyatkov
2014-12-25 18:39:18 +03:00
parent eca8ed2768
commit b94e9e63c3
7 changed files with 50 additions and 20 deletions
@@ -44,6 +44,7 @@ import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue
import org.jetbrains.jet.lang.psi.psiUtil.getReceiverExpression
import org.jetbrains.jet.lang.resolve.scopes.receivers.ExpressionReceiver
import org.jetbrains.jet.lang.types.expressions.ExpressionTypingUtils
import com.intellij.openapi.editor.Document
enum class ItemPriority {
MULTIPLE_ARGUMENTS_ITEM
@@ -170,3 +171,17 @@ fun DeclarationDescriptorWithVisibility.isVisible(
}
return false
}
fun InsertionContext.isAfterDot(): Boolean {
var offset = getStartOffset()
val chars = getDocument().getCharsSequence()
while (offset > 0) {
offset--
val c = chars.charAt(offset)
if (!Character.isWhitespace(c)) {
return c == '.'
}
}
return false
}
@@ -47,6 +47,7 @@ import org.jetbrains.jet.plugin.completion.qualifiedNameForSourceCode
import org.jetbrains.jet.lexer.JetTokens
import org.jetbrains.jet.lang.psi.JetTypeArgumentList
import com.intellij.codeInsight.lookup.Lookup
import org.jetbrains.jet.plugin.completion.isAfterDot
public abstract class KotlinCallableInsertHandler : BaseDeclarationInsertHandler() {
public override fun handleInsert(context: InsertionContext, item: LookupElement) {
@@ -69,8 +70,8 @@ public abstract class KotlinCallableInsertHandler : BaseDeclarationInsertHandler
if (file is JetFile && o is DeclarationDescriptorLookupObject) {
val descriptor = o.descriptor as? CallableDescriptor
if (descriptor != null) {
if (element.getStrictParentOfType<JetQualifiedExpression>() != null &&
descriptor.getExtensionReceiverParameter() == null) {
// for completion after dot, import insertion may be required only for extensions
if (context.isAfterDot() && descriptor.getExtensionReceiverParameter() == null) {
return@runReadAction
}
@@ -18,7 +18,6 @@ package org.jetbrains.jet.plugin.completion.handlers
import com.intellij.codeInsight.completion.InsertionContext
import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.openapi.editor.Document
import com.intellij.psi.PsiDocumentManager
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.plugin.codeInsight.ShortenReferences
@@ -30,7 +29,7 @@ import org.jetbrains.jet.lang.psi.JetNameReferenceExpression
import org.jetbrains.jet.plugin.caches.resolve.getResolutionFacade
import org.jetbrains.jet.lang.resolve.BindingContext
import org.jetbrains.jet.lang.resolve.lazy.BodyResolveMode
import org.jetbrains.jet.lang.descriptors.ClassKind
import org.jetbrains.jet.plugin.completion.isAfterDot
public object KotlinClassInsertHandler : BaseDeclarationInsertHandler() {
override fun handleInsert(context: InsertionContext, item: LookupElement) {
@@ -38,12 +37,13 @@ public object KotlinClassInsertHandler : BaseDeclarationInsertHandler() {
val file = context.getFile()
if (file is JetFile) {
val startOffset = context.getStartOffset()
val document = context.getDocument()
if (!isAfterDot(document, startOffset)) {
if (!context.isAfterDot()) {
val psiDocumentManager = PsiDocumentManager.getInstance(context.getProject())
psiDocumentManager.commitAllDocuments()
val startOffset = context.getStartOffset()
val document = context.getDocument()
val qualifiedName = qualifiedNameToInsert(item)
// first try to resolve short name for faster handling
@@ -88,17 +88,4 @@ public object KotlinClassInsertHandler : BaseDeclarationInsertHandler() {
else -> error("Unknown object in LookupElement with KotlinClassInsertHandler: $lookupObject")
}
}
private fun isAfterDot(document: Document, offset: Int): Boolean {
var curOffset = offset
val chars = document.getCharsSequence()
while (curOffset > 0) {
curOffset--
val c = chars.charAt(curOffset)
if (!Character.isWhitespace(c)) {
return c == '.'
}
}
return false
}
}
@@ -0,0 +1,9 @@
object XXX {
fun authorize(handler: String.() -> Unit) { }
}
fun f() {
XXX.authorize {
globalFun<caret>
}
}
@@ -0,0 +1,3 @@
package ppp
fun globalFun(){}
@@ -0,0 +1,11 @@
import ppp.globalFun
object XXX {
fun authorize(handler: String.() -> Unit) { }
}
fun f() {
XXX.authorize {
globalFun()<caret>
}
}
@@ -66,6 +66,10 @@ public class CompletionMultifileHandlerTest extends KotlinCompletionTestCase {
doTest();
}
public void testGlobalFunctionImportInLambda() throws Exception {
doTest();
}
public void doTest() throws Exception {
String fileName = getTestName(false);