From acc55c8c2494da40d2bfe32e39c0d40a2e96e5fd Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Fri, 5 Dec 2014 23:13:56 +0100 Subject: [PATCH] Optimization --- .../handlers/KotlinClassInsertHandler.kt | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/idea/src/org/jetbrains/jet/plugin/completion/handlers/KotlinClassInsertHandler.kt b/idea/src/org/jetbrains/jet/plugin/completion/handlers/KotlinClassInsertHandler.kt index 1b86fbc8f86..0e46676a337 100644 --- a/idea/src/org/jetbrains/jet/plugin/completion/handlers/KotlinClassInsertHandler.kt +++ b/idea/src/org/jetbrains/jet/plugin/completion/handlers/KotlinClassInsertHandler.kt @@ -27,6 +27,9 @@ import org.jetbrains.jet.lang.descriptors.ClassDescriptor import org.jetbrains.jet.plugin.completion.qualifiedNameForSourceCode import com.intellij.psi.PsiClass 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.DescriptorUtils public object KotlinClassInsertHandler : BaseDeclarationInsertHandler() { override fun handleInsert(context: InsertionContext, item: LookupElement) { @@ -37,13 +40,21 @@ public object KotlinClassInsertHandler : BaseDeclarationInsertHandler() { val startOffset = context.getStartOffset() val document = context.getDocument() if (!isAfterDot(document, startOffset)) { - val qualifiedName = qualifiedNameToInsert(item) - val psiDocumentManager = PsiDocumentManager.getInstance(context.getProject()) psiDocumentManager.commitAllDocuments() + val qualifiedName = qualifiedNameToInsert(item) + + // first try to resolve short name for faster handling val token = file.findElementAt(startOffset) - val tempPrefix = if (token.getParent() is JetNameReferenceExpression) + val nameRef = token.getParent() as? JetNameReferenceExpression + if (nameRef != null) { + val bindingContext = nameRef.getResolutionFacade().analyzeWithPartialBodyResolve(nameRef) + val target = bindingContext[BindingContext.REFERENCE_TARGET, nameRef] as? ClassDescriptor + if (target != null && DescriptorUtils.getFqNameSafe(target).asString() == qualifiedName) return + } + + val tempPrefix = if (nameRef != null) " " // insert space so that any preceding spaces inserted by formatter on reference shortening are deleted else "$;val v:" // if we have no reference in the current context we have a more complicated prefix to get one