Completion: the same universal insertion handler is used for java classes too

This commit is contained in:
Valentin Kipyatkov
2014-10-08 18:28:50 +04:00
parent 38a5b988f0
commit d8b962c7f3
3 changed files with 12 additions and 48 deletions
@@ -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 {
@@ -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()
@@ -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<JavaPsiClassReferenceElement> {
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
}
}