Shorten references inserts imports for global functions and properties too
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* 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.imports
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.descriptors.PackageViewDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.psi.JetSimpleNameExpression
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getReceiverExpression
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.types.JetType
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.getImportableDescriptor
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
|
||||
|
||||
public val DeclarationDescriptor.importableFqName: FqName?
|
||||
get() {
|
||||
val mayBeUnsafe = DescriptorUtils.getFqName(getImportableDescriptor())
|
||||
return if (mayBeUnsafe.isSafe()) mayBeUnsafe.toSafe() else null
|
||||
}
|
||||
|
||||
public val DeclarationDescriptor.importableFqNameSafe: FqName
|
||||
get() = DescriptorUtils.getFqNameSafe(getImportableDescriptor())
|
||||
|
||||
public fun DeclarationDescriptor.canBeReferencedViaImport(): Boolean {
|
||||
if (this is PackageViewDescriptor ||
|
||||
DescriptorUtils.isTopLevelDeclaration(this) ||
|
||||
(this is CallableDescriptor && DescriptorUtils.isStaticDeclaration(this))) {
|
||||
return !getName().isSpecial()
|
||||
}
|
||||
val parent = getContainingDeclaration()!!
|
||||
if (parent !is ClassDescriptor || !parent.canBeReferencedViaImport()) {
|
||||
return false
|
||||
}
|
||||
// inner class constructors can't be referenced via import
|
||||
if (this is ConstructorDescriptor && parent.isInner()) {
|
||||
return false
|
||||
}
|
||||
return this is ClassDescriptor || this is ConstructorDescriptor
|
||||
}
|
||||
|
||||
public fun JetType.canBeReferencedViaImport(): Boolean {
|
||||
val descriptor = getConstructor().getDeclarationDescriptor()
|
||||
return descriptor != null && descriptor.canBeReferencedViaImport()
|
||||
}
|
||||
|
||||
public fun isInReceiverScope(referenceElement: PsiElement, referencedDescriptor: DeclarationDescriptor): Boolean {
|
||||
val isExpressionWithReceiver = referenceElement is JetSimpleNameExpression && referenceElement.getReceiverExpression() != null
|
||||
return isExpressionWithReceiver && !referencedDescriptor.isExtension
|
||||
}
|
||||
@@ -40,6 +40,7 @@ import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
|
||||
import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
|
||||
import org.jetbrains.kotlin.resolve.scopes.receivers.ThisReceiver
|
||||
import org.jetbrains.kotlin.idea.util.ShortenReferences.Options
|
||||
import org.jetbrains.kotlin.idea.imports.*
|
||||
|
||||
public class ShortenReferences(val options: (JetElement) -> Options = { Options.DEFAULT }) {
|
||||
public data class Options(
|
||||
@@ -61,25 +62,12 @@ public class ShortenReferences(val options: (JetElement) -> Options = { Options.
|
||||
val targets = context[BindingContext.REFERENCE_TARGET, this]?.let { listOf(it) }
|
||||
?: context[BindingContext.AMBIGUOUS_REFERENCE_TARGET, this]
|
||||
?: listOf()
|
||||
return targets.map { descriptorToImport(it) }.toSet()
|
||||
}
|
||||
|
||||
private fun descriptorToImport(target: DeclarationDescriptor): DeclarationDescriptor {
|
||||
val descriptor = target.getImportableDescriptor()
|
||||
// if there is a class with the same fq-name then prefer to consider it as target (otherwise we won't insert import)
|
||||
if (descriptor is CallableDescriptor) {
|
||||
val container = descriptor.getContainingDeclaration()
|
||||
if (container is PackageFragmentDescriptor) {
|
||||
val classifier = container.getMemberScope().getClassifier(descriptor.getName())
|
||||
if (classifier != null) return classifier
|
||||
}
|
||||
}
|
||||
return descriptor
|
||||
return targets.map { it.getImportableDescriptor() }.toSet()
|
||||
}
|
||||
|
||||
private fun mayImport(descriptor: DeclarationDescriptor, file: JetFile): Boolean {
|
||||
if (descriptor !is ClassDescriptor && descriptor !is PackageViewDescriptor) return false
|
||||
return ImportInsertHelper.getInstance(file.getProject()).mayImportByCodeStyle(descriptor)
|
||||
return descriptor.canBeReferencedViaImport()
|
||||
&& ImportInsertHelper.getInstance(file.getProject()).mayImportByCodeStyle(descriptor)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user