Get rid of guava's collections and dependency on Idea's settings from util function
This commit prepares 'prepareOptimizedImports' function to move to the ide-common module to reuse it in the Eclipse plugin
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2015 JetBrains s.r.o.
|
* Copyright 2010-2016 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.imports
|
package org.jetbrains.kotlin.idea.imports
|
||||||
|
|
||||||
import com.google.common.collect.HashMultimap
|
|
||||||
import com.intellij.lang.ImportOptimizer
|
import com.intellij.lang.ImportOptimizer
|
||||||
import com.intellij.openapi.progress.ProgressIndicatorProvider
|
import com.intellij.openapi.progress.ProgressIndicatorProvider
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
@@ -155,17 +154,31 @@ class KotlinImportOptimizer() : ImportOptimizer {
|
|||||||
return visitor.descriptors
|
return visitor.descriptors
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun prepareOptimizedImports(file: KtFile,
|
||||||
|
descriptorsToImport: Collection<DeclarationDescriptor>): List<ImportPath>? {
|
||||||
|
val settings = KotlinCodeStyleSettings.getInstance(file.project)
|
||||||
|
return prepareOptimizedImports(
|
||||||
|
file,
|
||||||
|
descriptorsToImport,
|
||||||
|
settings.NAME_COUNT_TO_USE_STAR_IMPORT,
|
||||||
|
settings.NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS) { fqName ->
|
||||||
|
fqName.asString() in settings.PACKAGES_TO_USE_STAR_IMPORTS
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun prepareOptimizedImports(
|
fun prepareOptimizedImports(
|
||||||
file: KtFile,
|
file: KtFile,
|
||||||
descriptorsToImport: Collection<DeclarationDescriptor>
|
descriptorsToImport: Collection<DeclarationDescriptor>,
|
||||||
|
nameCountToUseStarImport: Int,
|
||||||
|
nameCountToUseStarImportForMembers: Int,
|
||||||
|
isInPackagesToUseStarImport: (FqName) -> Boolean
|
||||||
): List<ImportPath>? {
|
): List<ImportPath>? {
|
||||||
val importInsertHelper = ImportInsertHelper.getInstance(file.project)
|
val importInsertHelper = ImportInsertHelper.getInstance(file.project)
|
||||||
val codeStyleSettings = KotlinCodeStyleSettings.getInstance(file.project)
|
|
||||||
val aliasImports = buildAliasImportMap(file)
|
val aliasImports = buildAliasImportMap(file)
|
||||||
|
|
||||||
val importsToGenerate = HashSet<ImportPath>()
|
val importsToGenerate = HashSet<ImportPath>()
|
||||||
|
|
||||||
val descriptorsByParentFqName = HashMultimap.create<FqName, DeclarationDescriptor>()
|
val descriptorsByParentFqName = hashMapOf<FqName, MutableSet<DeclarationDescriptor>>()
|
||||||
for (descriptor in descriptorsToImport) {
|
for (descriptor in descriptorsToImport) {
|
||||||
val fqName = descriptor.importableFqName!!
|
val fqName = descriptor.importableFqName!!
|
||||||
val container = descriptor.containingDeclaration
|
val container = descriptor.containingDeclaration
|
||||||
@@ -176,7 +189,8 @@ class KotlinImportOptimizer() : ImportOptimizer {
|
|||||||
else -> true
|
else -> true
|
||||||
}
|
}
|
||||||
if (canUseStarImport) {
|
if (canUseStarImport) {
|
||||||
descriptorsByParentFqName.put(parentFqName, descriptor)
|
val descriptors = descriptorsByParentFqName.getOrPut(parentFqName) { hashSetOf() }
|
||||||
|
descriptors.add(descriptor)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
importsToGenerate.add(ImportPath(fqName, false))
|
importsToGenerate.add(ImportPath(fqName, false))
|
||||||
@@ -187,16 +201,15 @@ class KotlinImportOptimizer() : ImportOptimizer {
|
|||||||
|
|
||||||
fun isImportedByDefault(fqName: FqName) = importInsertHelper.isImportedWithDefault(ImportPath(fqName, false), file)
|
fun isImportedByDefault(fqName: FqName) = importInsertHelper.isImportedWithDefault(ImportPath(fqName, false), file)
|
||||||
|
|
||||||
for (parentFqName in descriptorsByParentFqName.keys()) {
|
for (parentFqName in descriptorsByParentFqName.keys) {
|
||||||
val descriptors = descriptorsByParentFqName[parentFqName]
|
val descriptors = descriptorsByParentFqName[parentFqName]!!
|
||||||
val fqNames = descriptors.map { it.importableFqName!! }.toSet()
|
val fqNames = descriptors.map { it.importableFqName!! }.toSet()
|
||||||
val isMember = descriptors.first().containingDeclaration is ClassDescriptor
|
val isMember = descriptors.first().containingDeclaration is ClassDescriptor
|
||||||
val nameCountToUseStar = if (isMember)
|
val nameCountToUseStar = if (isMember)
|
||||||
codeStyleSettings.NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS
|
nameCountToUseStarImportForMembers
|
||||||
else
|
else
|
||||||
codeStyleSettings.NAME_COUNT_TO_USE_STAR_IMPORT
|
nameCountToUseStarImport
|
||||||
val explicitImports = fqNames.size < nameCountToUseStar
|
val explicitImports = fqNames.size < nameCountToUseStar && !isInPackagesToUseStarImport(parentFqName)
|
||||||
&& parentFqName.asString() !in codeStyleSettings.PACKAGES_TO_USE_STAR_IMPORTS
|
|
||||||
if (explicitImports) {
|
if (explicitImports) {
|
||||||
for (fqName in fqNames) {
|
for (fqName in fqNames) {
|
||||||
if (!isImportedByDefault(fqName)) {
|
if (!isImportedByDefault(fqName)) {
|
||||||
@@ -232,18 +245,19 @@ class KotlinImportOptimizer() : ImportOptimizer {
|
|||||||
|
|
||||||
val parentFqName = fqName.parent()
|
val parentFqName = fqName.parent()
|
||||||
|
|
||||||
for (descriptor in descriptorsByParentFqName[parentFqName].filter { it.importableFqName == fqName }) {
|
val parentDescriptors = descriptorsByParentFqName[parentFqName]!!
|
||||||
descriptorsByParentFqName.remove(parentFqName, descriptor)
|
for (descriptor in parentDescriptors.filter { it.importableFqName == fqName }) {
|
||||||
|
parentDescriptors.remove(descriptor)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (descriptorsByParentFqName[parentFqName].isEmpty()) { // star import is not really needed
|
if (parentDescriptors.isEmpty()) { // star import is not really needed
|
||||||
importsToGenerate.remove(ImportPath(parentFqName, true))
|
importsToGenerate.remove(ImportPath(parentFqName, true))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: drop unused aliases?
|
//TODO: drop unused aliases?
|
||||||
aliasImports.mapTo(importsToGenerate) { ImportPath(it.value, false, it.key)}
|
aliasImports.mapTo(importsToGenerate) { ImportPath(it.value, false, it.key) }
|
||||||
|
|
||||||
val sortedImportsToGenerate = importsToGenerate.sortedWith(importInsertHelper.importSortComparator)
|
val sortedImportsToGenerate = importsToGenerate.sortedWith(importInsertHelper.importSortComparator)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user