No getFileKtScope
This commit is contained in:
@@ -43,7 +43,7 @@ public class DeclarationResolver(
|
||||
) {
|
||||
|
||||
public fun resolveAnnotationsOnFiles(c: TopDownAnalysisContext, scopeProvider: FileScopeProvider) {
|
||||
val filesToScope = c.getFiles().keysToMap { scopeProvider.getFileScopeChain(it) }
|
||||
val filesToScope = c.getFiles().keysToMap { scopeProvider.getFileResolutionScope(it) }
|
||||
for ((file, fileScope) in filesToScope) {
|
||||
annotationResolver.resolveAnnotationsWithArguments(fileScope, file.getAnnotationEntries(), trace)
|
||||
annotationResolver.resolveAnnotationsWithArguments(fileScope, file.getDanglingAnnotations(), trace)
|
||||
|
||||
@@ -27,7 +27,7 @@ class SingleImportScope(private val aliasName: Name, private val descriptors: Co
|
||||
override fun getDeclaredClassifier(name: Name, location: LookupLocation)
|
||||
= if (name == aliasName) descriptors.filterIsInstance<ClassifierDescriptor>().singleOrNull() else null
|
||||
|
||||
override fun getPackage(name: Name)
|
||||
override fun getContributedPackage(name: Name)
|
||||
= if (name == aliasName) descriptors.filterIsInstance<PackageViewDescriptor>().singleOrNull() else null
|
||||
|
||||
override fun getDeclaredVariables(name: Name, location: LookupLocation)
|
||||
|
||||
+1
-1
@@ -55,7 +55,7 @@ public class DeclarationScopeProviderImpl implements DeclarationScopeProvider {
|
||||
}
|
||||
|
||||
if (parentDeclaration == null) {
|
||||
return fileScopeProvider.getFileScopeChain((KtFile) elementOfDeclaration.getContainingFile());
|
||||
return fileScopeProvider.getFileResolutionScope((KtFile) elementOfDeclaration.getContainingFile());
|
||||
}
|
||||
|
||||
if (parentDeclaration instanceof KtClassOrObject) {
|
||||
|
||||
@@ -20,11 +20,11 @@ import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.resolve.scopes.ImportingScope
|
||||
|
||||
public interface FileScopeProvider {
|
||||
fun getFileScopeChain(file: KtFile): ImportingScope
|
||||
fun getFileResolutionScope(file: KtFile): ImportingScope
|
||||
fun getImportResolver(file: KtFile): ImportResolver
|
||||
|
||||
public object ThrowException : FileScopeProvider {
|
||||
override fun getFileScopeChain(file: KtFile) = throw UnsupportedOperationException("Should not be called")
|
||||
override fun getFileResolutionScope(file: KtFile) = throw UnsupportedOperationException("Should not be called")
|
||||
override fun getImportResolver(file: KtFile) = throw UnsupportedOperationException("Should not be called")
|
||||
}
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ public class FileScopeProviderImpl(
|
||||
|
||||
private val cache = storageManager.createMemoizedFunction { file: KtFile -> createScopeChainAndImportResolver(file) }
|
||||
|
||||
override fun getFileScopeChain(file: KtFile) = cache(file).scopeChain
|
||||
override fun getFileResolutionScope(file: KtFile) = cache(file).scopeChain
|
||||
|
||||
override fun getImportResolver(file: KtFile) = cache(file).importResolver
|
||||
|
||||
|
||||
@@ -191,10 +191,7 @@ class LazyImportScope(
|
||||
}
|
||||
}
|
||||
|
||||
override fun getPackage(name: Name): PackageViewDescriptor? {
|
||||
if (filteringKind == FilteringKind.INVISIBLE_CLASSES) return null
|
||||
return importResolver.selectSingleFromImports(name) { scope, name -> scope.getPackage(name) }
|
||||
}
|
||||
override fun getContributedPackage(name: Name) = null
|
||||
|
||||
override fun getDeclaredVariables(name: Name, location: LookupLocation): Collection<VariableDescriptor> {
|
||||
if (filteringKind == FilteringKind.INVISIBLE_CLASSES) return listOf()
|
||||
|
||||
@@ -208,7 +208,7 @@ public class ResolveSession implements KotlinCodeAnalyzer, LazyClassContext {
|
||||
}
|
||||
|
||||
private LazyAnnotations createAnnotations(KtFile file, List<KtAnnotationEntry> annotationEntries) {
|
||||
ImportingScope scope = fileScopeProvider.getFileScopeChain(file);
|
||||
ImportingScope scope = fileScopeProvider.getFileResolutionScope(file);
|
||||
LazyAnnotationsContextImpl lazyAnnotationContext =
|
||||
new LazyAnnotationsContextImpl(annotationResolve, storageManager, trace, scope);
|
||||
return new LazyAnnotations(lazyAnnotationContext, annotationEntries);
|
||||
|
||||
+1
-1
@@ -37,7 +37,7 @@ public class LazyPackageMemberScope(
|
||||
override fun getPackage(name: Name): PackageViewDescriptor? = null
|
||||
|
||||
override fun getScopeForMemberDeclarationResolution(declaration: KtDeclaration)
|
||||
= resolveSession.getFileScopeProvider().getFileScopeChain(declaration.getContainingJetFile())
|
||||
= resolveSession.getFileScopeProvider().getFileResolutionScope(declaration.getContainingJetFile())
|
||||
|
||||
override fun getNonDeclaredFunctions(name: Name, result: MutableSet<FunctionDescriptor>) {
|
||||
// No extra functions
|
||||
|
||||
+1
-1
@@ -95,7 +95,7 @@ public class LazyScriptDescriptor(
|
||||
override fun getScriptCodeDescriptor() = scriptCodeDescriptor()
|
||||
|
||||
override fun getScopeForBodyResolution(): LexicalScope {
|
||||
return LexicalScopeImpl(resolveSession.fileScopeProvider.getFileScopeChain(jetScript.getContainingJetFile()),
|
||||
return LexicalScopeImpl(resolveSession.fileScopeProvider.getFileResolutionScope(jetScript.getContainingJetFile()),
|
||||
this, false, implicitReceiver, "Scope for body resolution for " + this) {
|
||||
for (valueParameterDescriptor in getScriptCodeDescriptor().valueParameters) {
|
||||
addVariableDescriptor(valueParameterDescriptor)
|
||||
|
||||
@@ -95,6 +95,10 @@ public fun LexicalScope.getClassifier(name: Name, location: LookupLocation): Cla
|
||||
return findFirstFromMeAndParent { it.getDeclaredClassifier(name, location) }
|
||||
}
|
||||
|
||||
public fun LexicalScope.getPackage(name: Name): PackageViewDescriptor? {
|
||||
return findFirstFromImportingScopes { it.getContributedPackage(name) }
|
||||
}
|
||||
|
||||
public fun LexicalScope.takeSnapshot(): LexicalScope = if (this is LexicalWritableScope) takeSnapshot() else this
|
||||
|
||||
public fun LexicalScope.asKtScope(): KtScope {
|
||||
@@ -120,9 +124,7 @@ private class LexicalToKtScopeAdapter(lexicalScope: LexicalScope): KtScope {
|
||||
|
||||
override fun getClassifier(name: Name, location: LookupLocation) = lexicalScope.getClassifier(name, location)
|
||||
|
||||
override fun getPackage(name: Name): PackageViewDescriptor? {
|
||||
return lexicalScope.findFirstFromImportingScopes { it.getPackage(name) }
|
||||
}
|
||||
override fun getPackage(name: Name) = lexicalScope.getPackage(name)
|
||||
|
||||
override fun getProperties(name: Name, location: LookupLocation): Collection<VariableDescriptor> {
|
||||
return lexicalScope.collectAllFromImportingScopes { it.getDeclaredVariables(name, location) }
|
||||
@@ -179,7 +181,7 @@ private class LexicalToKtScopeAdapter(lexicalScope: LexicalScope): KtScope {
|
||||
}
|
||||
|
||||
private class MemberScopeToImportingScopeAdapter(override val parent: ImportingScope?, val memberScope: KtScope) : ImportingScope {
|
||||
override fun getPackage(name: Name): PackageViewDescriptor? = memberScope.getPackage(name)
|
||||
override fun getContributedPackage(name: Name): PackageViewDescriptor? = memberScope.getPackage(name)
|
||||
|
||||
override fun getSyntheticExtensionProperties(receiverTypes: Collection<KotlinType>, name: Name, location: LookupLocation)
|
||||
= memberScope.getSyntheticExtensionProperties(receiverTypes, name, location)
|
||||
|
||||
@@ -26,7 +26,7 @@ public class ExplicitImportsScope(private val descriptors: Collection<Declaratio
|
||||
override fun getDeclaredClassifier(name: Name, location: LookupLocation)
|
||||
= descriptors.filter { it.getName() == name }.firstIsInstanceOrNull<ClassifierDescriptor>()
|
||||
|
||||
override fun getPackage(name: Name)
|
||||
override fun getContributedPackage(name: Name)
|
||||
= descriptors.filter { it.getName() == name }.firstIsInstanceOrNull<PackageViewDescriptor>()
|
||||
|
||||
override fun getDeclaredVariables(name: Name, location: LookupLocation)
|
||||
|
||||
@@ -92,7 +92,7 @@ interface ImportingScope : LexicalScope {
|
||||
|
||||
// methods getDeclaredSmth for this scope will be delegated to importScope
|
||||
|
||||
fun getPackage(name: Name): PackageViewDescriptor?
|
||||
fun getContributedPackage(name: Name): PackageViewDescriptor?
|
||||
|
||||
fun getSyntheticExtensionProperties(receiverTypes: Collection<KotlinType>, name: Name, location: LookupLocation): Collection<PropertyDescriptor>
|
||||
fun getSyntheticExtensionFunctions(receiverTypes: Collection<KotlinType>, name: Name, location: LookupLocation): Collection<FunctionDescriptor>
|
||||
@@ -108,7 +108,7 @@ interface ImportingScope : LexicalScope {
|
||||
override fun getDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor>
|
||||
|
||||
object Empty : ImportingScope, LexicalScope by LexicalScope.Empty {
|
||||
override fun getPackage(name: Name) = null
|
||||
override fun getContributedPackage(name: Name) = null
|
||||
|
||||
override fun getSyntheticExtensionProperties(receiverTypes: Collection<KotlinType>, name: Name, location: LookupLocation): Collection<PropertyDescriptor> = emptyList()
|
||||
|
||||
|
||||
+1
-1
@@ -125,7 +125,7 @@ public class CodeFragmentAnalyzer(
|
||||
dataFlowInfo = contextForElement.getDataFlowInfo(correctedContext)
|
||||
}
|
||||
is KtFile -> {
|
||||
scopeForContextElement = resolveSession.getFileScopeProvider().getFileScopeChain(context)
|
||||
scopeForContextElement = resolveSession.getFileScopeProvider().getFileResolutionScope(context)
|
||||
dataFlowInfo = DataFlowInfo.EMPTY
|
||||
}
|
||||
else -> return null
|
||||
|
||||
@@ -36,8 +36,6 @@ import org.jetbrains.kotlin.resolve.QualifiedExpressionResolver
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.resolve.lazy.FileScopeProvider
|
||||
import org.jetbrains.kotlin.resolve.scopes.ImportingScope
|
||||
import org.jetbrains.kotlin.resolve.scopes.KtScope
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.asKtScope
|
||||
|
||||
public fun KtElement.getResolutionFacade(): ResolutionFacade {
|
||||
return KotlinCacheService.getInstance(getProject()).getResolutionFacade(listOf(this))
|
||||
@@ -104,10 +102,6 @@ public fun getResolveScope(file: KtFile): GlobalSearchScope {
|
||||
}
|
||||
}
|
||||
|
||||
public fun ResolutionFacade.getFileScopeChain(file: KtFile): ImportingScope {
|
||||
return frontendService<FileScopeProvider>().getFileScopeChain(file)
|
||||
}
|
||||
|
||||
public fun ResolutionFacade.getFileKtScope(file: KtFile): KtScope {
|
||||
return getFileScopeChain(file).asKtScope()
|
||||
public fun ResolutionFacade.getFileResolutionScope(file: KtFile): ImportingScope {
|
||||
return frontendService<FileScopeProvider>().getFileResolutionScope(file)
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ package org.jetbrains.kotlin.idea.kdoc
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getFileScopeChain
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getFileResolutionScope
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||
import org.jetbrains.kotlin.idea.references.KtMultiReference
|
||||
import org.jetbrains.kotlin.idea.resolve.ResolutionFacade
|
||||
@@ -178,7 +178,7 @@ private fun getOuterScope(descriptor: DeclarationDescriptorWithSource, resolutio
|
||||
if (parent is PackageFragmentDescriptor) {
|
||||
val containingFile = (descriptor.getSource() as? PsiSourceElement)?.psi?.getContainingFile() as? KtFile
|
||||
if (containingFile != null) {
|
||||
return resolutionFacade.getFileScopeChain(containingFile)
|
||||
return resolutionFacade.getFileResolutionScope(containingFile)
|
||||
}
|
||||
}
|
||||
return getResolutionScope(resolutionFacade, parent!!)
|
||||
|
||||
@@ -41,8 +41,8 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.resolve.scopes.receivers.ThisReceiver
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.findFirstFromImportingScopes
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.getClassifier
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.getPackage
|
||||
import java.util.*
|
||||
|
||||
public class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT }) {
|
||||
@@ -280,7 +280,7 @@ public class ShortenReferences(val options: (KtElement) -> Options = { Options.D
|
||||
val targetByName = if (target is ClassifierDescriptor)
|
||||
scope.getClassifier(name, NoLookupLocation.FROM_IDE)
|
||||
else
|
||||
scope.findFirstFromImportingScopes { it.getPackage(name) }
|
||||
scope.getPackage(name)
|
||||
val canShortenNow = targetByName?.asString() == target.asString()
|
||||
|
||||
processQualifiedElement(type, target, canShortenNow)
|
||||
|
||||
@@ -20,7 +20,7 @@ import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.idea.analysis.computeTypeInContext
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getFileScopeChain
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getFileResolutionScope
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
|
||||
import org.jetbrains.kotlin.idea.references.mainReference
|
||||
@@ -114,7 +114,7 @@ public fun PsiElement.getResolutionScope(bindingContext: BindingContext, resolut
|
||||
}
|
||||
|
||||
if (parent is KtFile) {
|
||||
return resolutionFacade.getFileScopeChain(parent)
|
||||
return resolutionFacade.getFileResolutionScope(parent)
|
||||
}
|
||||
}
|
||||
error("Not in JetFile")
|
||||
|
||||
+12
-8
@@ -34,7 +34,7 @@ import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getFileKtScope
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getFileResolutionScope
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference
|
||||
import org.jetbrains.kotlin.idea.codeInsight.shorten.performDelayedShortening
|
||||
@@ -52,7 +52,8 @@ import org.jetbrains.kotlin.psi.psiUtil.elementsInRange
|
||||
import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.scopes.KtScope
|
||||
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.parentsWithSelf
|
||||
import org.jetbrains.kotlin.types.ErrorUtils
|
||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
|
||||
@@ -199,7 +200,7 @@ public class KotlinCopyPasteReferenceProcessor() : CopyPastePostProcessor<Kotlin
|
||||
private fun findReferencesToRestore(file: PsiFile, blockStart: Int, referenceData: Array<out KotlinReferenceData>): List<ReferenceToRestoreData> {
|
||||
if (file !is KtFile) return listOf()
|
||||
|
||||
val fileResolutionScope = file.getResolutionFacade().getFileKtScope(file)
|
||||
val fileResolutionScope = file.getResolutionFacade().getFileResolutionScope(file)
|
||||
return referenceData.map {
|
||||
val reference = findReference(it, file, blockStart)
|
||||
if (reference != null)
|
||||
@@ -224,16 +225,19 @@ public class KotlinCopyPasteReferenceProcessor() : CopyPastePostProcessor<Kotlin
|
||||
return null
|
||||
}
|
||||
|
||||
private fun createReferenceToRestoreData(reference: KtReference, refData: KotlinReferenceData, file: KtFile, fileResolutionScope: KtScope): ReferenceToRestoreData? {
|
||||
private fun createReferenceToRestoreData(reference: KtReference, refData: KotlinReferenceData, file: KtFile, fileResolutionScope: LexicalScope): ReferenceToRestoreData? {
|
||||
val originalFqName = FqName(refData.fqName)
|
||||
val name = originalFqName.shortName()
|
||||
|
||||
if (refData.kind == KotlinReferenceData.Kind.EXTENSION_FUNCTION) {
|
||||
val functions = fileResolutionScope.getFunctions(originalFqName.shortName(), NoLookupLocation.FROM_IDE)
|
||||
if (functions.any { it.importableFqName == originalFqName }) return null // already imported
|
||||
if (fileResolutionScope.parentsWithSelf.any { scope ->
|
||||
scope.getDeclaredFunctions(name, NoLookupLocation.FROM_IDE).any { it.importableFqName == originalFqName }
|
||||
}) return null // already imported
|
||||
}
|
||||
else if (refData.kind == KotlinReferenceData.Kind.EXTENSION_PROPERTY) {
|
||||
val properties = fileResolutionScope.getProperties(originalFqName.shortName(), NoLookupLocation.FROM_IDE)
|
||||
if (properties.any { it.importableFqName == originalFqName }) return null // already imported
|
||||
if (fileResolutionScope.parentsWithSelf.any { scope ->
|
||||
scope.getDeclaredVariables(name, NoLookupLocation.FROM_IDE).any { it.importableFqName == originalFqName }
|
||||
}) return null // already imported
|
||||
}
|
||||
|
||||
val referencedDescriptors = try {
|
||||
|
||||
@@ -23,7 +23,7 @@ import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiFile
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getFileScopeChain
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getFileResolutionScope
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||
import org.jetbrains.kotlin.idea.core.formatter.JetCodeStyleSettings
|
||||
import org.jetbrains.kotlin.idea.core.getResolutionScope
|
||||
@@ -218,7 +218,7 @@ public class KotlinImportOptimizer() : ImportOptimizer {
|
||||
importsToGenerate.filter { it.isAllUnder() }.map { "import " + it.pathStr }.joinTo(this, "\n")
|
||||
}.toString()
|
||||
val fileWithImports = KtPsiFactory(file).createAnalyzableFile("Dummy.kt", fileWithImportsText, file)
|
||||
val scope = fileWithImports.getResolutionFacade().getFileScopeChain(fileWithImports)
|
||||
val scope = fileWithImports.getResolutionFacade().getFileResolutionScope(fileWithImports)
|
||||
|
||||
for (fqName in classNamesToCheck) {
|
||||
if (scope.getClassifier(fqName.shortName(), NoLookupLocation.FROM_IDE)?.importableFqName != fqName) {
|
||||
|
||||
+2
-2
@@ -31,7 +31,7 @@ import com.intellij.util.ui.UIUtil
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getFileScopeChain
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getFileResolutionScope
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
|
||||
import org.jetbrains.kotlin.idea.core.targetDescriptors
|
||||
@@ -57,7 +57,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
|
||||
public class ConflictingExtensionPropertyInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
|
||||
val file = session.file as? KtFile ?: return PsiElementVisitor.EMPTY_VISITOR
|
||||
val fileScope = file.getResolutionFacade().getFileScopeChain(file)
|
||||
val fileScope = file.getResolutionFacade().getFileResolutionScope(file)
|
||||
|
||||
return object : KtVisitorVoid() {
|
||||
override fun visitProperty(property: KtProperty) {
|
||||
|
||||
@@ -21,8 +21,7 @@ import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.impl.PsiModificationTrackerImpl
|
||||
import com.intellij.psi.util.PsiModificationTracker
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getFileKtScope
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getFileScopeChain
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getFileResolutionScope
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||
import org.jetbrains.kotlin.idea.core.formatter.JetCodeStyleSettings
|
||||
import org.jetbrains.kotlin.idea.core.targetDescriptors
|
||||
@@ -40,9 +39,12 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.getImportableDescriptor
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
|
||||
import org.jetbrains.kotlin.resolve.scopes.KtScope
|
||||
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
|
||||
import org.jetbrains.kotlin.resolve.scopes.getDescriptorsFiltered
|
||||
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.getClassifier
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.getPackage
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.processForMeAndParent
|
||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||
import java.util.*
|
||||
|
||||
@@ -104,20 +106,28 @@ public class ImportInsertHelperImpl(private val project: Project) : ImportInsert
|
||||
) {
|
||||
private val resolutionFacade = file.getResolutionFacade()
|
||||
|
||||
private fun isAlreadyImported(target: DeclarationDescriptor, topLevelScope: KtScope, targetFqName: FqName): Boolean {
|
||||
private fun isAlreadyImported(target: DeclarationDescriptor, topLevelScope: LexicalScope, targetFqName: FqName): Boolean {
|
||||
val name = target.name
|
||||
when (target) {
|
||||
is ClassDescriptor -> {
|
||||
val classifier = topLevelScope.getClassifier(name, NoLookupLocation.FROM_IDE)
|
||||
if (classifier?.importableFqName == targetFqName) return true
|
||||
}
|
||||
|
||||
is FunctionDescriptor -> {
|
||||
val functions = topLevelScope.getFunctions(name, NoLookupLocation.FROM_IDE)
|
||||
if (functions.map { it.importableFqName }.contains(targetFqName)) return true
|
||||
topLevelScope.processForMeAndParent {
|
||||
if (it.getDeclaredFunctions(name, NoLookupLocation.FROM_IDE).any { it.importableFqName == targetFqName }) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
is PropertyDescriptor -> {
|
||||
val properties = topLevelScope.getProperties(name, NoLookupLocation.FROM_IDE)
|
||||
if (properties.map { it.importableFqName }.contains(targetFqName)) return true
|
||||
topLevelScope.processForMeAndParent {
|
||||
if (it.getDeclaredVariables(name, NoLookupLocation.FROM_IDE).any { it.importableFqName == targetFqName }) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
@@ -127,7 +137,7 @@ public class ImportInsertHelperImpl(private val project: Project) : ImportInsert
|
||||
val target = descriptor.getImportableDescriptor()
|
||||
|
||||
val name = target.name
|
||||
val topLevelScope = resolutionFacade.getFileKtScope(file)
|
||||
val topLevelScope = resolutionFacade.getFileResolutionScope(file)
|
||||
|
||||
// check if import is not needed
|
||||
val targetFqName = target.importableFqName ?: return ImportDescriptorResult.FAIL
|
||||
@@ -232,7 +242,7 @@ public class ImportInsertHelperImpl(private val project: Project) : ImportInsert
|
||||
.filter(::isVisible)
|
||||
.map { it.getName() }
|
||||
|
||||
val topLevelScope = resolutionFacade.getFileScopeChain(file)
|
||||
val topLevelScope = resolutionFacade.getFileResolutionScope(file)
|
||||
val conflictCandidates: List<ClassifierDescriptor> = classNamesToImport
|
||||
.flatMap {
|
||||
importedScopes.map { scope -> scope.getClassifier(it, NoLookupLocation.FROM_IDE) }.filterNotNull()
|
||||
@@ -248,7 +258,7 @@ public class ImportInsertHelperImpl(private val project: Project) : ImportInsert
|
||||
|
||||
val addedImport = addImport(parentFqName, true)
|
||||
|
||||
val newTopLevelScope = resolutionFacade.getFileKtScope(file)
|
||||
val newTopLevelScope = resolutionFacade.getFileResolutionScope(file)
|
||||
if (!isAlreadyImported(target, newTopLevelScope, targetFqName)) {
|
||||
addedImport.delete()
|
||||
return ImportDescriptorResult.FAIL
|
||||
@@ -277,7 +287,7 @@ public class ImportInsertHelperImpl(private val project: Project) : ImportInsert
|
||||
|
||||
private fun addExplicitImport(target: DeclarationDescriptor): ImportDescriptorResult {
|
||||
if (target is ClassDescriptor || target is PackageViewDescriptor) {
|
||||
val topLevelScope = resolutionFacade.getFileScopeChain(file)
|
||||
val topLevelScope = resolutionFacade.getFileResolutionScope(file)
|
||||
val name = target.getName()
|
||||
|
||||
// check if there is a conflicting class imported with * import
|
||||
@@ -308,7 +318,7 @@ public class ImportInsertHelperImpl(private val project: Project) : ImportInsert
|
||||
}
|
||||
|
||||
if (importsToCheck.isNotEmpty()) {
|
||||
val topLevelScope = resolutionFacade.getFileScopeChain(file)
|
||||
val topLevelScope = resolutionFacade.getFileResolutionScope(file)
|
||||
for (classFqName in importsToCheck) {
|
||||
val classifier = topLevelScope.getClassifier(classFqName.shortName(), NoLookupLocation.FROM_IDE)
|
||||
if (classifier?.importableFqName != classFqName) {
|
||||
|
||||
Reference in New Issue
Block a user