diff --git a/idea/src/org/jetbrains/jet/plugin/completion/KotlinLookupElementFactory.kt b/idea/src/org/jetbrains/jet/plugin/completion/KotlinLookupElementFactory.kt index 5cdbff17ac5..cf2cfe58c20 100644 --- a/idea/src/org/jetbrains/jet/plugin/completion/KotlinLookupElementFactory.kt +++ b/idea/src/org/jetbrains/jet/plugin/completion/KotlinLookupElementFactory.kt @@ -37,7 +37,6 @@ import org.jetbrains.jet.plugin.completion.handlers.JetPropertyInsertHandler import com.intellij.psi.PsiClass import org.jetbrains.jet.asJava.KotlinLightClass import org.jetbrains.jet.lang.resolve.java.JavaResolverPsiUtils -import org.jetbrains.jet.plugin.completion.handlers.KotlinJavaClassInsertHandler import com.intellij.codeInsight.completion.JavaPsiClassReferenceElement public object KotlinLookupElementFactory { @@ -50,7 +49,7 @@ public object KotlinLookupElementFactory { } public fun createLookupElementForJavaClass(psiClass: PsiClass): LookupElement { - return JavaPsiClassReferenceElement(psiClass).setInsertHandler(KotlinJavaClassInsertHandler) + return JavaPsiClassReferenceElement(psiClass).setInsertHandler(KotlinClassInsertHandler) } private fun createLookupElement(analyzer: KotlinCodeAnalyzer, descriptor: DeclarationDescriptor, declaration: PsiElement?): LookupElement { 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 8e623de5eca..197ed9d6a56 100644 --- a/idea/src/org/jetbrains/jet/plugin/completion/handlers/KotlinClassInsertHandler.kt +++ b/idea/src/org/jetbrains/jet/plugin/completion/handlers/KotlinClassInsertHandler.kt @@ -25,6 +25,7 @@ import org.jetbrains.jet.plugin.codeInsight.ShortenReferences import org.jetbrains.jet.plugin.completion.DeclarationDescriptorLookupObject import org.jetbrains.jet.lang.descriptors.ClassDescriptor import org.jetbrains.jet.plugin.completion.qualifiedNameForSourceCode +import com.intellij.psi.PsiClass public object KotlinClassInsertHandler : BaseDeclarationInsertHandler() { override fun handleInsert(context: InsertionContext, item: LookupElement) { @@ -32,11 +33,10 @@ public object KotlinClassInsertHandler : BaseDeclarationInsertHandler() { val file = context.getFile() if (file is JetFile) { - val descriptor = (item.getObject() as DeclarationDescriptorLookupObject).descriptor as ClassDescriptor val startOffset = context.getStartOffset() val document = context.getDocument() if (!isAfterDot(document, startOffset)) { - val qualifiedName = qualifiedNameForSourceCode(descriptor)!! + val qualifiedName = qualifiedNameToInsert(item) // insert dot after because otherwise parser can sometimes produce no suitable reference here val tempSuffix = ".xxx" // we add "xxx" after dot because of some bugs in resolve (see KT-5145) document.replaceString(startOffset, context.getTailOffset(), qualifiedName + tempSuffix) @@ -57,6 +57,15 @@ public object KotlinClassInsertHandler : BaseDeclarationInsertHandler() { } } + private fun qualifiedNameToInsert(item: LookupElement): String { + val lookupObject = item.getObject() + return when (lookupObject) { + is DeclarationDescriptorLookupObject -> qualifiedNameForSourceCode(lookupObject.descriptor as ClassDescriptor)!! + is PsiClass -> lookupObject.getQualifiedName()!! + else -> error("Unknown object in LookupElement with KotlinClassInsertHandler: $lookupObject") + } + } + private fun isAfterDot(document: Document, offset: Int): Boolean { var curOffset = offset val chars = document.getCharsSequence() diff --git a/idea/src/org/jetbrains/jet/plugin/completion/handlers/KotlinJavaClassInsertHandler.kt b/idea/src/org/jetbrains/jet/plugin/completion/handlers/KotlinJavaClassInsertHandler.kt deleted file mode 100644 index e50fd691452..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/completion/handlers/KotlinJavaClassInsertHandler.kt +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2010-2014 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.jet.plugin.completion.handlers - -import com.intellij.codeInsight.completion.InsertHandler -import com.intellij.codeInsight.completion.InsertionContext -import com.intellij.codeInsight.completion.JavaPsiClassReferenceElement -import com.intellij.psi.PsiDocumentManager -import org.jetbrains.jet.lang.psi.JetFile -import org.jetbrains.jet.lang.resolve.name.FqName -import org.jetbrains.jet.plugin.quickfix.ImportInsertHelper - -/** - * Handler for inserting java class completion. - * - Should place import directive if necessary. - */ -object KotlinJavaClassInsertHandler : InsertHandler { - override fun handleInsert(context: InsertionContext, item: JavaPsiClassReferenceElement) { - PsiDocumentManager.getInstance(context.getProject()).commitAllDocuments() - - val file = context.getFile() - if (file is JetFile) { - ImportInsertHelper.addImportDirectiveOrChangeToFqName(FqName(item.getQualifiedName()!!), file, context.getStartOffset(), item.getObject()) - } - - // check annotation - // check auto insert parentheses - // check auto insert type parameters - } -}