Term changes
This commit is contained in:
@@ -70,19 +70,19 @@ class OptimizedImportsBuilder(
|
|||||||
|
|
||||||
private val importInsertHelper = ImportInsertHelper.getInstance(file.project)
|
private val importInsertHelper = ImportInsertHelper.getInstance(file.project)
|
||||||
|
|
||||||
private sealed class LockedImport {
|
private sealed class ImportRule {
|
||||||
// force presence of this import
|
// force presence of this import
|
||||||
data class Positive(val importPath: ImportPath) : LockedImport() {
|
data class Add(val importPath: ImportPath) : ImportRule() {
|
||||||
override fun toString() = importPath.toString()
|
override fun toString() = "+" + importPath.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
// force absence of this import
|
// force absence of this import
|
||||||
data class Negative(val importPath: ImportPath) : LockedImport() {
|
data class DoNotAdd(val importPath: ImportPath) : ImportRule() {
|
||||||
override fun toString() = "-" + importPath.toString()
|
override fun toString() = "-" + importPath.toString()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val lockedImports = HashSet<LockedImport>()
|
private val importRules = HashSet<ImportRule>()
|
||||||
|
|
||||||
fun buildOptimizedImports(): List<ImportPath>? {
|
fun buildOptimizedImports(): List<ImportPath>? {
|
||||||
// TODO: should we drop unused aliases?
|
// TODO: should we drop unused aliases?
|
||||||
@@ -93,13 +93,13 @@ class OptimizedImportsBuilder(
|
|||||||
val aliasName = it.alias
|
val aliasName = it.alias
|
||||||
aliasName != null && aliasName != it.fqnPart().shortName()
|
aliasName != null && aliasName != it.fqnPart().shortName()
|
||||||
}
|
}
|
||||||
.mapTo(lockedImports) { LockedImport.Positive(it) }
|
.mapTo(importRules) { ImportRule.Add(it) }
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
val lockedImportsBefore = lockedImports.size
|
val importRulesBefore = importRules.size
|
||||||
val result = tryBuildOptimizedImports()
|
val result = tryBuildOptimizedImports()
|
||||||
if (lockedImports.size == lockedImportsBefore) return result
|
if (importRules.size == importRulesBefore) return result
|
||||||
testLog?.append("Trying to build import list again with locked imports: ${lockedImports.joinToString()}\n")
|
testLog?.append("Trying to build import list again with import rules: ${importRules.joinToString()}\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,8 +116,8 @@ class OptimizedImportsBuilder(
|
|||||||
|
|
||||||
private fun tryBuildOptimizedImports(): List<ImportPath>? {
|
private fun tryBuildOptimizedImports(): List<ImportPath>? {
|
||||||
val importsToGenerate = HashSet<ImportPath>()
|
val importsToGenerate = HashSet<ImportPath>()
|
||||||
lockedImports
|
importRules
|
||||||
.filterIsInstance<LockedImport.Positive>()
|
.filterIsInstance<ImportRule.Add>()
|
||||||
.mapTo(importsToGenerate) { it.importPath }
|
.mapTo(importsToGenerate) { it.importPath }
|
||||||
|
|
||||||
val descriptorsByParentFqName = HashMap<FqName, MutableSet<DeclarationDescriptor>>()
|
val descriptorsByParentFqName = HashMap<FqName, MutableSet<DeclarationDescriptor>>()
|
||||||
@@ -129,7 +129,7 @@ class OptimizedImportsBuilder(
|
|||||||
|
|
||||||
val parentFqName = fqName.parent()
|
val parentFqName = fqName.parent()
|
||||||
val starImportPath = ImportPath(parentFqName, true)
|
val starImportPath = ImportPath(parentFqName, true)
|
||||||
if (canUseStarImport(descriptor, fqName) && !starImportPath.isNegativeLocked()) {
|
if (canUseStarImport(descriptor, fqName) && starImportPath.isAllowedByRules()) {
|
||||||
descriptorsByParentFqName.getOrPut(parentFqName) { HashSet() }.add(descriptor)
|
descriptorsByParentFqName.getOrPut(parentFqName) { HashSet() }.add(descriptor)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -147,7 +147,7 @@ class OptimizedImportsBuilder(
|
|||||||
val fqNames = descriptors.map { it.importableFqName!! }.toSet()
|
val fqNames = descriptors.map { it.importableFqName!! }.toSet()
|
||||||
val nameCountToUseStar = descriptors.first().nameCountToUseStar()
|
val nameCountToUseStar = descriptors.first().nameCountToUseStar()
|
||||||
val useExplicitImports = fqNames.size < nameCountToUseStar && !options.isInPackagesToUseStarImport(parentFqName)
|
val useExplicitImports = fqNames.size < nameCountToUseStar && !options.isInPackagesToUseStarImport(parentFqName)
|
||||||
|| starImportPath.isNegativeLocked()
|
|| !starImportPath.isAllowedByRules()
|
||||||
if (useExplicitImports) {
|
if (useExplicitImports) {
|
||||||
fqNames
|
fqNames
|
||||||
.filter { !isImportedByDefault(it) }
|
.filter { !isImportedByDefault(it) }
|
||||||
@@ -211,13 +211,13 @@ class OptimizedImportsBuilder(
|
|||||||
val starImportPath = ImportPath(fqName.parent(), true)
|
val starImportPath = ImportPath(fqName.parent(), true)
|
||||||
val importPaths = file.importDirectives.map { it.importPath }
|
val importPaths = file.importDirectives.map { it.importPath }
|
||||||
if (explicitImportPath in importPaths) {
|
if (explicitImportPath in importPaths) {
|
||||||
lockedImports.add(LockedImport.Positive(explicitImportPath))
|
importRules.add(ImportRule.Add(explicitImportPath))
|
||||||
}
|
}
|
||||||
else if (starImportPath in importPaths) {
|
else if (starImportPath in importPaths) {
|
||||||
lockedImports.add(LockedImport.Positive(starImportPath))
|
importRules.add(ImportRule.Add(starImportPath))
|
||||||
}
|
}
|
||||||
else { // there is no import for this descriptor in the original import list, so do not allow to import it by star-import
|
else { // there is no import for this descriptor in the original import list, so do not allow to import it by star-import
|
||||||
lockedImports.add(LockedImport.Negative(starImportPath))
|
importRules.add(ImportRule.DoNotAdd(starImportPath))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -322,5 +322,5 @@ class OptimizedImportsBuilder(
|
|||||||
descriptors1.zip(descriptors2).all { it.first.importableFqName == it.second.importableFqName } //TODO: can have different order?
|
descriptors1.zip(descriptors2).all { it.first.importableFqName == it.second.importableFqName } //TODO: can have different order?
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun ImportPath.isNegativeLocked(): Boolean = lockedImports.any { it is LockedImport.Negative && it.importPath == this }
|
private fun ImportPath.isAllowedByRules(): Boolean = importRules.none { it is ImportRule.DoNotAdd && it.importPath == this }
|
||||||
}
|
}
|
||||||
+1
-1
@@ -3,4 +3,4 @@ Additional checking of reference KtInvokeFunctionReference: MyFunction()
|
|||||||
Additional checking of reference KtInvokeFunctionReference: foo()
|
Additional checking of reference KtInvokeFunctionReference: foo()
|
||||||
Changed resolve of KtInvokeFunctionReference: foo()
|
Changed resolve of KtInvokeFunctionReference: foo()
|
||||||
Additional checking of reference KtInvokeFunctionReference: println("extension function")
|
Additional checking of reference KtInvokeFunctionReference: println("extension function")
|
||||||
Trying to build import list again with locked imports: bug.a.invoke
|
Trying to build import list again with import rules: +bug.a.invoke
|
||||||
|
|||||||
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
Additional checking of reference KtInvokeFunctionReference: foo()
|
Additional checking of reference KtInvokeFunctionReference: foo()
|
||||||
Changed resolve of KtInvokeFunctionReference: foo()
|
Changed resolve of KtInvokeFunctionReference: foo()
|
||||||
Trying to build import list again with locked imports: bug.a.*
|
Trying to build import list again with import rules: +bug.a.*
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@ Additional checking of reference Getter: foo
|
|||||||
Additional checking of reference KtSimpleNameReference: foo
|
Additional checking of reference KtSimpleNameReference: foo
|
||||||
Changed resolve of KtSimpleNameReference: foo
|
Changed resolve of KtSimpleNameReference: foo
|
||||||
Additional checking of reference KtInvokeFunctionReference: foo("")
|
Additional checking of reference KtInvokeFunctionReference: foo("")
|
||||||
Trying to build import list again with locked imports: pack1.foo, pack2.*
|
Trying to build import list again with import rules: +pack1.foo, +pack2.*
|
||||||
Additional checking of reference Getter: foo
|
Additional checking of reference Getter: foo
|
||||||
Additional checking of reference KtSimpleNameReference: foo
|
Additional checking of reference KtSimpleNameReference: foo
|
||||||
Additional checking of reference KtInvokeFunctionReference: foo("")
|
Additional checking of reference KtInvokeFunctionReference: foo("")
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
Additional checking of reference KtSimpleNameReference: +=
|
Additional checking of reference KtSimpleNameReference: +=
|
||||||
Changed resolve of KtSimpleNameReference: +=
|
Changed resolve of KtSimpleNameReference: +=
|
||||||
Trying to build import list again with locked imports: -p2.*, p1.plus
|
Trying to build import list again with import rules: -p2.*, +p1.plus
|
||||||
|
|||||||
@@ -6,4 +6,4 @@ Additional checking of reference KtSimpleNameReference: A
|
|||||||
Changed resolve of KtSimpleNameReference: A
|
Changed resolve of KtSimpleNameReference: A
|
||||||
Additional checking of reference KtInvokeFunctionReference: A("")
|
Additional checking of reference KtInvokeFunctionReference: A("")
|
||||||
Additional checking of reference KtInvokeFunctionReference: A(1)
|
Additional checking of reference KtInvokeFunctionReference: A(1)
|
||||||
Trying to build import list again with locked imports: p2.A, p1.*
|
Trying to build import list again with import rules: +p2.A, +p1.*
|
||||||
|
|||||||
Reference in New Issue
Block a user